Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-27-2012, 07:00 PM   PM User | #1
jakeroyl
New to the CF scene

 
Join Date: Mar 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
jakeroyl is an unknown quantity at this point
Post Help with arrays!

Okay, so these are the things that I need to do.

1 - create an array of customer records and stores them in a binary file.

2 - reads from the file and saves it in a 'anotherArray'

3 - Add a new customer record to the 'AnotherArray'

4 - reads the name,pin,withdraw and deposit amount from the user and uses the name & pin to find a match in the 'anotherArray' and if a match is found, then the balance should be deducted if a deposit is made or credited if a withdraw is made.

5 - write the 'anotherArray' in the same binary file after updating the balance

The array is in this format (name,pin,account,balance)

I've done a little, but I need help in the 4th and 5th part.




Code:
import java.io.Serializable;
import java.util.Scanner;

/**
 Serialized class for data on endangered species.
 Includes a main method.
*/
public class CustomerRecord implements Serializable
{
    private String name;
    private int pin;
    private int account;
    private static double balance;

    public CustomerRecord( )
    {
        name = null;
        pin = 0;
        account = 0;
        balance = 0.00;
    }

    public CustomerRecord(String initialName, int initialPin, int initialAccount, double initialBalance)
    {
        name = initialName;

        if (initialPin >= 1111 && initialPin <= 9999)
            	pin = initialPin;
        else
        {
            	System.out.println("ERROR: Pin is not acceptable.");
            	System.exit(0);
        }
	
	if (initialAccount >= 400111 && initialAccount <= 500111)
        	account = initialAccount;
	else
	{
		System.out.println("ERROR: Account number is not acceptable.");
            	System.exit(0);
	}

	if (initialBalance >= 0)
        	balance = initialBalance;
	else
	{
		System.out.println("ERROR: Initial Balance is not acceptable.");
            	System.exit(0);
	}
    }

    public String toString()
    {
        return ("Name = " + name
              	+ "  Account = " + account
		+ "  Balance = " + "$" + balance + "\n");
    }

    public void setCustomerRecord( )
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\nEnter new customer's name: ");
        name = keyboard.nextLine( );

        System.out.println("Enter new customer's pin: ");
        pin = keyboard.nextInt( );
        while (pin < 1111 || pin > 9999)
        {
            System.out.println("Pin should be in the range of 1111-9999.");
            System.out.println("Reenter pin:");
            pin = keyboard.nextInt( );
        }

        System.out.println("Enter new customer's account no: ");
	account = keyboard.nextInt( );
        while (account < 400111 || pin > 500111)
        {
            System.out.println("Account should be in the range of 400111-500111.");
            System.out.println("Reenter account number:");
            account = keyboard.nextInt( );
        }

	System.out.println("Enter new customer's initial balance: ");
        balance = keyboard.nextDouble( );
	while (balance < 0)
        {
            System.out.println("Initial balance should be positive or zero.");
            System.out.println("Reenter initial balance:");
            balance = keyboard.nextDouble( );
        }
    }

    public void writeOutput( )
    {
         System.out.print("Name = " + name + "\t");
         System.out.print("Account = " + account + "\t");
         System.out.print("Balance = " + "$" + balance + "\n");
    }

    public String getName( )
    {
        return name;
    }

    public int getPin( )
    {
        return pin;
    }

    public int getAccount( )
    {
        return account;
    }

    
    
    public void setBalance(double amount)
    {
        balance = amount;
    }

    public static void deposit(double aDeposit)
    {
    balance = balance + aDeposit;
    }
    public static void withdraw(double aWithdraw)
    {
    	if 
    	( balance >= aWithdraw)
    		balance = balance - aWithdraw;
    	else
    		System.out.println("Cannot withdraw that amount. You do not sufficient balance.");
    	Scanner keyboard = new Scanner(System.in);
		aWithdraw = keyboard.nextDouble();
    	return;
    }
    public double getBalance( )
    {
        return balance;
    }
    public boolean equal(CustomerRecord otherObject)
    {
        return (name.equalsIgnoreCase(otherObject.name) &&
               (pin == otherObject.pin) &&
               (account == otherObject.account) &&
               (balance == otherObject.balance));
    }
}

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Scanner;

public class BankCustomers
{
    public static void main(String[] args)
    {	
    	//----------------------------------------------------------------
    	//part1: Create array of customer records and write them 
    	//in the file customer_record.dat
        CustomerRecord[] oneArray = new CustomerRecord[100];
        oneArray[0] = new CustomerRecord("John Gray", 2222, 400222, 10000.00);
        oneArray[1] = new CustomerRecord("Ann Black", 3333, 400333, 1500.00);
        oneArray[2] = new CustomerRecord("Mary White", 4444, 400555, 16500.00);
        oneArray[3] = new CustomerRecord("Jack Green", 7777, 400888, 100.00);
        
        int index = 4;
        
        String fileName = "customer_records.dat";
        System.out.println("---Open file: " + fileName);
        System.out.println("---Customer records written to file: " + fileName);
        System.out.println("---Close file: " + fileName);
        try
        {
            ObjectOutputStream outputStream =
                  new ObjectOutputStream(
                      new FileOutputStream(fileName));
            outputStream.writeObject(oneArray);
            outputStream.close( );
        }
        catch(IOException e)
        {
            System.out.println("Error writing to file " +
                                fileName + ".");
            System.exit(0);
        }
        System.out.println("\n");
        
        
        //-------------------------------------------------------------------
        //Part2: Read from the file customer_record.dat and save 
        //the customer records in anotherArray
        System.out.println("---Open file: " + fileName);
        System.out.println("---Customer records read from file: " + fileName);
        System.out.println("---Close file: " + fileName);
        CustomerRecord[] anotherArray = null;
        try
        {
            ObjectInputStream inputStream = 
                       new ObjectInputStream(
                               new FileInputStream(fileName));
            anotherArray = (CustomerRecord[])inputStream.readObject( );
            inputStream.close( );
        }
        catch(Exception e)
        {
            System.out.println("Error reading file " + 
                                fileName + ".");
            System.exit(0);
        }
        		//Print the records on screen
        for (int i = 0; i < index; i++)
            System.out.print(anotherArray[i]);
          
        
        //------------------------------------------------
        //Part3: Add a new customer record to anotherArray
        
        anotherArray[index] = new CustomerRecord();
        anotherArray[index].setCustomerRecord();
        index++;
        		//Print the records on screen
        for (int i = 0; i < index; i++)
            System.out.print(anotherArray[i]);
        
        
        //------------------------------------------------
        //Part4: Find a customer record from anotherArray 
        //to do transaction(s) and update the record's balance
        
        char repeat; // User control to repeat or quit
        double balance;
        
        do{   
        	;
        	
        	System.out.println("Enter the name");
            String aName;
            Scanner keyboard = new Scanner(System.in);
            aName = keyboard.nextLine();
           
            System.out.println("Enter the pin");
            int aPin;
            aPin = keyboard.nextInt();
            
            System.out.println("Enter the amount you wish to withdraw");
            double aWithdraw;
            aWithdraw = keyboard.nextDouble();
            CustomerRecord.withdraw(aWithdraw);

            System.out.println("Enter the amount you wish to deposit");
            double aDeposit;
            aDeposit = keyboard.nextDouble();
            CustomerRecord.deposit(aDeposit);

                 for
                 (int i = 0; i < anotherArray.length; i++) {
            	  CustomerRecord record = anotherArray[i];
            	  if( record.getName().equalsIgnoreCase(aName))
{
            	     System.out.println(record);
            	    record.getBalance();
            	
            	  }
            	  else
            	  {System.out.println("");}
            	 
            	  }
        
        
            System.out.println("\nAnother Transaction? (y for yes)");
            repeat = keyboard.next().charAt(0);
            
        }while(repeat == 'y' || repeat == 'Y');
    
        	//Print the records on screen
        for (int i = 0; i < index; i++)
            System.out.print(anotherArray[i]);
        
        //------------------------------------------------
        //Part5: Write the customer records from anotherArray 
        //in the file customer_record.dat 
        
     // *** MISSING CODE ***
        
        //End of program
        System.out.println("\n---End of program.");
    }
}

Last edited by VIPStephan; 03-27-2012 at 07:05 PM.. Reason: converted icode BB tags to code tags
jakeroyl is offline   Reply With Quote
Old 03-28-2012, 01:56 PM   PM User | #2
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
why not just append all that new data to the file using the filewriter? link
alternatively you could re-write the entire file. If all of the code up there is working I am confused- you seem to have done the more complex stuff- this is just appending the text on the end.

also can you separate you classes in the future- took a while to scan through and actually find where it was you were having issues; provided your other class is working as it should (which you've indicated) I could have flipped past it (still important to have as reference though )
__________________

I code C hash-tag .Net
Reference: W3C W3CWiki .Net Lib
Validate: html CSS
Debug: Chrome FireFox IE
alykins is offline   Reply With Quote
Reply

Bookmarks

Tags
array, binary files, java, serializable

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:29 AM.


Advertisement
Log in to turn off these ads.