1

I have the following class:

public class Transaction {
public String Type;
public double Amount;
public double Balance;

Transaction(String Type,double Amount,double Balance){
    this.Type = Type;
    this.Amount = Amount;
    this.Balance = Balance;
}
public String toString(){
    String s = " Type: "+ Type +"\n Amount: "+ Amount+ "\n Balance: "+Balance;
    return s;
}

This is used to create instance of transactions with my main class so I can eventually print out all transactions in a long list like a statement.

In my main class Account I have this code so far:

public class Account {
private String name;
private double balance;
public double initDeposit;
protected ArrayList<Transaction>;


public Account(String name, double initDeposit){
    this.balance =initDeposit;
    this.name = name;
    Transaction a = new Transaction("Creation",initDeposit,balance);


}

I am trying to create a new transaction when a account is made and add it to the ArrayList but I am not declaring the array list properly. How could I do that? Thank you for any replies.

4
  • 10
    protected ArrayList<Transaction> giveMeAName; Commented Feb 10, 2014 at 21:09
  • 1
    You need a variable name, Commented Feb 10, 2014 at 21:09
  • Ah how silly of me. I then add the new transaction a with giveMeAName.add(a);. THank you Commented Feb 10, 2014 at 21:09
  • @Softey How this differs from private String name;? This is the same thing. Commented Feb 10, 2014 at 21:10

2 Answers 2

8

You forgot to name the ArrayList. Try this:

protected ArrayList<Transaction> transactions;

And following OO programming best practices, you should declare the attribute using an interface type, rather than a concrete class:

protected List<Transaction> transactions;

Also don't forget to instantiate the attribute in the constructor:

transactions = new ArrayList<Transaction>();

Or even simpler, if you're using Java 7 or newer:

transactions = new ArrayList<>();
Sign up to request clarification or add additional context in comments.

5 Comments

"if you're using Java 6 or newer". Java 7.
@ZouZou I'm future-proofing my answer :P
You might want to consider changing the declaration to: protected List<Transaction> transactions - using the List interface rather than the specific implementation but the rest of @ÓscarLópez suggestions are dead on.
@mikemil why is it better to declare the ArrayList as a List instead? I know the alternative isn't wrong but I'm wondering if you could send me a link explaining why this is better.
@asaini007 Specifying List as the 'declared' type is more generic than specifyign the concrete implementation type, like ArrayList. Any method that needs to work with the transactions will have a parameter type of ArrayList because that is the type of transactions. Whereas, if you declare as List, then any type of list could be used. That list of transactions could have been a LinkedList or some other type. The idea is to keep the type more generic. It's not wrong as it was stated. See this link for more. stackoverflow.com/questions/2279030/…
0

After commens from ZouZou:

public class Account {
private String name;
private double balance;
public double initDeposit;
protected ArrayList<Transaction> tran;


public Account(String name, double initDeposit){
    this.balance =initDeposit;
    this.name = name;
    tran = new ArrayList<Transaction>()
    Transaction a = new Transaction("Creation",initDeposit,balance);
    tran.add(a);

I now have a name that I can add transactions to. Once this is done I am able to then loop through the array list to get all transactions.

1 Comment

You forgot to initialize the ArrayList<Transaction> tran. In your code as is, it will be initialized with null and you'll have a NullPointerException when creating an instance of Account.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.