연락처 관리 프로그램 [ver.1.0]

yed·2021년 7월 20일
0

연락처 관리 프로그램 ver.1.0

데이터 클래스와 메인 클래스로 구성됨.

  • 데이터 : 이름, 전화번호, 이메일
  • 기능 : 등록, 전체검색, 상세검색, 수정
  • 연락처를 저장할 배열을 전역변수로 선언해서 메인클래스 안 모든 메소드에서 배열에 접근할 수 있게함.
public class Contact {
	//멤버변수
	private String name;
	private String phone;
	private String email;
	//생성자
	public Contact() {}
	public Contact(String name, String phone, String email) {
		this.name = name;
		this.phone = phone;
		this.email = email;
	}
	//getter, setter 메소드
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	//메소드
	public void displayInfo() {
		System.out.println("이름 : "+name);
		System.out.println("전화번호 : "+phone);
		System.out.println("이메일 : "+email);
	}//end displayInfo()
	
}
public class ContactMain01 {
	//다른곳에서도 쓰기 위해 전역변수로 선언
	public static Scanner sc=new Scanner(System.in); 
	public static final int MENU_QUIT=0;
	public static final int MENU_INSERT=1;
	public static final int MENU_SELECT_ALL=2;
	public static final int MENU_SELECT=3;
	public static final int MENU_UPDATE=4;
	
	//연락처 최대 저장 개수를 상수로 만듦
	public static final int MAX=100;
	
	//연락처 저장 배열
	public static Contact[] contactList=new Contact[MAX];
	public static int count=0; //배열저장 시 인덱스역할
	
	public static void main(String[] args) {
		System.out.println("<<연락처 Version 1.0>>");
		int choice=0; //메뉴 선택용
		boolean run=true; //메뉴 반복용
		while(run) {
			showMainMenu();
			choice=sc.nextInt(); //값 선택
			sc.nextLine(); //숫자 뒤 엔터값 제거
			
			//선택된 번호에 따라 각 기능 수행
			switch(choice) {
			case MENU_INSERT: //등록
				insertContact();
				break;
			case MENU_SELECT_ALL: //전체검색
				selectAllContact();
				break;
			case MENU_SELECT: //상세검색
				selectContact();
				break;
			case MENU_UPDATE: //수정
				updateContact();
				break;
			case MENU_QUIT: //종료
				run=false;
				break;
			default: //예외처리
				System.out.println("다시 선택하세요.");
				break;
			}
		}
	}//end main()
	
	private static void showMainMenu() {
		System.out.println("-------------------------------------------");
		System.out.println(" 1.등록 | 2.전체검색 | 3.상세검색 | 4.수정 | 0.종료");
		System.out.println("-------------------------------------------");
		System.out.print("선택>");
	}//end showMainMenu()
	
	private static void insertContact() {		
		System.out.println();
		System.out.println("[연락처 등록 메뉴]");
		System.out.print("이름 입력>");
		String name = sc.nextLine();
		System.out.print("전화번호 입력>");
		String phone=sc.nextLine();
		System.out.print("이메일 입력>");
		String email=sc.nextLine();
		
		//Contact의 인스턴스 생성
		Contact c=new Contact(name, phone, email);
		
		//배열에 인덱스 0부터 저장
		contactList[count]=c;
		count++; // 다음 호출 시 다음 인덱스에 저장 
		System.out.println("연락처 등록 완료!");
	}//end insertContact()
	
	private static void selectAllContact() {
		System.out.println("[전체 연락처 목록 ("+ count+")]");
		for(int i=0; i<count; i++) {
			System.out.println();
			System.out.println("-----연락처 "+i+"-----");
			contactList[i].displayInfo();
		}
	}//end selectAllContact()
	
	private static void selectContact() {
		System.out.print("검색할 연락처 번호>");
		int search = sc.nextInt();
		sc.nextLine();
		
		if(search>=0 && search<count) {
			System.out.println();
			System.out.println("-----연락처 "+search+"-----");
			contactList[search].displayInfo();
		}else {
			System.out.println("없는 연락처입니다.");
		}
	}//end selectContact()
	
	private static void updateContact() {
		System.out.println();
		System.out.print("수정할 번호 입력>");
		int index=sc.nextInt();
		sc.nextLine();
		
		if(index>=0 && index<count) {
			System.out.print("수정  이름 입력>");
			String name = sc.nextLine();
			contactList[index].setName(name);
			
			System.out.print("수정 할 전화번호 입력>");
			String phone=sc.nextLine();
			contactList[index].setPhone(phone);
			
			System.out.print("수정 할 이메일 입력>");
			String email=sc.nextLine();
			contactList[index].setEmail(email);
			
			System.out.println("연락처 수정 완료!");
		}else {
			System.out.println("없는 연락처입니다.");
		}
	}//end updateContact()
    
}//end ContactMain01

향후 업데이트

  1. public 변수를 private 변수로 적극 전환해 변수들의 의도치않은 값변화 방지.
  2. interface를 사용해 필요한 기능과 상수들을 Main에서 분리.
  3. singleton을 활용해 Database에 접근할 클래스 분리.
profile
6개월 국비과정 기록하기

0개의 댓글

관련 채용 정보