Skip to content
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

Java Assignment3 upload by HaesolChoi #40

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 9 additions & 0 deletions src/me/day05/practice/Practice01/AuthMethod.java
Copy link
Contributor

Choose a reason for hiding this comment

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

AuthMethod 클래스에 한국어보다는 영어로 상수 정의를 하는 게 좋지 않을까요?

Copy link
Contributor

Choose a reason for hiding this comment

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

AuthMethod 클래스에 한국어보다는 영어로 상수 정의를 하는 게 좋지 않을까요?

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package me.day05.practice.Practice01;

//본인인증 방법 — Enum형으로 정의
public enum AuthMethod {
지문인증,

Choose a reason for hiding this comment

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

강사님 의견처럼 저도 영어로 상수를 정의하는게 좋을 것 같습니다 ㅋㅋ

패턴인증,
핀인증,
얼굴인증
}
8 changes: 8 additions & 0 deletions src/me/day05/practice/Practice01/CompanyName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package me.day05.practice.Practice01;

//제조회사명 — Enum형으로 정의
public enum CompanyName {
SAMSUNG,
LG,
APPLE
}
104 changes: 104 additions & 0 deletions src/me/day05/practice/Practice01/Electronic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package me.day05.practice.Practice01;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Objects;

public class Electronic {

Choose a reason for hiding this comment

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

일반적으로 코드에도 저마다의 위치가 있는데요.

  • static 변수
  • 일반 변수
  • 생성자
  • public 메서드
  • private 메서드
    순으로 코드를 작성해봅시다~

private String productNo;
private String modelName;
private CompanyName companyName;
private LocalDate dateOfMade;
private AuthMethod[] authMethod; // 본인인증 방법 — 배열로 정의

Choose a reason for hiding this comment

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

사소한거지만 여러개를 담을 수 있으니 변수명에 복수를 사용하는게 어떨까요?

Suggested change
private AuthMethod[] authMethod; // 본인인증 방법 — 배열로 정의
private AuthMethod[] authMethods; // 본인인증 방법 — 배열로 정의


private static int orderNum = 0;

public Electronic(String modelName, CompanyName companyName, LocalDate dateOfMade, AuthMethod[] authMethod) {
orderNum++;
this.productNo = orderProductNo();
this.modelName = modelName;
this.companyName = companyName;
this.dateOfMade = dateOfMade;
this.authMethod = authMethod;
}


//ex. if 2023/03/30(now) then 230330 + 0001 (4자리 등록 순서) ⇒ 2303300001
private String orderProductNo() {

Choose a reason for hiding this comment

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

오! 메서드 분리를 해주셨네요.💯 private까지 좋습니다~

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMMdd");

Choose a reason for hiding this comment

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

요런 방법도 있겠군요👍🏻

String date = LocalDate.now().format(formatter);
String order_cnt = String.format("%04d", orderNum);
return date + order_cnt;

Choose a reason for hiding this comment

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

문자열 연산 방법중에 + 말고는 어떤게 있는지 한번 공부해볼까요?

}


public String getProductNo() {
return productNo;
}

public String getModelName() {
return modelName;
}

public void setModelName(String modelName) {
this.modelName = modelName;
}

public CompanyName getCompanyName() {
return companyName;
}

public void setCompanyName(CompanyName companyName) {
this.companyName = companyName;
}

public LocalDate getDateOfMade() {
return dateOfMade;
}

public void setDateOfMade(LocalDate dateOfMade) {
this.dateOfMade = dateOfMade;
}

public AuthMethod[] getAuthMethod() {
return authMethod;
}

public void setAuthMethod(AuthMethod[] authMethod) {
this.authMethod = authMethod;
}

public static void setOrderNum(int orderNum) {
Electronic.orderNum = orderNum;
}

@Override
public int hashCode() {
return Objects.hash(productNo, modelName, companyName, dateOfMade, Arrays.hashCode(authMethod));
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Electronic that = (Electronic) obj;
return Objects.equals(productNo, that.productNo) &&
Objects.equals(modelName, that.modelName) &&
companyName == that.companyName &&
Objects.equals(dateOfMade, that.dateOfMade) &&
Arrays.equals(authMethod, that.authMethod);
}

@Override
public String toString() {
return "Electronic{" +
"productNo='" + productNo + '\'' +
", modelName='" + modelName + '\'' +
", companyName=" + companyName +
", dateOfMade=" + dateOfMade +
", authMethod=" + Arrays.toString(authMethod) +
'}';
}
}

24 changes: 24 additions & 0 deletions src/me/day05/practice/Practice01/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package me.day05.practice.Practice01;

import java.time.LocalDate;

public class Test {
public static void main(String[] args) {
//checking electronic
Electronic device1 = new Electronic("iphone13", CompanyName.APPLE, LocalDate.of(2021, 9, 1), new AuthMethod[] {AuthMethod.얼굴인증});
Electronic device2 = new Electronic("iphone8", CompanyName.APPLE, LocalDate.of(2017, 9, 1), new AuthMethod[] {AuthMethod.지문인증});

System.out.println(device1);
System.out.println(device2);
/* 출력
Electronic{productNo='2304180001', modelName='iphone13', companyName=APPLE, dateOfMade=2021-09-01, authMethods=[얼굴인증]}
Electronic{productNo='2304180002', modelName='iphone8', companyName=APPLE, dateOfMade=2017-09-01, authMethods=[지문인증]}
*/

User user1 = new User("abc", "1234", "010-1111-2222", "[email protected]", "010101", new String[] {"device2"});
System.out.println(user1);

//출력 Users{userId='abc', userPassword='1234', userPhoneNumber='010-1111-2222', userEmail='[email protected]', userBirthDate='010101', electronicDevices=[device2], registerTime=2023-04-18}

}
}
120 changes: 120 additions & 0 deletions src/me/day05/practice/Practice01/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package me.day05.practice.Practice01;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.Objects;

public class User {
private String userId;
private String userPassword;
private String userPhoneNumber;
private String userEmail;
private String userBirthDate;
private String[] electronicDevices;

Choose a reason for hiding this comment

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

요거는 String[]이 아니라 Electronic[]이 되어야하지 않을까요??

private LocalDate registerTime;

//객체 생성시 시스템 시간으로 자동 설정
public User() {

Choose a reason for hiding this comment

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

사용되지 않고 있는 생성자네요. 과감히 제거해주시죠~

this.registerTime = LocalDate.now();
}

public User(String userId, String userPassword, String userPhoneNumber, String userEmail, String userBirthDate, String[] electronicDevices) {
this.userId = userId;
this.userPassword = userPassword;
this.userPhoneNumber = userPhoneNumber;
this.userEmail = userEmail;
this.userBirthDate = userBirthDate;
this.electronicDevices = electronicDevices;
this.registerTime = LocalDate.now();

Choose a reason for hiding this comment

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

👍🏻


Choose a reason for hiding this comment

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

불필요한 공백들도 다 제거해주세요~

}

public String getUserId() {
return userId;
}

public void setUserId(String userId) {
this.userId = userId;
}

public String getUserPassword() {
return userPassword;
}

public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}

public String getUserPhoneNumber() {
return userPhoneNumber;
}

public void setUserPhoneNumber(String userPhoneNumber) {
this.userPhoneNumber = userPhoneNumber;
}

public String getUserEmail() {
return userEmail;
}

public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}

public String getUserBirthDate() {
return userBirthDate;
}

public void setUserBirthDate(String userBirthDate) {
this.userBirthDate = userBirthDate;
}

public String[] getElectronicDevices() {
return electronicDevices;
}

public void setElectronicDevices(String[] electronicDevices) {
this.electronicDevices = electronicDevices;
}

public LocalDate getRegisterTime() {
return registerTime;
}

public void setRegisterTime(LocalDate registerTime) {
this.registerTime = registerTime;
}


@Override
public int hashCode() {
return Objects.hash(userId, userPassword, userPhoneNumber, userEmail, userBirthDate, electronicDevices);
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
User user = (User) obj;
return Objects.equals(userId, user.userId) &&
Objects.equals(userPassword, user.userPassword) &&
Objects.equals(userPhoneNumber, user.userPhoneNumber) &&
Objects.equals(userEmail, user.userEmail) &&
Objects.equals(userBirthDate, user.userBirthDate) &&
Arrays.equals(electronicDevices, user.electronicDevices);
}

@Override
public String toString() {
return "Users{" +
"userId='" + userId + '\'' +
", userPassword='" + userPassword + '\'' +
", userPhoneNumber='" + userPhoneNumber + '\'' +
", userEmail='" + userEmail + '\'' +
", userBirthDate='" + userBirthDate + '\'' +
", electronicDevices=" + Arrays.toString(electronicDevices) +
", registerTime=" + registerTime +
'}';
}
}

69 changes: 69 additions & 0 deletions src/me/day05/practice/Practice02/Users.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package me.day05.practice.Practice02;
import me.day05.practice.Practice01.User;

import java.util.Arrays;

public class Users {
private User[] userList;

Choose a reason for hiding this comment

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

변수명에 List와 같은 자료형이 굳이 포함될 필요는 없을 것 같아요. 심지어 배열이기도 하네요 😀

private Users() {

Choose a reason for hiding this comment

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

👍🏻

}

private static Users instance = new Users();

Choose a reason for hiding this comment

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

마찬가지 static 변수는 최상단으로 옮겨주세요~


//2-1 Users 클래스의 객체를 싱글톤으로 생성하는 함수를 작성
public static Users getInstance() {
if (instance == null) {
instance = new Users();
}
return instance;
}

public User[] getUserList() {

Choose a reason for hiding this comment

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

여기서 문제!

이렇게 반환된 배열이 외부에서 변경된다면 원본에도 영향이 있을까요? 없을까요?

return userList;
}

public void setUserList(User[] userList) {
this.userList = userList;
}

//2-2 userId를 통해 인자로 주어진 회원번호에 해당하는 회원을 반환하는 함수를 작성
public User findByUserId(String userId) {
for (User user : userList) {

Choose a reason for hiding this comment

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

동일한 동작을 Stream으로도 작성할 수 있을 것 같은데요. 어떻게 하면 좋을까요?

if (user.getUserId().equals(userId)) {
return user;
}
}
throw new IllegalArgumentException("Not found");

Choose a reason for hiding this comment

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

👍🏻

}

//2-3 인자로 주어진 회원 정보를 깊은 복사 (deepCopy) 하는 함수를 작성
public User copy(User original) {
User user_info = new User(
original.getUserId(),
original.getUserEmail(),
original.getUserPassword(),
original.getUserBirthDate(),
original.getUserPhoneNumber(),
original.getElectronicDevices()

Choose a reason for hiding this comment

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

이렇게 배열을 반환한다면 깊은 복사가 맞을까요?

서로 같은 곳을 참조하고 있지는 않을까요?

);
return user_info;
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Users users = (Users) obj;
return Arrays.equals(userList, users.userList);
}

@Override
public int hashCode() {
return Arrays.hashCode(userList);
}

@Override
public String toString() {
return "{" + "userList=" + Arrays.toString(userList) + "}";
}
}
Loading