-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
433 lines (386 loc) · 12.3 KB
/
Main.java
File metadata and controls
433 lines (386 loc) · 12.3 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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Facade bookstore = new Facade();
String command = scanner.next();
while (!command.equals("end")) {
switch (command) {
case "createBook" -> {
String title = scanner.next();
String author = scanner.next();
String price = scanner.next();
bookstore.createBook(title, author, price);
}
case "createUser" -> {
String user_type = scanner.next();
String username = scanner.next();
bookstore.createUser(username, user_type);
}
case "subscribe" -> {
String username = scanner.next();
bookstore.subscribe(username);
}
case "unsubscribe" -> {
String username = scanner.next();
bookstore.unsubscribe(username);
}
case "updatePrice" -> {
String title = scanner.next();
String price = scanner.next();
bookstore.updatePrice(title, price);
}
case "readBook" -> {
String username = scanner.next();
String title = scanner.next();
bookstore.readBook(username, title);
}
case "listenBook" -> {
String username = scanner.next();
String title = scanner.next();
bookstore.listenBook(username, title);
}
}
command = scanner.next();
}
}
}
/**
* The Facade class provides a simplified interface for interacting with the BookStore system.
* It encapsulates the complexity of the system and exposes high-level methods for common operations.
*/
class Facade {
private BookStore bookstore;
public Facade() {
bookstore = BookStore.getInstance();
}
/**
* Creates a new book with the use of Builder design pattern and adds it to the bookstore.
*
* @param title the title of the book
* @param author the author of the book
* @param price the price of the book
*/
public void createBook(String title, String author, String price) {
Book.BookBuilder bookBuilder = new Book.BookBuilder();
Book book = bookBuilder.nameBook(title)
.setAuthor(author)
.setPrice(price)
.forSell();
bookstore.addBook(book);
}
/**
* Creates a new user and adds them to the bookstore.
*
* @param username the username of the user
* @param userType the type of the user (standard or premium)
*/
public void createUser(String username, String userType) {
if (userType.equals("standard")) {
bookstore.addUser(new StandardUser(username));
} else {
bookstore.addUser(new PremiumUser(username));
}
}
/**
* Subscribes a user to the bookstore's notifications.
*
* @param username the username of the user to subscribe
*/
public void subscribe(String username) {
bookstore.registerSubscriber(bookstore.getUser(username));
}
/**
* Unsubscribes a user from the bookstore's notifications.
*
* @param username the username of the user to unsubscribe
*/
public void unsubscribe(String username) {
bookstore.removeSubscriber(bookstore.getUser(username));
}
/**
* Updates the price of a book and notifies subscribers.
*
* @param title the title of the book to update
* @param price the new price of the book
*/
public void updatePrice(String title, String price) {
bookstore.notifySubscribers(bookstore.getBook(title), price);
}
/**
* Allows a user to read a book from the bookstore.
*
* @param username the username of the user
* @param title the title of the book to read
*/
public void readBook(String username, String title) {
bookstore.readBook(bookstore.getBook(title), bookstore.getUser(username));
}
/**
* Allows a user to listen to an audiobook from the bookstore.
*
* @param username the username of the user
* @param title the title of the audiobook to listen to
*/
public void listenBook(String username, String title) {
bookstore.listenBook(bookstore.getBook(title), bookstore.getUser(username));
}
}
/**
* The Observable interface represents an object that can be observed by subscribers.
* It defines methods for managing subscribers and notifying them of changes.
*/
interface Observable {
void registerSubscriber(User user);
void removeSubscriber(User user);
void notifySubscribers(Book book, String price);
}
/**
* The BookStore class represents a store that manages books and users.
* It implements the Observable interface to allow users to subscribe and receive notifications about book prices updates.
*/
class BookStore implements Observable {
/** The list of books available in the bookstore. */
public List<Book> books;
/** The list of users registered in the bookstore. */
public List<User> users;
/** The list of subscribers who receive notifications about book updates. */
public List<User> subscribers;
/** The singleton instance of the BookStore class. */
private static BookStore instance;
private BookStore() {
books = new ArrayList<>();
users = new ArrayList<>();
subscribers = new ArrayList<>();
}
public static BookStore getInstance() {
if (instance == null) {
instance = new BookStore();
}
return instance;
}
public void addBook(Book book) {
for (Book item : books) {
if (item.title.equals(book.title)) {
System.out.println("Book already exists");
return;
}
}
books.add(book);
}
public void addUser(User user) {
for (User item : users) {
if (user.userName.equals(item.userName)) {
System.out.println("User already exists");
return;
}
}
users.add(user);
}
@Override
public void registerSubscriber(User user) {
if (subscribers.contains(user)) {
System.out.println("User already subscribed");
} else {
subscribers.add(user);
}
}
@Override
public void removeSubscriber(User user) {
if (!subscribers.contains(user)) {
System.out.println("User is not subscribed");
} else {
subscribers.remove(user);
}
}
@Override
public void notifySubscribers(Book book, String price) {
book.price = price;
for (User item : subscribers) {
item.update(book);
}
}
public void readBook(Book book, User user) {
user.doRead(book);
}
public void listenBook(Book book, User user) {
user.doListen(book);
}
public User getUser(String username) {
for (User user : users) {
if (username.equals(user.userName)) {
return user;
}
}
return null;
}
public Book getBook(String title) {
for (Book book : books) {
if (title.equals(book.title)) {
return book;
}
}
return null;
}
}
class Book {
String title;
String author;
String price;
public Book(String title, String author, String price) {
this.title = title;
this.author = author;
this.price = price;
}
/**
* The BookBuilder class provides a fluent interface for building Book objects.
*/
public static class BookBuilder {
private String title;
private String author;
private String price;
/**
* Sets the title of the book being built.
*
* @param title the title of the book
* @return the BookBuilder instance for method chaining
*/
public BookBuilder nameBook(String title) {
this.title = title;
return this;
}
/**
* Sets the author of the book being built.
*
* @param author the author of the book
* @return the BookBuilder instance for method chaining
*/
public BookBuilder setAuthor(String author) {
this.author = author;
return this;
}
/**
* Sets the price of the book being built.
*
* @param price the price of the book
* @return the BookBuilder instance for method chaining
*/
public BookBuilder setPrice(String price) {
this.price = price;
return this;
}
/**
* Constructs and returns a new Book object based on the provided parameters.
*
* @return the constructed Book object
*/
public Book forSell() {
return new Book(title, author, price);
}
}
}
/**
* The ReadingBehavior interface represents the behavior of user reading a book.
*/
interface ReadingBehavior {
void read(Book book, String userName);
}
/**
* The ListenBehavior interface represents the behavior of user listening to a book.
*/
interface ListenBehavior {
void listen(Book book, String userName);
}
/**
* The CanRead class implements the ReadingBehavior interface for users who can read books.
*/
class CanRead implements ReadingBehavior {
public void read(Book book, String userName) {
System.out.println(userName + " reading " + book.title + " by " + book.author);
}
}
/**
* The CanListen class implements the ListenBehavior interface for users who can listen to books.
*/
class CanListen implements ListenBehavior {
public void listen(Book book, String userName) {
System.out.println(userName + " listening " + book.title + " by " + book.author);
}
}
/**
* The CanNotListen class implements the ListenBehavior interface for users who cannot listen to books.
*/
class CanNotListen implements ListenBehavior {
public void listen(Book book, String userName) {
System.out.println("No access");
}
}
/**
* The User class represents a user of the bookstore.
*/
class User {
/** The behavior for reading books. */
public ReadingBehavior readingBehavior;
/** The behavior for listening to books. */
public ListenBehavior listenBehavior;
String userName;
public User(String userName) {
this.userName = userName;
}
/**
* Reads the specified book using the assigned reading behavior.
*
* @param book the book to be read
*/
void doRead(Book book) {
readingBehavior.read(book, this.userName);
}
/**
* Listens to the specified book using the assigned listening behavior.
*
* @param book the book to be listened to
*/
void doListen(Book book) {
listenBehavior.listen(book, this.userName);
}
/**
* Updates the user with the price update for the specified book.
*
* @param book the book with the updated price
*/
void update(Book book) {
System.out.println(userName + " notified about price update for " + book.title + " to " + book.price);
}
}
/**
* The StandardUser class represents a standard user of the bookstore.
*/
class StandardUser extends User {
/**
* Constructs a new StandardUser object with the specified username and behavior.
*
* @param userName the username of the standard user
*/
public StandardUser(String userName) {
super(userName);
readingBehavior = new CanRead();
listenBehavior = new CanNotListen();
}
}
/**
* The PremiumUser class represents a premium user of the bookstore.
*/
class PremiumUser extends User {
/**
* Constructs a new PremiumUser object with the specified username and behavior.
*
* @param userName the username of the premium user
*/
public PremiumUser(String userName) {
super(userName);
readingBehavior = new CanRead();
listenBehavior = new CanListen();
}
}