FileDocCategorySizeDatePackage
cashclient.javaAPI DocExample11592Thu Oct 05 17:25:10 BST 2000None

cashclient

public class cashclient extends Object

Fields Summary
public static final int
DEF_PORT
public static final String
DEF_HOST
public static String
host
private int
port
static String
accno
static String
pinno
static int
option
static Socket
soc
static PrintWriter
outStream
static BufferedReader
inStream
static DataInputStream
userin
Constructors Summary
public cashclient()

 
  
 

	System.out.println("\n\nEnter host name(e.g. usun22)-");
 	try
 	{
		 //reads host name entered by user
 		host = userin.readLine();
 	}

	//if host name is unreadable, terminate program
 	catch(IOException inputhost)
 	{
		System.out.println("Error reading host name");
		System.out.println("Exiting.........");
		System.exit(0);
 	}

	//if no host entered assume host = local host
	if(host.length() < 1)
 	{
		host=DEF_HOST;
 	}

 	try
 	{
		//create a socket connection using host name and server's port number
		soc = new Socket(host, DEF_PORT);
		
 		//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 iesoc)
	{
	System.out.println("Error - initialisation failed");
	System.out.println("Exiting.........");
	System.exit(0);
	}

 
Methods Summary
public voidbuild_and_send()

	//initialise local variables
	String user_amount = " ";
	float useramount = -1;
	String request = " ";	

	//begin building message
	request=accno + " ";
	request=request.concat(pinno) + " ";

	//build rest of message based on user's request in main_menu()
	switch(option)
	{
		//requests balance
		case 1:
		{
			//complete message for balance request	
			request=request.concat("b");
			break;
		}

		//requests withdrawal
		case 2:
		{
			//loop while invaild amount entered
			while(useramount<0)
			{	
				try
				{
					System.out.println("\nEnter Amount to Withdraw - ");
					try
					{
						//read in amount user wants to withdraw
						user_amount = userin.readLine();
						//convert string to int for validation
						useramount = Float.valueOf(user_amount).floatValue();
		
					}	
					//caught if amount could not be read
					catch(IOException ex5)
					{
						System.out.println("Error - Could not read amount to withdraw");
					}
				}
				
				//caught if user amount entered is invalid type
				catch(NumberFormatException nfe3)
				{
				useramount = -1;
				}
				
				//if amount invalid, display message to user
				if(useramount<0)
				{
					System.out.println("\nInvalid amount - please re-enter");
					try
					{
						Thread.sleep(1500);
					}
					catch(InterruptedException iesleep)
					{
						System.out.println("Error - couldn't activate Sleep");
					}
				}

			}//end of while
			
			//complete message for withdraw request			
			request=request.concat("w" + " " + user_amount);
			break;
		}

		//requests deposit
		case 3:
		{
			//loop while invaild amount entered
			while(useramount<0)
			{	
				try
				{
					System.out.println("\nEnter Amount to Deposit - ");
					try
					{
						//read in amount user wants to deposit
						user_amount = userin.readLine();
						//convert string to int for validation
						useramount = Float.valueOf(user_amount).floatValue();
		
					}
					//caught if amount could not be read	
					catch(IOException ex5)
					{
						System.out.println("Error - Could not read amount to withdraw");
					}
				}
				//caught if user amount entered is invalid type
				catch(NumberFormatException nfe3)
				{
				useramount = -1;
				}

				//if amount invalid ,display message to user
				if(useramount<0)
				{
					System.out.println("Invalid amount - please re-enter");
					try
					{
						Thread.sleep(1500);
					}
					catch(InterruptedException iesleep)
					{
						System.out.println("Error - couldn't activate Sleep");
					}
				}//end of if useramount<0

			}//end of while
			
			//complete message for withdraw request	
			request=request.concat("d" + " " + user_amount);
			break;
		}
		
	}//end of switch(option)
	
	//send request to server
	outStream.println(request);
	//flush output stream
	outStream.flush();
	
 
public voidinput_user_data()

	int cont=0;

	//display opening title
	System.out.println("\nWelcome to the Virtual Bank v1.0");
	System.out.println("--------------------------------");
	
	try
	{
		//wait 1500ms
		Thread.sleep(1500);
	}
	catch(InterruptedException iesleep)
	{
		System.out.println("Error - couldn't activate Sleep");
	}
	
	//while do not continue to next stage,loop
	while(cont==0)
	{
		cont = 1;
		try
		{
			System.out.println("\nPlease Enter Account no. - ");
 			try
 			{
				//read in account no. from user
				accno = userin.readLine();

				//convert string to int
				int acc_no = Integer.valueOf(accno).intValue();

				//if account no. is invalid account no. do not continue
				if(accno.length()!=6)
					{
					cont=0;
					}
 			}
			//caught if account no. could not be read,terminates program
 			catch(IOException ex2)
 				{
			 	System.out.println("Error - Could not read account no.");
				System.out.println("Exiting..........");
				System.exit(0);
 				}

			System.out.println("\nPlease Enter PIN no. - ");
 			try
 			{
				//read in pin no. from user input
				pinno = userin.readLine();

				//convert string to int
				int pin_no = Integer.valueOf(pinno).intValue();

				//if user input less than or greater than 4 digits do not continue
				if(pinno.length()!=4)
					{
					cont=0;
					}
 			}
			catch(IOException ex3)
		 	{
				System.out.println("Error - Could not read pin no.");
				System.out.println("Exiting.......");
				System.exit(0);
		 	}

		}//end ot try for numberformatexception
		catch(NumberFormatException nfe1)
		{
		cont = 0;
		}

		if(cont==0)
		{		
			System.out.println("\nError - incorrect entry");
			System.out.println("\nPlease try again");
			try
			{
				Thread.sleep(1500);
			}
			catch(InterruptedException iesleep2)
			{
				System.out.println("Error - couldn't activate Sleep");
			}
		}

	}//end of while(cont==0)

 
public static voidmain(java.lang.String[] args)

	//creates instance of cashclient
 	cashclient user_interface = new cashclient();
 	
	//loop forever
	while(true)
 	{
		//calls object to read in account no. and pin no. of account user wants to query
 		user_interface.input_user_data();

		option = 0;

		//while option doesn't equal return card,loop
 		while(option!=4)
  		{
			//displays main menu
 			user_interface.menu();

			if(option!=4)
			{	
				//builds message and sends
		 		user_interface.build_and_send();
		
				//displays transaction status message(if it was successful or not)
 				user_interface.receive_and_process();
			}
			else
			{
			System.out.println("\nPlease Take your Card");
			}

			try
			{
				//waits 1500ms
				Thread.sleep(1500);
			}
			catch(InterruptedException ie)
			{
				System.out.println("Error with sleep ");
			}

		}//end of while(option!=4)

	 }//end of while(true)
 
public voidmenu()

	option = 0;
	
	//while option is not a vaild option loop	
	while(option!=1 && option!=2 && option!=3 && option!=4)
	{
		//display main menu
		System.out.println("\n\n----------------------");
		System.out.println("Main Menu");
		System.out.println("----------------------");
		System.out.println("1 - Display Balance");
		System.out.println("2 - Withdrawal");
		System.out.println("3 - Deposit");
		System.out.println("4 - Return Card");
		System.out.println("\nPlease Enter option - ");

		try
		{
			//read users choice from user input
			String choice = userin.readLine();
			try
			{
				//convert string to integer for vaildiating
				option = Integer.valueOf(choice).intValue();
			}

			//caught if option entered is not an integer
			catch(NumberFormatException nfe2)
			{
			option = 0;
			}
		}
		
		//caught if choice could not be read in,terminates program
		catch(IOException ex4)
		{
			System.out.println("Error - Could not read option no.");
			System.out.println("Exiting");
			System.exit(0);
		}

		//displays invaild option message if option not equal to 1,2,3 or 4 
		if(option!=1 && option!=2 && option!=3 && option!=4)
		{
			System.out.println("\nInvalid selection - please try again!");
			try
			{
				//wait 1500ms
				Thread.sleep(1500);
			}
			catch(InterruptedException iesleep)
			{
				System.out.println("Error - couldn't activate Sleep");
			}
	
		}//end of if option!=1,2,3,4

	}//end of while option !=1,2,3,4

 
public voidreceive_and_process()

	//initialise local variable
	StringTokenizer reply_st= null;
	String reply = " ";

	
	//get reply from server and display it
	try
	{
	
		//wait for message to be received 
		reply = inStream.readLine();
	}
	//caught if message could not be read
	catch(IOException ioinStream)
	{
		System.out.println("Error - due to Server not responding");
		System.out.println("Exiting.......");
		System.exit(0);
	}
	
	try
	{
		//break up message to get first char
		reply_st = new StringTokenizer(reply);
	
	}
	catch(NullPointerException ne1)
	{
		System.out.println("Server not responding");
		System.out.println("Exiting....");
		System.exit(0);
	}
	
	String reply_s = reply_st.nextToken();    	
	char reply_type = reply_s.charAt(0);
	
	//process reply
	switch(reply_type)
	{
		//displays balance
		case 'b":
		{	
			//gets balance amount from message
			String replyamount = reply_st.nextToken();
			//converts string amount to float amount
			float reply_amount = Float.valueOf(replyamount).floatValue();
			//defines local region
			Locale local = new Locale("en","US");
			//displays balance in form of local currency
			System.out.println("\nYour current balance is " + NumberFormat.getCurrencyInstance().format(reply_amount)); 
			break;
		}
						
		//acknowledges successful deposit
		case 'd":
		{
			System.out.println("\nDeposit transaction successful");
			break;
		}
					
		//acknowledges successful withdrawal
		case 'w":
		{
		System.out.println("\nWithdrawal transaction Successful");
		System.out.println("Please take your cash");
		break;
		}
			
		//informs user pin no. didn't match for given account no.
		case 'i":
		{
		System.out.println("\nIncorrect PIN code entered!!");
		option = 4;
		break;
		}
						
		//informs user withdrawal transaction failed due to lack of funds
		case 'o":
		{
		System.out.println("\nWithdrawal transaction not sucessful - due to lack of funds!");
		break;
		}
				
	}//end of switch

	try
	{
		//waits for 1500ms
		Thread.sleep(1500);
	}
	catch(InterruptedException iesleep)
	{
		System.out.println("Error - couldn't activate Sleep");
	}