1. 전화번호 관리 프로그램 - 전화번호 05단계
우리가 정의한 Manager 클래스는 생성되는 인스턴스의 수가 하나인 클래스이다. 이 클래스의 성격을 봐서 알겠지만, 이 클래스의 인스턴스는 둘 이상 생성될 필요가 업으며, 혹시라도 둘 이상의 인스턴스가 생성된다면 이는 실수로 인한 것일 확률이 높다. 그래서 이번 단계에서는 Manager 클래스의 인스턴스 수가 최대 하나를 넘지 않도록 코드를 변경하고자 한다. 그리고 본 프로젝트에서는 프로그램 사용자로부터 다음 중 하나의 선택을 입력 받아서 프로그램을 실행하고 있다.
1. 데이터 입력
2. 데이터 검색
3. 데이터 삭제
4. 모든 데이터 보기
5. 프로그램 종료
뿐만 아니라, 위의 다섯 가지 중에서 '데이터 입력'을 선택하면, 다음 세가지 중 하나의 선택을 추가로 입력 받아서, 그에 맞는 입력의 과정을 진행하고 있다.
1. 일반
2. 대학
3. 회사
한 가지 안타까운 점은 이들 메뉴에 대한 정보가 이름이 아닌(이름이 부여된 상수가 아닌) 숫자로 처리되고 있다는 점이다. 때문에 'interface 기반의 상수표현'으로 메뉴의 선택과 그에 따른 처리가, 이름이 부여된 상수를 기반으로 진행되도록 변경하고자 한다. 이를 통해서 우리는 코드의 내용이 보다 명확해진다는 이점을 얻게 될 것이다.
데이터 정보 삭제시 정말 삭제 하시겠습니까? 라는 메세지를 띄우면서 한번 더 물어보기.
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
309
310
311
312
313
|
class PhoneInfo
{
private String name, phone;
PhoneInfo(String name, String phone)
{
this.name = name;
this.phone = phone;
}
public String getName()
{
return name;
}
public void showPhoneInfo()
{
System.out.println("이름 : " + name);
System.out.println("전화번호 : " + phone);
}
}
class PhoneUnivInfo extends PhoneInfo
{
private String major;
private int year;
PhoneUnivInfo(String name, String phone, Strnig major, String year)
{
super(name, phone);
this.major = major;
this.year = yaer;
}
public void showPhoneInfo()
{
super.showPhoneInfo();
System.out.println("전공 : " + major);
System.out.println("학년 : " + year);
}
}
class PhoneCompanyInfo extends PhoneInfo
{
private String company;
PhoneCompany(String name, String phone, String company)
{
super(name, phone);
this.company = company;
}
public void showPhoneInfo()
{
super.showPhoneInfo();
System.out.println("회사 : " + company);
}
}
class Manager
{
private static Manager mg;
private PhoneInfo[] info;
private int numOfPage;
Manager(int size)
{
info = new PhoneInfo[size];
numOfPage = 0;
}
public static Manager getManager(int size)
{
if( mg == null )
mg = new Manager(size);
return mg;
}
public void insertInfo(PhoneInfo info)
{
int i=0, j=0;
if(numOfPage >= this.info.length)
{
System.out.println("저장 공간 부족");
return;
}
for(i=0; i<numOfPage; i++)
{
if(this.info[i].getName.compareTo(info.getName()) > 0)
{
for(j=numOfPage-1; j>=i; j--)
{
this.info[j+1] = this.info[j];
}
break;
}
}
this.info[i] = info;
numOfPage++;
}
public void searchInfo(String name)
{
int result = search(name);
if(result != -1)
info[result].showPhoneInfo();
else
System.out.println("찾으시는 데이터가 없습니다.");
}
public void deleteInfo(int idx)
{
int i=0;
for(i=idx;i<numOfPage-1;i++)
info[i] = info[i+1];
info[i] = null;
numOfPage--;
System.out.println("삭제가 완료되었습니다.");
}
public int search(String name)
{
for(int i=0;i<numOfPage;i++)
{
if(info[i].getName().compareTo(name) == 0)
return i;
}
return -1;
}
public void showAllInfo()
{
for(int i=0; i<numOfPage; i++)
info[i].showPhoneInfo();
}
}
interface MenuString
{
int INSERT_INFO = 1;
int SEARCH_INFO = 2;
int DELETE_INFO = 3;
int SHOW_ALL_INFO = 4;
int QUIT = 5;
int GENERAL = 1;
int UNIVERCITY = 2;
int COMPANY = 3;
int YES = 1;
int NO = 2;
}
class PhoneUI implements MenuString
{
private static final int MAX=100;
public static Scanner sc = new Scanner(System.in);
private static Manager mg = Manager.getManager(MAX);
public static void mainMenu()
{
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 insertMenu()
{
System.out.println("1. 일반, 2. 대학, 3. 회사");
}
public static void insertMenuChoice()
{
int choice=0;
choice = sc.nextInt();
sc.nextLine();
switch(choice)
{
case GENERAL:
inputGeneral();
break;
case UNIVERCITY:
insertUniversity();
break;
case COMPANY:
insertCompany();
break;
default:
System.out.println("잘못 입력 하셨습니다.");
}
}
public static void insertGeneral()
{
String name;
String phone;
System.out.println("데이터 입력을 시작합니다.");
System.out.print("이름 : ");
name = sc.nextLine();
System.out.print("전화번호 : ");
phone = sc.nextLine();
System.out.println("데이터 입력이 완료되었습니다.");
mg.insertInfo( new PhoneInfo(name, phone) );
}
public static void insertUniversity()
{
String name;
String phone;
String major;
int year;
System.out.println("데이터 입력을 시작합니다.");
System.out.print("이름 : ");
name = sc.nextLine();
System.out.print("전화번호 : ");
phone = sc.nextLine();
System.out.print("전공 : ");
major = sc.nextLine();
System.out.print("학년 : ");
year = sc.nextInt();
sc.nextLine();
System.out.println("데이터 입력이 완료되었습니다.");
mg.insertInfo( new PhoneUnivInfo(name, phone, major, year) );
}
public static void insertCompany()
{
String name;
String phone;
String company;
System.out.println("데이터 입력을 시작합니다.");
System.out.print("이름 : ");
name = sc.nextLine();
System.out.print("전화번호 : ");
phone = sc.nextLine();
System.out.print("회사 : ");
company = sc.nextLine();
System.out.println("데이터 입력이 완료되었습니다.");
mg.insertInfo( new PhoneCompanyInfo(name, phone, company) );
}
public static void searchInfo()
{
String name;
System.out.println("데이터 검색을 시작합니다.");
System.out.println("검색하시고자 하는 이름을 입력하세요.");
name = sc.nextLine();
mg.searchInfo(name);
}
public static void deleteInfo()
{
String name;
int result=0, answer=0;
System.out.println("검색하시고자 하는 이름을 입력하세요.");
name = sc.nextLine();
result = mg.search(name);
if(result != -1)
{
System.out.println("정말 삭제하시겠습니까? 1. Yes 2. No");
answer = sc.nextInt();
sc.nextLine();
switch(answer)
{
case YES:
pb.deleteInfo(result);
break;
case NO:
break;
default:
System.out.println("잘못 누르셨습니다.");
}
}
else
System.out.println("삭제하시려는 데이터가 없습니다.");
}
public static void showAllInfo()
{
mg.showAllInfo();
}
}
class PhoneMain implements MenuString
{
public static void main(String[] args)
{
int choice=0;
while(true)
{
PhoneUI.mainMenu();
choice = PhoneUI.sc.nextInt();
PhoneUI.sc.nextLine();
switch(choice)
{
case INSERT_INFO:
PhoneUI.insertMenu();
PhoneUI.insertMenuChoice();
break;
case SEARCH_INFO:
PhoneUI.searchInfo();
break;
case DELETE_INFO:
PhoneUI.deleteInfo();
break;
case SHOW_ALL_INFO:
PhoneUI.showAllInfo();
break;
case QUIT:
return;
default:
System.out.println("잘못 선택하셨습니다.");
}
}
}
}
|
cs |
'Java > Day21' 카테고리의 다른 글
[Java] 예외 처리 / try~catch구문 & throws & 실습문제 (0) | 2021.11.26 |
---|