Results 1 to 4 of 4
  1. #1
    wndtjr2 is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default 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

    --------------------------------------------------------------------------


    Java 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;  
    	}
    }
    New Transaction class

    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;
        }
    }

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: how am i supposed to store 3 types of data in first ArrayList

    And your question is....?

  3. #3
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default 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...

  4. #4
    wndtjr2 is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: how am i supposed to store 3 types of data in first ArrayList

    I'm sorry the question was so vague.
    But what DiamondSoul guessed was I wanted to ask.
    Thank you it was helpful for me

Similar Threads

  1. Replies: 4
    Last Post: 01-11-2011, 03:31 PM
  2. How to Store ArrayList into an Object?
    By TheGame in forum New To Java
    Replies: 6
    Last Post: 10-10-2010, 10:38 PM
  3. Comparing two data types in order to store in array
    By gwithey in forum New To Java
    Replies: 3
    Last Post: 05-01-2009, 10:27 AM
  4. Replies: 3
    Last Post: 03-28-2009, 05:02 AM
  5. Can I use vectors to store multiple types of objects
    By Nathand in forum Advanced Java
    Replies: 6
    Last Post: 04-28-2008, 07:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •