Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions MyDao/src/main/java/org/example/Main.java
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();
}
}
18 changes: 18 additions & 0 deletions MyDao/src/main/java/org/example/controller/BhvrController.java
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();

Copy link
Copy Markdown
Member

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를 할 수 있게 됩니다.
혹시 이해가 잘 안가신다면 다형성에 대해 공부해보시는걸 추천드려요!

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());
}
}

}
18 changes: 18 additions & 0 deletions MyDao/src/main/java/org/example/controller/Controller.java
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;
}
}
}
}
2 changes: 1 addition & 1 deletion MyDao/src/main/java/org/example/dao/MyDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String[]는 다른 사람이 봤을 때 수정될 내용이 어떤게 들어있는지 파악하기 힘들 것 같아요

void delete(String name);
}
13 changes: 11 additions & 2 deletions MyDao/src/main/java/org/example/dao/MyListDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이미 중복된 이름이 있다면..?


}

@Override
Expand All @@ -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]);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

데이터 수정을 하는 메서드에서 출력을 하는 기능이 있다는 것은 두 개 이상의 책임을 가지고 있다는 것을 뜻합니다
https://lktprogrammer.tistory.com/42
이런 글을 참고해보시면 좋을 것 같네요

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

하나의 메서드는 하나의 기능을 해야 하는 것을 잊고 있었네요. 처음에는 와닿지 않았는데, 코드를 짜다보니까 메서드들을 연결, 실행해주는 역할의 필요성이 느껴졌습니다 . !

person.setBirth(updateDataArray[0]);
person.setMe(updateDataArray[1]);
}
}
return null;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

null 반환은 가능하면 하지 않는 것이 좋습니다.
또 해당 메서드는 '유저 데이터 수정'이라는 목적(책임)을 갖고 있기 때문에 해당 목적에 달성을 실패했을 때는 예외가 발생하는게 맞다고 생각이 드네요 🙂

}

Expand Down
2 changes: 1 addition & 1 deletion MyDao/src/main/java/org/example/dao/MyMapDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public Person find(String name) {
}

@Override
public Person update(String name, String updateName) {
public Person update(String name, String[] updateName) {
return null;
}

Expand Down
16 changes: 0 additions & 16 deletions MyDao/src/main/java/org/example/domain/Controller.java

This file was deleted.

21 changes: 20 additions & 1 deletion MyDao/src/main/java/org/example/domain/InputView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

만약 0, 1 외에 다른 값을 입력한다면 어떻게 될까요?

Expand Down
21 changes: 14 additions & 7 deletions MyDao/src/main/java/org/example/domain/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ public class Person {
private String name;
private String birth;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

생일처럼 날짜를 표현할 때는 자바의 LocalDate라는 기능을 쓰시는 걸 추천드립니다!

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    Integer[] Age = stringToInt(birth.split("\\."));
    LocalDate localDate = LocalDate.of(Age[0], Age[1], Age[2]);
    LocalDate birthData = LocalDate.now().minusYears(localDate.getYear());

이 부분에서 localDate 기능을 사용하긴 했는데, 보여주신 코드에서 어느 점을 고쳐야 할지 모르겠습니다

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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;
Expand All @@ -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());
}

}