package chapter20230905.TreeSet;
public class Member implements Comparable<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 compareTo(Member member) {
return(this.memberID - member.memberID) * (1);
}
}
package chapter20230905.TreeSet;
import java.util.*;
public class MemberTreeSet {
private TreeSet<Member> treeSet;
public MemberTreeSet() {
treeSet = new TreeSet<>();
}
public void addMember(Member member) {
treeSet.add(member);
}
public boolean removeMemeber(int memberId) {
for(Member member : treeSet) {
int tempId = member.getMemberID();
if(tempId == memberId) {
treeSet.remove(member);
return true;
}
}
System.out.println(memberId + "가 존재하지 않습니다.");
return false;
}
public void showAllMember() {
for(Member member : treeSet) {
System.out.println(member);
}
System.out.println();
}
}
package chapter20230905.TreeSet;
public class MemberTreeSetTest {
public static void main(String[] args) {
MemberTreeSet memberTreeSet = new MemberTreeSet();
Member memberPark = new Member(1003, "박서훤");
Member memberLee = new Member(1001, "이지원");
Member memberSon = new Member(1002, "손민국");
memberTreeSet.addMember(memberLee);
memberTreeSet.addMember(memberSon);
memberTreeSet.addMember(memberPark);
memberTreeSet.showAllMember();
Member memberHong = new Member(1003, "홍길동");
memberTreeSet.addMember(memberHong);
memberTreeSet.showAllMember();
}
}