Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Using the ArrayList Class

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Cool Using the ArrayList Class

    This problem wants me to use an account class from another problem and add stuff to it.

    Design a new Account class as follows:
    • Add a new data field name of the String type to store the name of the customer.
    • Add a new constructor that constructs an account with the specified name, id, and balance.
    • Add a new data field named transactions whose type is ArrayList that stores the transaction for the accounts. Each transaction is an instance of the Transaction class.
    • Modify the withdraw and deposit methods to add a transaction to the transactions array list.

    Write a test program that creates an Account with annual interest rate 1.5%, balance 1000, id 1122, and name George. Deposit $30, $40, $50 to the account and withdraw $5, $4, $2 from the account. Print an account summary that shows account holder name, interest rate, balance, and all transactions.

    Here is what I have so far:
    import java.util.Date;
    import java.util.ArrayList;
     
    public class NewAccountClass {
    	public static void main(String[] args) {
    //create an instance  object of class Account
    		Account myAccount = new Account(0.015, 1000.00, 1122, "George");
    		myAccount.deposit(30.00);
    		myAccount.deposit(40.00);
    		myAccount.deposit(50.00);
    		myAccount.withdraw(5.00);
    		myAccount.withdraw(4.00);
    		myAccount.withdraw(2.00);
    //display account holder name, interest rate, balance and all transactions
    		System.out.println("Account Holder Name: " + myAccount.name);
    		System.out.println("Monthly Interest: " + myAccount.getMonthlyInterestRate());
    		System.out.println("Balance: " + myAccount.balance);
    		java.util.ArrayList transactions = new java.util.ArrayList();
    		System.out.println(transactions.toString());
    	}
    }
     
    class Account {
    //define var1, var2
    	int id;
    	double balance;
    	double annualInterestRate;
    	Date dateCreated;
    	String name;
    	ArrayList transactions;
    //transactions.add(amount);
    //no arg constructer
    	Account() {
    		id = 0;
    		balance = 0.0;
    		annualInterestRate = 0.0;
    	}
    //constructor with specific id and initial balance
    	Account(int newId, double newBalance) {
    		id = newId;
    		balance = newBalance;
    	}
    	Account(int newId, double newBalance, double newAnnualInterestRate) {
    		id = newId;
    		balance = newBalance;
    		annualInterestRate = newAnnualInterestRate;
    	}
    	Account(String newName, int newId, double newBalance) {
    		name = newName;
    		id = newId;
    		balance = newBalance;
    	}
    	Account(double newAnnualInterestRate, double newBalance, int newId, String newName) {
    		annualInterestRate = newAnnualInterestRate;
    		balance = newBalance;
    		id = newId;
    		name = newName;
    	}
    //accessor/mutator methods for id, balance, and annualInterestRate
    	public int getId() {
    		return id;
    	}
    	public double getBalance() {
    		return balance;
    	}
    	public double getAnnualInterestRate() {
    		return annualInterestRate;
    	}
    	public void setId(int newId) {
    		id = newId;
    	}
    	public void setBalance(double newBalance) {
    		balance = newBalance;
    	}
    	public void setAnnualInterestRate(double newAnnualInterestRate) {
    		annualInterestRate = newAnnualInterestRate;
    	}
    //accessor method for dateCreated
    	public void setDateCreated(Date newDateCreated) {
    		dateCreated = newDateCreated;
    	}
    //define method getMonthlyInterestRate
    	double getMonthlyInterestRate() {
    		return annualInterestRate/12;
    	}
    //define method withdraw
    	double withdraw(double amount) {
    		return balance -= amount;
    	}	
    //define method deposit
    	double deposit(double amount) {
    		return balance += amount;
    	}
    }
    class Transaction {
    //define variables
    	Date dateTransaction;
    	char type;
    	double amount;
    	double balance;
    	String description;
    //constructor with specific date, type, balance, and description
    	Transaction(Date newDateTransaction, char newType, double newBalance, String newDescription) {
    		dateTransaction = newDateTransaction;
    		type = newType;
    		balance = newBalance;
    		description = newDescription;
    	}	
    }

    This is my output.
    Account Holder Name: George
    Monthly Interest: 0.00125
    Balance: 1109.0
    []
    My code compiles. My output is correct except for the last line. The problem with my code is storing each transaction. I know I need to put this somewhere in my code but I don't know where.
    transactions.add(amount);
    Another problem is my withdraw and deposit methods. I don't know how to modify them to add a transaction to the transactions array list. How would I do this?
    Last edited by m2msucks; November 20th, 2011 at 02:30 AM.


  2. #2
    Junior Member
    Join Date
    Nov 2011
    Location
    Budapest, Hungary
    Posts
    10
    My Mood
    Mellow
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Using the ArrayList Class

    Firstly, you need to make sure that the transaction arraylist in your Account class is set to a new arraylist somewhere. Otherwise you've just got an empty reference that could refer to an array list but doesn't.

    ArrayList transactions = new ArrayList();
    or, better still: ArrayList<Transaction> transactions = new ArrayList<Transaction>();

    Secondly, you need to make sure dateCreated is set to a new Date object somewhere. Again, you have a reference that goes nowhere, but it is needed by the constructor of Transaction. Change it to

    Date dateCreated = new Date();

    (This will set today's date by default)

    -- or else use your setDateCreated().

    Now you can create new Transaction objects in your withdraw and deposit methods and add them to the transaction array.

    Take withdraw as an example. You need to get rid of that unhealthy return balance -= amount; Split this code up into two steps.

    First decrement the balance.

    Then create a new transaction and add it to the transaction ArrayList.
    Transaction transaction = new Transaction(dateCreated, 'w', balance, "withdrawal made");
    transactions.add(transaction);

    Then return the balance.

    Finally, at the end you need a method to get the transactions array list from your Account class. When you've got it, iterate over it with a 'for' loop. For each transaction, you can print out some details about it.

    The easiest thing here is if you give Transaction a toString() method (look this up on Google!). Then you can just use System.out.println() to print each transaction.

    Hope that helps ...
    Cave of Programming -- programming info and help, exercises, tutorials and other stuff.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Using the ArrayList Class

    I made the changes to my code however I'm still getting the same output as before. I'm not sure why.

    Here's my revised code.
    import java.util.Date;
    import java.util.ArrayList;
     
    public class NewAccountClass {
    	public static void main(String[] args) {
    //create an instance  object of class Account
    		Account myAccount = new Account(0.015, 1000.00, 1122, "George");
    		myAccount.deposit(30.00);
    		myAccount.deposit(40.00);
    		myAccount.deposit(50.00);
    		myAccount.withdraw(5.00);
    		myAccount.withdraw(4.00);
    		myAccount.withdraw(2.00);
    //display account holder name, interest rate, balance and all transactions
    		System.out.println("Account Holder Name: " + myAccount.name);
    		System.out.println("Monthly Interest: " + myAccount.getMonthlyInterestRate());
    		System.out.println("Balance: " + myAccount.balance);
    		java.util.ArrayList transactions = new java.util.ArrayList();
    		System.out.println(transactions.toString());
    	}
    }
     
    class Account {
    //define var1, var2
    	int id;
    	double balance;
    	double annualInterestRate;
    	Date dateCreated = new Date();
    	String name;
    	ArrayList transactions = new ArrayList();
    //no arg constructer
    	Account() {
    		id = 0;
    		balance = 0.0;
    		annualInterestRate = 0.0;
    	}
    //constructor with specific id and initial balance
    	Account(int newId, double newBalance) {
    		id = newId;
    		balance = newBalance;
    	}
    	Account(int newId, double newBalance, double newAnnualInterestRate) {
    		id = newId;
    		balance = newBalance;
    		annualInterestRate = newAnnualInterestRate;
    	}
    	Account(String newName, int newId, double newBalance) {
    		name = newName;
    		id = newId;
    		balance = newBalance;
    	}
    	Account(double newAnnualInterestRate, double newBalance, int newId, String newName) {
    		annualInterestRate = newAnnualInterestRate;
    		balance = newBalance;
    		id = newId;
    		name = newName;
    	}
    //accessor/mutator methods for id, balance, and annualInterestRate
    	public int getId() {
    		return id;
    	}
    	public double getBalance() {
    		return balance;
    	}
    	public double getAnnualInterestRate() {
    		return annualInterestRate;
    	}
    	public void setId(int newId) {
    		id = newId;
    	}
    	public void setBalance(double newBalance) {
    		balance = newBalance;
    	}
    	public void setAnnualInterestRate(double newAnnualInterestRate) {
    		annualInterestRate = newAnnualInterestRate;
    	}
    //accessor method for dateCreated
    	public void setDateCreated(Date newDateCreated) {
    		dateCreated = newDateCreated;
    	}
    //define method getMonthlyInterestRate
    	double getMonthlyInterestRate() {
    		return annualInterestRate/12;
    	}
    //define method withdraw
    	double withdraw(double amount) { 
    		balance -= amount;
    		Transaction transaction = new Transaction(dateCreated, 'W', balance, "withdrawal made");
    		transactions.add(transaction);
    		return balance;	
    	}	
    //define method deposit
    	double deposit(double amount) {
    		balance += amount;
    		Transaction transaction = new Transaction(dateCreated, 'D', balance, "deposit made");
    		transactions.add(transaction);
    		return balance;
    	}
    }
    class Transaction {
    //define variables
    	Date dateTransaction;
    	char type;
    	double amount;
    	double balance;
    	String description;
    	ArrayList transactions = new ArrayList();
    //constructor with specific date, type, balance, and description
    	Transaction(Date newDateTransaction, char newType, double newBalance, String newDescription) {
    		dateTransaction = newDateTransaction;
    		type = newType;
    		balance = newBalance;
    		description = newDescription;
    	}		
    	public String toString() {	
    		for (int i = 0; i < transactions.size(); i++) {
    			System.out.println(transactions.get(i));
    		}
    		return "Transactions: " + transactions;
    	}
    }

    What else am I doing wrong? And thanks for your help.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Using the ArrayList Class

    My output is correct except for the last line
    What should the last line look like? Are you talking about this: []

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Using the ArrayList Class

    Instead of showing "[]", the last line should display an array that shows all the transactions.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Using the ArrayList Class

    Where do you add any elements to the transactions ArrayList before you print it?
    Your code:
        java.util.ArrayList transactions = new java.util.ArrayList();  // create 
        System.out.println(transactions.toString());  // prints an empty list: []

  7. The Following User Says Thank You to Norm For This Useful Post:

    m2msucks (November 20th, 2011)

  8. #7
    Junior Member
    Join Date
    Nov 2011
    Location
    Budapest, Hungary
    Posts
    10
    My Mood
    Mellow
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Using the ArrayList Class

    I think your Account class looks OK. But you need a getTransactions() method that returns the ArrayList of transactions.

    The Transaction class does not need a list of transactions though. In your toString() method, you just need to return a string that is meaningful for that particular single transaction. To start with you could just return dateTransaction.toString() or whatever.

    Then in your main method, at the moment you're creating a new arraylist (as Norm points out). Instead of that, use getTransactions() on your Account to get the transaction list from Account. Then loop through that list and do System.out.println(transactions.get(i)); for each transaction in the list (for instance), like you're doing at the moment, wrongly, in toString().
    Cave of Programming -- programming info and help, exercises, tutorials and other stuff.

  9. The Following User Says Thank You to Squiffy For This Useful Post:

    m2msucks (November 20th, 2011)

  10. #8
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Using the ArrayList Class

    @Norm I didn't realize that. Thanks.

    @Squiffy Thanks. I made those changes to my code and got the right output.

    Here's the final code:
    import java.util.Date;
    import java.util.ArrayList;
     
    public class NewAccountClass {
    	public static void main(String[] args) {
    //create an instance  object of class Account
    		Account myAccount = new Account(0.015, 1000.00, 1122, "George");
    		myAccount.deposit(30.00);
    		myAccount.deposit(40.00);
    		myAccount.deposit(50.00);
    		myAccount.withdraw(5.00);
    		myAccount.withdraw(4.00);
    		myAccount.withdraw(2.00);
    //display account holder name, interest rate, balance and all transactions
    		System.out.println("Account Holder Name: " + myAccount.name);
    		System.out.println("Monthly Interest: " + myAccount.getMonthlyInterestRate());
    		System.out.println("Balance: " + myAccount.balance);
    		myAccount.getTransactions();
    	}
    }
     
    class Account {
    //define var1, var2
    	int id;
    	double balance;
    	double annualInterestRate;
    	Date dateCreated = new Date();
    	String name;
    	ArrayList transactions = new ArrayList();
    //no arg constructer
    	Account() {
    		id = 0;
    		balance = 0.0;
    		annualInterestRate = 0.0;
    	}
    //constructor with specific id and initial balance
    	Account(int newId, double newBalance) {
    		id = newId;
    		balance = newBalance;
    	}
    	Account(int newId, double newBalance, double newAnnualInterestRate) {
    		id = newId;
    		balance = newBalance;
    		annualInterestRate = newAnnualInterestRate;
    	}
    	Account(String newName, int newId, double newBalance) {
    		name = newName;
    		id = newId;
    		balance = newBalance;
    	}
    	Account(double newAnnualInterestRate, double newBalance, int newId, String newName) {
    		annualInterestRate = newAnnualInterestRate;
    		balance = newBalance;
    		id = newId;
    		name = newName;
    	}
    //accessor/mutator methods for id, balance, and annualInterestRate
    	public int getId() {
    		return id;
    	}
    	public double getBalance() {
    		return balance;
    	}
    	public double getAnnualInterestRate() {
    		return annualInterestRate;
    	}
    	public void setId(int newId) {
    		id = newId;
    	}
    	public void setBalance(double newBalance) {
    		balance = newBalance;
    	}
    	public void setAnnualInterestRate(double newAnnualInterestRate) {
    		annualInterestRate = newAnnualInterestRate;
    	}
    //accessor method for dateCreated
    	public void setDateCreated(Date newDateCreated) {
    		dateCreated = newDateCreated;
    	}
    //define method getMonthlyInterestRate
    	double getMonthlyInterestRate() {
    		return annualInterestRate/12;
    	}
    //define method withdraw
    	double withdraw(double amount) { 
    		balance -= amount;
    		Transaction transaction = new Transaction(dateCreated, 'W', balance, "withdrawal made");
    		transactions.add(transaction);
    		return balance;	
    	}	
    //define method deposit
    	double deposit(double amount) {
    		balance += amount;
    		Transaction transaction = new Transaction(dateCreated, 'D', balance, "deposit made");
    		transactions.add(transaction);
    		return balance;
    	}
    //define getTransaction method
    	ArrayList getTransactions() {
    		for (int i = 0; i < transactions.size(); i++) {
    			System.out.println(transactions.get(i));
    		}
    		return transactions;
    	}
    }
    class Transaction {
    //define variables
    	Date dateTransaction;
    	char type;
    	double amount;
    	double balance;
    	String description;
    	ArrayList transactions = new ArrayList();
    //constructor with specific date, type, balance, and description
    	Transaction(Date newDateTransaction, char newType, double newBalance, String newDescription) {
    		dateTransaction = newDateTransaction;
    		type = newType;
    		balance = newBalance;
    		description = newDescription;
    	}		
    	public String toString() {	
    		return dateTransaction.toString() + " " + type + " " + balance + " " + description;
    	}
    }

  11. #9
    Junior Member
    Join Date
    Nov 2011
    Location
    Budapest, Hungary
    Posts
    10
    My Mood
    Mellow
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Using the ArrayList Class

    Hey nice work. But I'd suggest one small change. Your getTransactions() method should simply "return transactions", nothing else.

    In your main, then you can do: ArrayList transactions = account.getTransactions().
    Then your for loop (which is currently in getTransactions()) can go in main()
    Cave of Programming -- programming info and help, exercises, tutorials and other stuff.

  12. #10
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Using the ArrayList Class

    Thanks. I already emailed the program to my professor so it's to late to make that change now.

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. How to use this 2d ArrayList class?
    By J05HYYY in forum Object Oriented Programming
    Replies: 14
    Last Post: January 19th, 2011, 01:48 PM
  3. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  4. [SOLVED] Passing arrayList inside class
    By KrisTheSavage in forum Collections and Generics
    Replies: 1
    Last Post: March 27th, 2010, 12:45 PM
  5. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 17th, 2009, 01:12 PM