-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathClientModel.java
135 lines (115 loc) · 3.7 KB
/
ClientModel.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
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
package restaurantClient;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import restaurant.Customer;
import restaurant.Menu;
import restaurant.Order;
public class ClientModel
{
// Static variables:
private static final int EXIT_FAILURE = 1;
// Instance variables
private String host;
private int port;
private Socket connectionSocket;
private ObjectOutputStream outputStream;
private ObjectInputStream inputStream;
private Menu menu;
private Order order;
private Customer customer;
private String serverOrderResponse;
private String serverErrorMessage;
private String orderPrintout;
private boolean customerInterestedToOrder;
// Constructor
public ClientModel(String serverHost, int serverPort) throws IOException
{
host = serverHost;
port = serverPort;
connectionSocket = new Socket(host, port);
customer = new Customer();
customerInterestedToOrder = true;
}
// Terminate execution after printing customized error message:
public static void terminateWithError(String message)
{
System.err.printf(message);
System.exit(EXIT_FAILURE);
}
// Utility method for initializing I\O streams:
public void setStreams() throws IOException
{
outputStream = new ObjectOutputStream(connectionSocket.getOutputStream());
outputStream.flush();
inputStream = new ObjectInputStream(connectionSocket.getInputStream());
}
// Utility method for reseting connections:
public void setConnection() throws IOException
{
connectionSocket = new Socket(host, port);
}
// Utility method for closing stream & resources:
public void closeStreams() throws IOException
{
if (inputStream != null)
inputStream.close();
if (outputStream != null)
outputStream.close();
if(connectionSocket != null)
connectionSocket.close();
}
// Sent menu request message to server:
public Menu requestMenu() throws ClassNotFoundException, IOException
{
outputStream.writeObject("MENU");
outputStream.flush();
menu = (Menu)inputStream.readObject();
return menu;
}
// Sent order request message to server:
public String requestOrder() throws ClassNotFoundException, IOException
{
System.out.printf("%s", this.order);
this.setConnection();
this.setStreams();
outputStream.writeObject("ORDER");
outputStream.flush();
outputStream.writeObject(this.order);
outputStream.flush();
serverOrderResponse = (String)inputStream.readObject();
if (serverOrderResponse.equals("APPROVED"))
this.orderPrintout = (String)inputStream.readObject();
else
this.serverErrorMessage = (String)inputStream.readObject();
return serverOrderResponse;
}
public void startNewOrder(ClientView view)
{
view.setVisible(false);
try
{
setConnection();
setStreams();
}
catch (IOException ioException)
{
ioException.printStackTrace();
}
new ClientView(this, menu);
}
// Getters:
public Menu getMenu() {return menu;}
public Order getOrder() {return order;}
public Customer getCustomer() {return customer;}
public boolean isCustomerInterestedToOrder() {return customerInterestedToOrder;}
public String getServerOrderResponse() {return serverOrderResponse;}
public String getServerErrorMessage() {return serverErrorMessage;}
public String getOrderPrintout() {return orderPrintout;}
// Setters:
public void setMenu(Menu menu) {this.menu = menu;}
public void setCustomer(Customer customer) {this.customer = customer;}
public void setOrder(Order order) {this.order = order;}
public void setCustomerInterestedToOrder(boolean b) {this.customerInterestedToOrder = b;}
}