//PhoeBookVersion07
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
class MenuChoiceException extends Exception
{
int wrongChoice;
public MenuChoiceException(int wrongChoice)
{
super("잘못된 선택을 하셨습니다.");
this.wrongChoice=wrongChoice;
}
public void showException()
{
System.out.printf("%d에 해당하는 선택은 존재하지 않습니다.", wrongChoice);
System.out.println("");
//System.out.println("메뉴 선택을 처음부터 다시 진행합니다.");
}
}
interface Data1
{
int INPUT=1, SEARCH=2, DELETE=3, QUIT=4;
}
interface Data2
{
int GENERAL=1, UNIV=2, COMPANY=3;
}
class PhoneBookManager
{
//int cnt=0;
//final int MAX_CNT=100;
//PhoneInfo[] piArr = new PhoneInfo[MAX_CNT];
Scanner sc = new Scanner(System.in);
HashSet<PhoneInfo> piArr = new HashSet<PhoneInfo>();
//PhoneBookManager 클래스의 인스턴스를 한 개만 생성하기 위해서!
private static PhoneBookManager pbn = null;
public static PhoneBookManager getSimpleNumberInst()
{
if(pbn==null)
pbn=new PhoneBookManager();
return pbn;
}
//
public void storeInfo() throws MenuChoiceException
{
PhoneInfo info = null;
boolean isAdded;
int choice=0;
Scanner sc = new Scanner(System.in);
String name=null;
String phone=null;
String major=null;
int year=0;
String company=null;
System.out.println("데이터 입력을 시작합니다.");
System.out.println("1. 일반, 2. 대학, 3. 회사");
System.out.print("선택>> ");
choice = sc.nextInt();
sc.nextLine();
if(choice<Data2.GENERAL ||choice>Data2.COMPANY)
throw new MenuChoiceException(choice);
if(choice == Data2.GENERAL)
{
System.out.print("이름: ");
name=sc.nextLine();
System.out.print("전화번호: ");
phone=sc.nextLine();
info = new PhoneInfo(name, phone);
isAdded = piArr.add(info);
//piArr[cnt] = new PhoneInfo(name, phone);
//cnt++;
if(isAdded==true)
System.out.println("\n데이터 입력이 완료되었습니다.");
else
System.out.println("이미 저장된 데이터입니다.");
return;
}
if(choice == Data2.UNIV)
{
System.out.print("이름: ");
name=sc.nextLine();
System.out.print("전화번호: ");
phone=sc.nextLine();
System.out.print("전공: ");
major=sc.nextLine();
System.out.print("학년: ");
year=sc.nextInt();
info = new PhoneUnivInfo(name, phone, major, year);
isAdded = piArr.add(info);
//piArr[cnt] = new PhoneUnivInfo(name, phone, major, year);
if(isAdded==true)
System.out.println("\n데이터 입력이 완료되었습니다.");
else
System.out.println("이미 저장된 데이터입니다.");
//cnt++;
return;
}
else if(choice == Data2.COMPANY)
{
System.out.print("이름: ");
name=sc.nextLine();
System.out.print("전화번호: ");
phone=sc.nextLine();
System.out.print("회사: ");
company=sc.nextLine();
info = new PhoneCompanyInfo(name, phone, company);
isAdded = piArr.add(info);
//piArr[cnt] = new PhoneCompanyInfo(name, phone, company);
if(isAdded==true)
System.out.println("\n데이터 입력이 완료되었습니다.");
else
System.out.println("이미 저장된 데이터입니다.");
//cnt++;
return;
}
}
private PhoneInfo search(String sname)
{
/*
for(int i=0; i<cnt;i++)
{
PhoneInfo curInfo = piArr[i];
if(sname.compareTo(curInfo.name)==0)
return i;
}
return -1;
*/
Iterator<PhoneInfo> itr=piArr.iterator();
while(itr.hasNext())
{
PhoneInfo curInfo = itr.next();
if(sname.compareTo(curInfo.name)==0)
return curInfo;
}
return null;
}
public void searchInfo()
{
String sname=null;
Scanner sc = new Scanner(System.in);
System.out.println("데이터 검색을 시작합니다..");
System.out.print("이름: ");
sname = sc.nextLine();
//int i= search(sname);
PhoneInfo info = search(sname);
if(info==null)
System.out.println("찾는 데이터가 없습니다.");
else
{
info.showInfo();
System.out.println("데이터 검색이 완료되었습니다.");
}
/*
if(i<0)
System.out.println("찾는 데이터가 없습니다.");
else
{
piArr[i].showInfo();
System.out.println("데이터 검색이 완료되었습니다.");
}
*/
}
public void deleteInfo()
{
String sname=null;
Scanner sc = new Scanner(System.in);
System.out.println("데이터 삭제를 시작합니다..");
System.out.print("이름: ");
sname = sc.nextLine();
//int i=search(sname);
PhoneInfo info=search(sname);
Iterator<PhoneInfo> itr=piArr.iterator();
while(itr.hasNext())
{
PhoneInfo curInfo = itr.next();
if(sname.compareTo(curInfo.name)==0)
{
itr.remove();
System.out.println("데이터 삭제가 완료되었습니다. \n");
return;
}
}
System.out.println("찾는 데이터가 없습니다.");
/*
if(i<0)
System.out.println("찾는 데이터가 없습니다.");
else
{
for(int idx=i;idx<cnt-1;idx++)
piArr[idx]=piArr[idx+1];
cnt--;
System.out.println("데이터 삭제가 완료되었습니다. \n");
}
*/
}
}
class PhoneInfo
{
String name;
String phoneNumber;
public PhoneInfo(String name, String phoneNumber)
{
this.name = name;
this.phoneNumber = phoneNumber;
}
public void showInfo()
{
System.out.println("Name: " + name);
System.out.println("Phone Number: " + phoneNumber);
//System.out.println("");
}
public int hashCode()
{
return name.hashCode();
}
public boolean equals(Object obj)
{
PhoneInfo comp = (PhoneInfo)obj;
if(name.compareTo(comp.name)==0)
return true;
else
return false;
}
}
class PhoneUnivInfo extends PhoneInfo
{
String major;
int year;
public PhoneUnivInfo(String name, String phoneNumber, String major, int year)
{
super(name, phoneNumber);
this.major=major;
this.year=year;
}
public void showInfo()
{
super.showInfo();
System.out.println("Major: " + major);
System.out.println("Year: " + year);
System.out.println("");
}
}
class PhoneCompanyInfo extends PhoneInfo
{
String company;
public PhoneCompanyInfo(String name, String phoneNumber, String company)
{
super(name, phoneNumber);
this.company=company;
}
public void showInfo()
{
super.showInfo();
System.out.println("Company: " + company);
System.out.println("");
}
}
public class PhoneBookVersion01 {
static Scanner sc = new Scanner(System.in);
public static void showMenu(){
System.out.println("선택하세요...");
System.out.println("1. 데이터 입력");
System.out.println("2. 데이터 검색");
System.out.println("3. 데이터 삭제");
System.out.println("4. 프로그램 종료");
System.out.print("선택: ");
}
public static void main(String[] args) {
//PhoneBookManager pbn = new PhoneBookManager();
PhoneBookManager pbn = PhoneBookManager.getSimpleNumberInst();
int sel;
while(true)
{
try
{
showMenu();
sel=sc.nextInt();
sc.nextLine();
if(sel< Data1.INPUT || sel>Data1.QUIT)
throw new MenuChoiceException(sel);
if(sel==Data1.INPUT)
pbn.storeInfo();
else if(sel==Data1.SEARCH)
pbn.searchInfo();
else if(sel==Data1.DELETE)
pbn.deleteInfo();
else if(sel==Data1.QUIT)
{
System.out.println("프로그램을 종료합니다.");
return;
}
}
catch(MenuChoiceException e)
{
System.out.println(e.getMessage());
e.showException();
System.out.println("메뉴 선택을 처음부터 다시 진행합니다.\n");
}
}
}
}