-
Notifications
You must be signed in to change notification settings - Fork 2
Feature/1 my dao #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: hamsterdmin
Are you sure you want to change the base?
Feature/1 my dao #30
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,14 @@ | ||
| package org.example; | ||
| import org.example.controller.BhvrController; | ||
| import org.example.controller.Controller; | ||
| import org.example.dao.MyListDao; | ||
| import org.example.domain.DataBhvr; | ||
| import org.example.domain.InputView; | ||
| import org.example.domain.OutputView; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| InputView inputView = new InputView(); | ||
| OutputView outputView = new OutputView(); | ||
| MyListDao myListDao = new MyListDao(); | ||
| inputView.inputDataType(); | ||
| inputView.inputDataBhvr(); | ||
| outputView.outputSave(inputView.inputData()); | ||
|
|
||
| Controller controller = new Controller(); | ||
| controller.processRun(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package org.example.controller; | ||
| import org.example.dao.MyListDao; | ||
| import org.example.domain.DataBhvr; | ||
| import org.example.domain.InputView; | ||
|
|
||
| public class BhvrController { | ||
| public void bhvrController(DataBhvr bhvr){ | ||
| InputView inputView = new InputView(); | ||
| MyListDao myListDao = new MyListDao(); | ||
| switch(bhvr){ | ||
| case save: myListDao.save(inputView.inputData()); | ||
| case find: myListDao.find(inputView.inputFindName()); | ||
| case update: myListDao.update(inputView.inputUpdateName(),inputView.inputUpdateData()); | ||
| case delete: myListDao.delete(inputView.inputDeleteName()); | ||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package org.example.controller; | ||
|
|
||
| import org.example.dao.MyListDao; | ||
| import org.example.domain.DataBhvr; | ||
| import org.example.domain.InputView; | ||
|
|
||
| public class Controller { | ||
| public void processRun() { | ||
| InputView inputView = new InputView(); | ||
| inputView.inputDataType(); | ||
| while (true) { | ||
| inputView.inputDataBhvr(); | ||
| if (inputView.inputDecision() == 1) { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,6 @@ | |
| public interface MyDao { | ||
| Person save(Person person); | ||
| Person find(String name); | ||
| Person update(String name, String updateName); | ||
| Person update(String updateName, String[] updateData); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. String[]는 다른 사람이 봤을 때 수정될 내용이 어떤게 들어있는지 파악하기 힘들 것 같아요 |
||
| void delete(String name); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,9 @@ public MyListDao() { | |
| public Person save(Person dao) { | ||
| Person person = new Person(dao.getName(), dao.getBirth(),dao.getMe()); | ||
| peopleList.add(person); | ||
| return null; | ||
| outputView.outputSave(person); | ||
| return person; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이미 중복된 이름이 있다면..? |
||
|
|
||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -30,7 +32,14 @@ public Person find(String name) { | |
| } | ||
|
|
||
| @Override | ||
| public Person update(String name, String updateName) { | ||
| public Person update(String updateName,String[] updateDataArray) { | ||
| for (Person person : peopleList){ | ||
| if (person.getName().equals(updateName)){ | ||
| outputView.outputUpdate(person, updateDataArray[0], updateDataArray[1]); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 데이터 수정을 하는 메서드에서 출력을 하는 기능이 있다는 것은 두 개 이상의 책임을 가지고 있다는 것을 뜻합니다
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 하나의 메서드는 하나의 기능을 해야 하는 것을 잊고 있었네요. 처음에는 와닿지 않았는데, 코드를 짜다보니까 메서드들을 연결, 실행해주는 역할의 필요성이 느껴졌습니다 . ! |
||
| person.setBirth(updateDataArray[0]); | ||
| person.setMe(updateDataArray[1]); | ||
| } | ||
| } | ||
| return null; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. null 반환은 가능하면 하지 않는 것이 좋습니다. |
||
| } | ||
|
|
||
|
|
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,11 +32,30 @@ public int inputDataBhvr(){ | |
| } | ||
| return dataBhvr; | ||
| } | ||
| public String inputName(){ | ||
| public String inputUpdateName(){ | ||
| String updateName = sc.nextLine(); | ||
| return updateName; | ||
| } | ||
| public String[] inputUpdateData() { | ||
| String[] updateData = null; | ||
| try { | ||
| updateData = sc.nextLine().split(","); | ||
|
|
||
| } catch (IllegalArgumentException e) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 제가 이전 과제 참고 코드에서도 그렇고 이번 코드에서도 그렇고 예외처리 하는 방법을 넣어놨으니 참고해주시면 좋을 것 같아요 ㅎㅎ |
||
| System.out.println("잘못된 데이터 형식입니다"); | ||
| } | ||
| return updateData; | ||
| } | ||
| public String inputFindName(){ | ||
| System.out.println("조회할 이름을 입력해주세요."); | ||
| String name = sc.nextLine(); | ||
| return name; | ||
| } | ||
| public String inputDeleteName(){ | ||
| System.out.println("삭제할 이름을 입력해주세요."); | ||
| String name = sc.nextLine(); | ||
| return name; | ||
| } | ||
| public int inputDecision(){ | ||
| System.out.println("계속하시겠습니까? (CONTINUE: 0, EXIT: 1)"); | ||
| int dc = sc.nextInt(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 만약 0, 1 외에 다른 값을 입력한다면 어떻게 될까요? |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,17 @@ public class Person { | |
| private String name; | ||
| private String birth; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 생일처럼 날짜를 표현할 때는 자바의 LocalDate라는 기능을 쓰시는 걸 추천드립니다!
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분에서 localDate 기능을 사용하긴 했는데, 보여주신 코드에서 어느 점을 고쳐야 할지 모르겠습니다
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 제가 말하고 싶었던 부분은 날짜 자체는 String 보다 LocalDate 자료형으로 저장을 하는게 더 좋다는 뜻이었습니다. LocalDate birthData = LocalDate.now().minusYears(localDate.getYear());이 값은 생일이 아니라 (한국식)나이가 될 것 같네요 |
||
| private String me; | ||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public void setBirth(String birth) { | ||
| this.birth = birth; | ||
| } | ||
|
|
||
| public void setMe(String me) { | ||
| this.me = me; | ||
| } | ||
| public Person(String name, String birth, String me){ | ||
| this.name = name; | ||
| this.birth = birth; | ||
|
|
@@ -31,14 +42,10 @@ private Integer[] stringToInt(String[] array){ | |
| return intArray; | ||
| } | ||
| public String getAge(){ | ||
| <<<<<<< HEAD | ||
| Integer[] Age = stringToInt(birth.split(",")); | ||
| ======= | ||
| Integer[] Age = StringToInt(birth.split("\\.")); | ||
| >>>>>>> 8ede79768f450ca32b711f2d28e7b3d834b1c7b4 | ||
| Integer[] Age = stringToInt(birth.split("\\.")); | ||
| LocalDate localDate = LocalDate.of(Age[0], Age[1], Age[2]); | ||
| LocalDate localDate1 = LocalDate.now().minusYears(localDate.getYear()); | ||
| return Integer.toString(localDate1.getYear()); | ||
| LocalDate birthData = LocalDate.now().minusYears(localDate.getYear()); | ||
| return Integer.toString(birthData.getYear()); | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
여기서 타입을 MyListDao로 하기보단, InputView의 inputDataType에 따라 MyListDao 또는 MyMapDao를 리턴하는 메서드나 클래스를 만들고, 반환 타입을 둘의 인터페이스인 MyDao로 하면 BhvrController는 어떤 Dao인지에 상관 없이 crud를 할 수 있게 됩니다.
혹시 이해가 잘 안가신다면 다형성에 대해 공부해보시는걸 추천드려요!