-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
197 lines (168 loc) · 6.51 KB
/
Copy pathClient.java
File metadata and controls
197 lines (168 loc) · 6.51 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
import java.util.*;
import java.io.*;
public class Client implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private String address;
private String phone;
private String id;
private double balance;
private static final String CLIENT_STRING = "C";
// private List booksBorrowed = new LinkedList();
private List<CartItem> cart; // Cart containing items and quantities
private List<Transaction> transactions;
public Client(String name, String address, String phone) {
this.name = name;
this.address = address;
this.phone = phone;
this.balance = 0;
this.id = CLIENT_STRING + (ClientIdServer.instance()).getId();
this.transactions = new LinkedList<Transaction>();
this.cart = new LinkedList<CartItem>();
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
public String getAddress() {
return address;
}
public String getId() {
return id;
}
public double getBalance() {
return balance;
}
public void setName(String newName) {
name = newName;
}
public void setAddress(String newAddress) {
address = newAddress;
}
public void setPhone(String newPhone) {
phone = newPhone;
}
public void setBalance(double newBalance) {
balance = newBalance;
}
public boolean equals(String id) {
return this.id.equals(id);
}
public boolean addToCart(CartItem item) {
cart.add(item);
return true;
}
public void editCart(String itemName, int newQuant) {
Iterator<CartItem> cart = getCartItems();
while (cart.hasNext()) {
CartItem item = (CartItem) cart.next();
if (itemName.equals(item.getProduct().getName())) {
item.setQuantity(newQuant);
if (newQuant == 0) {
removeCart(itemName);
//System.out.println("debug: item removed from cart");
}
}
}
}
public boolean removeCart(String targetName) {
Iterator<CartItem> cart = getCartItems();
while (cart.hasNext()) {
CartItem item = (CartItem) cart.next();
if (targetName.equals(item.getProduct().getName())) {
cart.remove();
return true; // removal successful
}
}
return false; // if removal did not occur
}
public Iterator<CartItem> getCartItems() {
return cart.iterator();
}
public boolean inCart(String itemName) {
Iterator<CartItem> cart = getCartItems();
while (cart.hasNext()) {
CartItem item = (CartItem) cart.next();
if (itemName.equals(item.getProduct().getName())) {
//System.out.println("debug: item IS in cart");
return true;
}
}
//System.out.println("debug: item is NOT cart");
return false;
}
public Iterator<Transaction> getTransactions(Calendar date) {
List<Transaction> result = new LinkedList<Transaction>();
for (Iterator<Transaction> iterator = transactions.iterator(); iterator.hasNext();) {
Transaction transaction = (Transaction) iterator.next();
if (transaction.onDate(date)) {
result.add(transaction);
}
}
return (result.iterator());
}
public String toString() {
return "Client name: " + name + " | address: " + address + " | id: " + id + " | phone: " + phone + " | balance: " + balance;
}
public Invoice processOrder() { // For each item in cart, subtract requested from available quantity. Leftover requested will be waitlisted
double grandTotal = 0;
double totalPrice = 0;
double balance = 0;
Iterator<CartItem> carti = cart.iterator();
// for (each item in cart) {
while (carti.hasNext()) {
CartItem item = (CartItem) (carti.next());
// get price from product
double price = item.getProduct().getPrice(); // Price for one of this item
String itemName = item.getProduct().getName();
//System.out.println("Processing item: " + itemName);
// qty available of product (product waitlist the rest)
int quantAvailable = item.getProduct().getQuantity(); // Get the quantity available from the Product in the cart
int quantRequested = item.getQuantity(); // Get the quantity requested by the Order
System.out.println("available: " + quantAvailable); // for debug
System.out.println("requested: " + quantRequested);
// if enough are available:
if (quantRequested <= quantAvailable) { // Process one item at a time until all that remains are out-of-stock
totalPrice += (price * quantRequested);
quantAvailable -= quantRequested;
quantRequested = 0;
item.getProduct().setQuantity(quantAvailable); // Update quantity available in catalog
item.setQuantity(quantRequested);
System.out.println("CartItem available. Added to order");
}
else { // if not enough are available
totalPrice += (price * quantAvailable);
quantRequested -= quantAvailable; // quantRequested now represents the amount leftover to be waitlisted
quantAvailable = 0; // Out of stock
item.getProduct().setQuantity(quantAvailable); // Update in-stock quantity to 0
item.setQuantity(quantRequested); // Update in-cart quantity to how much remains (to be waitlisted)
// Need to wait-list the remaining quantity (quantRequested - quantAvailable)
// add product/quantity to wait-list
System.out.println("Adding " + quantRequested + " of " + itemName + " to waitlist");
item.setQuantity(0); // Reset in-cart quantity
// create WaitlistItem object
Client client = item.getClient();
Product product = item.getProduct();
WaitlistItem waitlistItem = new WaitlistItem(client, product, quantRequested);
product.addToWaitlist(waitlistItem);
}
// Increment grandTotal
grandTotal += totalPrice;
// System.out.println("Total price = " + totalPrice);
// (ship product)
// Create an invoice line with productqty, date, cost (CartItem.toString()?)
// Record waitlist entry if needed
}
balance = this.getBalance();
balance = balance + grandTotal;
this.setBalance(balance);
transactions.add(new Transaction ("Order Processed ", Double.toString(balance)));
// Creates invoice
List<CartItem> copycart = this.cart;
// reset cart
this.cart = new LinkedList<CartItem>();
return new Invoice(grandTotal, copycart);
}
}