0

This makes my brain hurt...
I'd like to add multiple variables such as date and time to an arraylist, and add that arraylist to another arraylist holding different variables such as name and age.

Example: An ArrayList holds multiple accounts. Each account has an id, name, balance and transaction history. Each transaction has an id, date, time and transaction amount.

So the accounts ArrayList would contain: (int | String | double | ArrayList),
and the transactions ArrayList would contain: (int | Date | Time | double).

I'd appreciate it if you could help me understand how to do this, or offer a better solution.

EDIT:
I can't really show all my code because I'm using a model view controller which accesses a model and 4 views. I can try to describe what it does though.

Click "Create Account" button:

  1. make a new account (using Account class)
  2. set accountID = length of accounts ArrayList
  3. set accountName = textbox input
  4. set accountBalance = 0
  5. create transactions ArrayList (using Transactions class)
  6. add all to the accounts ArrayList.

Click "Deposit" button:

  1. add TextBox input to accountBalance
  2. set transactionID = length of transactions ArrayList
  3. set transactionDate = current date
  4. set transactionTime = current time
  5. add all to selected account's transactions ArrayList
Joseph Webber
  • 2,010
  • 1
  • 19
  • 38

2 Answers2

7

Do not use only lists for this, use concrete classes, i.e:

class Account {
    String id;
    String name;
    float balance;
    List<Transaction> history;
}

class Transaction {
    String id;
    Date date;
    double amount;
}

And finally have a Map with your relevant data:

Map<Account, List<Transaction>> data = new HashMap<Account, List<Transaction>>();
epoch
  • 16,396
  • 4
  • 43
  • 71
  • why would you recommend a `Map` over just an `ArrayList`? – supersam654 Apr 22 '13 at 14:12
  • @supersam654, to me it seems like OP wants to associate Transaction(s) with Account(s), and a map is perfect for that. – epoch Apr 22 '13 at 14:14
  • I prefer to use `id` instead `Account` as key in `Map`. Because `Account` contains `Transaction` therefore you have duplicate data. – Maxim Shoustin Apr 22 '13 at 14:14
  • @Epoch Yes, but the Account object already stores that information. I would have to agree with @Maxim and use `id` or `name`. I know the OP didn't ask for key associations, but they seem useful. – supersam654 Apr 22 '13 at 14:16
  • Oh, I interpreted the history as completely different to the Transaction association, I guess any way other than `List>>` would work :) – epoch Apr 22 '13 at 14:21
  • I'm not familiar with using Lists or Maps. I'd just like it so when I click the add account button on my view it adds a new account to my accountlist, and when I deposit or withdraw from that account it adds an account transaction to the transaction arraylist associated to that account. – Joseph Webber Apr 22 '13 at 14:55
0

Create an Account Class and a Transaction class. The Account class will hava as an attribute a list of transaction and when creating a List just declare it as following:

List<Account> myAccounts = new ArrayList<Account>();

and when you want to add an account just write :

myAccounts.add(new Account());
javadev
  • 1,639
  • 2
  • 17
  • 35