generated from fmigeon/estore-public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProvider.java
60 lines (48 loc) · 1.64 KB
/
Provider.java
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
package estore.services.implem;
import java.util.HashMap;
import java.util.Map;
import estore.services.interfaces.IConsultProvider;
import estore.services.interfaces.IJustHaveALook;
import estore.services.interfaces.IOrderProvider;
import estorePojo.exceptions.UnknownItemException;
public class Provider implements IConsultProvider, IOrderProvider {
private Map<String, Double> itemPrices = new HashMap<>();
/**
* Constructs a new ProviderImpl
*/
public Provider() {
itemPrices.put("CD", 15d);
itemPrices.put("DVD", 20d);
}
/**
* Get the price of an item provided by this provider.
*
* @param item
* @return
*/
@Override
public double getPrice(Object item) throws UnknownItemException {
if (!itemPrices.containsKey(item))
throw new UnknownItemException("Item " + item + " is not an item delivered by this provider.");
Double price = (Double) itemPrices.get(item);
return price.doubleValue();
}
/**
* Emit an order for items. The provider returns the delay for delivering the
* items.
*
* @param store the store that emits the order
* @param item the item ordered
* @param qty the quantity ordered
* @return the delay (in hours)
*/
@Override
public int order(IJustHaveALook store, Object item, int qty) throws UnknownItemException {
if (!itemPrices.containsKey(item))
throw new UnknownItemException("Item " + item + " is not an item delivered by this provider.");
// Actually the production process is quite chaotic
// We only know that the production a random number of hours!!
double r = Math.random() * 10 * qty;
return (int) r;
}
}