-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimpleClient.java
More file actions
96 lines (75 loc) · 2.52 KB
/
SimpleClient.java
File metadata and controls
96 lines (75 loc) · 2.52 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
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Scanner;
import accountType.Account;
import java.io.ObjectInputStream;
import java.net.InetAddress;
import java.net.Socket;
public class SimpleClient {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int userSelction = -1;
Socket clientSocket; // TCP/IP socket
try {
clientSocket = new Socket(InetAddress.getByName("localhost"), Integer.parseInt(args[0]));
DataInputStream dataIn = new DataInputStream(clientSocket.getInputStream());
DataOutputStream dataOut = new DataOutputStream(clientSocket.getOutputStream());
ObjectInputStream recvObj = new ObjectInputStream(clientSocket.getInputStream());
System.out.println("Connected to " + clientSocket.getInetAddress().getHostName());
String bankMessage = dataIn.readUTF();
System.out.println(bankMessage);
String username = scanner.nextLine();
dataOut.writeUTF(username);
dataOut.flush();
//main loop
do {
bankMessage = dataIn.readUTF();
System.out.println(bankMessage);
userSelction = scanner.nextInt();
scanner.nextLine();//flush out \n
dataOut.writeInt(userSelction);
dataOut.flush();
switch (userSelction) {
case 1:
Account accList[] = (Account[]) recvObj.readObject();
System.out
.println("information about ALL accounts received, displayed LINE BY LINE as seen below:");
printAccounts(accList);
break;
case 2:
System.out.println("# question from the server: " + dataIn.readUTF());
dataOut.writeUTF(scanner.nextLine());
dataOut.flush();
Account accList1[] = (Account[]) recvObj.readObject();
printAccounts(accList1);
break;
case 0:
System.out.println("Thank You!");
break;
default:
System.out.println("Invalid Input!");
break;
}
} while (userSelction != 0);
dataOut.close();
dataIn.close();
recvObj.close();
clientSocket.close();
} catch (IOException | ClassNotFoundException ioe) {
ioe.printStackTrace();
}
System.out.println("the client is going to stop runing...");
scanner.close();
} // end main
private static void printAccounts(Account[] accList) {
int count = 0;
try {
for (Account account : accList) {
System.out.println((++count) +". "+ account.getAccountNumber()+", "+account.getFullName()+", $"+account.getAccountBalance());
}
}catch (Exception e) {
System.out.print("Invalid Input!");
}
}
}