Bank account program...help me please!
807598Dec 9 2005 — edited Aug 29 2008Hi everyone,
I want to create a class to model a bank account. I have created to classes: Account and TestAccount.
Users are allowed to specify the number of Account objects to be managed. I have used an int variable to store the number of Account objects to be created. The problem is when I compile and run the program, it won't work as the way I want.
This is Account class
public class Account
{
private String accountID;
private String accountName;
private double accountBalance;
public Account(String accID, String name, double amount)
{
accountID = accID;
accountName = name;
accountBalance = amount;
}
public double getBalance()
{
return accountBalance;
}
public String getID()
{
return accountID;
}
public String getName()
{
return accountName;
}
public void deposit(double amountdeposit)
{
accountBalance += amountdeposit;
}
public boolean withdraw(double amountwithdraw)
{
if (accountBalance >= amountwithdraw)
{
accountBalance -= amountwithdraw;
return true;
}
else
return false;
}
public boolean transfer(Account accounttransfer, double amounttransfer)
{
if (withdraw(amounttransfer))
{
accounttransfer.deposit(amounttransfer);
return true;
}
else
return false;
}
}
This is the TestAccount class
import java.util.StringTokenizer;
public class TestAccount
{
public static ConsoleReader console = new ConsoleReader(System.in);
//get the user's input until it's valid
public static int getNumberOfAcc()
{
boolean success = false;
int result = 0;
do
{
int numberOfAccounts = 0;
int[] numOfAccnts = new int[numberOfAccounts];
System.out.print("Please specify the number of Account objects to be managed: ");
numberOfAccounts = console.readInt();
if (numberOfAccounts%1 == 0 && numberOfAccounts>0)
{
success = (result == numberOfAccounts);
}
if (!success)
{
System.out.println("\nInvalid value, please re-enter!\n");
}
}while(!success);
return result;
}
//store the Account objects created in an array
public static String getUserInput()
{
boolean success = false;
String result = null;
int accounts = getNumberOfAcc();
int[] account = new int[accounts];
String accValues;
Account[] accountArray = new Account[accounts];
do
{
int i;
for (i = 0; i < accounts; i++)
{
System.out.println("Enter ID, name, and balance of account number " + (i + 1) + "separated by commas.");
accValues = console.readLine();
String delimiters = ", ";
StringTokenizer word = new StringTokenizer(accValues, delimiters);
String ID = null, name = null;
double balance = 0;
if (word.countTokens() > 0)
{
ID = word.nextToken();
name = word.nextToken();
balance = Double.parseDouble(word.nextToken());
Account newAccount = new Account(ID,name,balance);
accountArray[i] = newAccount;
result = result + ID + name + balance + " ";
success = true;
}
if (!success)
{
System.out.println("Fatal Error.");
}
}
}while (!success);
return result;
}
//*****************************************************************************
//main method
public static void main(String[] args)
{
System.out.println("\n\t\t******This program is written by Linda Trang******\n\n");
String display = getUserInput();
}
}
/////////////////////////////////////////////////////////////////////////////////////
Could anyone help me please? This is very urgent. Please!!!!!! Thank you so much if you could help me.