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 TaehyoungSong #36

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions src/me/day05/practice/Practice01/AuthMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package me.day05.practice.Practice01;

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

public enum CompanyName {
SAMSUNG, LG, APPLE
}
121 changes: 121 additions & 0 deletions src/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;

public class Electronic {
private static int serialNum = 0;
private String productNo;
private String modelName;
private CompanyName companyName;
private LocalDate dateOfMade;
private ArrayList<AuthMethod> authMethod;
Copy link
Member

Choose a reason for hiding this comment

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

ArrayList의 타입을 ArrayList보다 List를 사용해보세요.
이유가 뭘까요? 어떤 장점이 있을까요?

Copy link
Author

Choose a reason for hiding this comment

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

authMethod 안에 들어가는 내용이 0~4개까지 유동적이기 때문에 ArrayList로 변경을 해서 작성을 해보았습니다.
List로 구현을 했을 경우에 추후에 ArrayList로 변경을 할 수 있으니
List로 선언을 해두는 것이 더 유연하고 확장성이 좋을 듯 하네요.

Copy link
Member

Choose a reason for hiding this comment

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

네 맞아요. 구현체에 변화를 주기 좋은 부분은 상위 타입으로 선언하고 그 타입 안에서 메서드를 사용하면 좋아요.
태형님 말씀대로 구현체를 바꿔도 문제가 없을테니 말이죠.


private Electronic() {
serialNum++;
setProductNo();
}

public Electronic(String productNo) {
this.productNo = productNo;
}

public Electronic(String modelName, CompanyName companyName, LocalDate dateOfMade, ArrayList<AuthMethod> authMethod) {
this();
this.modelName = modelName;
this.companyName = companyName;
this.dateOfMade = dateOfMade;
this.authMethod = authMethod;
}
Copy link
Member

Choose a reason for hiding this comment

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

생성자 꼼꼼하시네요!!!
productNo를 직접 넣어 생성하는 상황까지 잘 고려해주셨습니다. 👍

Copy link
Author

Choose a reason for hiding this comment

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

생성자를 만드는 기준은 로직상에 생성되는 조건들을 고려하여 생성하면 되는걸까요?
과제에 대한 부분은 일부만 사용하기 떄문에 전체적인 것을 고려하는게 잘 되지 않는 것 같습니다.
생성자를 만드는 기준이 있을까요?

Copy link
Member

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

private String setProductNo() {

StringBuilder sb = new StringBuilder(10);
sb.append(LocalDate.now().format(DateTimeFormatter.ofPattern("yyMMdd")));

if ( (int)Math.log10(serialNum)+1 == 1){
sb.append("000");
sb.append(serialNum);
} else if ( (int)Math.log10(serialNum)+1 == 2) {
sb.append("00");
sb.append(serialNum);
} else if ( (int)Math.log10(serialNum)+1 == 3) {
sb.append("0");
sb.append(serialNum);
} else {
sb.append(serialNum);
}

productNo = String.valueOf(sb);
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 ArrayList<AuthMethod> getAuthMethod() {
return authMethod;
}

public void setAuthMethod(ArrayList<AuthMethod> authMethod) {
this.authMethod = authMethod;
}

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

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

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

public int authMethodSize(){
AuthMethod[] values = AuthMethod.values();

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

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

public class User {

private String userId;
private String userPassword;
private String userPhoneNumber;
private String userEmail;
private LocalDate userBirthDate;
private String[] electronicDevices;
private LocalDateTime registerTime;

public User() {
registerTime = LocalDateTime.now();
}

public User(String userId) {
this.userId = userId;
}

public User(String userId, String userPassword) {
this();
this.userId = userId;
this.userPassword = userPassword;

Copy link
Member

Choose a reason for hiding this comment

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

이상하게 띄워져 있네요.

}

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

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 String[] getElectronicDevices() {
return electronicDevices;
}

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

public LocalDateTime getRegisterTime() {
return registerTime;
}

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

@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) && Arrays.equals(electronicDevices, user.electronicDevices) && Objects.equals(registerTime, user.registerTime);
}

@Override
public int hashCode() {
int result = Objects.hash(userId, userPassword, userPhoneNumber, userEmail, userBirthDate, registerTime);
result = 31 * result + Arrays.hashCode(electronicDevices);
Copy link
Member

Choose a reason for hiding this comment

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

31은 어떤 의미일까요?

Copy link
Author

Choose a reason for hiding this comment

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

31이라는 값이 소수로 자바에서 hashcode를 생성하는데에 고유한 값을 만든다고 실시간 강의에서 하셨던 것 같습니다.

s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]

구글 링을 해보니 31 이외에도 성능 또는 프로세서와 이유로 다른 소수를 선택하기도 한다고 하네요.
29, 33, 37, 92821 ... 여러 소수들이 프로세서에 따라 속도의 차이가 약간 있는 듯 합니다.
31은 최고의 속도는 아니지만 일정 수준을 보존하는 가성비가 있는 값인 것 같습니다.

return result;
}

@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 src/me/day05/practice/Practice01/userTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package me.day05.practice.Practice01;

import me.day05.practice.Practice02.Users;
import me.day05.practice.Practice03.Electronics;

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

public class userTest {
public static void main(String[] args) {
Copy link
Member

Choose a reason for hiding this comment

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

오오
테스트를 위한 코드를 따로 빼신 것 너무 좋습니다.
일반 Class 내부에 많이들 Main을 넣으셨는데 다 제거 요청드렸거든요. 잘하셨습니다.

User userSong = new User("Song", "a1234");

System.out.println(userSong);

String[] electronicDevices = {"GalaxyS", "GalaxyTablet"};

User userTae = new User("Tae","b5678",
"010-8885-9677","[email protected]", LocalDate.of(1995,8,29), electronicDevices);

System.out.println(userTae);

ArrayList<AuthMethod> authArray = new ArrayList<>();
ArrayList<AuthMethod> authArray2 = new ArrayList<>();

authArray.add(AuthMethod.FINGERPRINT);
authArray.add(AuthMethod.PIN);
authArray2.add(AuthMethod.PATTERN);
authArray2.add(AuthMethod.FACE);
authArray2.add(AuthMethod.FINGERPRINT);

Electronic electronic = new Electronic("Galaxy", CompanyName.SAMSUNG, LocalDate.now(), authArray);
Electronic electronic2 = new Electronic("IPhone", CompanyName.APPLE, LocalDate.now(), authArray2);

System.out.println(electronic);
System.out.println(electronic2);

Users user = Users.getInstance();
User userHyoung = user.createUser("Hyoung", "c8901");
User userTaeHyoung = user.createUser("TaeHyoung", "d9021");
User userSonng = user.createUser("Tae","b5678",
"010-8885-9677","[email protected]", LocalDate.of(1995,8,29), electronicDevices);

System.out.println(userHyoung);
System.out.println(userTaeHyoung);
System.out.println(userSonng);
System.out.println(Arrays.toString(user.getUserList()));
System.out.println(user.findByUserId("Hyoung"));
System.out.println(Users.copy(userHyoung));


System.out.println("=============");

Electronics elec = Electronics.getInstance();
Electronic elecA = elec.createElectronic("Galaxy", CompanyName.SAMSUNG, LocalDate.now(), authArray);
Electronic elecB = elec.createElectronic("IPhone", CompanyName.APPLE, LocalDate.now(), authArray2);

System.out.println(elecA);
System.out.println(elec.findByProductNo("2304180003"));
System.out.println(Arrays.toString(elec.groupByCompanyName(CompanyName.SAMSUNG)));
System.out.println(elecB);
System.out.println(Arrays.toString(elec.groupByAuthMethod(AuthMethod.FINGERPRINT)));





}
}
Loading