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 SeongminLee #39

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
88 changes: 88 additions & 0 deletions Practice_03/Electronic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package Practice_03;

import java.util.*;
import java.time.*;
import java.time.format.DateTimeFormatter;

public class Electronic {

public enum Company { SAMSUNG, LG, APPLE }

Choose a reason for hiding this comment

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

Enum 클래스도 분리하여 별도로 관리되면 좋을 것 같네요~

public enum AuthMethod { FINGERPRINT, PIN, PATTERN, FACE }

private static int registrationNo;
private String productNo;
private String modelName;
private Company company;
private LocalDate dateOfMade;
private AuthMethod[] authMethods;

public Electronic(String modelName, Company company, AuthMethod[] authMethods) {
this.modelName = modelName;
this.company = company;
this.authMethods = authMethods;
this.productNo = generateProductNo();

Choose a reason for hiding this comment

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

사소하지만 멤버 변수 선언 순서와 생성자에서의 순서를 일치시켜주시면 좋을 것 같습니다.

}

private String generateProductNo() {

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(DateTimeFormatter.ofPattern("yyMMdd"));
registrationNo++;
registrationNo %= 100000;
String registrationNoStr = String.format("%04d", registrationNo);
return date + registrationNoStr;

Choose a reason for hiding this comment

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

String 연산에 어떤 것들이 추가적으로 있을까요? 한번 공부해보시면 좋을 것 같네요.

}

public boolean containsAuthMethod(AuthMethod authMethod) {

Choose a reason for hiding this comment

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

👍🏻 너무 좋은데요?

return Arrays.asList(authMethods).contains(authMethod);
}

public String getProductNo() {
return productNo;
}

public String getModelName() {
return modelName;
}

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

public Company getCompany() {
return company;
}

public void setCompany(Company company) {
this.company = company;
}

public LocalDate getDateOfMade() {
return dateOfMade;
}

public AuthMethod[] getAuthMethods() {
return authMethods;
}

public void setAuthMethods(AuthMethod[] authMethods) {
this.authMethods = authMethods;
}

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

@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(this.productNo, that.productNo);
}

@Override
public String toString() {
return String.format("Electronic { productNo=%s, modelName=%s, company=%s, authMethods=%s }",
productNo, modelName, company, Arrays.toString(authMethods));
}
}
71 changes: 71 additions & 0 deletions Practice_03/Electronics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package Practice_03;

import java.util.*;
import Practice_03.Electronic.*;

public class Electronics {
private static Electronics instance;
private static Electronic[] electronicList;

private Electronics() {
}

public static Electronics getInstance() {
if (instance == null) {
instance = new Electronics();
}
return instance;
}

public Electronic findByProductNo(String productNo) {
for (Electronic electronic : electronicList) {
if (electronic.getProductNo().equals(productNo)) {
return electronic;
}
}
return null;
}

public Electronic[] groupByCompanyName(Company company) {
List<Electronic> electronics = new ArrayList<>();
for (Electronic electronic : electronicList) {
if (electronic.getCompany() == company) {
electronics.add(electronic);
}
}
return electronics.toArray(new Electronic[electronics.size()]);
}

public Electronic[] groupByAuthMethod(AuthMethod authMethod) {
List<Electronic> groupAuthList = new ArrayList<>();
for (Electronic electronic : electronicList) {
if (Arrays.equals(electronic.getAuthMethods(), new AuthMethod[] {authMethod})) {
groupAuthList.add(electronic);
}
}
return groupAuthList.toArray(new Electronic[groupAuthList.size()]);
}

public static Electronic[] getElectronicList() {
return electronicList;
}

public static void setElectronicList(Electronic[] electronicList) {
Electronics.electronicList = electronicList;
}

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

@Override
public boolean equals(Object obj) {
return super.equals(obj);
}

@Override
public String toString() {
return "Electronics{}";
}
}
118 changes: 118 additions & 0 deletions Practice_03/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package Practice_03;

import java.time.*;
import java.util.*;

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

Choose a reason for hiding this comment

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

생년월일을 표현하는 변수가 String은 조금 어색한 것 같습니다. 적절한 것이 없을까요?

private Electronic[] electronicDevices;
private LocalDateTime registerTime;

public User() {

Choose a reason for hiding this comment

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

해당 생성자가 내부에서만 사용된다면 private으로 지정해도 괜찮을 것 같네요~

this.electronicDevices = new Electronic[10];
this.registerTime = LocalDateTime.now();
}

public User(String userId, String userPassword, String userPhoneNumber, String userEmail, String userBirthDate) {
this.userId = userId;
this.userPassword = userPassword;
this.userPhoneNumber = userPhoneNumber;
this.userEmail = userEmail;
this.userBirthDate = userBirthDate;
this.electronicDevices = new Electronic[10];
this.registerTime = LocalDateTime.now();
}

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 Electronic[] getElectronicDevices() {

Choose a reason for hiding this comment

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

여기서 문제~!

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

return electronicDevices;
}

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

public LocalDateTime getRegisterTime() {
return registerTime;
}

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

@Override
public boolean equals(Object object) {
if (this == object) return true;
if (object == null || this.getClass() != object.getClass()) return false;

User user = (User) object;

if (!Objects.equals(userId, user.userId)) return false;
if (!Objects.equals(userPassword, user.userPassword)) return false;
if (!Objects.equals(userPhoneNumber, user.userPhoneNumber)) return false;
if (!Objects.equals(userEmail, user.userEmail)) return false;
if (!Objects.equals(userBirthDate, user.userBirthDate)) return false;
return Objects.equals(registerTime, user.registerTime);
}

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

@Override
public String toString() {
return "User{" +
"userId='" + userId + '\'' +
", userPassword='" + userPassword + '\'' +
", userPhoneNumber='" + userPhoneNumber + '\'' +
", userEmail='" + userEmail + '\'' +
", userBirthDate='" + userBirthDate + '\'' +
", electronicDevices=" + Arrays.toString(electronicDevices) +
", registerTime=" + registerTime +
'}';
}
}
69 changes: 69 additions & 0 deletions Practice_03/Users.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package Practice_03;

import java.util.*;

public class Users {
private static Users instance;

Choose a reason for hiding this comment

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

Suggested change
private static Users instance;
private static Users instance;

줄바꿈을 추가해주면 좋을 것 같습니다~

private User[] userList;

Choose a reason for hiding this comment

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

List라는 단어가 굳이 변수명에 포함될 필요는 없을 것 같아요. 심지어 List도 아닌 배열이네요😀


private Users() {

Choose a reason for hiding this comment

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

👍🏻

}

public static Users getInstance() {
if (instance == null) {
instance = new Users();
}
return instance;
}

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

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

public User findByUserId(String userId) {
if (userList == null) {
return null;

Choose a reason for hiding this comment

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

꼼꼼하게 널 체크를 해주는 습관은 좋습니다.
다만, null을 바로 반환하지말고 빈 객체, Optional 또는 적절한 예외를 반환하는게 어떨까요?

}
for (User userInfo : userList) {

Choose a reason for hiding this comment

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

동일한 동작을 Stream을 사용해서 수행할 수 있을 것 같습니다. 어떻게 하면 좋을까요?

if (userInfo.getUserId().equals(userId)) {
return userInfo;
}
}
return null;
}

public User copy(User user) {
User copiedUser = new User();
copiedUser.setUserId(user.getUserId());
copiedUser.setUserEmail(user.getUserEmail());
copiedUser.setUserPassword(user.getUserPassword());
copiedUser.setUserBirthDate(user.getUserBirthDate());
copiedUser.setUserPhoneNumber(user.getUserPhoneNumber());
copiedUser.setElectronicDevices(Arrays.copyOf(user.getElectronicDevices(), user.getElectronicDevices().length));

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.

Cloneable 인터페이스와 clone() 메서드에 대해 추가적으로 학습해봐요.

현재 상황에서는 User 내부의 Electronic[]을 복사본과 원본이 공유하게 됩니다.

return copiedUser;
}

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

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

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