-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
383 lines (280 loc) · 9.87 KB
/
Copy pathMain.java
File metadata and controls
383 lines (280 loc) · 9.87 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*組員:
* ACS109151 楊竣凱
* ACS109150 林裕晉
*/
import java.util.*;
import java.io.*;
public class Main
{
public static FileInputStream SystemInput;
public static int NextID;
public static Scanner scanner;
public static ArrayList<book> CopyMenu;
public static ArrayList<Control> FuntionMenu;
public static void main(String[] args)
{
File Enter= new File(args[0]);
try
{
SystemInput= new FileInputStream(Enter);
scanner= new Scanner(SystemInput);
Copybuild();
Funtionbuild();
CommandExecute();
}
catch(Exception e)
{
System.out.println("Main_ERROR");
System.out.println(e.getMessage());
System.exit(0);
}
}
//初始化使用者
public static void Funtionbuild()
{
FuntionMenu= new ArrayList<Control>();
try
{
int Count= Integer.valueOf(scanner.nextLine());
for(;Count>0;Count--)
{
StringTokenizer Tokens= new StringTokenizer(scanner.nextLine());
if(Tokens.countTokens()==2)
FuntionMenu.add(new Control(Tokens.nextToken(), Tokens.nextToken()));
else if(Tokens.countTokens()==3)
FuntionMenu.add(new Control(Tokens.nextToken(), Tokens.nextToken(), Integer.valueOf(Tokens.nextToken())));
}
}
catch(Exception e)
{
System.out.println("USER_BUILD_WRONG");
System.exit(0);
}
}
//找出使用者位置(若找無則回傳-1)
public static int SearchUser(String InputName) //return index of user in list
{
for(int i=0;i< FuntionMenu.size();i++)
{
if(InputName.equals(FuntionMenu.get(i).getName()))
{
return i;
}
}
return -1;
}
public static void FINDBORROWER(String User, String Variable)
{
int PO= SearchUser(User); //使用者位置
try
{
int bookId= Integer.valueOf(Variable);
FuntionMenu.get(PO).findBorrower(bookId, CopyMenu);
}
catch(Exception e)
{
System.out.println("CAN_NOT_FIND");
System.exit(0);
}
}
//找出使用者之借書內容
public static void FINDCHECKED(String user, String Variable)
{
int Po= SearchUser(user);
try
{
int TryPo= SearchUser(Variable);
FuntionMenu.get(Po).findChecked(FuntionMenu.get(TryPo), CopyMenu);
}
catch(Exception e)
{
System.out.println("Error when find checked.");
System.exit(0);
}
}
//列出使用者支借書清單
public static void LISTSUBJECT(String user, String Variable)
{
int Po= SearchUser(user);
try
{
FuntionMenu.get(Po).listSubject(Variable, CopyMenu);
}
catch(Exception e)
{
System.out.println("LIST_ERROR");
System.exit(0);
}
}
//CMD執行
public static void CommandExecute()
{
try
{
while(scanner.hasNextLine())
{
String Command= scanner.nextLine();
StringTokenizer tokens= new StringTokenizer(Command);
//判斷2個token的指令
if(tokens.countTokens()==2)
{
String User= tokens.nextToken();
String Program= tokens.nextToken();
int index= SearchUser(User);
if(index== -1) //若沒找到相同使用者
throw new Exception("HE_IS_NOT_USER\""+User +"\".");
else if(!Program.equals("addBook")) //錯誤指令
throw new Exception("COMMAND_ERROR\""+Command +"\".");
else
UsingBook(index);
}
//判斷3個token的指令
else if(tokens.countTokens()==3)
{
String User= tokens.nextToken();
String Program= tokens.nextToken();
String Variable= tokens.nextToken();
int Direct= SearchUser(User);
//判斷是否找到使用者及指令
if((Direct==-1))
throw new Exception("HE_IS_NOT_USER\""+User+"\".");
else if(Program.equals("removeBook"))
REMOVECOPY(User, Variable);
else if(Program.equals("checkout"))
CHECKOUT(User, Variable);
else if(Program.equals("return"))
RETURN(User, Variable);
else if(Program.equals("listAuthor"))
LISTAUTHOR(User, Variable);
else if(Program.equals("listSubject"))
LISTSUBJECT(User, Variable);
else if(Program.equals("findChecked"))
FINDCHECKED(User, Variable);
else if(Program.equals("findBorrower"))
FINDBORROWER(User, Variable);
else throw new Exception("COMMAND_ERROR\""+Command +"\".");
}
else
{
throw new Exception("COMMAND_ERROR\""+Command +"\".");
}
}
}
catch(Exception e)
{
System.out.println("COMMAND_EXECUTE_ERROR");
System.out.println(e.getMessage());
System.exit(0);
}
}
//列出作者
public static void LISTAUTHOR(String user, String Variable)
{
int Po= SearchUser(user);
try
{
FuntionMenu.get(Po).listAuthor(Variable, CopyMenu);
}
catch(Exception e)
{
System.out.println("AUTHOR_ERROR");
System.exit(0);
}
}
//還書
public static void RETURN(String user, String Variable)
{
int Po= SearchUser(user);
try
{
int CopyId= Integer.valueOf(Variable);
String BorrowerName= "";
//由被借的書之ID找出借書者
for(book Copy : CopyMenu)
{
if(Copy.getId()== CopyId)
BorrowerName= Copy.getBorrower();
}
CopyMenu= FuntionMenu.get(Po).returnBook(CopyId, CopyMenu);
//若書尚未借出
if(!BorrowerName.equals("Nothing"))
{
int Po2= SearchUser(BorrowerName);
int lim= FuntionMenu.get(Po2).getMax();
FuntionMenu.get(Po2).setMax(lim+1);
}
}
catch(Exception e)
{
System.out.println("RETURN_ERROR");
System.exit(0);
}
}
//初始化書
public static void Copybuild()
{
CopyMenu= new ArrayList<book>();
NextID= 0;
try
{
int Count= Integer.valueOf(scanner.nextLine());
for(;Count>0;Count--)
{
StringTokenizer Tokens= new StringTokenizer(scanner.nextLine());
CopyMenu.add(new book(Tokens.nextToken(), Tokens.nextToken(), NextID++));
}
}
catch(Exception e)
{
System.out.println("BOOK_BUILD_WRONG");
System.exit(0);
}
}
//新增書
public static void UsingBook(int USERPO)
{
try
{
StringTokenizer Tokens= new StringTokenizer(scanner.nextLine());
book AddCopy= new book(Tokens.nextToken(), Tokens.nextToken(), NextID++);
CopyMenu= FuntionMenu.get(USERPO).addBook(AddCopy, CopyMenu);
}
catch(Exception e)
{
System.out.println("ADD_BOOK_ERROR");
System.exit(0);
}
}
//移除書
public static void REMOVECOPY(String user, String Variable)
{
int USERPO= SearchUser(user);
try
{
CopyMenu= FuntionMenu.get(USERPO).removeBook(Integer.valueOf(Variable), CopyMenu);
}
catch(Exception e)
{
System.out.println("REMOVE_BOOK_WRONG");
System.exit(0);
}
}
//借書
public static void CHECKOUT(String user, String Variable)
{
int USERPO= SearchUser(user);
int USERPO2= SearchUser(Variable);
try
{
StringTokenizer tokens= new StringTokenizer(scanner.nextLine());
ArrayList<Integer> IDPRINT= new ArrayList<Integer>();
while(tokens.hasMoreTokens()) IDPRINT.add(Integer.valueOf(tokens.nextToken()));
CopyMenu= FuntionMenu.get(USERPO).checkout(FuntionMenu.get(USERPO2), IDPRINT, CopyMenu);
}
catch(Exception e)
{
System.out.println("CHECKOUT_ERROR");
System.exit(0);
}
}
}