Welcome to Dream.In.Code
Become a Java Expert!

Join 149,853 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,351 people online right now. Registration is fast and FREE... Join Now!




ArrayLists program for a bank program

 
Reply to this topicStart new topic

ArrayLists program for a bank program

maviegoes
17 Feb, 2007 - 09:29 PM
Post #1

New D.I.C Head
*

Joined: 24 Sep, 2006
Posts: 3


My Contributions
I am working on a program that has three main classes: one for savings, checking (these two extend a CommonAccount class), and then a bank manager that uses an array list to control the number of accounts that come and go into the system.
The problem is that the account number for each account is supposed to be a String value. It is required to be. In the BankManager class, the parameters for each method contain the account number as a String in order to access the array in the list with that account number. Yet, i don't know why it is that way and how to access an object at a specific spot if the index I need is a string. My array list is of type CommonAccount.
I know i'm supposed to post code, but I feel this is more of a conceptual question than a problem with the code itself.

also, I did try using the Integer class to attempt to change the string value to an int, but it threw an exception at me, so I'm assuming that's not allowed.
User is offlineProfile CardPM
+Quote Post

Programmist
RE: ArrayLists Program For A Bank Program
17 Feb, 2007 - 09:57 PM
Post #2

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,253



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
One of the reasons we ask you to post code is because it's often easier to understand what your talking about than trying to decipher statements like this:

QUOTE
In the BankManager class, the parameters for each method contain the account number as a String in order to access the array in the list with that account number...

I can only guess at what you're trying to say here.

QUOTE
Yet, i don't know why it is that way and how to access an object at a specific spot if the index I need is a string. My array list is of type CommonAccount.

It would be helpful to know what the CommonAccount class is.

You're so confident that it's a "conceptual problem" that you don't post your code or even what kind of exception your getting. You are, however, wrong. It's almost certainly a problem with your code. If you tried Integer.parseInt( String s ) and got a NumberFormatException, then you didn't have a String value in there that could be converted to an integer. Set a break point before you run that method and check the value of the String your passing it. Does it have a space or some non-numerical character?

Frankly, I would not use an ArrayList in this situation, though. A Map (HashMap, TreeMap, etc.) is a much better data stricture if you've got unique keys like account numbers that map to values/objects.

Or, you could post some code snippets and I can stop guessing.
User is offlineProfile CardPM
+Quote Post

maviegoes
RE: ArrayLists Program For A Bank Program
17 Feb, 2007 - 10:37 PM
Post #3

New D.I.C Head
*

Joined: 24 Sep, 2006
Posts: 3


My Contributions
Sorry to be unclear, I just assumed code would almost make it less clear. But you are right, so here is some code:

Here is the skeleton code for CommonAccount:
[public class CommonAccount {

private long value; //in cents
private Date startDate;
private String owner;
private String accountNumber;

//the constructor
public CommonAccount(long startValue, String name, String number, Date date) {
this.value = startValue;
this.owner = name;
this.accountNumber = number;
this.startDate = date;
}

//***method bodies have been removed since the code for these two classes is fine***
// makes a deposit
public boolean deposit(long value);
//withdrawals money from the account
public boolean withdrawal(long value);
//get the amount of money in the account
public long getValue();
//get the name of the person who owns the account
public String getAccountName();
// get the account number of the account
public String getAccountNumber();
//the date the account was opened
public Date getStartDate();
//a method that doesn't do anything for checking, but computes interest for savings
public void endOfDay();
// a typical equals method
public boolean equals(Object other);
// clone method
public CommonAccount clone();

}]


from this base class the classes SavingsAccount and CheckingAccount extend. Savings account has an extra instance variable of "interestRate" which helps compute the final value of the end of the day for the endOfDay() method.

The class i'm having problems with though is the BankManager class. Here is the skeleton code for it:

[public class BankManager {

private ArrayList<CommonAccount> accountList;
private Date date

//constructor here to initialize variables

public Date getCurrentDate();
public int getNumberOfAccounts();

public String addCheckingAccount(String name, long initialValue);
public String addSavingsAccount(String name, long initialValue, double rate);
public long removeAccount(String accountNumber);

public AccountInterface lookupAccountByNumber(String accountNumber);
public AccountInterface[] lookupAccountByName(String name);
public String getAccountInformation(String accountNumber);
public long getAccountValue(String accountNumber);

public void endOfDay();
public boolean deposit(String accountNumber, long value);
public boolean withdrawal(String accountNumber, long value);

]

the method bodies aren't important for my question, i hope. When I am writing the method bodies for removeAccount(), getAccountInformation(), or getAccountValue() I don't understand why the parameter is a String for the account number. Or, where to pass that parameter into. I am assuming it's supposed to represent the index of an object in my arraylist, but how would I use a string for an index?
User is offlineProfile CardPM
+Quote Post

sreedharsanni
RE: ArrayLists Program For A Bank Program
20 Feb, 2007 - 09:51 AM
Post #4

New D.I.C Head
Group Icon

Joined: 23 Jan, 2007
Posts: 20


Dream Kudos: 100
My Contributions
I guess AccountNumber can have alpha numerics and that is the reason for it being a String. And as it is a String you can not use it as an index while accessing arrayList.

BankManager class deals with only accounts and the handle here is accountNumber and this is a field in CommonAccount objects of arraylist so your method body should span thru the arraylist to identify an account with desired account#(parameter) and then do the required operation in the method.

User is offlineProfile CardPM
+Quote Post

Programmist
RE: ArrayLists Program For A Bank Program
20 Feb, 2007 - 12:14 PM
Post #5

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,253



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
The ideal data structure for storing values/objects with unique identifiers (e.g. accounts and account numbers) is a Map. Java has several implementations of this data structure. Here's an example:

CODE

Map<CommonAccount> map = new HashMap<CommonAccount>();

// pretend we construct two objects
// acct1 has account number "12345XYZ"
// and acct2 has account number "6789ABC"
CommonAccount acct1 ...
CommonAccount acct2 ...

// add accounts and their unique keys to the map
// put( key, value )
map.put( acct1.getAccountNumber, acct1 );
map.put( acct2.getAccountNumber, acct2 );

// say a request is made for account "12345XYZ"
// if this map contains that key the get method will return the account associated with it
if ( map.containsKey("12345XYZ") ) {
   CommonAccount acct3 = map.get("12345XYZ");
}

At this point acct1 and acct3 are references to the same object. Look at the Java API for HashMap and see if it suits your needs.
http://java.sun.com/j2se/1.5.0/docs/api/ja...il/HashMap.html

This post has been edited by alcdotcom: 20 Feb, 2007 - 12:16 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:26AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month