Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

java - Mapping to pojo total from list of lists

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From what I see you are trying to obtain the customer balance of all accounts they own.

public static void calculateAccountTotal(){
    for (Customer c : CUSTOMERS) {
        double total = 0;
        for (Account a : c.getAccounts()) {
            total += a.getAccountBalance();
        }
        c.setTotal(total);
    }
}

Storing the account balance in account would make your account object to hold wrong data. A better option the balance at customer level.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...