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

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
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 AuthMethod[] authMethod;

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

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

public Electronic(String modelName, CompanyName companyName, LocalDate dateOfMade, 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);
}
String order = String.format("%04d", serialNum);
String date = LocalDate.now(ZoneId.systemDefault()).toString().replace("-", "").substring(2);

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 AuthMethod[] getAuthMethod() {
return authMethod;
}

public void setAuthMethod(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=" + Arrays.toString(authMethod) +
'}';
}

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

return values.length;
}
}
124 changes: 124 additions & 0 deletions src/me/day05/practice/Practice01/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
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;
}

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

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

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

import static me.day05.practice.Practice01.AuthMethod.*;


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


//Users user = new Users(); - 싱글톤 패턴이기 때문에 불가능
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 electronics = Electronics.getInstance();

AuthMethod[] authArray = {FINGERPRINT, PIN};
AuthMethod[] authArray2 = {PATTERN, FACE};

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

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

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

Electronic electronicsA = electronics.createElectronic("Galaxy", CompanyName.SAMSUNG, LocalDate.now(), authArray);
Electronic electronicsB = electronics.createElectronic("IPhone", CompanyName.APPLE, LocalDate.now(), authArray2);

System.out.println(electronicsA);
System.out.println(electronics.findByProductNo("2304250003"));
System.out.println(Arrays.toString(electronics.groupByCompanyName(CompanyName.SAMSUNG)));
System.out.println(Arrays.toString(electronicsB.getAuthMethod()));
System.out.println(Arrays.toString(electronics.groupByAuthMethod(FINGERPRINT)));
}
}
Loading