-
Java Arraylist help
Right I have an Arraylist for bank accounts which is casuing me to bang my head several times below is an example of the class BankAccount.
Code:
public class BankAccount
{
private Name accountName;
private Address accountAddress;
private int balance; // as pence
private static int accountNumberGenerator = 10000001;
private String accountNumber;
/**
* Constructor for BankAccount
* @param number account number
* @param name account name
*/
public BankAccount(String firstName, String lastName,
String addr1, String addr2, String addr3, String postcode, double openingBalance)
{
accountNumber = "" + accountNumberGenerator++;
accountName = new Name(firstName, lastName);
accountAddress = new Address (addr1, addr2, addr3, postcode);
balance = convertToPence(openingBalance);
}
// accessors (queries)
/**
* @return the account number
*/
public String getAccountNumber()
{
return accountNumber;
}
/**
* @return the account name
*/
public String getAccountName()
{
return accountName.getFullName();
}
/**
* @return the account address
*/
public String getAccountAddress()
{
return accountAddress.getFullAddress();
}
/**
* @return the balance in the account
*/
public String getBalance()
{
return penceToPounds(balance);
}
// mutators
/**
* sets new value(s) for account name
*
* @param firstName first name
* @param lastName last name
*/
public void changeAccountName(String firstName, String lastName)
{
accountName.setFirst(firstName);
accountName.setLast(lastName);
}
/**
* set new value(s) for account address
*
* @param addr1 first line of address
* @param addr2 second line of address
* @param addr3 third line of address or ""
* @param postcode the postcode
*/
public void changeAccountAddress(String addr1, String addr2, String addr3, String postcode)
{
accountAddress.setFullAddress(addr1, addr2, addr3, postcode);
}
/**
* adds an amount to the balance
* @param amount the amount to add to balance
*/
public void deposit(double amount)
{
balance = balance + convertToPence(amount);
}
/**
* withdraws a given amount from the account
* @param amount the amount to withdraw
* @return the new balance
*/
public void withdraw(double amount)
{
if (balance - convertToPence(amount) <= 0)
{
System.out.println("Transaction refused: Insufficient funds.");
}
else
{
balance = balance - convertToPence(amount);
}
}
// other methods
private int convertToPence(double amount)
{
return (int)(100*amount);
}
private String penceToPounds(int pence)
{
int pounds;
int remainingPence;
String result;
pounds = pence / 100;
remainingPence = pence % 100;
result = "£"+pounds+".";
if (remainingPence < 10)
{
result = result+"0"+remainingPence;
}
else
{
result = result+remainingPence;
}
return result;
}
/**
* Indicates whether some other object is "equal to" this one.
* Checks for matches of accountNumber
* @return true if a match found otherwise false
*/
public boolean equals(Object o)
{
if ( o == this )
return true;
BankAccount account = (BankAccount)o;
if ( (accountNumber.equals(account.getAccountNumber())))
return true;
return false;
}
/**
* @return all data values as a formatted string
*/
public String toString()
{
String output = accountName.getFullName() +"\n" + accountAddress.getFullAddress() + "\n"
+ "Account Number: " + accountNumber + "\tBalance: " + getBalance();
return output;
}
}
Below is the Arraylist class I am having problems with the RemoveAccount,makeDeposit and makeWithdrawl methods.
I want them to use the findAccount method to return the index of a particular account based on the accountNumber. then make the changes within that method.
Code:
import java.util.*;
public class BankAccountArrayList
{
private ArrayList < BankAccount > accounts;
/**
* Constructor for objects of class BankAccountArrayList
*/
public BankAccountArrayList()
{
accounts = new ArrayList < BankAccount >();
}
public void openBankAccount(String firstName, String lastName,
String addr1, String addr2, String addr3, String postcode, double openingBalance)
{
accounts.add(new BankAccount(firstName, lastName, addr1, addr2, addr3, postcode, openingBalance));
}
public void removeAccount(String accountNumber)
{
int index = 0;
index = findAccount( accountNumber);
accounts.remove(accountNumber);
}
public int getNumberOfBankAccounts()
{
return accounts.size();
}
public void printAccount(String accountNumber)
{
int index = 0;
index = findAccount(accountNumber);
}
public String toString()
{ String output = "";
for ( BankAccount acc : accounts )
output = output + acc +"\n";
return output;
}
public void makeDeposit(String accountNumber,double amount)
{
int index = 0;
index = findAccount(accountNumber);
}
public void makeWithdrawal(String accountNumber, double amount)
{
int index = 0;
index = findAccount(accountNumber);
}
public int findAccount(String accountNumber)
{
int index = 0;
for ( BankAccount bankAccount : accounts)
{
if( bankAccount.getAccountNumber().equals(accountNumber))
return index;
index++;
}
return index -1;
}
}
-
Let us know what the error you are getting or whats the deviation you are getting.Try to be more specific so that guys in here can help you.
What will this do .. accountNumber = "" + accountNumberGenerator++
Try creating two accounts and print the account numbers for each. Try being more specific.
Happy Coding ....
warm regards
Vinod M