단계별 프로젝트 7 - 문제
단계별 프로젝트 03단계에서는 동명이인의 데이터가 존재하지 않는다고 가정했지만 배열을 대상으로 저장이 이뤄졌기 때문에, 사실상 동명이인의 데이터가 저장되는 상황을 막지는 못했다.
따라서 이번 단계에서는 동일한 데이터의 저장을 허용하지 않는 HashSet<E> 클래스를 대상으로 저장이 이뤄지도록 프로젝트를 변경하고자 한다. 그리고 이 과정에서 동일한 인스턴스의 기준을 다음과 같이 정의하고자 한다. (HashSet 구현후 TreeSet도 구현해 본다.)
"전화번호만 같으면, 그 이외의 정보가 아무리 달라도 동일한 데이터로(인스턴스로) 간주한다."
실행 예
선택하세요...
1. 데이터 입력
2. 데이터 검색
3. 데이터 삭제
4. 전체보기
5. 프로그램 종료
선택 : 1
데이터 입력을 시작합니다.
1. 일반 2. 대학 3. 회사
선택>> 1
이름 : 조한석
전화번호 : 011-222-4444
데이터 입력이 완료되었습니다.
선택하세요...
1. 데이터 입력
2. 데이터 검색
3. 데이터 삭제
4. 전체보기
5. 프로그램 종료
선택 : 1
데이터 입력을 시작합니다.
1. 일반 2. 대학 3. 회사
선택>> 2
이름 : 조한석
전화번호 : 011-222-4444
전공 : 전자공학
학년 : 1
이미 저장된 데이터 입니다.
선택하세요...
1. 데이터 입력
2. 데이터 검색
3. 데이터 삭제
4. 전체보기
5. 프로그램 종료
선택 :
단계별 프로젝트 7 - 풀이1 (HashSet 풀이)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
public class PhoneInfo {
private String name;
private String phoneNumber;
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);
}
@Override
public boolean equals(Object obj) {
return phoneNumber.equals(((PhoneInfo)obj).phoneNumber);
}
@Override
public int hashCode() {
return phoneNumber.hashCode();
}
}
public class PhoneUnivInfo extends PhoneInfo {
private String major;
private int year;
public PhoneUnivInfo(String name, String phoneNumber, String major, int year) {
super(name, phoneNumber);
this.major = major;
this.year = year;
}
@Override
public void printCurrentState() {
super.printCurrentState();
System.out.println("전공 : " + major);
System.out.println("학년 : " + year);
}
}
public class PhoneCompanyInfo extends PhoneInfo {
private String company;
public PhoneCompanyInfo(String name, String phoneNumber, String company) {
super(name, phoneNumber);
this.company = company;
}
@Override
public void printCurrentState() {
super.printCurrentState();
System.out.println("회사 : " + company);
}
}
public interface Menu {
int INSERT_PHONE_INFO=1; // 상수로 준 이유 : 가독성을 높이기 위해서
int SEARCH_PHONE_INFO=2;
int DELETE_PHONE_INFO=3;
int SHOW_ALL_PHONE_INFO=4;
int QUIT_PHONE_INFO=5;
}
public class MenuChoiceException extends Exception {
public MenuChoiceException(int menu)
{
super(menu + "에 해당하는 선택은 존재하지 않습니다.\n" +
"메뉴 선택을 처음부터 다시 진행합니다.");
}
}
public class PhoneBook {
private static PhoneBook pb;
private HashSet<PhoneInfo> set;
private Iterator<PhoneInfo> itr;
private PhoneBook()
{
set = new HashSet<PhoneInfo>();
}
public static PhoneBook getPhoneBook()
{
if(pb==null)
pb = new PhoneBook();
return pb;
}
public boolean insertPhoneInfo(PhoneInfo phoneInfo)
{
return set.add(phoneInfo);
}
public boolean searchPhoneInfoByName(String name)
{
PhoneInfo pInfo = null;
itr = set.iterator();
boolean result = false;
while(itr.hasNext())
{
pInfo = itr.next();
if(pInfo.getName().equals(name))
{
pInfo.printCurrentState();
result = true;
}
}
return result;
}
public boolean deletePhoneInfoByPhoneNumber(String phoneNumber)
{
PhoneInfo pInfo = null;
itr = set.iterator();
while(itr.hasNext())
{
pInfo = itr.next();
if(pInfo.getPhoneNumber().equals(phoneNumber))
{
itr.remove();
return true;
}
}
return false;
}
public void printAllPhoneInfo()
{
itr = set.iterator();
while(itr.hasNext())
{
itr.next().printCurrentState();
}
// for(PhoneInfo info: set)
// info.printCurrentState();
}
}
public class PhoneBookUI {
private PhoneBook pb;
public static Scanner sc = new Scanner(System.in);
public PhoneBookUI()
{
this.pb = PhoneBook.getPhoneBook();
}
public 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.println("선택 : ");
}
public void inputMenu()
{
System.out.println("데이터 입력을 시작합니다.");
System.out.println("1. 일반, 2. 대학, 3. 회사");
System.out.print("선택 >>");
}
public void inputPhoneInfo(int menu)
{
String name, phoneNumber, major, company;
int year=0;
boolean result;
PhoneInfo phoneInfo = null;
System.out.println("데이터 입력을 시작합니다.");
System.out.println("이름 : ");
name = sc.nextLine();
System.out.println("전화번호 : ");
phoneNumber = sc.nextLine();
if(menu == 1) // 추가
phoneInfo = new PhoneInfo(name, phoneNumber);
else if(menu == 2)
{
System.out.println("전공 : ");
major = sc.nextLine();
System.out.println("학년 : ");
year = sc.nextInt();
phoneInfo = new PhoneUnivInfo(name, phoneNumber, major, year);
}
else if(menu==3)
{
System.out.println("회사 : ");
company = sc.nextLine();
phoneInfo = new PhoneCompanyInfo(name, phoneNumber, company);
}
result = pb.insertPhoneInfo(phoneInfo);
if(result == false)
System.out.println("이미 등록된 데이터 입니다.");
else System.out.println("데이터 입력이 완료되었습니다.");
}
public void searchPhoneInfoByName()
{
String name;
System.out.println("검색하시고자 하는 이름을 입력해 주세요.");
name = sc.nextLine();
System.out.println("사용자 검색을 시작합니다.");
if( !pb.searchPhoneInfoByName(name)) System.out.println("찾으시는 사용자가 없습니다.");
}
public void deletePhoneInfoByPhoneNumber()
{
String phoneNumber;
System.out.println("삭제하시고자 하는 전화번호를 입력해 주세요.");
phoneNumber = sc.nextLine();
boolean result = pb.deletePhoneInfoByPhoneNumber(phoneNumber);
if(result) System.out.println("삭제가 완료되었습니다.");
else System.out.println("삭제하시고자 하는 전화번호 정보가 없습니다.");
}
public void printAllPhoneInfo()
{
System.out.println("모든 사용자 정보를 출력합니다.");
pb.printAllPhoneInfo();
}
public void quitProgram()
{
System.out.println("프로그램을 종료합니다.");
sc.close();
}
}
public class PhoneMain{
public static void main(String[] args) {
int menu=0;
PhoneBookUI pbUI = new PhoneBookUI();
Scanner sc = PhoneBookUI.sc;
while(true)
{
pbUI.printMenu();
try
{
menu = sc.nextInt();
sc.nextLine();
if(menu<Menu.INSERT_PHONE_INFO || menu > Menu.QUIT_PHONE_INFO)
{
throw new MenuChoiceException(menu);
}
switch(menu)
{
case Menu.INSERT_PHONE_INFO:
pbUI.inputMenu();
menu = sc.nextInt();
sc.nextLine();
if(menu<1 || menu > 3)
{
throw new MenuChoiceException(menu);
}
pbUI.inputPhoneInfo(menu);
break;
case Menu.SEARCH_PHONE_INFO:
pbUI.searchPhoneInfoByName();
break;
case Menu.DELETE_PHONE_INFO:
pbUI.deletePhoneInfoByPhoneNumber();
break;
case Menu.SHOW_ALL_PHONE_INFO:
pbUI.printAllPhoneInfo();
break;
case Menu.QUIT_PHONE_INFO:
pbUI.quitProgram();
return;
}
}
catch(MenuChoiceException e)
{
System.out.println(e.getMessage());
}
}
}
}
|
cs |
단계별 프로젝트 7 - 풀이2 (TreeSet 풀이)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
public class PhoneInfo implements Comparable<PhoneInfo>{
private String name;
private String phoneNumber;
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);
}
@Override
public int compareTo(PhoneInfo pInfo) {
return name.compareTo(pInfo.name);
}
}
public class PhoneUnivInfo extends PhoneInfo {
private String major;
private int year;
public PhoneUnivInfo(String name, String phoneNumber, String major, int year) {
super(name, phoneNumber);
this.major = major;
this.year = year;
}
@Override
public void printCurrentState() {
super.printCurrentState();
System.out.println("전공 : " + major);
System.out.println("학년 : " + year);
}
}
public class PhoneCompanyInfo extends PhoneInfo {
private String company;
public PhoneCompanyInfo(String name, String phoneNumber, String company) {
super(name, phoneNumber);
this.company = company;
}
@Override
public void printCurrentState() {
super.printCurrentState();
System.out.println("회사 : " + company);
}
}
public interface Menu {
int INSERT_PHONE_INFO=1; // 상수로 준 이유 : 가독성을 높이기 위해서
int SEARCH_PHONE_INFO=2;
int DELETE_PHONE_INFO=3;
int SHOW_ALL_PHONE_INFO=4;
int QUIT_PHONE_INFO=5;
}
public class MenuChoiceException extends Exception {
public MenuChoiceException(int menu)
{
super(menu + "에 해당하는 선택은 존재하지 않습니다.\n" +
"메뉴 선택을 처음부터 다시 진행합니다.");
}
}
public class PhoneBook {
private static PhoneBook pb;
private TreeSet<PhoneInfo> set;
private Iterator<PhoneInfo> itr;
private PhoneBook()
{
set = new TreeSet<PhoneInfo>();
}
public static PhoneBook getPhoneBook()
{
if(pb==null)
pb = new PhoneBook();
return pb;
}
public boolean insertPhoneInfo(PhoneInfo phoneInfo)
{
return set.add(phoneInfo);
}
public boolean searchPhoneInfoByName(String name)
{
PhoneInfo pInfo = null;
itr = set.iterator();
boolean result = false;
while(itr.hasNext())
{
pInfo = itr.next();
if(pInfo.getName().equals(name))
{
pInfo.printCurrentState();
result = true;
}
}
return result;
}
public boolean deletePhoneInfoByPhoneNumber(String phoneNumber)
{
PhoneInfo pInfo = null;
itr = set.iterator();
while(itr.hasNext())
{
pInfo = itr.next();
if(pInfo.getPhoneNumber().equals(phoneNumber))
{
itr.remove();
return true;
}
}
return false;
}
public void printAllPhoneInfo()
{
itr = set.iterator();
while(itr.hasNext())
{
itr.next().printCurrentState();
}
// for(PhoneInfo info: set)
// info.printCurrentState(); 같은뜻이다.
}
}
public class PhoneBookUI {
private PhoneBook pb;
public static Scanner sc = new Scanner(System.in);
public PhoneBookUI()
{
this.pb = PhoneBook.getPhoneBook();
}
public 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.println("선택 : ");
}
public void inputMenu()
{
System.out.println("데이터 입력을 시작합니다.");
System.out.println("1. 일반, 2. 대학, 3. 회사");
System.out.print("선택 >>");
}
public void inputPhoneInfo(int menu)
{
String name, phoneNumber, major, company;
int year=0;
boolean result;
PhoneInfo phoneInfo = null;
System.out.println("데이터 입력을 시작합니다.");
System.out.println("이름 : ");
name = sc.nextLine();
System.out.println("전화번호 : ");
phoneNumber = sc.nextLine();
if(menu == 1) // 추가
phoneInfo = new PhoneInfo(name, phoneNumber);
else if(menu == 2)
{
System.out.println("전공 : ");
major = sc.nextLine();
System.out.println("학년 : ");
year = sc.nextInt();
phoneInfo = new PhoneUnivInfo(name, phoneNumber, major, year);
}
else if(menu==3)
{
System.out.println("회사 : ");
company = sc.nextLine();
phoneInfo = new PhoneCompanyInfo(name, phoneNumber, company);
}
result = pb.insertPhoneInfo(phoneInfo);
if(result == false)
System.out.println("이미 등록된 데이터 입니다.");
else System.out.println("데이터 입력이 완료되었습니다.");
}
public void searchPhoneInfoByName()
{
String name;
System.out.println("검색하시고자 하는 이름을 입력해 주세요.");
name = sc.nextLine();
System.out.println("사용자 검색을 시작합니다.");
if( !pb.searchPhoneInfoByName(name)) System.out.println("찾으시는 사용자가 없습니다.");
}
public void deletePhoneInfoByPhoneNumber()
{
String phoneNumber;
System.out.println("삭제하시고자 하는 전화번호를 입력해 주세요.");
phoneNumber = sc.nextLine();
boolean result = pb.deletePhoneInfoByPhoneNumber(phoneNumber);
if(result) System.out.println("삭제가 완료되었습니다.");
else System.out.println("삭제하시고자 하는 전화번호 정보가 없습니다.");
}
public void printAllPhoneInfo()
{
System.out.println("모든 사용자 정보를 출력합니다.");
pb.printAllPhoneInfo();
}
public void quitProgram()
{
System.out.println("프로그램을 종료합니다.");
sc.close();
}
}
public class PhoneMain{
public static void main(String[] args) {
int menu=0;
PhoneBookUI pbUI = new PhoneBookUI();
Scanner sc = PhoneBookUI.sc;
while(true)
{
pbUI.printMenu();
try
{
menu = sc.nextInt();
sc.nextLine();
if(menu<Menu.INSERT_PHONE_INFO || menu > Menu.QUIT_PHONE_INFO)
{
throw new MenuChoiceException(menu);
}
switch(menu)
{
case Menu.INSERT_PHONE_INFO:
pbUI.inputMenu();
menu = sc.nextInt();
sc.nextLine();
if(menu<1 || menu > 3)
{
throw new MenuChoiceException(menu);
}
pbUI.inputPhoneInfo(menu);
break;
case Menu.SEARCH_PHONE_INFO:
pbUI.searchPhoneInfoByName();
break;
case Menu.DELETE_PHONE_INFO:
pbUI.deletePhoneInfoByPhoneNumber();
break;
case Menu.SHOW_ALL_PHONE_INFO:
pbUI.printAllPhoneInfo();
break;
case Menu.QUIT_PHONE_INFO:
pbUI.quitProgram();
return;
}
}
catch(MenuChoiceException e)
{
System.out.println(e.getMessage());
}
}
}
}
|
cs |