-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddressLogic
More file actions
148 lines (124 loc) · 4.65 KB
/
Copy pathAddressLogic
File metadata and controls
148 lines (124 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package addressbook;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.Vector;
import common.util.CommonUtilities;
public class AddressLogic {
Map<Character, List<Address>> address = new HashMap<Character, List<Address>>();
//valueList = new Vector<Contact>();
List<Address> valueList = null;
int gotNumb= 0;
public AddressLogic() {
List<Address>address = new Vector<Address>();
}
public void mainmenu() {
System.out.println("=====================메인 메뉴===================");
System.out.println("1.입력 2.출력 3.수정 4.삭제 5.검색 9.종료");
System.out.println("==============================================");
System.out.println("원하시는 메뉴 번호를 입력해주세요");
}
public int getmenu() {
Scanner sc= new Scanner(System.in);
while(true) {
try {
gotNumb=Integer.parseInt(sc.nextLine());
return gotNumb;
}
catch(Exception e) {
System.out.println("올바른 메뉴번호를 입력해주세요");
}
}/////while
}///////////getmenu()
public void separation(int gotNumb) {
switch(gotNumb){
case 1: importData(); break;
case 2: printData(); break;
case 5: searchData(); break;
case 9:
default: System.out.println("존재하지 않는 메뉴번호 입니다");
}
}/////////seperation()
public void importData() {
Scanner sc = new Scanner(System.in);
boolean goingout= false;
while(!goingout) {
System.out.println("이름을 입력하세요");
String name = sc.nextLine();
// 2]초성(ㄱ,ㄴ,ㄷ....ㅎ)얻기
char firstChar = CommonUtilities.getfirstCharacter(name);
System.out.println("입력한 글자의 초성자음:" + firstChar);
if (firstChar == '0') {
System.out.println("한글명이 아닙니다"); continue;
}
System.out.println("주소를 입력하세요");
String addr = sc.nextLine();
System.out.println("전화번호를 입력하세요");
String tel = sc.nextLine();
System.out.println("나이를 입력하세요");
int age = Integer.parseInt(sc.nextLine());
// 3] 초성이 한글 이름인 경우
// 맵컬렉션(address)에 firstChar 키값이 존재하는지 판단
if (!address.containsKey(firstChar)) {// 키값이 없는 경우.
// 해당 키값이
// value타입인 List<Address>객체 생성
List<Address> valueList = new Vector<Address>();
}
else {// 키값이 존재한다면
// 해당 키값으로 기존에 저장된 객체를 얻어온다
List<Address> valueList = null;
valueList = address.get(firstChar);
}
// 입력한 객체를 추가
valueList.add(new Address(name, tel, addr, age));
address.put(firstChar, valueList);
//출력
System.out.println("키값을 알때: "+address.get("ㄱ"));
Set<Character> keys = address.keySet();
for (Character key : keys) {
System.out.println("[" + key + "로 시작하는 명단]");
List<Address> values = address.get(key);
for (Address value : values)
value.printAddress();
}
goingout=true;
}////while
} ////////importData
private void printData() {
Set<Character> keys = address.keySet();
System.out.println(address.keySet());
for (Character key : keys) {
System.out.println("[" + key + "로 시작하는 명단]");
List<Address> values = address.get(key);
for (Address value : values)
value.printAddress();
}
}
private void searchData() {
while (true) {
Scanner sc= new Scanner(System.in);
System.out.println("찾는 사람은 누구입니까?");
String name = sc.nextLine();
char searchKey = CommonUtilities.getfirstCharacter(name);
if (address.containsKey(searchKey)) {// 찾는 사람이 있는 경우
// 키값으로 밸류값 꺼내오기
List<Address> values = address.get(searchKey);
boolean isExist = false;
for (Address value : values) {
if (name.equals(value.name)) {
value.printAddress();
isExist = true;
break;
}
} //// for
if (isExist == true)
break;
System.out.println("해당하는 사람이 없어요(키값 존재)");
} /// if
else
System.out.println("해당하는 사람이 없어요(키값 미존재)");
} /////// while
}
}///////////////////////AddressLogic