FileDocCategorySizeDatePackage
cashpoint.javaAPI DocExample10946Thu Oct 05 17:25:10 BST 2000None

cashpoint

public class cashpoint extends Object

Fields Summary
static int
acc_no
static int
pin_no
static int
option
static int
locate
static int
found
static int
no_of_accounts
static DataInputStream
userin
static int[]
accno_record
static int[]
pinno_record
static float[]
bal_record
Constructors Summary
public cashpoint()


  
 
	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.exit(0);
	}

Methods Summary
public voidinput_user_data()

	
	int cont=0;
	String accno="";
	String pinno = "";
	
	try
	{
		//wait for 1500ms
		Thread.sleep(1500);
	}
	catch(InterruptedException iesleep1)
	{
		System.out.println("Error - couldn't activate Sleep");
	}
	
	//display opening title
 	System.out.println("\n\nWelcome to the Virtual Bank v1.0");
	System.out.println("--------------------------------");

	//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();
				acc_no = Integer.valueOf(accno).intValue();
 			}
			//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);
 			}
			
			//if account no. is invalid account no. do not continue
			if(accno.length()!=6)
			{
				cont=0;
			}
			
			System.out.println("\nPlease Enter PIN no. - ");
 			try
 			{
				//read in pin no. from user input
				pinno = userin.readLine();
		  		pin_no = Integer.valueOf(pinno).intValue();
 			}
			catch(IOException ex3)
	 		{
				System.out.println("Error - Could not read pin no.");
				System.out.println("Exiting.........");
				System.exit(0);
 			}

			//if user input less than or greater than 4 digits do not continue
			if(pinno.length()!=4)
			{
				cont=0;
			}
		}
		//catchs invalid data input(e.g. not int type)
		catch(NumberFormatException nfe1)
		{
		cont = 0;
		}
	
		//if invalid data entered,asks user to enter again
		if(cont==0)
		{		
			System.out.println("\nError - incorrect entry");
			System.out.println("\nPlease try again\n\n");
			try
			{
				//wait 1500ms
				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)

	//create instance of cashpoint
	cashpoint machine = new cashpoint();
	
	while(true)
	{	
		//calls object to read in account no. and pin no. of account user wants to query
		machine.input_user_data();

		option=0;

		//while option doesn't equal return card,loop
 		while(option!=4)
		{	
			//calls object to serach accounts file for specified account
			machine.search_accounts();

			if(found==1)
			{
				//displays main menu
				machine.menu();
				
				//process user request made from main menu
				machine.process_request();
	
				//saves changed accounts file 
				machine.save_bank_data();
		 
			}		

		}//end of if option!=4

	}//end of while(true) 

 
public voidmenu()

	option = 0;

	//while option is not equal to 1,2,3 or 4 loop	
	while(option!=1 && option!=2 && option!=3 && option!=4)
	{
		//display menu
		System.out.println("\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 validating
				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 while option !=1,2,3,4

 
public voidprocess_request()

	float user_amount = -1;
	
	//process user query
	switch(option)
	{
		//process balance rquest
		case 1:
		{
			Locale local = new Locale("en","US");
			//display balance for given account no.
			System.out.println("\nBalance = " + NumberFormat.getCurrencyInstance().format(bal_record[locate]));
			break;
		}
	 	
		//process withdraw transaction
		case 2:
		{	
			//while user amount not vaild loop
			while(user_amount<0)
			{	
				try
				{
					
					System.out.println("\nEnter Amount to Withdraw - ");
					try
					{
						//read amount user wants to withdraw from user input
						user_amount = Float.valueOf(userin.readLine()).floatValue();
		
					}	
					//caught if amount could not be read
					catch(IOException ex5)
					{
						System.out.println("Error - Could not read amount to withdraw");
						user_amount = -1;
					}
				}
				//caught if user amount entered is invalid type
				catch(NumberFormatException nfe3)
				{
				user_amount = -1;
				}

				if(user_amount<0)
				{
					System.out.println("Invalid amount - please re-enter");
				}

			}//end of while
			
			//if amount requesed is greater than balance in account, then fail transaction
			if(user_amount > bal_record[locate])
			{
				System.out.println("\nWithdraw transaction failed - due to funds unavailable");
			}

			//else update balance if amount greater than 0
			else if(user_amount > 0)
			{
				bal_record[locate]  = bal_record[locate] - user_amount;
				System.out.println("\nWithdrawal Transaction Complete\n");	
			}
			break;
		}

		//process deposit request
		case 3:
		{	
			//while amount entered invaild loop
			while(user_amount<0)
			{
				System.out.println("\nEnter Amount to Deposit - ");
				try
				{
					try
					{
						//read in amount to deposit from user input
						user_amount = Float.valueOf(userin.readLine()).floatValue();
					}

					//caught if user amount could not be read
					catch(IOException ex6)
					{
						System.out.println("Error - Could not read amount to deposit");
						user_amount=-1;
					}
				}

				//caught if user amount not valid type 
				catch(NumberFormatException nfe4)
				{
				user_amount=-1;
				}
				
				//if amount less than zero do not process
				if(user_amount < 0)
				{
					System.out.println("\nIncorrect amount entered - Please re-enter");
				}
				
				//else update balance if user amount greater than 0
				else if(user_amount>0)
				{	
					bal_record[locate]  = bal_record[locate] + user_amount;
					System.out.println("\nDeposit Transaction Complete\n");				
				}
			}
			break;
		}
		
		//process user end session
		case 4 : 
		{
			System.out.println("\nPlease Take your Card\n");
			break;
		}
		
	}//end of switch

	try
	{
		//wait 1500ms
		Thread.sleep(1500);
	}
	catch(InterruptedException ie1)
	{
		System.out.println("Error - couldn't activate Sleep");
	}
	
 
public voidsave_bank_data()

	int i =0;
 	try
	{	
		//create output stream to write data to bank accounts file
		PrintWriter accdat2= new PrintWriter(new BufferedWriter(new FileWriter("bank.dat")));

		//write all accounts in array into bank.dat
		for(i=0;i<no_of_accounts;i++)
		{
			accdat2.write(accno_record[i] + " " + pinno_record[i] + " " + bal_record[i]+"\n");
		}
		//close accounts file
		accdat2.close();
	}	
	//caught if could not write accounts data to file,terminates program
	catch(IOException ex2)
	{
		System.out.println("\nError writing to file");
		System.out.println("Exiting........");
		System.exit(0);
	}

 
public voidsearch_accounts()

	int i = 0;
	locate = 0;
	found = 0;
	
	//find account no. specified,found=0 if account no. doesn't exist
 	for(i=0;i<no_of_accounts;i++)
	{
		if(acc_no == accno_record[i])
		{
			locate = i;
			found = 1;
			break;
		}
	}

	//if pin is doesn't match 
	if(pin_no != pinno_record[locate])
	{
		found = 0;
		//display incorrect pin message
		System.out.println("\nIncorrect PIN Entered! - Please try again");
		System.out.println("\n\n");
		option=4;
		try
		{
			//wait for 1500ms
			Thread.sleep(1500);
		}
			catch(InterruptedException ie2)
		{
			System.out.println("Error - couldn't activate Sleep");
		}
	}