-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
c5b2494
968d478
d0ff19b
ef3d3df
007dcdd
7b47775
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
} |
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 | ||
} |
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; | ||
|
||
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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 생성자 꼼꼼하시네요!!! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 생성자를 만드는 기준은 로직상에 생성되는 조건들을 고려하여 생성하면 되는걸까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
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; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 31은 어떤 의미일까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 이외에도 성능 또는 프로세서와 이유로 다른 소수를 선택하기도 한다고 하네요. |
||
return result; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "User{" + | ||
"userId='" + userId + '\'' + | ||
", userPassword='" + userPassword + '\'' + | ||
", userPhoneNumber='" + userPhoneNumber + '\'' + | ||
", userEmail='" + userEmail + '\'' + | ||
", userBirthDate=" + userBirthDate + | ||
", electronicDevices=" + Arrays.toString(electronicDevices) + | ||
", registerTime=" + registerTime + | ||
'}'; | ||
} | ||
} |
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오오 |
||
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))); | ||
|
||
|
||
|
||
|
||
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ArrayList의 타입을 ArrayList보다 List를 사용해보세요.
이유가 뭘까요? 어떤 장점이 있을까요?
There was a problem hiding this comment.
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로 선언을 해두는 것이 더 유연하고 확장성이 좋을 듯 하네요.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
네 맞아요. 구현체에 변화를 주기 좋은 부분은 상위 타입으로 선언하고 그 타입 안에서 메서드를 사용하면 좋아요.
태형님 말씀대로 구현체를 바꿔도 문제가 없을테니 말이죠.