-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookstoreSystem.java
More file actions
86 lines (67 loc) · 2.3 KB
/
Copy pathBookstoreSystem.java
File metadata and controls
86 lines (67 loc) · 2.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
import java.util.List;
import java.util.ArrayList;
public class BookstoreSystem {
private static BookstoreSystem instance;
private Owner owner;
private List<Book> books;
private List<Customer> customers;
private BookstoreSystem() {
this.owner = new Owner();
this.customers = new ArrayList<>();
this.books = new ArrayList<>();
}
public static synchronized BookstoreSystem getInstance() {
if (instance == null) {
instance = new BookstoreSystem();
}
return instance;
}
public void setCustomers(List<Customer> customers) {
this.customers = customers;
}
public void setBooks(List<Book> books) {
this.books = books;
}
public List<Customer> getCustomers() {
return customers;
}
public List<Book> getBooks() {
return books;
}
public User authenticateUser(String username, String password) {
if (owner.login(username, password)) {
return owner;
}
for (Customer customer : customers) {
if (customer.login(username, password)) {
return customer;
}
}
return null;
}
public void addCustomer(String username, String password) {
customers.add(new Customer(username, password));
}
public void removeCustomer(Customer customer) {
customers.remove(customer);
}
public void addBook(String name, double price) {
books.add(new Book(name, price));
}
public void removeBook(Book book) {
books.remove(book);
}
public double calculatePurchase(User user, List<Book> selectedBooks, boolean redeem) {
if (user instanceof Customer) {
Customer customer = (Customer) user;
double totalCost = selectedBooks.stream().mapToDouble(Book::getPrice).sum();
if (redeem) {
int pointsToRedeem = customer.getPoints();
double discount = pointsToRedeem / 100.0;
return Math.max(0, totalCost - discount);
}
return totalCost;
}
return 0;
}
}