I need to process my array list and calculate the total that is take from each account balance.
I can't have another data structure as per homework.
ArrayList<Customer> = {
{ Forename= Blue, Surname= Red, CustomerId= 2119954221, Age= 98, Accounts= [Account Name = Current Account, Account ID = 1, Accounts Balance = 0.0, Account total = 0.0, Account Name = Current Account, Account ID = 2, Accounts Balance = 0.0, Account total = 0.0] }
{ Forename= Orange, Surname= Yellow, CustomerId= 1448877171, Age= 32, Accounts= [Account Name = Current Account, Account ID = 3, Account Balance = 10000.0, Account total = 10000.0, Account Name = Current Account, Account ID = 4, Account Balance = -100.0, Account total = -100.0, Account Name = Savings Account, Account ID = 26634, Account Balance = 10.0, Account total = 10.0] }
...................
}
POJO's
class Account {
private final String accountName;
private final int accountId;
private final double accountBalance;
private double accountTotal;
}
class Customer {
private final String forename;
private final String surname;
private final long customerId;
private final int age;
private List<Account> accounts;
}
First though was to loop over the list and
public static void calculateAccountTotal(){
double total = 0;
for (Customer c : CUSTOMERS) {
for (Account a : c.getAccounts()) {
total += a.getAccountBalance();
a.setTotal(total);
}
}
}
This does not work as the previous total is added to the next customer.
A potential solution would be to add an accountKey to the Customer and Account, so I can have a constant in both lists, to check if they match and calculate the total.
Maybe that would stop the addition of balance to the next customer.
I don't think that is a go, tho.
Thank you for reading.
question from:
https://stackoverflow.com/questions/65877807/mapping-to-pojo-total-from-list-of-lists 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…