Repository navigation
Expand file tree
/
Copy pathExampleJava.java
More file actions
314 lines (249 loc) · 7.3 KB
/
Copy pathExampleJava.java
File metadata and controls
314 lines (249 loc) · 7.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
import java.util.*;
public class ExampleJava{
public static void main(String[] args) {
try {
Customer burcu = new Customer("Burcu");
CreditCard card = new CreditCard(1234567812345678L, "Burcu Yılmaz", new Date(), 123);
burcu.setPaymentMethod(card);
Item normalProduct = new Product("Laptop", 550) {
};
burcu.addItem(normalProduct);
System.out.println("Cart Contents:");
burcu.display();
System.out.println("\nPurchase Summary:");
burcu.purchase();
} catch (CreditCardException | InvalidPriceException e) {
System.out.println("ERROR: " + e.getMessage());
}
}
}
interface Item extends Comparable<Item> {
public double getPrice();
public String getName();
}
interface PaymentMethod {
public boolean pay(List<Item> cart);
}
interface Colorable {
public void paint(String color);
}
abstract class Product implements Item {
private String name;
private double price;
Product(String name, double price) throws InvalidPriceException {
SetName(name);
setPrice(price);
}
public void setPrice(double price) throws InvalidPriceException {
if (price < 0) {
throw new InvalidPriceException(price);
} else {
this.price = price;
}
}
public void SetName(String name) {
this.name = name;
}
@Override
public double getPrice() {
return price;
}
@Override
public String getName() {
return name;
}
@Override
public int compareTo(Item other) {
return Double.compare(this.price, other.getPrice());
}
}
class Tax implements Item {
private double taxRate;
private Item item;
public Tax(int taxRatePercent, Item item) {
if (taxRatePercent < 0 || taxRatePercent > 100) {
throw new RuntimeException("Invalid tax rate: " + taxRatePercent);
}
this.taxRate = taxRatePercent / 100.0;
SetItem(item);
}
public void SetItem(Item item) {
this.item = item;
}
@Override
public double getPrice() {
return item.getPrice() + item.getPrice() * taxRate;
}
@Override
public String getName() {
return item.getName();
}
@Override
public int compareTo(Item other) {
return Double.compare(this.getPrice(), other.getPrice());
}
}
class Discount implements Item {
private double percent;
private Item item;
public Discount(int percentInt, Item item) {
if (percentInt < 0 || percentInt > 100) {
throw new RuntimeException("Invalid discount: " + percentInt);
}
this.percent = percentInt / 100.0;
this.item = item;
}
@Override
public double getPrice() {
return item.getPrice() - item.getPrice() * percent;
}
@Override
public String getName() {
return item.getName();
}
@Override
public int compareTo(Item other) {
return Double.compare(this.getPrice(), other.getPrice());
}
}
class CreditCard implements PaymentMethod {
private long cardNumber;
private String holderName;
private Date expirationDate;
private int cvv;
public CreditCard(long cardNumber, String holderName, Date expirationDate, int cvv) throws CreditCardException {
this.holderName = holderName;
this.expirationDate = expirationDate;
setCardNumber(cardNumber);
setCvv(cvv);
}
public void setCardNumber(long cardNumber) throws CreditCardException {
String cardnS = String.valueOf(cardNumber);
if (cardnS.length() != 16) {
throw new CreditCardException(cardNumber);
} else {
this.cardNumber = cardNumber;
}
}
public void setCvv(int cvv) throws CreditCardException {
String cvvS = String.valueOf(cvv);
if (cvvS.length() != 3) {
throw new CreditCardException(cvv);
} else {
this.cvv = cvv;
}
}
@Override
public boolean pay(List<Item> cart) {
List<Item> taxedList = new ArrayList<>();
double total = 0;
for (Item item : cart) {
Item taxedItem = new Tax(4, item);
taxedList.add(taxedItem);
total += taxedItem.getPrice();
}
System.out.println("Paid: " + total + " with " + getClass().getSimpleName() + " " + holderName);
cart.clear();
return true;
}
}
class Paypal implements PaymentMethod {
private String userName;
private String password;
public Paypal(String userName, String password) {
setUserName(userName);
SetPassword(password);
}
public void setUserName(String userName) {
this.userName = userName;
}
public void SetPassword(String password) {
this.password = password;
}
@Override
public boolean pay(List<Item> cart) {
List<Item> discountdList = new ArrayList<>();
double total = 0;
for (Item item : cart) {
Item discountitem = new Discount(6, item);
discountdList.add(discountitem);
total += discountitem.getPrice();
}
System.out.println("Paid: " + total + " with " + getClass().getSimpleName());
cart.clear();
return true;
}
}
class Customer {
private String name;
private List<Item> cart;
private PaymentMethod paymentMethod;
public Customer(String name) {
setName(name);
this.cart = new ArrayList<>();
}
public void setPaymentMethod(PaymentMethod paymentMethod) {
this.paymentMethod = paymentMethod;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public boolean addItem(Item item) {
if (item instanceof Colorable) {
item = new Discount(8, item);
}
if (item.getPrice() > 500) {
item = new Tax(20, item);
}
item = new Tax(12, item);
return cart.add(item);
}
public boolean removeItem(Item item) {
return cart.remove(item);
}
public boolean purchase() {
if (paymentMethod == null) {
System.out.println("No payment method selected.");
return false;
}
return paymentMethod.pay(cart);
}
public void display() {
Collections.sort(cart);
for (Item sorrt : cart) {
System.out.println(sorrt.getName() + "-" + sorrt.getPrice());
}
}
}
class InvalidPriceException extends RuntimeException {
private double price;
public InvalidPriceException(double price) {
System.out.println("ERROR: Invalid price: " + price);
this.price = price;
}
public double getPrice() {
return price;
}
}
class CreditCardException extends Exception {
private long cardNumber;
private int cvv;
public CreditCardException(int cvv) {
super("ERROR: Invalid cvv" + cvv);
this.cvv = cvv;
;
}
public CreditCardException(long cardNumber) {
super("ERROR: Invalid card number" + cardNumber);
this.cardNumber = cardNumber;
}
public long getCardNumber() {
return cardNumber;
}
public int getCvv() {
return cvv;
}
}