-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUI.java
More file actions
179 lines (169 loc) · 4.43 KB
/
Copy pathUI.java
File metadata and controls
179 lines (169 loc) · 4.43 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
package GradeSystemProjcect;
public class UI {
public GradeSystems aGradeSystem = null;
/* method checkID
* 要 aGradeSystem做containsID(ID)看ID是否在aGradeSystem內
*
* @param ID The specified ID
* @return aGradeSystem有無ID的布林值
*
* Time estimate: O(1)
* Example:
* UI.checkID("123456789"); return false;
* UI.checkID("962001051"); return true;
* Pseudo code:
* 1. 要aGradeSystem 做containsID(ID) 看 ID 是否含在 aGradeSystem內
*/
private boolean checkID(String ID) {
return aGradeSystem.containsID(ID);
}
/* method promptID
* get user ID or 結束使用指令
*
* @return 使用者輸入的String
*
* Time estimate: O(1)
* Example:
* UI.promptID();
* 螢幕顯示;
* 輸入ID 或 Q(結束使用)?
* 使用者輸入 :123456789
* Pseudo code:
* 1. 提示使用者輸入ID或Q
*/
private String promptID() {
System.out.println("輸入ID 或 Q(結束使用)?");
return Main.scanner.next();
}
/* method showWelcomeMsg
* print welcome message
*
* Time estimate: O(1)
* Example:
* UI.showWelcomeMsg("962001051");
* 螢幕顯示; Welcome, 李威廷
* Pseudo code:
* 1. 得到ID對應的使用者名稱
* 2. 螢幕顯示; Welcome, [使用者]
*/
public void showWelcomeMsg(String ID) {
Grades tmp = aGradeSystem.aMap.get(ID);
System.out.println("Welcome, " + tmp.name);
}
/* method promptCommand
* get user command
*
* @return 使用者是否決定登出
*
* Time estimate: O(n), n 是aGradeSystem內全班人數
* Example:
* UI.promptCommand();
* 螢幕顯示;
* 輸入指令
* 1) G 顯示成績 (Grade)
* 2) R 顯示排名 (Rank)
* 3) A 顯示平均 (Average)
* 4) W 更新配分 (Weight)
* 5) E 離開選單 (Exit)
* 使用者輸入 :R
* Pseudo code:
* 1. prompt user for inputCommand
* 2. if inputCommand is not G (Grade),R (Rank), W (Weights), or E (Exit),
* throws an object of NoSuchCommandException
* 3. if inputCommand is E (Exit) then break
* else: G aGradeSystem.showGrade(ID), R showRank(ID), A showAverage(), W updateWeights() end if
*/
public boolean promptCommand(String inputID) throws NoSuchCommandExceptions {
System.out.println("輸入指令\n" +
"1) G 顯示成績 (Grade)\n" +
"2) R 顯示排名 (Rank)\n" +
"3) A 顯示平均 (Average)\n" +
"4) W 更新配分 (Weight)\n" +
"5) E 離開選單 (Exit)");
String inputCmd = Main.scanner.next();
switch(inputCmd) {
case "E":
return false;
case "G":
aGradeSystem.showGrade(inputID);
return true;
case "R":
aGradeSystem.showRank(inputID);
return true;
case "A":
aGradeSystem.showAverage();
return true;
case "W":
aGradeSystem.updateWeights();
return true;
default:
throw new NoSuchCommandExceptions(inputCmd);
}
}
/* method showFinishMsg
* print finish message
*
* Time estimate: O(1)
* Example:
* UI.showFinishMsg();
* 螢幕顯示;
* 再見...
* Pseudo code:
* 1. 輸出 "再見..."
*/
private void showFinishMsg() {
System.out.println("再見...");
}
/* constructor
* 建構 aGradeSystem
*
* @throws NoSuchIDException -
* if the specified ID is not in aGradeSystem
*
* @throws NoSuchCommandException -
* if the specified ID is null
*
* Time estimate: O(mn), n 是aGradeSystem內全班人數, m 是使用者指令總數目
* Example:
* UI aUI = new UI();
*
* Pseudo code:
* 1.call GradeSystems() to建構 aGradeSystem
* 2.loop1 until Q (Quit)
* 1. promptID() to get user ID 輸入ID或 Q (結束使用)?
* 2. checkID (ID) 看 ID 是否在 aGradeSystem內
* 3. showWelcomeMsg (ID) e.g. Welcome李威廷
* 4. loop2 until E (Exit)
* promptCommand() to prompt for input command
* end loop2
* end loop1
* 3.showFinishMsg() 結束了
* end try
* finally {}
*/
public UI() throws NoSuchIDExceptions, NoSuchCommandExceptions {
aGradeSystem = new GradeSystems();
String inputID = null;
while(true) {
inputID = promptID();
if(inputID.equals("Q"))
{
this.showFinishMsg();
break;
}
if(!checkID(inputID)) {
throw new NoSuchIDExceptions();
}
this.showWelcomeMsg(inputID);
while(true) {
try {
if(!this.promptCommand(inputID)) {
break;
}
}catch(NoSuchCommandExceptions e) {
throw new NoSuchCommandExceptions(e.getMessage());
}
}
}
}
}