FileDocCategorySizeDatePackage
cashserver.javaAPI DocExample6751Thu Oct 05 17:25:12 BST 2000None

cashserver

public class cashserver extends Object

Fields Summary
public static final int
DEF_PORT
public static final String
DEF_HOST
static ServerSocket
listenSocket
Socket
soc
PrintWriter
outStream
BufferedReader
inStream
static char
choice
static int
acc_no
static int
pin_no
static int
option
static int
locate
static int
found
static int
no_of_accounts
static String
request
static StringTokenizer
request_st
static DataInputStream
userin
static int[]
accno_record
static int[]
pinno_record
static float[]
bal_record
Constructors Summary
public cashserver()

	

  
 
 	//create socket listener
 	try
 	{
		//ServerSocket establishes the port where server waits for connections
		listenSocket = new ServerSocket(DEF_PORT);
 	}
	catch(IOException ex)
 	{
		System.out.println("Failed to listen on port" + DEF_PORT);
		System.out.println("Exiting......");
		System.exit(0);
	}

	System.out.println("\n\nServer waiting for connection on port " + DEF_PORT);

	try
	{
		//creates a socket and waits here until a connection request is received
		soc = listenSocket.accept();
		System.out.println("Connection established");
	
		//create an input and output stream for new connection
		outStream = new PrintWriter(new OutputStreamWriter(soc.getOutputStream()));
		inStream = new BufferedReader(new InputStreamReader(soc.getInputStream()));
	}
	catch(IOException initfailed)
	{
	System.out.println("Error - initialisation failed");
	System.out.println("Exiting......");
	System.exit(0);
	}
	
 
Methods Summary
public static voidmain(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 voidopen_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 voidprocess_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 voidreceive_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 voidsave_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 voidsearch_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;
	}