-
Notifications
You must be signed in to change notification settings - Fork 0
Java Assignment3 upload by HyunsooJung #21
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
base: main
Are you sure you want to change the base?
Changes from all commits
7c1cac0
e99be9a
b4110f8
c4d5d74
8335a7a
8c81625
82a9719
b532bf1
9b83298
0fce5ad
fbec7c0
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,105 @@ | ||
| package me.day05.practice; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.util.Arrays; | ||
| import java.util.Objects; | ||
|
|
||
| public class Electronic { | ||
|
|
||
| enum CompanyName { SAMSUNG, LG, APPLE } | ||
| enum AuthMethod { FINGERPRINT, PIN, PATTERN, FACE } | ||
|
|
||
| private static final int MAX_REGISTRATION_NUMBER = 9999; | ||
|
|
||
| private static int registrationNo; | ||
|
|
||
| private String productNo; | ||
| private String modelName; | ||
| private CompanyName companyName; | ||
| private String dateOfMade; | ||
| private AuthMethod[] authMethod; | ||
|
|
||
| Electronic () { | ||
| registrationNo++; | ||
| setDateOfMade(); | ||
| setProductNo(); | ||
| } | ||
|
|
||
| Electronic (String modelName, CompanyName companyName, AuthMethod[] authMethod) { | ||
| this(); | ||
| this.modelName = modelName; | ||
| this.companyName = companyName; | ||
| this.authMethod = authMethod; | ||
| } | ||
|
|
||
| private void setDateOfMade(){ | ||
| DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyMMdd"); | ||
| dateOfMade = timeFormatter.format(LocalDate.now()); | ||
| } | ||
|
|
||
| private void setProductNo(){ | ||
| if (registrationNo > MAX_REGISTRATION_NUMBER) registrationNo = 1; | ||
| productNo = dateOfMade + String.format("%4d", registrationNo).replace(" ", "0"); | ||
| } | ||
|
|
||
| public boolean isContainsAuthMethod(AuthMethod authMethod){ | ||
| for (AuthMethod auth : this.authMethod) | ||
| if (authMethod.equals(auth)) return true; | ||
| return false; | ||
| } | ||
|
|
||
| public String getProductNo() { | ||
| 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 String getDateOfMade() { | ||
| return dateOfMade; | ||
| } | ||
|
|
||
| public AuthMethod[] getAuthMethod() { | ||
| return authMethod; | ||
| } | ||
|
|
||
| public void setAuthMethod(AuthMethod[] authMethod) { | ||
| this.authMethod = authMethod; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(productNo, modelName, companyName, dateOfMade, Arrays.hashCode(authMethod)); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| if (this == obj) return true; | ||
| if (obj == null || getClass() != obj.getClass()) return false; | ||
| return Objects.equals(productNo, ((Electronic)obj).productNo); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Electronic { " + | ||
| "productNo=" + productNo + | ||
| ", modelName=" + modelName + | ||
| ", companyName= " + companyName + | ||
| ", dateOfMade=" + dateOfMade + | ||
| ", authMethod=" + Arrays.toString(authMethod) + " }"; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| package me.day05.practice; | ||
|
|
||
| import me.day05.practice.Electronic.AuthMethod; | ||
| import me.day05.practice.Electronic.CompanyName; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| public class Electronics { | ||
|
|
||
| private static final Electronic[] EMPTY_ELECTRONIC_LIST = {}; | ||
|
|
||
| private static Electronic[] electronicList; | ||
| private static Electronics electronicsInstance; | ||
|
|
||
| private int size; | ||
| private int capacity; | ||
|
|
||
| private Electronics(){ | ||
| electronicList = EMPTY_ELECTRONIC_LIST; | ||
| } | ||
|
Member
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. 싱글톤으로 사용하고자 한다면 생성자는 private으로 막아주는 것이 일반적입니다.
Author
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. 헉 너무 기본적인 실수,, 열번 백번 더 확인하겠습니다,,! |
||
|
|
||
| // TODO: 1. Electronics 클래스의 객체를 싱글톤으로 생성하는 함수를 작성하시오. | ||
| public static Electronics getInstance() { | ||
| if (electronicsInstance == null) electronicsInstance = new Electronics(); | ||
| return electronicsInstance; | ||
| } | ||
|
|
||
| // TODO: 2. 전자제품 일련번호 productNo를 통해 인자로 주어진 일련번호에 해당하는 전자제품을 반환하는 함수를 작성하시오. | ||
| public Optional<Electronic> findByProductNo(String productNo){ | ||
| for (Electronic electronic : electronicList) | ||
| if (productNo.equals(electronic.getProductNo())) | ||
| return Optional.of(electronic); | ||
|
|
||
| return Optional.empty(); | ||
| } | ||
|
Comment on lines
+29
to
+35
Author
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.
다시 생각해보니
Member
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. 이정도면 충분할 것 같습니다. (사실 해도 됩니다. null 체크야 해서 나쁠건 없지만, 지금 코드가 보편적이긴 합니다.) |
||
|
|
||
| // TODO: 3. 전자제품들 중 인자로 주어진 제조 회사를 찾아서 하나의 배열에 반환하는 함수를 작성하시오. | ||
| public Optional<Electronic[]> groupByCompanyName(CompanyName company){ | ||
|
|
||
| List<Electronic> temp = new ArrayList<>(); | ||
|
|
||
| for (Electronic electronic : electronicList) | ||
| if (electronic.getCompanyName().equals(company)) | ||
| temp.add(electronic); | ||
|
|
||
| Electronic[] companyNameGroup = | ||
| temp.isEmpty() ? null : listToArray(temp); | ||
|
|
||
| return Optional.ofNullable(companyNameGroup); | ||
| } | ||
|
|
||
| public Optional<Electronic> findByCompanyName(CompanyName company){ | ||
| for (Electronic electronic : electronicList) | ||
| if (company.equals(electronic.getCompanyName())) | ||
| return Optional.of(electronic); | ||
|
|
||
| return Optional.empty(); | ||
| } | ||
|
|
||
| // TODO: 4. 전자제품들 중 인자로 주어진 인증 방법을 찾아서 하나의 배열에 반환하는 함수를 작성하시오. | ||
| public Optional<Electronic[]> groupByAuthMethod(AuthMethod authMethod){ | ||
|
|
||
| List<Electronic> temp = new ArrayList<>(); | ||
|
|
||
| for (Electronic electronic : electronicList) | ||
| if (electronic.isContainsAuthMethod(authMethod)) | ||
| temp.add(electronic); | ||
|
|
||
| Electronic[] authMethodNameGroup = | ||
| temp.isEmpty() ? null : listToArray(temp); | ||
|
|
||
| return Optional.ofNullable(authMethodNameGroup); | ||
| } | ||
|
|
||
| private Electronic[] listToArray(List<Electronic> list){ | ||
| Electronic[] array = new Electronic[list.size()]; | ||
|
|
||
| for (int i = 0; i < array.length; i++) | ||
| array[i] = list.get(i); | ||
|
|
||
| return array; | ||
| } | ||
|
|
||
| public int getSize() { | ||
| return size; | ||
| } | ||
|
|
||
| public void setSize(int size) { | ||
| this.size = size; | ||
| } | ||
|
|
||
| public int getCapacity() { | ||
| return capacity; | ||
| } | ||
|
|
||
| public void setCapacity(int capacity) { | ||
| this.capacity = capacity; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(Arrays.hashCode(electronicList), size, capacity); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| if (this == obj) return true; | ||
| if (obj == null || getClass() != obj.getClass()) return false; | ||
| return Objects.equals(hashCode(), ((Electronics)obj).hashCode()); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Users { " + | ||
| "size=" + size + | ||
| ", capacity=" + capacity + | ||
| ", electronicList= " + Arrays.toString(electronicList) + " }"; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| package me.day05.practice; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.util.Arrays; | ||
| import java.util.Objects; | ||
|
|
||
| public class User { | ||
|
|
||
| private String userId; | ||
| private String userPassword; | ||
| private String userPhoneNumber; | ||
| private String userEmail; | ||
| private String userBirthDate; | ||
| private Electronic[] electronicDevice; | ||
| private LocalDate registerTime; | ||
|
|
||
| User() { | ||
| setRegisterTime(); | ||
| } | ||
|
|
||
| User(String userId, String userPassword, String userPhoneNumber, String userEmail, String userBirthDate, Electronic[] electronicDevice) { | ||
| this(); | ||
| this.userId = userId; | ||
| this.userPassword = userPassword; | ||
| this.userPhoneNumber = userPhoneNumber; | ||
| this.userEmail = userEmail; | ||
| this.userBirthDate = userBirthDate; | ||
| this.electronicDevice = electronicDevice; | ||
| } | ||
|
|
||
| private User(String userId, String userPassword, String userPhoneNumber, String userEmail, String userBirthDate, Electronic[] electronicDevice, LocalDate registerTime){ | ||
| this.userId = userId; | ||
| this.userPassword = userPassword; | ||
| this.userPhoneNumber = userPhoneNumber; | ||
| this.userEmail = userEmail; | ||
| this.userBirthDate = userBirthDate; | ||
| this.electronicDevice = electronicDevice; | ||
| this.registerTime = registerTime; | ||
| } | ||
|
|
||
| private void setRegisterTime() { | ||
| registerTime = LocalDate.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[] getElectronicDevice() { | ||
| return electronicDevice; | ||
| } | ||
|
|
||
| public void setElectronicDevice(Electronic[] electronicDevice) { | ||
| this.electronicDevice = electronicDevice; | ||
| } | ||
|
|
||
| public LocalDate getRegisterTime() { | ||
| return registerTime; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(userId, userPassword, userEmail, userBirthDate, Arrays.hashCode(electronicDevice), registerTime); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| if (this == obj) return true; | ||
| if (obj == null || getClass() != obj.getClass()) return false; | ||
| return Objects.equals(hashCode(), ((User)obj).hashCode()); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "User { " + | ||
| "userId=" + userId + | ||
| ", userPassword=" + userPassword + | ||
| ", userPhoneNumber= " + userPhoneNumber + | ||
| ", userEmail=" + userEmail + | ||
| ", userBirthDate=" + userBirthDate + | ||
| ", electronicDevice=" + Arrays.toString(electronicDevice) + | ||
| ", registerTime=" + registerTime + " }"; | ||
| } | ||
|
|
||
| @Override | ||
| protected User clone() { | ||
| return new User(userId, userPassword, userPhoneNumber, userEmail, userBirthDate, electronicDevice, registerTime); | ||
| } | ||
| } | ||
|
|
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.
꼭 모든 프로퍼티가 아니라 특정 몇개의 프로퍼티로 hashCode를 만들기도 하죠. 좋은 아이디어네요.
다만 hashCode는 속도 비교가 아니라 registrationNo가 Electoronic의 해시값이 될 수 있도록 골고루 분산되어 있을까를 더 고민해야합니다.
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.
이 부분은 한번 찾아보고, 고민해 보겠습니다