Methods Summary |
---|
public static void | main(java.lang.String[] args)
cashserver bank_machine = new cashserver();
bank_machine.open_accounts();
while(true)
{
System.out.println("\nWaiting for request");
bank_machine.receive_message();
bank_machine.search_accounts();
bank_machine.process_and_send_response();
bank_machine.save_bank_data();
}
|
public void | open_accounts()
//initialise local variables
int i=0;
String s= " ";
//initialise all accounts arrays to 0
for(i=0;i<100;i++)
{
accno_record[i] = 0;
pinno_record[i] = 0;
bal_record[i] = 0;
}
try
{
//open a input stream for reading data from bank accounts file
BufferedReader inStream = new BufferedReader(new FileReader("bank.dat"));
int j=0;
try
{
//while not end of file read account
while((s=inStream.readLine()) != null)
{
//seperate line read in from accounts file into arrays
StringTokenizer st = new StringTokenizer(s);
//count no of accounts in bank acounts file
no_of_accounts = no_of_accounts + 1;
//copy components of seperated line into correct arrays
accno_record[j] = Integer.valueOf(st.nextToken()).intValue();
pinno_record[j] = Integer.valueOf(st.nextToken()).intValue();
bal_record[j] = Float.valueOf(st.nextToken()).floatValue();
j = j+1;
}
}
//caught if accounts file could not be read,terminates program
catch(IOException ex)
{
System.out.print("Error Reading from bank.dat file");
System.out.println("Exiting......");
System.exit(0);
}
}
//caught if bank accounts file could not be opened,terminates program
catch(FileNotFoundException brex)
{
System.out.println("\nError - Couldn't open 'bank.dat' file");
System.out.println("Exiting......");
System.exit(0);
}
|
public void | process_and_send_response()
String response = " ";
float user_amount = 0;
if(found==1)
{
switch(choice)
{
case 'b":
{
String balance = String.valueOf(bal_record[locate]);
response = "b ";
response = response.concat(balance);
break;
}
case 'w":
{
user_amount = Float.valueOf(request_st.nextToken()).floatValue();
if(user_amount > bal_record[locate])
{
response = "overdrawn";
break;
}
bal_record[locate] = bal_record[locate] - user_amount;
response = "w" + " ";
response.concat("withdraw transaction complete");
break;
}
case 'd":
{
user_amount = Float.valueOf(request_st.nextToken()).floatValue();
bal_record[locate] = bal_record[locate] + user_amount;
response = "d" + " ";
response.concat("deposit transaction complete");
break;
}
}//end of switch(choice)
}//end of if
else
{
response = ("i Incorrect PIN");
}
//send response to client
outStream.println(response);
//flush output buffer
outStream.flush();
System.out.println("Sent: " + response);
|
public void | receive_message()
//Stops here until a line of ASCII datat is read
try
{
try
{
request = inStream.readLine();
}
catch(NullPointerException ne1)
{
System.out.println(" hello");
}
}
catch(IOException ioinStream)
{
System.out.println("Client terminated");
System.out.println("Exiting.........");
System.exit(0);
}
System.out.println("\nReceived: " + request);
if(request==null)
{
System.out.println("Client not responding");
System.out.println("Terminating...");
System.exit(0);
}
request_st = new StringTokenizer(request);
acc_no = Integer.valueOf(request_st.nextToken()).intValue();
pin_no = Integer.valueOf(request_st.nextToken()).intValue();
String option = request_st.nextToken();
choice = option.charAt(0);
|
public void | save_bank_data()
int i = 0;
//save accounts file
try
{
PrintWriter accdat2= new PrintWriter(new BufferedWriter(new FileWriter("bank.dat")));
for(i=0;i<no_of_accounts;i++)
{
accdat2.write(accno_record[i] + " " + pinno_record[i] + " " + bal_record[i]+"\n");
}
accdat2.close();
}
catch(IOException ex2)
{
System.out.println("\nError writing to file");
System.out.println("Exiting......");
System.exit(0);
}
|
public void | search_accounts()
int i= 0;
found = 0;
for(i=0;i<no_of_accounts;i++)
{
if(acc_no == accno_record[i])
{
locate = i;
found = 1;
break;
}
}
if(pin_no != pinno_record[locate])
{
found = 0;
}
|