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:
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;
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.
Quote:
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.
Code :
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?
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 ...
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.
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.
Re: Using the ArrayList Class
Quote:
My output is correct except for the last line
What should the last line look like? Are you talking about this: []
Re: Using the ArrayList Class
Instead of showing "[]", the last line should display an array that shows all the transactions.
Re: Using the ArrayList Class
Where do you add any elements to the transactions ArrayList before you print it?
Your code:
Code :
java.util.ArrayList transactions = new java.util.ArrayList(); // create
System.out.println(transactions.toString()); // prints an empty list: []
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().
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:
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;
}
}
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()
Re: Using the ArrayList Class
Thanks. I already emailed the program to my professor so it's to late to make that change now.