-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.java
196 lines (166 loc) · 8.29 KB
/
Client.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import java.io.*;
import java.net.*;
import java.util.ArrayList;
public class Client {
public static void main(String[] args) {
String hostName = "localhost";
int port = 8080;
try (
Socket socket = new Socket(hostName, port);
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
PrintWriter printWriter = new PrintWriter(outputStream, true);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
) {
System.out.println("Successfully connected to port " + port);
System.out.println("Welcome, please select an option below: "
+ "\n\nOptions:\t\t\t\t\tCommand:"
+ "\nAdd booking. \t\t\t\tADD <BookingID> <BookingDate> <StartTime> <ClientID> <SpecialismID> <TrainerID>"
+ "\nList all bookings \t\t\tLISTALL"
+ "\nList personal trainer. \tLISTPT <TrainerID>"
+ "\nList client bookings. \t\tLISTCLIENT <ClientID>"
+ "\nList booking date. \t\tLISTDAY <BookingDate>"
+ "\nUpdate booking. \t\t\tUPDATE UPDATE <BookingDetail> <NewValue> <BookingID>"
+ "\nDelete booking. \t\t\tDELETE <BookingID>"
+ "\nExit. \t\t\t\t\t\tEXIT");
while (true) {
System.out.print(">> Option: ");
String userInput = stdIn.readLine();
String[] user = userInput.split("\\s+");
if (user[0].equals("ADD")) {
ADD(user, printWriter, bufferedReader);
} else if (user[0].equals("LISTALL")) {
LISTALL(user, inputStream, printWriter);
} else if (user[0].equals("LISTPT")) {
LISTPT(user, printWriter, inputStream);
} else if(user[0].equals("LISTCLIENT")) {
LISTCLIENT(user, printWriter, inputStream);
} else if(user[0].equals("LISTDAY")) {
LISTDAY(user, printWriter, inputStream);
}else if (user[0].equals("UPDATE")) {
UPDATEBOOKING(user, printWriter, bufferedReader);
} else if (user[0].equals("DELETE")) {
DELETEBOOKING(user, printWriter, bufferedReader);
} else if (user[0].equals("EXIT")) {
System.out.println("Exiting...");
System.exit(0);
} else {
System.out.println("Invalid Command!");
}
}
} catch (Exception ex) {
System.out.println(ex);
}
}
public static void ADD(String[] args, PrintWriter printWriter, BufferedReader bufferedReader) throws Exception {
if (args.length != 7) {
System.err.println("Usage: ADD <BookingID> <BookingDate> <StartTime> <ClientID> <SpecialismID> <TrainerID>");
System.exit(1);
} else {
for (int i = 0; i < 7; i++) {
printWriter.println(args[i]);
}
String message = bufferedReader.readLine();
System.out.println(message);
}
}
public static void LISTALL(String[] args, InputStream inputStream, PrintWriter printWriter) throws Exception {
printWriter.println(args[0]);
if (args.length != 1) {
System.err.println("Usage: LISTALL");
System.exit(1);
} else {
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
ArrayList<Booking> bookingArrayList = (ArrayList<Booking>) objectInputStream.readObject();
for (Booking booking : bookingArrayList) {
System.out.println("BookingID : " + booking.getBookingID()
+ "\nBooking Date: " + booking.getBookingDate()
+ "\nStart time: " + booking.getStartTime()
+ "\nClientID: " + booking.getClientID()
+ "\nSpecialismID: " + booking.getSpecialismID()
+ "\nTrainerID: " + booking.getTrainerID());
}
}
}
public static void LISTPT(String[] args, PrintWriter printWriter, InputStream inputStream) throws Exception {
if (args.length != 2) {
System.err.println("Usage: LISTPT <TrainerID>");
System.exit(1);
} else {
printWriter.println(args[0]);
printWriter.println(args[1]);
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
ArrayList<Booking> bookingArrayList = (ArrayList<Booking>) objectInputStream.readObject();
for(Booking booking: bookingArrayList) {
System.out.println("TrainerID: " + booking.getTrainerID()
+ "\nBookingID : " + booking.getBookingID()
+ "\nBooking Date: " + booking.getBookingDate()
+ "\nStart time: " + booking.getStartTime()
+ "\nClientID: " + booking.getClientID()
+ "\nSpecialismID: " + booking.getSpecialismID());
}
}
}
public static void LISTCLIENT(String[] args, PrintWriter printWriter, InputStream inputStream) throws Exception {
if (args.length != 2) {
System.err.println("Usage: LISTCLIENT <ClientID>");
System.exit(1);
} else {
printWriter.println(args[0]);
printWriter.println(args[1]);
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
ArrayList<Booking> bookingArrayList = (ArrayList<Booking>) objectInputStream.readObject();
for(Booking booking: bookingArrayList) {
System.out.println("ClientID: " + booking.getClientID()
+ "\nBooking Date: " + booking.getBookingDate()
+ "\nStart time: " + booking.getStartTime()
+ "\nBookingID : " + booking.getBookingID()
+ "\nSpecialismID: " + booking.getSpecialismID()
+ "\nTrainerID: " + booking.getTrainerID());
}
}
}
public static void LISTDAY(String[] args, PrintWriter printWriter, InputStream inputStream) throws Exception {
if (args.length != 2) {
System.err.println("Usage: LISTDAY <BookingDate>");
System.exit(1);
} else {
printWriter.println(args[0]);
printWriter.println(args[1]);
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
ArrayList<Booking> bookingArrayList = (ArrayList<Booking>) objectInputStream.readObject();
for(Booking booking: bookingArrayList) {
System.out.println("Booking Date: " + booking.getBookingDate()
+ "\nClientID: " + booking.getClientID()
+ "\nStart time: " + booking.getStartTime()
+ "\nBookingID : " + booking.getBookingID()
+ "\nSpecialismID: " + booking.getSpecialismID()
+ "\nTrainerID: " + booking.getTrainerID());
}
}
}
public static void UPDATEBOOKING(String[] args, PrintWriter printWriter, BufferedReader bufferedReader) throws Exception {
if (args.length != 4) {
System.err.println("Usage: UPDATE <BookingDetail> <NewValue> <BookingID>");
System.exit(1);
} else {
for (int i = 0; i < 4; i++) {
printWriter.println(args[i]);
}
String message = bufferedReader.readLine();
System.out.println(message);
}
}
public static void DELETEBOOKING(String[] args, PrintWriter printWriter, BufferedReader bufferedReader) throws Exception {
if (args.length != 2) {
System.err.println("Usage: DELETE <BookingID>");
System.exit(1);
} else {
printWriter.println(args[0]);
printWriter.println(args[1]);
String message = bufferedReader.readLine();
System.out.println(message);
}
}
}