본문 바로가기
수업시간 JAVA/문제

연락처 등록/조회/수정/삭제

by SEOKIHOUSE 2023. 4. 8.
package practice31day;

public class Contact {
	// 필수
	private int num;
	private String name;
	private String phoneNum;
	// 필수아닌것
	private String email;

	public Contact() {

	}

	public Contact(int num, String name, String phoneNum) {
		this.num = num;
		this.name = name;
		this.phoneNum = phoneNum;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public int getNum() {
		return num;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setPhoneNum(String phoneNum) {
		this.phoneNum = phoneNum;
	}

	public String getPhoneNum() {
		return phoneNum;
	}
	
	public void setEmail(String email) {
		this.email = email;
	}

	public String getEmail() {
		return email;
	}
	
	public String toString() {
		if(email == null) {
			return "등록번호: " + num+ " 이름: " +name +" 폰번호s: " +phoneNum;
		}
		return "등록번호: " + num+ " 이름: " +name +" 폰번호s: " +phoneNum+" 이메일: "+ email;
	}
	
	

}
package practice31dayokok;

import java.util.Scanner;

public class ContactManager {
	Scanner sc = new Scanner(System.in);
	Contact[] store;
	int index = 0;

	ContactManager() {
		store = new Contact[5];
	}

	// 등록
	// 저장소
	public void registerMember(Contact c) {
		if (index >= 0 && index < store.length) {
			store[index] = c;
			index++;
		} else {
			System.out.println("배열범위를 벗어났다 에러");
		}
	}

	// 조회
	public Contact findYourInfo(int num, String name) {
		for (int i = 0; i < store.length; i++) {
			if (store[i].getNum() == num && store[i].getName().equals(name)) {
				return store[i];
			}
		}
		return null;
	}
//	public void findYourInfo(String name) {
//		for(int i =0; i<index; i++) {
//			if(store[i].getName().equals(name)) {
//				System.out.println(store[i]);
//				System.out.println("==========");
//				System.out.println(store[i].toString());
//				
//			}
//		}
//	}

	// 수정
	// 전체적인 부분 업데이트
//	public Contact update(Contact c, int num) {
//		store[num-1] = c;
//		return store[num-1];
//	}
	// 전체적인 부분 업데이트

	// void부분 ====================================================
	public void repairInfo(int num1, int num2) {
		for (int i = 0; i < index; i++) {
			if (store[i].getNum() == num1) {
				if (num2 == 1) {
					System.out.print("수정할 이름을 적으세요>> ");
					String name = sc.nextLine();
					store[i].setName(name);
				} else if (num2 == 2) {
					System.out.print("수정할 폰번호는?>> ");
					String phone = sc.nextLine();
					store[i].setPhoneNum(phone);
				} else if (num2 == 3) {
					System.out.print("수정할 이메일은?>> ");
					String email = sc.nextLine();
					store[i].setEmail(email);
				}
			}
		}
	}
	// return부분 ================================================
//	public Contact repairInfo(int num1) {
//		for (int i = 0; i < index; i++) {
//			if(store[i].getNum() == num1) {
//				return store[i];
//			}
//		}
//		return null;
//	}

	// 삭제
	public void deleteInfo(String name, int num) {
		for (int i = 0; i < index; i++) {
			if (store[i].getName().equals(name) && store[i].getNum() == num) {
				if (store[i] == store[index - 1]) { // 0 or 마지막항 선택시 삭제
					store[i] = null;
				}
				for (int j = i; j < index - 1; j++) { // 그 사이에 있는 것들 선택시 삭제
					store[j] = store[j + 1];
					int nums = store[j].getNum() - 1; // 등록번호 -1
					store[j].setNum(nums);
					if (store[j + 1] == store[index - 1]) { // 앞배열에 하나씩 넣고 마지막행 삭제
						store[index - 1] = null;
					}
				}
				index--;
			}
		}
	}

	// 삭제 이름 중복체크
	public int deleteCheck(String name) {
		int result = 0;
		for (int i = 0; i < index; i++) {
			if (store[i].getName().equals(name)) {
				if (store[i] == store[index - 1]) { // 마지막행일때
					for (int j = 0; j < index-1; j++) { // 마지막행이 앞의 행과 중복인거 체크
						if (store[j].getName().equals(store[index - 1].getName())) {
							result = -1; // 중복
						}
					}
				} else { // 나머지행들 중복체크
					for (int j = 0; j < index-1; j++) {
						if (store[j].getName().equals(store[index - 1].getName())) {
							result = -1; // 중복
						}
					}
				}
			}
		}
		return result;
	}

	// 1. 중복시 등록번호넘기기
	public int deleteGive(int num) {
		for (int i = 0; i < index; i++) {
			if (store[i].getNum() == num) {
				return store[i].getNum();
			}
		}
		return 0;
	}

	// 2. 삭제 중복아닐때 등록번호 넘겨주기
	public int deleteSingle(String name) {
		for (int i = 0; i < index; i++) {
			if (store[i].getName().equals(name)) {
				return store[i].getNum();
			}
		}
		return 0;
	}
}
package practice31dayokok;

import java.util.Scanner;

public class ContactTest {

	public static void main(String[] args) {
		// 1 name 전화번호 email
		// 등록 조회 수정 삭제
		ContactManager cm = new ContactManager();
		Scanner sc = new Scanner(System.in);

		while (true) {
			System.out.println("전화번호 등록시스템입니다");
			System.out.println("1.등록 2.조회 3.수정 4.삭제 0.종료");
			int choose = sc.nextInt();
			if (choose == 1) {
				System.out.print("이름을 등록하세요>> ");
				String name = sc.nextLine();
				name = sc.nextLine();
				System.out.print("전화번호를 등록하세요>> ");
				String phone = sc.nextLine();
				Contact c = new Contact(cm.index + 1, name, phone);

				System.out.print("이메일 등록하시려면 1번 아니라면 0번");
				int emailNum = sc.nextInt();
				if (emailNum == 1) {
					System.out.print("이메일을 입력하세요>> ");
					String email = sc.nextLine();
					email = sc.nextLine();
					c.setEmail(email);
				} else if (emailNum == 0) {
					System.out.println("메뉴로 돌아갑미다");
				}
				// 배열에 저장
				cm.registerMember(c);
			}
			// 조회
			else if (choose == 2) {
				System.out.print("1.개별조회 2.전체조회>> ");
				int a = sc.nextInt();
				// 1.개별조회
				if (a == 1) {
					System.out.print("번호가 어떻게 되시나용?>> ");
					a = sc.nextInt();
					System.out.print("이름이 어떻게 되세용?>> ");
					String whatYourName = sc.nextLine();
					whatYourName = sc.nextLine();
//					cm.findYourInfo(whatYourName); //void

					Contact scs = cm.findYourInfo(a, whatYourName);
					System.out.println(scs);
//					System.out.println(cm.findYourInfo(a, whatYourName).toString());

				} else if (a == 2) {
					// 2.전체조회
					for (int i = 0; i < cm.index; i++) {
						System.out.println(cm.store[i]);
					}
					System.out.println("index값: " + cm.index);
				}
			}

			// 전체적인 부분 업데이트
//			else if (choose == 3) {
//				System.out.print("수정할 번호를 고르세요");
//				int a = sc.nextInt();
//		
//				Contact c = new Contact();
//				System.out.println("수정할 부분을 선택하세요 ");
//				System.out.println("1.이름 2.폰번호 3.이메일");
//				int b = sc.nextInt();
//				c.setNum(a);
//				if (b == 1) {
//					System.out.print("수정할 이름을 적으세요>> ");
//					String name = sc.nextLine();
//					name = sc.nextLine();
//					c.setName(name);
//				} else if (b == 2) {
//					System.out.print("수정할 폰번호를 적으세요>> ");
//					String name = sc.nextLine();
//					name = sc.nextLine();
//					c.setPhoneNum(name);
//				} else if (b == 3) {
//					System.out.print("수정할 이메일을 적으세요>> ");
//					String name = sc.nextLine();
//					name = sc.nextLine();
//					c.setEmail(name);
//				}
//				cm.update(c, a);
//			}
			// 전체적인 부분 업데이트
			// void부분 ====================================================
			else if (choose == 3) {

				System.out.print("번호가 어떻게 되시죠?>> ");
				int a = sc.nextInt();

				System.out.println("수정할 부분을 선택하세요 ");
				System.out.println("1.이름 2.폰번호 3.이메일");
				int b = sc.nextInt();

				cm.repairInfo(a, b);
			}

			// return부분 ================================================
//				else if (choose == 3) {
//					int cont =1;
//					while(cont ==1) {
//						System.out.print("번호가 어떻게 되시죠?>> ");
//						int a = sc.nextInt();
//						
//						System.out.println("수정할 부분을 선택하세요 ");
//						System.out.println("1.이름 2.폰번호 3.이메일");
//						int b = sc.nextInt();
//						if(b ==1) {
//							System.out.print("수정할 이름을 적으세요>> ");
//							String name = sc.nextLine();
//							name = sc.nextLine();
//							cm.repairInfo(a).setName(name);
//						}else if (b ==2) {
//							System.out.print("수정할 폰번호를 적으세요>> ");
//							String name = sc.nextLine();
//							name = sc.nextLine();
//							cm.repairInfo(a).setPhoneNum(name);
//						}else if (b ==3) {
//							System.out.print("수정할 이메일을 적으세요>> ");
//							String name = sc.nextLine();
//							name = sc.nextLine();
//							cm.repairInfo(a).setEmail(name);
//						}
//						
//						System.out.print("계속하시려면 1 아님 0");
//						cont = sc.nextInt();
//					}
			// return부분 ================================================

			// 삭제
			else if (choose == 4) {
				System.out.print("삭제하고 싶은 이름을 선택하세요");
				String name = sc.nextLine();
				name = sc.nextLine();
				int ss = cm.deleteCheck(name);
//				System.out.println("ss"+ss);
				if (ss == 0) { // 중복x
					int s1 = cm.deleteSingle(name);
					cm.deleteInfo(name, s1);
				} else if (ss != 0) { // 중복
					System.out.print("중복된 이름이 있습니다 어떤 등록번호를 지우시겠습니까");
					int num = sc.nextInt();
					int a = cm.deleteGive(num);
					cm.deleteInfo(name, a);
				}
			} else if (choose == 0) {
				System.out.println("byebyebye");
				break;
			}
		}

	}
}