package chapter20230905.HashSet;
public class Member {
private int memberID;
private String memberName;
public Member(int memberID, String memberName) {
this.memberID = memberID;
this.memberName = memberName;
}
public int getMemberID() {
return memberID;
}
public void setMemberID(int memberID) {
this.memberID = memberID;
}
public String getMemberName() {
return memberName;
}
public void setMemberName(String memberName) {
this.memberName = memberName;
}
@Override
public String toString() {
return memberName + "회원님의 아이디는 " + memberID + " 입니다.";
}
@Override
public int hashCode() {
return memberID;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof Member) {
Member member = (Member)obj;
if(this.memberID == member.memberID)
return true;
}
return false;
}
}
package chapter20230905.HashSet;
import java.util.*;
public class MemberHashSet {
private HashSet<Member> hashSet;
public MemberHashSet() {
hashSet = new HashSet<>();
}
public void addMember(Member member) {
hashSet.add(member);
}
public boolean removeMemeber(int memberId) {
for(Member member : hashSet) {
int tempId = member.getMemberID();
if(tempId == memberId) {
hashSet.remove(member);
return true;
}
}
System.out.println(memberId + "가 존재하지 않습니다.");
return false;
}
public void showAllMember() {
for(Member member : hashSet) {
System.out.println(member);
}
System.out.println();
}
}
package chapter20230905.HashSet;
public class MemberHashSetTest {
public static void main(String[] args) {
MemberHashSet memberHashSet = new MemberHashSet();
Member memberLee = new Member(1001, "이지원");
Member memberSon = new Member(1002, "손민국");
Member memberPark = new Member(1003, "박서훤");
memberHashSet.addMember(memberLee);
memberHashSet.addMember(memberSon);
memberHashSet.addMember(memberPark);
memberHashSet.showAllMember();
memberHashSet.addMember(memberLee);
memberHashSet.showAllMember();
Member memberHong = new Member(1003, "홍길동");
memberHashSet.addMember(memberHong);
memberHashSet.showAllMember();
memberHashSet.removeMemeber(1002);
memberHashSet.showAllMember();
}
}