This commit was manufactured by cvs2svn to create tag 'buildscript'.
[IRC.git] /
1 // Bank App in Java
2
3 // Author: Danish Lakhani
4
5 // BankAppServer - Bank Application Server that services one client at a time
6
7 import java.io.*;
8 import java.net.*;
9 import java.util.*;
10
11 class BankAppClientTeller
12 {
13         static int SUCCESS = 0;
14         static int ERROR = 1;
15
16         static int LOGIN_ACCOUNT = 1;
17         static int LOGIN_PIN = 2;
18
19         static int ACCOUNT_SAVINGS = 1;
20         static int ACCOUNT_CHECKING = 2;        
21         static int ACCOUNT_TELLER = 3;
22         
23         private Socket mySocket = null;
24         private PrintWriter out = null;
25         private BufferedReader in = null;
26         private static int serverPort = 44444;
27
28         public BankAppClientTeller()
29         {
30         }
31         
32         private void establishConnection()
33                 throws IOException
34         {
35                 System.out.println("Connecting to server...");
36                 mySocket = new Socket("localhost", serverPort);
37                 out = new PrintWriter(mySocket.getOutputStream(), true);
38                 in = new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
39                 System.out.println("Connection Established!");
40         }
41         
42         private void closeConnection()
43                 throws IOException
44         {
45                 if (out != null)
46                         out.close();
47                 if (in != null)
48                         in.close();
49                 if (mySocket != null)
50                         mySocket.close();
51         }
52
53         private void displayMenu()
54         {
55                 System.out.println("\nBankAppClientTeller");
56                 System.out.println("----------------");
57                 System.out.println("1. Login");
58                 System.out.println("2. Logout");
59                 System.out.println("3. Deposit");
60                 System.out.println("4. Withdraw");
61                 System.out.println("5. Check Balance");
62                 System.out.println("6. Open Account");
63                 System.out.println("7. Close Account");
64                 System.out.println("8. Modify Account");
65                 System.out.println("9. Check Account Info");
66                 System.out.println("0. Exit\n");
67                 System.out.print("Enter Choice: ");
68                 return;
69         }
70         
71         private void startClient()
72                 throws IOException
73         {
74                 int clientQuit = 0;
75                 int status = SUCCESS;
76                 int isConnected = 0;
77                 boolean loggedIn = false;
78                 BufferedReader local_in = new BufferedReader(new InputStreamReader(System.in));
79                 while (clientQuit == 0)
80                 {
81                         if (!loggedIn)
82                                 establishConnection();
83                         isConnected = 1;
84                         while (isConnected == 1)
85                         {
86                                 displayMenu();
87                                 String input = local_in.readLine();
88                                 int selection = Integer.parseInt(input);
89                                 String response;
90                                 switch (selection)
91                                 {
92                                         case 0:
93                                                 System.out.println("Exitting...");
94                                                 out.println("EXIT");
95                                                 System.exit(0);
96                                                 break;
97                                         case 1:
98                                                 System.out.println("Login");
99                                                 out.println("LOGIN");
100                                                 response = in.readLine();
101                                                 if (response.equals("OK"))
102                                                 {
103                                                         System.out.print("Enter account number: ");
104                                                         String accountNumber = local_in.readLine();
105                                                         System.out.print("Enter PIN: ");
106                                                         String pinNumber = local_in.readLine();
107                                                         out.println(accountNumber);
108                                                         out.println(pinNumber);
109                                                         response = in.readLine();
110                                                         if (response.equals(accountNumber))
111                                                         {
112                                                                 System.out.println("Login Successful! Account: " + response);
113                                                                 loggedIn = true;
114                                                         }
115                                                         else
116                                                         {
117                                                                 System.out.println(response);
118                                                         }                                       
119                                                 }
120                                                 else
121                                                 {
122                                                         System.out.println(response);
123                                                 }
124                                                 break;
125                                         case 2:
126                                                 System.out.println("Logout");
127                                                 out.println("LOGOUT");
128                                                 response = in.readLine();
129                                                 if (response.equals("OK"))
130                                                 {
131                                                         response = in.readLine();
132                                                         System.out.println("Logout Successful! Account: " + response);
133                                                 }
134                                                 else
135                                                 {
136                                                         System.out.println(response);
137                                                 }
138                                                 break;
139                                         case 3:
140                                                 System.out.println("Deposit");
141                                                 out.println("TELLERDEPOSIT");
142                                                 response = in.readLine();
143                                                 if (response.equals("OK"))
144                                                 {
145                                                         System.out.print("Enter Account Number: ");
146                                                         String accNum = local_in.readLine();
147                                                         out.println(accNum);
148                                                         response = in.readLine();
149                                                         if (!response.equals("OK"))
150                                                         {
151                                                                 System.out.println(response);
152                                                                 break;
153                                                         }
154                                                         System.out.print("Enter Deposit Amount: ");
155                                                         String depAmount = local_in.readLine();
156                                                         out.println(depAmount);
157                                                         response = in.readLine();
158                                                         if (response.equals("OK"))
159                                                         {
160                                                                 response = in.readLine();
161                                                         }
162                                                 }
163                                                 System.out.println(response);
164                                                 break;
165                                         case 4:
166                                                 System.out.println("Withdraw");
167                                                 out.println("TELLERWITHDRAW");
168                                                 response = in.readLine();
169                                                 if (response.equals("OK"))
170                                                 {
171                                                         System.out.print("Enter Account Number: ");
172                                                         String accNum = local_in.readLine();
173                                                         out.println(accNum);
174                                                         response = in.readLine();
175                                                         if (!response.equals("OK"))
176                                                         {
177                                                                 System.out.println(response);
178                                                                 break;
179                                                         }
180                                                         System.out.print("Enter Withdrawal Amount: ");
181                                                         String wdAmount = local_in.readLine();
182                                                         out.println(wdAmount);
183                                                         response = in.readLine();
184                                                         if (response.equals("OK"))
185                                                         {
186                                                                 response = in.readLine();
187                                                         }
188                                                 }
189                                                 System.out.println(response);
190                                                 break;
191                                         case 5:
192                                                 System.out.println("Check Balance");
193                                                 out.println("TELLERCHECK");
194                                                 response = in.readLine();
195                                                 if (response.equals("OK"))
196                                                 {
197                                                         System.out.print("Enter Account Number: ");
198                                                         String accNum = local_in.readLine();
199                                                         out.println(accNum);
200                                                         response = in.readLine();
201                                                         if (!response.equals("OK"))
202                                                         {
203                                                                 System.out.println(response);
204                                                                 break;
205                                                         }
206                                                         response = in.readLine();
207                                                 }
208                                                 System.out.println(response);
209                                                 break;
210                                         case 6:
211                                                 System.out.println("Account Open");
212                                                 out.println("TELLEROPEN");
213                                                 response = in.readLine();
214                                                 if (!response.equals("OK"))
215                                                 {
216                                                         System.out.println(response);
217                                                         break;
218                                                 }
219                                                 System.out.print("Enter Account Number: ");
220                                                 out.println(local_in.readLine());
221                                                 System.out.print("Enter First Name: ");
222                                                 out.println(local_in.readLine());
223                                                 System.out.print("Enter Middle Name: ");
224                                                 out.println(local_in.readLine());
225                                                 System.out.print("Enter Last Name: ");
226                                                 out.println(local_in.readLine());
227                                                 System.out.print("Enter Account Type: ");
228                                                 out.println(local_in.readLine());
229                                                 System.out.print("Enter Initial Balance: ");
230                                                 out.println(local_in.readLine());
231                                                 System.out.print("Enter PIN: ");
232                                                 out.println(local_in.readLine());
233                                                 response = in.readLine();
234                                                 if (response.equals("OK"))
235                                                         response = in.readLine();
236                                                 System.out.println(response);
237                                                 break;
238                                         case 7:
239                                                 System.out.println("Account Close");
240                                                 out.println("TELLERCLOSE");
241                                                 response = in.readLine();
242                                                 if (!response.equals("OK"))
243                                                 {
244                                                         System.out.println(response);
245                                                         break;
246                                                 }
247                                                 System.out.print("Enter Account Number: ");
248                                                 out.println(local_in.readLine());
249                                                 response = in.readLine();
250                                                 if (response.equals("OK"))
251                                                         response = in.readLine();
252                                                 System.out.println(response);
253                                                 break;
254                                         case 8:
255                                                 System.out.println("Modify Account");
256                                                 out.println("TELLERMODIFY");
257                                                 response = in.readLine();
258                                                 if (!response.equals("OK"))
259                                                 {
260                                                         System.out.println(response);
261                                                         break;
262                                                 }
263                                                 System.out.print("Enter Account Number: ");
264                                                 String accNum = local_in.readLine();
265                                                 out.println(accNum);
266                                                 response = in.readLine();
267                                                 if (!response.equals("OK"))
268                                                 {
269                                                         System.out.println(response);
270                                                         break;
271                                                 }
272                                                 int done = 0;
273                                                 while (done == 0)
274                                                 {
275                                                         System.out.println("1. Change Name");
276                                                         System.out.println("2. Change Type");
277                                                         System.out.println("3. Change PIN");
278                                                         System.out.println("4. Change Balance");
279                                                         System.out.println("5. Done\n");
280                                                         System.out.print("Enter Choice: ");
281                                                         int choice = Integer.parseInt(local_in.readLine());
282                                                         switch (choice)
283                                                         {
284                                                                 case 1:
285                                                                         out.println("CHANGENAME");
286                                                                         System.out.print("Enter New First Name: ");
287                                                                         out.println(local_in.readLine());
288                                                                         System.out.print("Enter New Middle Name: ");
289                                                                         out.println(local_in.readLine());
290                                                                         System.out.print("Enter New Last Name: ");
291                                                                         out.println(local_in.readLine());
292                                                                         response = in.readLine();
293                                                                         if (!response.equals("OK"))
294                                                                         {
295                                                                                 System.out.println(response);
296                                                                         }
297                                                                         break;
298                                                                 case 2:
299                                                                         out.println("CHANGETYPE");
300                                                                         System.out.print("Enter New Account Type: ");
301                                                                         out.println(local_in.readLine());
302                                                                         response = in.readLine();
303                                                                         if (!response.equals("OK"))
304                                                                         {
305                                                                                 System.out.println(response);
306                                                                         }                                                               
307                                                                         break;
308                                                                 case 3:
309                                                                         out.println("CHANGEPIN");
310                                                                         System.out.print("Enter New PIN: ");
311                                                                         out.println(local_in.readLine());
312                                                                         response = in.readLine();
313                                                                         if (!response.equals("OK"))
314                                                                         {
315                                                                                 System.out.println(response);
316                                                                         }
317                                                                         break;
318                                                                 case 4:
319                                                                         out.println("CHANGEBALANCE");
320                                                                         System.out.print("Enter New Balance: ");
321                                                                         out.println(local_in.readLine());
322                                                                         response = in.readLine();
323                                                                         if (!response.equals("OK"))
324                                                                         {
325                                                                                 System.out.println(response);
326                                                                         }
327                                                                         break;
328                                                                 case 5:
329                                                                         done = 1;
330                                                                         out.println("DONE");
331                                                                         break;
332                                                                 default:
333                                                                         System.out.println("Invalid selection");
334                                                                         break;
335                                                         }
336                                                 }
337                                                 response = in.readLine();
338                                                 System.out.println(response);
339                                                 break;
340                                         case 9:
341                                                 System.out.println("View Account");
342                                                 out.println("TELLERVIEW");
343                                                 response = in.readLine();
344                                                 if (!response.equals("OK"))
345                                                 {
346                                                         System.out.println(response);
347                                                         break;
348                                                 }
349                                                 System.out.print("Enter Account Number: ");
350                                                 String accNumber = local_in.readLine();
351                                                 out.println(accNumber);
352                                                 response = in.readLine();
353                                                 if (response.equals("OK"))
354                                                         response = in.readLine();
355                                                 System.out.println(response);
356                                                 break;
357                                         default:
358                                                 System.out.println("Invalid Selection");
359                                                 break;
360                                 }
361                                 System.out.println("Press Enter to Continue...");
362                                 local_in.readLine();
363                         }
364                 }
365         }
366
367         public static void main(String [] args)
368                 throws IOException
369         {
370                 System.out.println("BankAppClientTeller in Java");
371                 BankAppClientTeller client = new BankAppClientTeller();
372                 client.startClient();
373         }
374 }