-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControl.java
More file actions
284 lines (219 loc) · 5.48 KB
/
Copy pathControl.java
File metadata and controls
284 lines (219 loc) · 5.48 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
import java.util.*;
public class Control {
public int maxborrow;
public String name;
public String identity;
//借書數上限
public void setMax(int max)
{
maxborrow= max;
}
public int getMax()
{
return maxborrow;
}
public void setName(String nam)
{
this.name = nam;
}
public String getName()
{
return this.name;
}
public void setIdentity(String iden)
{
this.identity = iden;
}
public String getIdentity()
{
return this.identity;
}
//設置借閱人
public Control(String iden, String name)
{
if(iden.equals("Borrower"))
{
System.out.println("Error! Wrong format for adding borrower.");
return;
}
setIdentity(iden);
setName(name);
}
//設置員工
public Control(String iden, String name, int mxborrow)
{
if(iden.equals("Staff")&&mxborrow!=0)
{
System.out.println("Error! Wrong format for adding staff.");
return;
}
setIdentity(iden);
setName(name);
setMax(mxborrow);
}
//列出符合作者之書本及其內容
public void listAuthor(String aut,ArrayList<book> a)
{
for(int i=0;i<a.size();i++)
{
if(a.get(i).getAuthor().equalsIgnoreCase(aut))
{
System.out.println("ID:"+a.get(i).getId()+
" Author:"+a.get(i).getAuthor()+
" Subject:"+a.get(i).getSubject());
}
}
}
//列出符合主題之書本及其內容
public void listSubject(String sub,ArrayList<book> a)
{
for(int i=0;i<a.size();i++)
{
if(a.get(i).getSubject().equalsIgnoreCase(sub))
{
System.out.println("ID:"+a.get(i).getId()+
" Author:"+a.get(i).getAuthor()+
" Subject:"+a.get(i).getSubject());
}
}
}
//執行新增書
public ArrayList<book> addBook(book Book, ArrayList<book> a)
{
book temp = new book(Book.getAuthor(), Book.getSubject(), Book.getId());
if(identity.equals("Staff"))
a.add(temp);
else
System.out.println("Borrower can not add book.");
return a;
}
//執行移除書
public ArrayList<book> removeBook(int id, ArrayList<book> a)
{
if(!identity.equals("Staff"))
{
System.out.println("Borrower can not remove book.");
return a;
}
for(int i=0; i<a.size(); i++)
{
if(a.get(i).getId() == id)
a.remove(i);break;
}
return a;
}
//執行借書
public ArrayList<book> checkout(Control user2, ArrayList<Integer> id, ArrayList<book> a)
{
if(!identity.equals("Staff"))
{
System.out.println("Borrower can not check out the books.");
return a;
}
//若超過借書上限
if(id.size() > user2.maxborrow){
System.out.println("Can not check out since the number of books exceed the limitation of user can check-out");
return a;
}
//檢查書是否已被借出並設置false
for(int i : id)
{
for(book s : a)
{
if(s.getId() == id.get(i)){
if(!s.getStatus())
{
System.out.println(i + " " + s.getAuthor() + " " + s.getSubject() + "is already checked out");
break;
}
else
{
s.setStatus(false);
s.setBorrower(user2.getName());
user2.setMax(user2.getMax()-1);
}
}
}
}
return a;
}
public ArrayList<book> returnBook(int id, ArrayList<book> a)
{
for(book s : a)
{
if(s.getId() == id)
{
if(s.getStatus())
{
System.out.println("Can not return since the book isn't checked out ");
return a;
}
else
{
s.setStatus(true);
s.setBorrower("Nothing");
}
}
}
return a;
}
//找出借閱人
public void findChecked(Control user2, ArrayList<book> a)
{
boolean flag = false;//檢查書的最後借閱人是否為 user2
if(identity.equals("Borrower") && name.equals(user2.getName())) //兩者皆為借書人
{
for(book s : a)
{
if(s.getBorrower().equals(user2.getName()))
{
flag = true;
System.out.printf("ID:"+s.getId()+
" Author:"+s.getAuthor()+
" Subject:"+s.getSubject());
}
}
}
else if(identity.equals("Staff")) //user1 為員工
{
for(book s : a)
{
if(s.getBorrower().equals(user2.getName()))
{
flag = true;
System.out.println("ID:"+s.getId()+
" Author:"+s.getAuthor()+
" Subject:"+s.getSubject());
}
}
}
else if(identity.equals("Borrower") && user2.getIdentity().equals("Borrower")) //user1 != user2,且兩者皆為借書人
{
System.out.println("Borrower can not find books checked out by other users.");
return;
}
else
{
System.out.println("Staff can not find books checked out by other staffs.");
return;
}
if(!flag)
System.out.println("there isn't any book whose last borrower is " + user2.getName());
}
//找出借書人
public void findBorrower(int id, ArrayList<book> a)
{
if(identity.equals("Staff")) //only員工可查詢
{
for(book s : a)
{
if(s.getId() == id)
{
System.out.println("User: "+s.getBorrower());
}
}
}
else
System.out.println("Borrower can not find borrower");
}
}