package chapter20230901.member.arraylist;
import java.util.ArrayList;
import chapter20230901.member.Member;
public class MemberArrayList {
private final ArrayList<Member> arrayList;
public MemberArrayList() {
this.arrayList = new ArrayList<>();
}
public void addMember(Member member) {
arrayList.add(member);
}
public boolean removeMember(int memberID) {
for(Member member : arrayList) {
if(member.getMemberID() == memberID) {
arrayList.remove(member);
return true;
}
}
System.out.println(memberID + " 가 존재하지 않습니다.");
return false;
}
public void showAllMember() {
for (Member member : arrayList) {
System.out.println(member);
}
System.out.println();
}
}
package chapter20230901.member.arraylist;
import chapter20230901.member.Member;
public class MemberArrayListTest {
public static void main(String[] args) {
MemberArrayList memberArrayList = new MemberArrayList();
Member memberLee = new Member(1001, "이지원");
Member memberSon = new Member(1002, "손민국");
Member memberPark = new Member(1003, "박서원");
Member memberHong = new Member(1004, "홍길동");
memberArrayList.addMember(memberLee);
memberArrayList.addMember(memberSon);
memberArrayList.addMember(memberPark);
memberArrayList.addMember(memberHong);
memberArrayList.showAllMember();
memberArrayList.removeMember(1004);
memberArrayList.showAllMember();
}
}
package chapter20230901.member;
public class Member {
private int memberID;
private String memberName;
public Member(int memberID, String memberName) {
super();
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 + " 입니다.";
}
}