java - MemberLinkedList

imjingu·2023년 9월 3일
0

개발공부

목록 보기
443/481
package chapter20230901.member.linkedlist;

import java.util.*;

import chapter20230901.member.Member;

public class MemberLinkedList {

private final LinkedList<Member> linkedList; // ArraList 선언, arrayList - 주소만 바뀌지 않으면 final이라도 값 변경 가능
	
	public MemberLinkedList() {
		this.linkedList = new LinkedList<>(); // Memeber 형으로 선언한 ArrayList 생성
	}
	
	public void addMember(Member member) {
		linkedList.add(member); // 회원을 추가
	}
	
	public boolean removeMember(int memberID) {
		for(Member member : linkedList) {
			if(member.getMemberID() == memberID) { // 회원 아이디와 매개변수가  일치하면
				linkedList.remove(member); // 해당 회원을 삭제
				return true;
			}
		}
		System.out.println(memberID + " 가 존재하지 않습니다."); // 반복문을 돌려서 해당 아이디를 찾지 못한 경우
		return false;
	}
	
	public void showAllMember() { // 전체 회원을 출력하는 메서드
		for (Member member : linkedList) {
			System.out.println(member);
		}
		System.out.println();
	}

}

package chapter20230901.member.linkedlist;

import chapter20230901.member.Member;

public class MemberLinkedListTest {

	public static void main(String[] args) {
		MemberLinkedList memberArrayList = new MemberLinkedList();
		
		// 새로운 회원 인스턴스 생성
		Member memberLee = new Member(1001, "이지원");
		Member memberSon = new Member(1002, "손민국");
		Member memberPark = new Member(1003, "박서원");
		Member memberHong = new Member(1004, "홍길동");
		
		// ArrayList에 회원 추가
		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 + " 입니다.";
	}

}

0개의 댓글