2013년 1월 18일 금요일
[Java] PhoneManager
public class Manage {
Scanner sc = new Scanner(System.in);
HashMap<String, ArrayList<PhoneInfo>> map = new HashMap<String, ArrayList<PhoneInfo>>();
int choice;
Manage(){
}
void disp(){
System.out.println("-------전화번호부 관리 프로그램--------");
System.out.println("1.입력 2.출력 3.수정 4.삭제 5.정렬 6.저장 7.불러오기");
}
void choice()throws Exception{
while(true){
disp();
try{choice = sc.nextInt();
sc.nextLine();
}catch(Exception e){
System.out.println("숫자만 입력하시오!");
return;
}
switch(choice){
case 1:
Input();
break;
case 2:
show();
break;
case 3:
modify();
break;
case 4:
delete();
break;
case 5:
sort();
break;
case 6:
write();
break;
case 7:
read();
break;
default:
return;
}
}
}
void addGroup(String group){
if(!map.containsKey(group)){
map.put(group, new ArrayList<PhoneInfo>());
}
}
void Input(){
System.out.print("그룹 입력 : ");
String group = sc.nextLine();
System.out.print("이름 입력 : ");
String name = sc.nextLine();
System.out.print("전화 번호 입력 : ");
String phone = sc.nextLine();
System.out.print("생일 입력 : ");
String birth = sc.nextLine();
addGroup(group);
ArrayList<PhoneInfo> list = map.get(group);
list.add(new PhoneInfo(name, phone, birth));
System.out.println();
}
void show(){
Set set = map.entrySet();
Iterator iter = set.iterator();
while(iter.hasNext()){
Map.Entry e = (Map.Entry)iter.next();
System.out.println(e.getKey() + "[" + ((ArrayList)e.getValue()).size() + "]");
Iterator iterSub = ((ArrayList)e.getValue()).iterator();
while(iterSub.hasNext()){
PhoneInfo pi = (PhoneInfo)(iterSub.next());
pi.showInfo();
}
System.out.println();
}
System.out.println();
}
void modify(){
System.out.print("전화번호부 수정할 사람의 이름을 입력해 주십시오");
String name = sc.nextLine();
Set set = map.entrySet();
Iterator iter = set.iterator();
while(iter.hasNext()){
Map.Entry e = (Map.Entry)iter.next();
Iterator iterSub = ((ArrayList)e.getValue()).iterator();
while(iterSub.hasNext()){
PhoneInfo pi = (PhoneInfo)(iterSub.next());
if(pi.name.equals(name)){
System.out.print("수정할 전화번호를 입력하세요");
String phone = sc.nextLine();
pi.phoneNum = phone;
}
}
}
System.out.println();
}
void delete(){
System.out.print("삭제할 사람의 이름을 입력해 주십시오.");
String name = sc.nextLine();
Set set = map.entrySet();
Iterator iter = set.iterator();
while(iter.hasNext()){
Map.Entry e = (Map.Entry)iter.next();
Iterator iterSub = ((ArrayList)e.getValue()).iterator();
while(iterSub.hasNext()){
PhoneInfo pi = (PhoneInfo)(iterSub.next());
if(pi.name.equals(name)){
iterSub.remove();
}
}
}
}
void sort(){
Comparator<PhoneInfo> cp = new Comparator<PhoneInfo>() {
@Override
public int compare(PhoneInfo o1, PhoneInfo o2) {
// TODO Auto-generated method stub
if(o1.name.compareTo(o2.name)<0)
return -1;
else if(o1.name.compareTo(o2.name)>0)
return 1;
return 0;
}
};
Set set = map.entrySet();
Iterator iter = set.iterator();
System.out.print("정렬할 그룹를 입력하세요");
String group = sc.nextLine();
while(iter.hasNext()){
Map.Entry e = (Map.Entry)iter.next();
if(e.getKey().equals(group))
Collections.sort(((ArrayList)e.getValue()), cp);
}
System.out.println();
}
private void write() throws Exception {
// TODO Auto-generated method stub
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("PhonInfo.ser"));
oos.writeObject(map);
oos.close();
//map = null;
System.out.println("저장되었습니다.");
}
private void read() throws Exception {
// TODO Auto-generated method stub
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("PhonInfo.ser"));
map = (HashMap<String, ArrayList<PhoneInfo>>)ois.readObject();
ois.close();
System.out.println("불러오기를 완료했습니다.");
}
}
피드 구독하기:
댓글 (Atom)
Why does a casino not give you a bonus for good luck?
답글삭제The reason why it 속초 출장안마 is not allowed in casinos is not because the 과천 출장샵 casinos are not trustworthy. 보령 출장마사지 This gives 계룡 출장안마 the 안성 출장마사지 gambler a chance to bet with other