blob: 8344bcaf404506295caab91f671cbbeb846ef6d4 (
plain) (
blame)
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
384
385
386
387
388
389
390
391
392
393
|
/**
* Copyright (c) 2015 Camil Staps <info@camilstaps.nl>
* See the LICENSE file for copying permission.
*/
package com.camilstaps.shop;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;
/**
* The Shop database
* @author Camil Staps, s4498062
*/
public class Database {
/**
* This is a singleton
*/
private static Database instance;
/**
* Keep the database in working memory during runtime
*/
private Set<User> users;
private Set<Article> articles;
private Set<Category> categories;
private Set<Order> orders;
/**
* Files to store the database
* Considering backups, it's nicer to have different objects stored in
* different files, so that if the Orders file breaks, we still have the
* Users, etc.
*/
private final File FILE_DB = new File("./db");
private final File FILE_USERS = new File("./db/users.db");
private final File FILE_ARTICLES = new File("./db/articles.db");
private final File FILE_CATEGORIES = new File("./db/categories.db");
private final File FILE_ORDERS = new File("./db/orders.db");
/**
* Don't use this constructor. This is a singleton: use getInstance() instead.
* @see self#getInstance()
*/
public Database() {
if (!FILE_DB.exists())
FILE_DB.mkdir();
readUsers();
readArticles();
readCategories();
readOrders();
}
/**
* Get an instance of the database. This is a singleton.
* @return the database
*/
public static Database getInstance() {
if (instance == null) {
instance = new Database();
}
return instance;
}
/**
* Add an item to the database
* @param item the item
* @throws com.camilstaps.shop.DuplicateEntryException
* @throws ClassCastException if the item to remove is not a DatabaseItem
*/
public void addItem(DatabaseItem item) throws DuplicateEntryException {
if (item instanceof User) {
if (isUserExists(((User) item).getNr())) {
throw new DuplicateEntryException();
}
users.add((User) item);
} else if (item instanceof Article) {
if (isArticleExists(((Article) item).getName())) {
throw new DuplicateEntryException();
}
articles.add((Article) item);
} else if (item instanceof Category) {
if (isCategoryExists(((Category) item).getName())) {
throw new DuplicateEntryException();
}
categories.add((Category) item);
} else if (item instanceof Order) {
orders.add((Order) item);
} else {
throw new ClassCastException();
}
}
/**
* Remove an item from the database
* @param item the item to remove
* @throws ClassCastException if the item to remove is not a DatabaseItem
*/
public void removeItem(DatabaseItem item) {
if (item instanceof User) {
users.remove((User) item);
} else if (item instanceof Article) {
articles.remove((Article) item);
} else if (item instanceof Category) {
categories.remove((Category) item);
} else if (item instanceof Order) {
orders.remove((Order) item);
} else {
throw new ClassCastException();
}
}
/**
* Save the database to the filesystem.
* @return true on success, false on failure
*/
public boolean write() {
return writeUsers() && writeArticles() && writeCategories() && writeOrders();
}
/**
* Check if there exists a user with a certain U/S-number
* @param nr
* @return
*/
public boolean isUserExists(String nr) {
for (User user : users) {
if (user.getNr().equals(nr)) {
return true;
}
}
return false;
}
/**
* Check if an article with a name exists
* @param name
* @return
*/
public boolean isArticleExists(String name) {
for (Article article : articles) {
if (article.getName().equals(name)) {
return true;
}
}
return false;
}
/**
* Check if a category with a name exists
* @param name
* @return
*/
public boolean isCategoryExists(String name) {
for (Category category : categories) {
if (category.getName().equals(name)) {
return true;
}
}
return false;
}
/**
* Get the set of users
* @return
*/
public Set<User> getUsers() {
return users;
}
/**
* Get the set of articles
* @return
*/
public Set<Article> getArticles() {
return articles;
}
/**
* Get the set of categories
* @return
*/
public Set<Category> getCategories() {
return categories;
}
/**
* Get an array of the names of the categories
* @return
*/
public String[] getCategoryNames() {
String[] categoryNames = new String[categories.size()];
int i = 0;
for (Category c : categories) {
categoryNames[i++] = c.getName();
}
return categoryNames;
}
/**
* Get the set of orders
* @return
*/
public Set<Order> getOrders() {
return orders;
}
/**
* Get the set of articles of which the name or description matches a regular expression
* @param p the regular expression
* @return
*/
public Set<Article> searchArticle(Pattern p) {
Set<Article> result = new HashSet<>();
for (Article a : articles) {
if (p.matcher(a.getName()).find() || p.matcher(a.getDescription()).find()) {
result.add(a);
}
}
return result;
}
/**
* Get a user by his number
* @param number
* @return
* @throws com.camilstaps.shop.ItemNotFoundException
*/
public User getUser(String number) throws ItemNotFoundException {
for (User u : users) {
if (u.getNr().equals(number)) {
return u;
}
}
throw new ItemNotFoundException();
}
/**
* Get an article by its name
* @param name
* @return
* @throws ItemNotFoundException
*/
public Article getArticle(String name) throws ItemNotFoundException {
for (Article a : articles) {
if (a.getName().equals(name)) {
return a;
}
}
throw new ItemNotFoundException();
}
/**
* Get a category by a name
* @param name
* @return
*/
public Category getCategory(String name) throws ItemNotFoundException {
for (Category c : categories) {
if (c.getName().equals(name)) {
return c;
}
}
throw new ItemNotFoundException();
}
/**
* Read the users from the database into RAM
*/
private void readUsers() {
users = new HashSet<>();
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(FILE_USERS))) {
users = (Set) in.readObject();
in.close();
} catch (FileNotFoundException ex) {
} catch (IOException | ClassNotFoundException ex) {
}
}
/**
* Read the articles from the database into RAM
*/
private void readArticles() {
articles = new HashSet<>();
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(FILE_ARTICLES))) {
articles = (Set) in.readObject();
in.close();
} catch (FileNotFoundException ex) {
} catch (IOException | ClassNotFoundException ex) {
}
}
/**
* Read the categories from the database into RAM
*/
private void readCategories() {
categories = new HashSet<>();
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(FILE_CATEGORIES))) {
categories = (Set) in.readObject();
in.close();
} catch (FileNotFoundException ex) {
} catch (IOException | ClassNotFoundException ex) {
}
}
/**
* Read the orders from the database into RAM
*/
private void readOrders() {
orders = new HashSet<>();
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(FILE_ORDERS))) {
orders = (Set) in.readObject();
in.close();
} catch (FileNotFoundException ex) {
} catch (IOException | ClassNotFoundException ex) {
}
}
/**
* Write the users from RAM to the database
*/
private boolean writeUsers() {
System.err.println("Saving users...");
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(FILE_USERS))) {
out.writeObject(users);
out.close();
return true;
} catch (IOException ex) {
ex.printStackTrace();
}
return false;
}
/**
* Write the articles from RAM to the database
*/
private boolean writeArticles() {
System.err.println("Saving articles...");
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(FILE_ARTICLES))) {
out.writeObject(articles);
out.close();
return true;
} catch (IOException ex) {
ex.printStackTrace();
}
return false;
}
/**
* Write the categories from RAM to the database
*/
private boolean writeCategories() {
System.err.println("Saving categories...");
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(FILE_CATEGORIES))) {
out.writeObject(categories);
out.close();
return true;
} catch (IOException ex) {
ex.printStackTrace();
}
return false;
}
/**
* Write the orders from RAM to the database
*/
private boolean writeOrders() {
System.err.println("Saving orders...");
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(FILE_ORDERS))) {
out.writeObject(orders);
out.close();
return true;
} catch (IOException ex) {
ex.printStackTrace();
}
return false;
}
}
|