Results 1 to 4 of 4
- 11-01-2011, 05:55 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 10
- Rep Power
- 0
how am i supposed to store 3 types of data in first ArrayList
I'm coding a bank account program and I've been doing for so long.
I got an instruction from my prof but i don't get it.
here is the instuction.
New data and methods for the CheckingAccount Class
---------------------------------------------------------------------------
private ArrayList<Transaction> transList; // keeps a list of Transaction objects for the account
private int transCount; // the count of Transaction objects and used as the ID for each transaction
public void addTrans( Transaction newTrans) // adds a transaction object to the transList
public int gettransCount() //returns the current value of transCount;
public Transaction getTrans(int i) // returns the i-th Transaction object in the list
--------------------------------------------------------------------------
New Transaction classJava Code:import java.util.ArrayList; public class checkingAccount{ private double balance = 0; private double totalServiceCharge = 0; public checkingAccount(double initialBalance){ balance = initialBalance; } public double getBalance(){ return balance; } public void setBalance(double transAmt, int tCode){ if(tCode == 1) balance -= transAmt; else //if(tCode == 2) balance += transAmt; } public double getServiceCharge(){ return totalServiceCharge; } public void setServiceCharge(double currentServiceCharge){ totalServiceCharge += currentServiceCharge; } }
Java Code:public class Transaction { private int transNumber; private int transId; private double transAmt; public Transaction(int number, int id, double amount) { transNumber = number; transId = id; transAmt = amount; } public int getTransNumber() { return transNumber; } public int getTransId() { return transId; } public double getTransAmount() { return transAmt; } }
- 11-01-2011, 06:08 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: how am i supposed to store 3 types of data in first ArrayList
And your question is....?
- 11-01-2011, 07:27 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 83
- Rep Power
- 0
Re: how am i supposed to store 3 types of data in first ArrayList
I'm not entirely sure what you're asking, but if you want to store multiple types of data in an ArrayList, that sounds like polymorphism to me. As a set of different types of bank accounts is a frequently used example for teaching inheritance/polymorphism, I'm guessing you want an ArrayList containing 3 different types of bank accounts that extend a single BankAccount class? If that's the case, you can simply create an object of type ArrayList<BankAccount> and add instances of any of the 3 subclasses to said ArrayList and it will work.
I realize that was rather vague, but so was your question...
- 11-01-2011, 07:58 PM #4
Member
- Join Date
- Oct 2011
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
Extract data from file and store into separate ArrayList
By pawarkomal in forum New To JavaReplies: 4Last Post: 01-11-2011, 03:31 PM -
How to Store ArrayList into an Object?
By TheGame in forum New To JavaReplies: 6Last Post: 10-10-2010, 10:38 PM -
Comparing two data types in order to store in array
By gwithey in forum New To JavaReplies: 3Last Post: 05-01-2009, 10:27 AM -
Using arrayList to store data from one class to another.
By nickc88 in forum New To JavaReplies: 3Last Post: 03-28-2009, 05:02 AM -
Can I use vectors to store multiple types of objects
By Nathand in forum Advanced JavaReplies: 6Last Post: 04-28-2008, 07:55 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks