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 Hyeonjinyun #18

Open
wants to merge 2 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
5 changes: 5 additions & 0 deletions Practice01/AuthMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package Practice01;

public class AuthMethod {
public enum authMethod{Fingerprint, Pattern, Pin, Face};

Choose a reason for hiding this comment

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

Enum 자체가 클래스가 될 수 있답니다!!

}
5 changes: 5 additions & 0 deletions Practice01/Company.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package Practice01;

public class Company {
public enum companyName {SAMSUNG, LG, APPLE}
}
78 changes: 78 additions & 0 deletions Practice01/Electronic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package Practice01;

import java.util.Arrays;
import java.util.Objects;

public class Electronic {

Choose a reason for hiding this comment

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

요구사항을 꼼꼼히 읽어주시면 좋을 것 같아요!!

생성자가 빠져있네요 ㅠ

private String productNo;
private String modelName;
private String dateOfMade;

Choose a reason for hiding this comment

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

LocalDateTime이나 LocalDate를 사용하는 게 어떨까요?


private Company companyName;
private AuthMethod[] arrAuth;

Choose a reason for hiding this comment

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

굳이 어떤 자료형인지를 변수명에 나타낼 필요는 없을 것 같아요~

Suggested change
private AuthMethod[] arrAuth;
private AuthMethod[] authMethods;



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

public String getProductNo() {
return productNo;
}

public void setProductNo(String productNo) {
this.productNo = productNo;
}

public String getModelName() {
return modelName;
}

public void setModelName(String modelName) {
this.modelName = modelName;
}
public Company getCompanyName() {
return companyName;
}

public void setCompanyName(Company companyName) {
this.companyName = companyName;
}
public String getDateOfMade() {
return dateOfMade;
}

public void setDateOfMade(String dateOfMade) {
this.dateOfMade = dateOfMade;
}

public AuthMethod[] getArrAuth() {
return arrAuth;
}

public void setArrAuth(AuthMethod[] arrAuth) {
this.arrAuth = arrAuth;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Electronic e = (Electronic) o;
return Objects.equals(productNo, e.productNo) &&
Objects.equals(modelName, e.modelName) &&
Objects.equals(dateOfMade, e.dateOfMade);
}

@Override
public int hashCode() {
int result=Objects.hash(productNo, modelName, dateOfMade);
result=31*result+ Arrays.hashCode(arrAuth);
return result;
}
}
101 changes: 101 additions & 0 deletions Practice01/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package Practice01;

import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Objects;

public class User {

String userId;
hybiis marked this conversation as resolved.
Show resolved Hide resolved
String userPassword;
String userPhoneNumber;
String userEmail;
String userBirthDate;

ArrayList<String> electronicDevices =new ArrayList<>();

Choose a reason for hiding this comment

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

String이 아닌 위에서 만든 Electronic가 담겨야하지 않을까요?

LocalTime registertime=LocalTime.now();//현재 시간 출력

Choose a reason for hiding this comment

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

생성자에서 초기화해주면 좋을 것 같아요~




public String toString(){
return "[User info] Id: " + userId+"PassWord: "+userPassword+"PhoneNumber: "
+userPhoneNumber+"Email: "+userEmail+"BirthDate: "+userBirthDate+
"Registertime: "+registertime+"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 String getUserBirthDate() {
return userBirthDate;
}

public void setUserBirthDate(String userBirthDate) {
this.userBirthDate = userBirthDate;
}

public ArrayList<String> getElectronicDevices() {
return electronicDevices;
}

public void setElectronicDevices(ArrayList<String> electronicDevices) {
this.electronicDevices = electronicDevices;
}

public LocalTime getRegistertime() {
return registertime;
}

public void setRegistertime(LocalTime registertime) {
this.registertime = registertime;
}

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

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User u = (User) o;
return Objects.equals(electronicDevices, u.electronicDevices) &&
Objects.equals(registertime, u.registertime)&&
Objects.equals(userBirthDate, u.userBirthDate)&&
Objects.equals(userEmail, u.userEmail)&&
Objects.equals(userId, u.userId)&&
Objects.equals(userPassword, u.userPassword)&&
Objects.equals(userPhoneNumber, u.userPhoneNumber) ;
}
}
84 changes: 84 additions & 0 deletions Practice02/Users.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package Practice02;

import Practice01.User;

import java.util.Arrays;

//실습2. 객체 배열 `User[] userList`를 필드로 있는 `Users` 클래스를 작성하시오.
// - 필드 - User[] userList (생성된 User 객체들을 모두 저장)
// - 메소드 - 생성자, getter(), setter(), hashCode(), equals(), toString()
public class Users {
private static User[] userslist;

Choose a reason for hiding this comment

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

해당 변수가 static인 이유가 있나요?

private static Users singleton;
private Users(){
userslist =new User[10];
}

public User[] getUserslist() {
return userslist;
}

public void setUserslist(User[] userslist) {
this.userslist = userslist;
}

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

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

@Override
public String toString() {
String s="";
for(int i=0;i<userslist.length;i++){
s+=userslist[i];
}
return s;
}

//2-1 Users 클래스의 객체를 싱글톤으로 생성하는 함수를 작성(Double Checked Locking 방식)

public Users getInstance(){

Choose a reason for hiding this comment

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

인스턴스를 반환하는 메서드는 생성자 근처에 위치하면 좋을 것 같습니다.

if(singleton==null){
synchronized (Users.class) {

Choose a reason for hiding this comment

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

https://www.baeldung.com/java-singleton-double-checked-locking

이 글을 한번 읽어보세요~!

생성비용이 비싼 객체의 경우에는 Double Checked Locking을 사용할 순 있을 것 같네요.

좋은 시도십니다~!👍🏻

Choose a reason for hiding this comment

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

본인이 작성한 코드에 명확하고 정당한 이유가 있다면 저는 새로운 시도 = 굉장한 도전 이라고 생각합니다~😀

if (singleton == null) {
singleton = new Users();
}
}
}
return singleton;
}

//2-2 회원 아이디 userId를 통해 인자로 주어진 회원번호에 해당하는 회원을 반환하는 함수를 작성
public User findByUserId(String userId){
for(int i=0; i<userslist.length;i++){

Choose a reason for hiding this comment

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

Stream을 사용해 해당 동작을 구현할 수 없을까요?

if(userslist[i].getUserId()==userId){
return userslist[i];
}
}
return null;
}


//2-3 인자로 주어진 회원 정보를 깊은 복사 (deepCopy - '실제 값'을 새로운 메모리 공간에 복사하는 것) 하는 함수를 작성

Choose a reason for hiding this comment

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

👍🏻

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

Choose a reason for hiding this comment

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

제대로된 깊은 복사가 이루어졌을까요?

copy.setRegistertime(user.getRegistertime());

return copy;
}
}
92 changes: 92 additions & 0 deletions Practice03/Electronics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package Practice03;

import Practice01.AuthMethod;
import Practice01.Company;
import Practice01.Electronic;
import Practice02.Users;

import java.util.*;
import java.util.Arrays;

public class Electronics {
//- 객체 배열 `Electronic[] electronicList`를 필드로 가지고 있는 `Electronics`클래스를 작성하시오.
// - 필드 - Electronic[] electronicList (생성된 Electronic 객체들을 모두 저장)
// - 메소드 - 생성자, getter(), setter(), hashCode(), equals(), toString()
private static Electronic[] electroniclist;
private Electronics singleton;
private Electronics(){

Choose a reason for hiding this comment

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

기본 생성자에서 electroniclist 을 초기화해야하지 않을까요?


}

public Electronic[] getElectroniclist() {

Choose a reason for hiding this comment

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

여기서 문제!

이렇게 반환된 배열의 값을 바꾸면 원본 배열에도 영향이 있을까요 없을까요?

return electroniclist;
}

public void setElectroniclist(Electronic[] electroniclist) {
this.electroniclist = electroniclist;
}
@Override
public int hashCode() {
return Arrays.hashCode(electroniclist);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Electronics e = (Electronics) o;
return Arrays.equals(electroniclist, e.electroniclist);
}

@Override
public String toString() {

return Arrays.toString(electroniclist);
}

//3-1 Electronic 클래스의 객체를 싱글톤으로 생성하는 함수를 작성(Double Checked Locking 방식)

public Electronics getInstance(){
if(singleton==null){
synchronized (Users.class) {
if (singleton == null) {
singleton = new Electronics();
}
}
}
return singleton;
}

//3-2 전자제품 일련번호 productNo를 통해 인자로 주어진 일련번호에 해당하는 전자제품을 반환하는 함수를 작성
public static Electronic findByProductNo(String productNo){
for(int i=0; i<electroniclist.length;i++){
if(electroniclist[i].getProductNo()==productNo){
return electroniclist[i];
}
}
return null;
}

//3-3 전자제품들 중 인자로 주어진 제조 회사를 찾아서 하나의 배열에 반환하는 함수를 작성
public static List<Electronic> groupByCompanyName(Company company){

Choose a reason for hiding this comment

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

List가 아닌 배열을 반환하는 것이 요구사항인 것 같네요~

List<Electronic> groupByCompanyNamelist =new ArrayList<>();
for(int i=0; i<electroniclist.length;i++){
if(electroniclist[i].getCompanyName().equals(company)){
groupByCompanyNamelist.add(electroniclist[i]);
}
}
return groupByCompanyNamelist;
}

//3-4 전자제품들 중 인자로 주어진 인증 방법을 찾아서 하나의 배열에 반환하는 함수를 작성
public static List<Electronic> groupByAuthMethod(AuthMethod authMethod){

List<Electronic> groupByAuthMethodlist =new ArrayList<>();
for(int i=0; i<electroniclist.length;i++){
if(electroniclist[i].getArrAuth().equals(authMethod)){

Choose a reason for hiding this comment

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

배열과 객체를 비교하면 제대로 동작하지 않을 것 같습니다~

groupByAuthMethodlist.add(electroniclist[i]);
}
}
return groupByAuthMethodlist ;
}
}