SUSE Linux iTOps Tube

Sunday, May 6, 2012

inheretance and ArrayList in java ..i want to know the mistake in my code

hello every body



i am tring to solve this problem ,but i think there is smth wrong

cna any body help me




Quote:









Write a program client program that deals with different classes:

1. BankAccount class similar to the one in textbook but with additional data member accountName a string.

2. BranchBank class which consists of an arraylist of element of type BankAccount (should be protected) and an IdBranch an integer identifying the branch id. This class should contain the class constructor which reads from a file bank the branch id and the information about the accounts to be created in this branch. This class should have the following methods: searchAccount which takes as a parameter an account name and returns the corresponding BankAccount. addAccount which will add a new account in the branch.






this is my code



for q.1


Quote:











package assg3;



import java.text.DecimalFormat;



public class BankAccount {







/** BankAccount class, version 1

* Anderson, Franceschi

* Represents a generic bank account

*/



public final DecimalFormat MONEY = new DecimalFormat( "$#,##0.00" );





protected double balance;///private double balance;

protected String accountName;



/** Default constructor

* sets balance to 0.0

*/

public BankAccount( )

{

balance = 0.0;

accountName = "";

}









/** Overloaded constructor

* @param startBalance beginning balance

*



*/

public BankAccount( double startBalance,String newAccountName1 )

{

deposit( startBalance );

accountName = newAccountName1;

}



/** Accessor for balance

* @return current account balance

*/

public double getBalance( )

{

return balance;

}



public String getAccountName( )

{

return accountName;

}



//

public void setAccountName( String newAccountName)

{

accountName = newAccountName;

}

/** Deposit amount to account

* @param amount amount to deposit;

* amount must be >= 0.0

*/

public void deposit( double amount )

{

if ( amount >= 0.0 )

balance += amount;

else

System.err.println( "Deposit amount must be positive." );

}



/** withdraw amount from account

* @param amount amount to withdraw;

* amount must be >= 0.0

* amount must be <= balance

*/

public void withdraw( double amount )

{

if ( amount >= 0.0 && amount <= balance )

balance -= amount;

else

System.err.println( "Withdrawal amount must be positive "

+ "and cannot be greater than balance" );

}



/** toString

* @return the balance formatted as money

*

*

*

*

*/

public String toString( )

{

return ( "balance is " + MONEY.format( balance )+ " and accountName is "

+accountName);

}

}







for q.2

i have a problem with return value




Quote:









package ass3;

import java.util.ArrayList;

import java.util.Scanner;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

import sun.misc.Cleaner;



public class BranchBank extends BankAccount{//////eeeeeeeeeeeeeeeeextend in other classes



protected ArrayList<BankAccount>BranchBanklist;



private int IdBranch; //branch id



public BranchBank (){



super();

int id;

String name1;

double balance1;





// This class should contain the class constructor which reads from

// a file bank the branch id and the information

// about the accounts to be created in this branch



try

{

Scanner file = new Scanner( new File ( "bank.txt" ) );





while ( file.hasNext( ) ) // test for the end of the file

{







// read next integer

id = file.nextInt( );

IdBranch = id;

//read next string

name1 = file.next( );

//read next double

balance1 = file.nextDouble();





}



// release resources associated with dataFile.txt

file.close( );



BranchBanklist = new ArrayList<BankAccount>();

IdBranch = id;

}



catch ( FileNotFoundException fnfe )

{

System.out.println( "Unable to find dataFile.txt, exiting" );

}



catch ( IOException ioe )

{

ioe.printStackTrace( );

}







}





// This class should have the following

// methods: searchAccount which takes as a parameter an account name and returns

// the corresponding BankAccount.



//



public BankAccount searchAccount(String accountName){





try

{



BankAccount b;

String nameRead;

double balance11;

int id1;



Scanner file = new Scanner( new File ( "bank.txt" ) );





while ( file.hasNext( ) ) // test for the end of the file

{







// read next integer

id1 = file.nextInt( );

//read next string

nameRead = file.next( );

//read next double number

balance11= file.nextDouble();





if (accountName == nameRead)



b.accountName = nameRead;

b.balance = balance11;





return b;









}



// release resources associated with dataFile.txt

file.close( );

}



catch ( FileNotFoundException fnfe )

{

System.out.println( "Unable to find dataFile.txt, exiting" );

}



catch ( IOException ioe )

{

ioe.printStackTrace( );

}



return b;

}



///// check file or like this?



// addAccount : add a new account in the branch.

public void addAccount(BankAccount newAccount){



BranchBanklist.add(newAccount);



}



}








---------- Post added 05-05-12 at 07:38 PM ----------



the client prog i am still work on it.











2 comments: