-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOrder.java
More file actions
134 lines (130 loc) · 4.56 KB
/
Copy pathOrder.java
File metadata and controls
134 lines (130 loc) · 4.56 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
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Vector;
class Record {
// one time definition
public final String timestamp;
public final int quantity, rate, price;
// changables
public Item item;
Record(String timestamp, Item item, int quantity){
this.timestamp=timestamp;
// this.t_ms=G.time();
this.item=item;
this.rate=item.rate;
this.quantity=quantity;
this.price=item.rate*quantity;
}
}
public class Order {
// Class variables
private static int order_count=0;
public static String header="orderno,\tcust_id,\t address,\t order status,\t timestamp,\t item,\t rate,\t qty,\t price\n";
// Object variables
private final int id;
private final String cust_name,cust_addr;
public boolean rejected;
public int sold_day,exp_delivery;
// private LocalDateTime sold_date;
// public long sold_at;//milliseconds
Vector<Record> records;
Order(String cust_name,String cust_addr){
this.order_count++;//on each instance
this.id=this.order_count; //store current val
this.cust_name=cust_name;
this.cust_addr=cust_addr;
this.records = new Vector();
this.rejected = false;
// this.sold_at = -1;
}
public String get_receipt(){
String receipt="";
// receipt+=(this.rejected?"Out of stock":("Sold at: "+(this.sold_at-G.start_time)+"ms"))+"\n";
// receipt+=this.header;
String sepa=" |\t ";
long total_price=0;
for (Record r : records){
receipt+= "Order"+this.id+sepa+cust_name+sepa+cust_addr+sepa;
receipt+=(this.rejected?"REJECTED!":"SOLD!")+sepa;
receipt+=r.timestamp+sepa;//(r.t_ms-G.start_time)+"ms,
receipt+=r.item.type+sepa;
receipt+=r.rate+sepa+r.quantity+sepa+r.price;
receipt+="\n";
total_price+=r.price;
}
if(!this.rejected)receipt+= "\n\tTotal Cost: "+total_price+"\n";
return receipt;
}
public void add_record(Item item, int quantity){
records.add(new Record(G.timestamp(),item,quantity));
}
public void clicked(){
// acquire lock on each record types
for(Record r : records){
try{
r.item.sema.acquire();
}
catch(InterruptedException ex){
Logger.getLogger(Order.class.getName()).log(Level.SEVERE, null, ex);
}
}
boolean all_avl=true;
for(Record r : records){
if(r.item.quantity>-1){
if(r.item.quantity<r.quantity){
all_avl=false;
break;
}
}
}
if(all_avl){
//modify
int teaCount=0,coffeeCount = 0;
for(Record r : records){
if(r.item.type == Itemtype.TEA)
teaCount += r.quantity;
else if(r.item.type == Itemtype.COFFEE)
coffeeCount += r.quantity;
}
// this.sold_at=G.time();
// this.sold_date=G.timestamp();
this.sold_day=G.day_number;
try{
// Delivery time calc:
this.exp_delivery = G.DLRY_TIME;
// add to tea counter only if tea is ordered
if(teaCount>0){
G.teaSema.acquire();
G.teaCounter+=teaCount;
this.exp_delivery += G.PREP_TIME * G.teaCounter;
G.teaSema.release();
}
if(coffeeCount>0){
G.coffeeSema.acquire();
G.coffeeCounter+=coffeeCount;
this.exp_delivery += G.PREP_TIME * G.coffeeCounter;
G.coffeeSema.release();
}
this.exp_delivery *= G.MINUTE_DURN;
// this.delivered_at = (G.time()-G.start_time)/60000 + exp_delivery;
}
catch(InterruptedException ex){
Logger.getLogger(Order.class.getName()).log(Level.SEVERE, null, ex);
}
for(Record r : records){
if(r.item.quantity>-1){
r.item.quantity -= r.quantity;
if(r.item.quantity < r.item.threshold)
r.item.to_purchase=true;
}
}
}
else{
//reject
this.rejected=true;
}
for(Record r : records){
r.item.sema.release();
}
}
}