Results 1 to 2 of 2
Thread: Java Arraylist help
- 12-01-2010, 10:56 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 1
- Rep Power
- 0
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.
Java 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.
Java 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; } }
- 12-02-2010, 06:31 AM #2
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 MLast edited by Vinod Mukundan; 12-02-2010 at 06:35 AM.
_______________________________________________
give me beans .........
Similar Threads
-
how to add Arraylist filter for a jsp page showing results from a servlet-Arraylist
By alok_sharma in forum Java ServletReplies: 7Last Post: 11-22-2010, 01:26 PM -
Java ArrayList : Generics & Sorting
By Eranga in forum Java TutorialReplies: 0Last Post: 10-19-2010, 04:43 AM -
java collection arraylist in jdk 1.4
By jagdish_ in forum Advanced JavaReplies: 1Last Post: 06-12-2010, 02:19 PM -
Java ArrayList out of bounds exception
By grahamb314 in forum New To JavaReplies: 5Last Post: 11-22-2008, 07:21 PM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks