-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientHandler_ClientSide.java
More file actions
357 lines (342 loc) · 11.4 KB
/
Copy pathClientHandler_ClientSide.java
File metadata and controls
357 lines (342 loc) · 11.4 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package bankingsystem;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
public class ClientHandler_ClientSide
{
private boolean close = false;
private int state;
private DataOutputStream dos;
private DataInputStream dis;
private Socket client;
public String receiveFromServer()
{
String r = "";
try
{
r = dis.readUTF();
}
catch(IOException e)
{
System.out.println(e.getMessage() + ": Error receiving from server!");
}
return r;
}
public void connectToServer(String address, int port)
{
try
{
client = new Socket(address, port);
dos = new DataOutputStream(client.getOutputStream());
dis = new DataInputStream(client.getInputStream());
}
catch(IOException e)
{
System.out.println(e.getMessage() + ": Error connecting to server!");
}
}
public boolean getCloseFlag()
{
return close;
}
public void ShowOptions()
{
System.out.println("Choose an option:\n1)Check on current balance.\n2)Deposit cash.\n3)Withdraw cash.\n4)Transfer money to another account within the same bank.\n5)Transfer money to another account in another bank.\n6)View transaction history.\n7)Exit.");
}
public void GUI()
{
boolean loopAgain = true;
while (loopAgain)
{
loopAgain=false;
System.out.println("1)Back to options menu \n2)Exit");
Scanner choice = new Scanner(System.in);
String c = choice.nextLine();
if (c.equals("1")) state = 4;
else if(c.equals("2")) close = true;
else
{
System.out.println("wrong entry, please try again");
loopAgain =true;
}
}
}
public boolean transaction(String amount, String accountID)
{
try
{
while(true)
{
String serverMessage= this.receiveFromServer();
switch(serverMessage)
{
case "connected":
dos.writeUTF("server-transfer");
break;
case "amount?account?":
dos.writeUTF(amount + "\n" + accountID);
break;
case "done":
dos.writeUTF("exit");
break;
case "errorb":
return false;
case "bye":
return true;
}
}
}
catch(IOException e)
{
System.out.println(e.getMessage() + ": Error communicating with another banking system!");
}
return false;
}
public void checkServerMessage(String msg)
{
if (msg.equals("connected"))
{
state = 1;
System.out.println("1)Sign In\n2)Sign Up");
}
else if(msg.equals("username?amount?"))
{
state = 2;
}
else if(msg.equals("details?")) // sign up
{
state = 2;
System.out.println("Enter Full name, Password, Telephone, SSN, Amount to deposit");
}
else if(msg.equals("username?password?")) //sign in
{
state = 3;
System.out.println("Enter Account ID, Password");
}
else if(msg.equals("verifiedlogin"))
{
state = 4;
System.out.println("Signed in Successfully!");
}
else if(msg.equals("verifiednew"))
{
try
{
state = 4;
System.out.println("Signed in Successfully!");
String id = dis.readUTF();
System.out.println("Your account ID is " + id + " ,please keep it.");
}
catch(Exception e)
{
System.out.println(e.getMessage() + "Error receiving ID after sign up");
}
}
else if(msg.equals("notverifiedlogin")) // error while sign in
{
state = 3;
System.out.println("ERROR try again!");
System.out.println("Enter Username, Password");
}
else if(msg.equals("options"))
{
state = 5;
ShowOptions();
}
else if(msg.matches("[0-9.]*"))//balance + go back to options
{
System.out.println("Current balane is: " + msg);
GUI();
}
else if(msg.equals("amount?")) // option 2 deposit
{
state = 6;
System.out.println("Please enter the amount");
}
else if(msg.equals("amountw?")) // option 3 withdraw
{
state = 7;
System.out.println("Please enter the amount");
}
else if(msg.equals("amount?account?")) // transfer within same bank
{
state = 8;
System.out.println("Please enter the amount to be transfered and the account id");
}
else if(msg.equals("invalidid"))
{
state = 4 ;
System.out.println("Invalid account ID!");
//.out.println("Please enter the amount to be transfered and the account id");
}
else if(msg.equals("errorb"))
{
state = 4 ;
System.out.println("Cannot transfer this amount!");
}
else if(msg.equals("done"))
{
System.out.println("Transaction is done.");
GUI();
}
else if(msg.equals("bankname?amount?account?")) // option 5 transfer to another bank
{
state = 9;
System.out.println("Please enter the Bank name , the amount to be transfered and the account id");
}
else if (msg.equals("errorw"))
{
state = 4 ;
System.out.println("Your Current Balance is not enough!");
}
else if((msg.substring(0,1)).matches("#"))
{
msg = msg.substring(1,msg.length()-1);
String[] lines = msg.split("\n");
for(int i = 0; i < lines.length; i++)
{
String[] segments = lines[i].split("=");
if(segments[2].equals("Check balance"))
System.out.println(String.format("%-20s %-20s %-50s" , segments[0], segments[1], segments[2]));
else
System.out.println(String.format("%-20s %-20s %-50s %-20s" , segments[0], segments[1], segments[2], segments[3]));
}
GUI();
}
else if(msg.equals("bye"))
{
close = true;
}
}
public void checkClientInput()
{
try {
String username; String password;
String deposit; String toBeSent;
String ssn; String telephone;
String amount; String accountID;
boolean loop = true;
Scanner m = new Scanner(System.in);
if (state == 1)
{
Scanner uinput = new Scanner(System.in);
while(loop)
{
String userInput = uinput.nextLine();
if(userInput.equals("1"))
{
dos.writeUTF("Sign In");loop=false;
}
else if (userInput.equals("2"))
{
dos.writeUTF("Sign Up");loop=false;
}
else
{
System.out.println("wrong entry, please try again");
System.out.println("1)Sign In\n2)Sign Up");
loop=true;
}
}
}
else if (state == 2) // sign up
{
Scanner uname = new Scanner(System.in);
username = uname.nextLine();
password = uname.nextLine();
telephone = uname.nextLine();
ssn = uname.nextLine();
deposit = uname.nextLine();
toBeSent = username + "\n" + password + "\n" + telephone + "\n" + ssn + "\n" + deposit;
dos.writeUTF(toBeSent);
}
else if (state == 3)
{
Scanner signin = new Scanner(System.in);
username = signin.nextLine();
password = signin.nextLine();
toBeSent = username + "\n" + password;
dos.writeUTF(toBeSent);
}
else if (state == 4)
{
dos.writeUTF("showoptions");
}
else if (state == 5)
{
Scanner opno = new Scanner(System.in);
String OptionNo = opno.nextLine();
if (OptionNo.matches("[0-9]+"))
{
switch (Integer.parseInt(OptionNo))
{
case 1:
dos.writeUTF("check");
break;
case 2:
dos.writeUTF("deposit");
break;
case 3:
dos.writeUTF("withdraw");
break;
case 4:
dos.writeUTF("transfersame");
break;
case 5:
dos.writeUTF("transferother");
break;
case 6:
dos.writeUTF("view");
break;
case 7:
dos.writeUTF("exit");
break;
default:
System.out.println("wrong entry, please try again");
dos.writeUTF("showoptions");
//ShowOptions();
break;
}
}
else
{
System.out.println("wrong entry, please try again");
dos.writeUTF("showoptions");
}
}
else if (state == 6)
{
Scanner b = new Scanner(System.in);
String balance = b.nextLine();
dos.writeUTF(balance);
}
else if(state == 7)
{
Scanner w = new Scanner(System.in);
String withdraw = w.nextLine();
dos.writeUTF(withdraw);
}
else if(state == 8)
{
amount = m.nextLine();
accountID = m.nextLine();
toBeSent = amount + "\n" + accountID;
dos.writeUTF(toBeSent);
}
else if(state == 9)
{
String bankname = m.nextLine();
amount = m.nextLine();
accountID = m.nextLine();
toBeSent = bankname + "\n" + amount + "\n" + accountID;
dos.writeUTF(toBeSent);
}
}
catch (IOException e)
{
System.out.println(e.getMessage() + "Error sending options to server!");
}
}
}