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

[ADD] Mingi Park's 답변 #35

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
8 changes: 8 additions & 0 deletions me/day05/practice/Practice01/Auth.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package me.day05.practice.Practice01;

public enum Auth {
FINGERPRINT,
PATTERN,
PIN,
FACE
}
7 changes: 7 additions & 0 deletions me/day05/practice/Practice01/Company.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.day05.practice.Practice01;

public enum Company {
SAMSUNG,
LG,
APPLE
}
121 changes: 121 additions & 0 deletions me/day05/practice/Practice01/Electronic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package me.day05.practice.Practice01;

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

/**
* - `Electronic` 클래스
* - 필드
* - 제품 일련번호 `productNo`
* - 제품 번호는 `Electronic` 클래스의 객체 생성시 자동 생성한다고 가정
* - 등록한 날짜
* - 연도 (2자리) + 월 (2자리) + 일 (자리) — [LocalDate](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html) 클래스 이용
* - 등록된 제품 순서
* - 4자리 등록 순서 — static 변수로 총 객체 수 저장
* - 예시
* - 현재 작성일 기준 일자는 2023/03/30 이다.
* - 230330 + 0001 (4자리 등록 순서) ⇒ *2303300001*
* - 전자기기 모델명 `modelName`
* - 제조 회사명 `companyName`
* - SAMSUNG, LG, APPLE — Enum형으로 정의
* - 하나의 전자 기기에는 다수의 인증 방법이 있을 수도 있음 — 배열로 정의
* -
* - 메소드
* - 생성자, getter(), setter(), hashCode(), equals(), toString()
*/
public class Electronic {
private String productNo;
private String modelName;
private Company companyName;
private LocalDate dateOfMade;
private ArrayList<Auth> authMethods = new ArrayList<>();
private static int serialNum = 0;

Choose a reason for hiding this comment

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

static 변수는 일반 변수보다 위에 위치하도록 하는 것이 일반적이예요! 위로 옮겨볼까요~


public Electronic() {}

public Electronic(String productNo, String modelName, Company companyName, LocalDate dateOfMade, Auth auth) {
LocalDate dt = LocalDate.now();
serialNum++;

Choose a reason for hiding this comment

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

이 부분은 밑에 코드와 합쳐도 될 것 같아요!

this.modelName = modelName;
this.productNo = dt.format(DateTimeFormatter.ofPattern("yyMMdd"));
this.productNo += String.format("%04d", serialNum);

Choose a reason for hiding this comment

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

데이터의 변환 과정이 잘 보이게 작성해주셨는데, + 연산보다 더 좋은 연산이 있지 않을까요? String에서 + 연산은 속도가 느려요! 지금은 간단한 연산이기에 크게 차이를 못 느낄 수 있지만, 연산이 복잡해질 수록 차이는 확연해져요! 다른 연산을 한번 찾아볼까요?

this.companyName = companyName;
authMethods.add(auth);

this.productNo = productNo;
this.modelName = modelName;
this.companyName = companyName;
this.dateOfMade = dateOfMade;
}

public String getProductNo() {
return productNo;
}

public void setProductNo(String productNo) {
this.productNo = productNo;
}

public String getModelName() {
return modelName;
}

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

public Company getCompanyName() {
return companyName;
}

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

public LocalDate getDateOfMade() {
return dateOfMade;
}

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

public ArrayList<Auth> getAuthMethods() {
return authMethods;
}

public void setAuthMethods(ArrayList<Auth> authMethods) {
this.authMethods = authMethods;
}

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

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

@Override
public String toString() {
return "Electronic{" +
"productNo='" + productNo + '\'' +
", modelName='" + modelName + '\'' +
", companyName=" + companyName +
", dateOfMade=" + dateOfMade +
", authMethods=" + authMethods +
'}';
}
}
133 changes: 133 additions & 0 deletions me/day05/practice/Practice01/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package me.day05.practice.Practice01;

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

/**
* - `User` 클래스
* - 필드
* - 회원 아이디 `userId`
* - 회원 비밀번호 `userPassword`
* - 회원 핸드폰번호 `userPhoneNumber`
* - 회원 이메일 `userEmail`
* - 회원 생년월일 `userBirthDate`
* - 사용 중인 전자 제품들 `electronicDevices`
* - 하나의 사용자는 여러 개의 사용 중인 전자 제품이 있을 수 있음 — 배열로 정의
* - 회원 정보가 등록된 시스템 시간 `registerTime`
* - 객체 생성시 시스템 시간으로 자동 설정됨
* - 메소드
* - 생성자, getter(), setter(), hashCode(), equals(), toString()
*
*/
public class User {

private String userId;
private String userPassword;
private String userPhoneNumber;
private String userEmail;
private LocalDate userBirthDate;
private LocalDate registerTime;

Choose a reason for hiding this comment

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

시간을 의미하는 변수이니 더 적절한 자료형이 있지 않을까요~?

private ArrayList<Electronic> electronicDevices;

public User() {}

public User(String userId, String userPassword, String userPhoneNumber, String userEmail, LocalDate userBirthDate) {
this.userId = userId;
this.userPassword = userPassword;
this.userPhoneNumber = userPhoneNumber;
this.userEmail = userEmail;
this.userBirthDate = userBirthDate;
registerTime = setRegisterTime();

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 LocalDate getUserBirthDate() {
return userBirthDate;
}

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

public LocalDate getRegisterTime() {
return registerTime;
}

public LocalDate setRegisterTime() {
return LocalDate.now(ZoneId.systemDefault());
}

public ArrayList<Electronic> getElectronicDevices() {
return electronicDevices;
}

public void setElectronicDevices(ArrayList<Electronic> electronicDevices) {
this.electronicDevices = electronicDevices;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
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) &&
Objects.equals(registerTime, user.registerTime) &&
Objects.equals(electronicDevices, user.electronicDevices);
}

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

@Override
public String toString() {
return "User{" +
"userId='" + userId + '\'' +
", userPassword='" + userPassword + '\'' +
", userPhoneNumber='" + userPhoneNumber + '\'' +
", userEmail='" + userEmail + '\'' +
", userBirthDate=" + userBirthDate +
", registerTime=" + registerTime +
", electronicDevices=" + electronicDevices +

Choose a reason for hiding this comment

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

이렇게 출력하게 되면 원하는 결과가 나올까요~?

'}';
}
}
88 changes: 88 additions & 0 deletions me/day05/practice/Practice02/Users.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package me.day05.practice.Practice02;

import me.day05.practice.Practice01.Electronic;
import me.day05.practice.Practice01.User;

import java.util.ArrayList;
import java.util.Arrays;

/**
* - 객체 배열 `User[] userList`를 필드로 있는 `Users` 클래스를 작성하시오.
* - 필드 - User[] userList (생성된 User 객체들을 모두 저장)
* - 메소드 - 생성자, getter(), setter(), hashCode(), equals(), toString()
* - `Users` 클래스를 통해 아래의 문제를 해결해보세요.
* 1. `Users` 클래스의 객체를 싱글톤으로 생성하는 함수를 작성하시오.
* -> Users getInstance();
* 2. 회원 아이디 userId를 통해 인자로 주어진 회원번호에 해당하는 회원을 반환하는 함수를 작성하시오.
* -> User findByUserId(String userId);
* 3. 인자로 주어진 회원 정보를 깊은 복사 (deepCopy) 하는 함수를 작성하시오.
* -> User findByUserId(String userId);
*/
public class Users {
private User [] userList; //생성된 User 객체들을 모두 저장

Choose a reason for hiding this comment

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

사소하지만 이런 부분들이 Code Convention으로 보일 수 있기 때문에 불필요한 공백은 제거해 주세요~ 👍

private static Users instance;


private Users () {}

public User[] getUserList() {
return userList;
}

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

public static Users getInstance() {

Choose a reason for hiding this comment

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

일반 메소드보다 위에 위에 위치하는 것이 좋을 것 같습니다!

if (instance == null) {
instance = new Users();
}
return instance;
}
//2. 회원 아이디 userId를 통해 인자로 주어진 회원번호에 해당하는 회원을 반환하는 함수를 작성.
public User findByUserId(String userId) {
if (userId == null) return null;

Choose a reason for hiding this comment

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

오~ 좋아요~ userId에 대한 유효성 검증을 추가하시다니! 좋습니다.


for (User user : userList) {
if (user == null) continue;
if (user.getUserId().equals(userId)) return user;

Choose a reason for hiding this comment

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

잘하셨어요~ ㅎㅎ하나 과제를 내드리면, stream으로 한번 수정해 보시죠!

}
return null;
}

public User copy(User user) {
if (user == null) return null;
int userListsize = user.getElectronicDevices().size();

User copyUser = new User(user.getUserId(), user.getUserPassword(), user.getUserPhoneNumber(), user.getUserEmail(), user.getUserBirthDate());

ArrayList<Electronic> usingDevices = new ArrayList<Electronic>();

for (int i = 0; i < userListsize; i++) {
usingDevices.add(user.getElectronicDevices().get(i));
}
copyUser.setElectronicDevices(usingDevices);

return copyUser;
}

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

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

Choose a reason for hiding this comment

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

조금 더 견고하게 작성할 수 있을까요~?

}

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