-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCustomer.java
More file actions
161 lines (151 loc) · 4.75 KB
/
Copy pathCustomer.java
File metadata and controls
161 lines (151 loc) · 4.75 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
import java.io.*;
import java.net.*;
import java.util.Vector;
class Customer implements Runnable {
public final Socket socket;
public final Vector<Order> orders;
public String name,address;
private DataInputStream in = null;
private DataOutputStream out = null;
Customer(Socket socket,String name,String address){
this.name=name;
this.address=address;
this.socket=socket;
this.orders= new Vector();
}
public Order order(Vector<Integer> quantities){
// Vector of {Itemtype itemtype, int quantity}
Order o = new Order(this.name,this.address);
int i=0,q;
for(Item item : G.items){
q = (Integer)quantities.get(i);
if(q>0){
o.add_record(item,q);
}
i++;
}
this.orders.add(o);
// Synchronized by locks on ordered records
o.clicked();
return o;
}
public void printStocks(){
G.print("\t Current Stocks: ");
for (Item item: G.items){
G.print(item.type+": "+(item.quantity==-1?"inf":item.quantity)+"\t");
}
G.println("");
}
@Override
public void run(){
G.println_t("Client "+this.name+" connected");
printStocks();
try{
// takes input from the client socket
in = new DataInputStream(
new BufferedInputStream(socket.getInputStream()));
// sends output to the socket
out = new DataOutputStream(socket.getOutputStream());
sendName();
String line = "";
// reads message from client until "exit" is sent
while (!line.equals("exit"))
{
try
{
line = in.readUTF();
if(line.startsWith("set")){
G.print("Customer "+this.name+" changed details to ");
this.name=line.split(" ")[1];
this.address=line.split(" ")[2];
G.println(this.name+" at "+this.address);
sendName();
}
else if(line.equals("availability")){
sendAvls();
}
else if(line.equals("allorders")){
sendOrders();
}
else{
// order
G.println_t("Got Order string : "+line);
if(line.equals("") || line.split(",").length!=G.items.size()){
G.print("Invalid input: \""+line+"\"");
continue;
}
Vector quantities = new Vector<Integer>();
boolean nonzero=false;
for(String s : line.split(",")){
int x=Integer.parseInt(s);
quantities.add(x);
if(x!=0)nonzero=true;
}
if(nonzero){
Order o = this.order(quantities);
sendOrder(o);
}
else{
sendMessage("Empty Order!");
}
printStocks();
}
}
catch(SocketException i) {
return;
}
}
System.out.println("Closing connection");
// close connection
socket.close();
in.close();
}
catch(IOException i)
{
System.out.println(i);
return;
}
}
public void sendMessage(String msg){
try{
out.writeUTF(msg);
out.writeUTF("<END>");
}
catch(IOException ex){
System.out.println("IOer: "+ex);
}
}
public void sendName(){
sendMessage("Customer: "+this.name+" at address: "+this.address);
G.println("Customer: "+this.name+" at address: "+this.address);
}
public void sendOrder(Order o){
sendMessage(Order.header+o.get_receipt()+"\n\tExpected delivery: "+(o.rejected?"ORDER REJECTED!":(o.exp_delivery/G.MINUTE_DURN)+" minutes."));
}
public void sendAvls(){
/*
No use of lock on availablity. because by the time response reaches client, the other thread would have updated item quantities anyway.
*/
String reply="";
int i=0;
for(Item it : G.items){
reply+=it.quantity;
if(i!=G.items.size())
reply+=",";
i++;
}
sendMessage(reply);
}
public void sendOrders(){
String reply;
if(this.orders.size()==0){
reply="Nothing ordered yet!";
}
else{
reply=Order.header;
for(Order o : this.orders)
reply+=o.get_receipt();
}
sendMessage(reply);
}
}