Repository navigation
Expand file tree
/
Copy pathClient.java
More file actions
85 lines (70 loc) · 2.81 KB
/
Copy pathClient.java
File metadata and controls
85 lines (70 loc) · 2.81 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
package estore.services.implem;
import estore.data.Cart;
import estore.data.Order;
import estore.services.interfaces.ICartLane;
import estore.services.interfaces.IFastLane;
import estore.services.interfaces.IJustHaveALook;
import estorePojo.exceptions.InsufficientBalanceException;
import estorePojo.exceptions.InvalidCartException;
import estorePojo.exceptions.UnknownAccountException;
import estorePojo.exceptions.UnknownItemException;
public class Client implements Runnable {
private IJustHaveALook forLookingStore;
private IFastLane fastStore;
private ICartLane cartStore;
public Client(IJustHaveALook forLookingStore, IFastLane fastStore, ICartLane cartStore) {
this.forLookingStore = forLookingStore;
this.fastStore = fastStore;
this.cartStore = cartStore;
}
// -----------------------------------------------------
// Implementation of the Runnable interface
// -----------------------------------------------------
public void run() {
// Scenario 1
// Direct ordering of an item
// The scenario is run twice
System.out.println("Scenario 1");
scenario1("CD", 2, "Lille", "Bob");
scenario1("CD", 1, "Lille", "Anne");
System.out.println();
// Scenario 2
// Ordering of several items by using a cart
System.out.println("Scenario 2");
scenario2(new String[] { "DVD", "CD" }, new int[] { 2, 1 }, "Lille", "Bob");
System.out.println();
}
private void scenario1(String item, int qty, String address, String account) {
try {
_scenario1(item, qty, address, account);
} catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
e.printStackTrace();
}
}
private void _scenario1(String item, int qty, String address, String account)
throws UnknownItemException, InsufficientBalanceException, UnknownAccountException {
System.out.println("Ordering " + qty + " " + item + " for " + account + "...");
Order order = fastStore.oneShotOrder(this, item, qty, address, account);
System.out.println(order);
}
private void scenario2(String[] items, int[] qties, String address, String account) {
try {
_scenario2(items, qties, address, account);
} catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
e.printStackTrace();
}
}
private void _scenario2(String[] items, int[] qties, String address, String account)
throws InsufficientBalanceException, UnknownAccountException, UnknownItemException, InvalidCartException {
System.out.println("Ordering for " + account + "...");
Cart cart = null;
for (int i = 0; i < items.length; i++) {
System.out.println("Item: " + items[i] + ", quantity: " + qties[i]);
cart = cartStore.addItemToCart(cart, this, items[i], qties[i]);
}
Order order = cartStore.pay(cart, address, account);
System.out.println(order);
}
}