20220817
한 번에 끝내는 Java/Spring 웹 개발 마스터
1) Member 클래스
package ch10;
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 + "입니다";
}
}
2) MemberArrayList 클래스
package ch10;
import java.util.ArrayList;
public class MemberArrayList {
private ArrayList<Member> arrayList;
public MemberArrayList() {
arrayList = new ArrayList<>();
}
public MemberArrayList(int size) {
arrayList = new ArrayList<>(size);
}
public void addMember(Member member) {
arrayList.add(member);
}
public boolean removeMember(int memberId) {
for(int i=0; i<arrayList.size(); i++) {
Member member = arrayList.get(i);
int tempId = member.getMemberId();
if(tempId == memberId) {
arrayList.remove(i);
return true;
}
}
System.out.println(memberId + "가 존재하지 않습니다.");
return false;
}
public void showAllMember() {
for(Member member : arrayList) {
System.out.println(member);
}
System.out.println();
}
}
3) 출력하는 MemberArrayList 클래스
package ch10;
public class MemberArrayListTest {
public static void main(String[] args) {
MemberArrayList memberarraylist = new MemberArrayList();
Member memberLee = new Member(1001, "이진석");
Member memberKang = new Member(1002, "강기욱");
Member memberYong = new Member(1003, "용현정");
memberarraylist.addMember(memberLee);
memberarraylist.addMember(memberKang);
memberarraylist.addMember(memberYong);
memberarraylist.showAllMember();
memberarraylist.removeMember(memberKang.getMemberId());
memberarraylist.showAllMember();
}
}
- Member 클래스를 만들고, 아이디와 이름을 멤버 변수로 선언하였다.
- 이후, 멤버를 순차적으로 관리하기 위해서 ArrayList를 이용해서 MemberArrayList클래스를 구현했다.