전화번호부 프로그램03

5574 단어 JavaJava

public class PhoneBook
{
private static PhoneBook pb;
private static PhoneInfo[] pInfoArr;
private static int numOfPhoneInfo;

private PhoneBook(int size)
{
	pInfoArr=new PhoneInfo[size];
}
public static PhoneBook getPhoneBook(int size)
{
	if(pb==null)
		pb=new PhoneBook(size);
	return pb;
}
public boolean insertPhoneInfo(String name,String phoneNumber,String birthday)
{
	if(numOfPhoneInfo>=pInfoArr.length)
		return false;
	pInfoArr[numOfPhoneInfo++]=new PhoneInfo(name,phoneNumber,birthday);
	return true;
}
public boolean searchPhoneInfoByName(String name)
{
	boolean result=false;
	for(int i=0; i<numOfPhoneInfo; i++)
	{	
		if(name.compareTo(pInfoArr[i].getName())==0)
		{	
			pInfoArr[i].printCurrentState();
			result=true;
		}
	}
	return result;
}
public int searchPhoneInfoByPhoneNumber(String phoneNumber)
{
	for(int i=0; i<numOfPhoneInfo; i++)
	{
		if(phoneNumber.compareTo(pInfoArr[i].getPhoneNumber())==0)
				return i;
	}
	return -1;
}
public boolean deletePhoneInfoByPhoneNumber(String phoneNumber)
{
	boolean deleteResult=false;
	int searchResult=searchPhoneInfoByPhoneNumber(phoneNumber);
	if(searchResult !=-1)
	{
		for(int i=searchResult; i<numOfPhoneInfo-1; i++)
		{
			pInfoArr[i]=pInfoArr[i+1];
		}
		deleteResult=true;
		pInfoArr[numOfPhoneInfo-1]=null;
		numOfPhoneInfo--;
	}
	return deleteResult;
}
public void printAllPhoneInfo()
{
	for(int i=0; i<numOfPhoneInfo; i++)
		pInfoArr[i].printCurrentState();
	System.out.println();
}

}

import java.util.Scanner;
public class PhoneBookUI
{
private static PhoneBook pb=PhoneBook.getPhoneBook(100);
public static Scanner sc= new Scanner(System.in);

private PhoneBookUI(){};

public static void printMenu()
{
	System.out.println("선택하세요...");
	System.out.println("1.데이터 입력");
	System.out.println("2.데이터 검색");
	System.out.println("3.데이터 삭제");
	System.out.println("4.모든 데이터 보기");
	System.out.println("5.프로그램 종료");
	System.out.print("선택:");
}
public static void inputPhoneInfo()
{
	String name,phoneNumber,birthday;
	boolean result;
	
	System.out.print("이름:");
	name=sc.nextLine();
	System.out.print("전화번호:");
	phoneNumber=sc.nextLine();
	System.out.print("생년월일:");
	birthday=sc.nextLine();
	result=pb.insertPhoneInfo(name,phoneNumber,birthday);
	if(result==false)
		System.out.println("더이상 데이터 저장 불가");
	else System.out.println("데이터 입력이 완료되었습니다.");
}
public static void searchPhoneInfoByName()
{
	String name;
	boolean result;
	System.out.println("데이터 검색을 시작합니다.");
	System.out.print("이름:");
	name=sc.nextLine();
	result=pb.searchPhoneInfoByName(name);
	if(result==false)
		System.out.println("검색한 데이터를 찾을 수 없습니다.");
	else System.out.println("데이터 검색이 완료되었습니다.");
}
public static void deletePhoneInfoByPhoneNumber()
{
	String phoneNumber;
	boolean result;
	System.out.println("데이터 삭제를 시작합니다.");
	System.out.print("전화번호:");
	phoneNumber=sc.nextLine();
	result=pb.deletePhoneInfoByPhoneNumber(phoneNumber);
	if(result==false)
		System.out.println("삭제할 데이터가 없습니다.");
	else System.out.println("데이터 삭제가 완료되었습니다.");
}
public static void printAllPhoneInfo()
{
	System.out.println("모든 데이터를 출력합니다.");
	pb.printAllPhoneInfo();
}
public static void quitProgram()
{
	System.out.println("시스템을 종료합니다.");
	sc.close();
}
public static void wrongNumber()
{
	System.out.println("숫자를 잘못 입력했습니다");
}

}

public class PhoneInfo
{
private String name;
private String phoneNumber;
private String birthday;

public PhoneInfo(String name,String phoneNumber,String birthday)
{
	this.name=name;
	this.phoneNumber=phoneNumber;
	this.birthday=birthday;
}
public PhoneInfo(String name, String phoneNumber)
{
	this.name=name;
	this.phoneNumber=phoneNumber;
}
public String getName()
{
	return name;
}
public String getPhoneNumber()
{
	return phoneNumber;
}
public void printCurrentState()
{
	System.out.println("이름:"+name);
	System.out.println("전화번호"+phoneNumber);
	System.out.println("생년월일"+birthday);
}

}

import java.util.*;

public class PhoneMain
{
private static final int INSERT_PHONE_INPO=1;
private static final int SEARCH_PHONE_INPO=2;
private static final int DELETE_PHONE_INPO=3;
private static final int ALL_SHOW_PHONE_INPO=4;
private static final int QUIT_PHONE_INPO=5;

public static void main(String[] args)
{
	int menu;
	Scanner sc=PhoneBookUI.sc;
	while(true)
	{	
		PhoneBookUI.printMenu();
		menu=sc.nextInt();
		sc.nextLine();
		switch(menu)
		{
			case INSERT_PHONE_INPO:
				PhoneBookUI.inputPhoneInfo();
				break;
			case SEARCH_PHONE_INPO:
				PhoneBookUI.searchPhoneInfoByName();
				break;
			case DELETE_PHONE_INPO:
				PhoneBookUI.deletePhoneInfoByPhoneNumber();
				break;
			case ALL_SHOW_PHONE_INPO:
				PhoneBookUI.printAllPhoneInfo();
				break;
			case QUIT_PHONE_INPO:
				PhoneBookUI.quitProgram();
				return;
			default:
				PhoneBookUI.wrongNumber();
		}
	}
	
}

}

좋은 웹페이지 즐겨찾기