patternjavaMinor
Family finances - tracking and reporting
Viewed 0 times
trackingreportingfinancesandfamily
Problem
I am looking for clean way to accumulate amounts within a input dataset. In this example, there are 18 rows of data, and the output is a 3 rows of data, accumulated by Key, by ExpenseType.
My biggest concern is whether hashMap is the correct utility for this, or if there is an easier/cleaner way.
FamilyExpenseProcessor.java:
```
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
public class FamilyExpenseProcessor {
enum ExpenseType {
Grocery, Entertainment,Transportation
}
public static void main(String[] args) {
Map feHashMap = new HashMap<>();
accumulate(feHashMap, "Jeffersons",ExpenseType.Entertainment, new BigDecimal(5.00));
accumulate(feHashMap, "Jetsons", ExpenseType.Entertainment, new BigDecimal(10.00));
accumulate(feHashMap, "Johnsons", ExpenseType.Entertainment, new BigDecimal(15.00));
accumulate(feHashMap, "Jeffersons",ExpenseType.Entertainment, new BigDecimal(20.00));
accumulate(feHashMap, "Jetsons", ExpenseType.Entertainment, new BigDecimal(25.00));
accumulate(feHashMap, "Johnsons", ExpenseType.Entertainment, new BigDecimal(30.00));
accumulate(feHashMap, "Jeffersons",ExpenseType.Grocery, new BigDecimal(35.00));
accumulate(feHashMap, "Jetsons", ExpenseType.Grocery, new BigDecimal(40.00));
accumulate(feHashMap, "Johnsons", ExpenseType.Grocery, new BigDecimal(45.00));
accumulate(feHashMap, "Jeffersons",ExpenseType.Grocery, new BigDecimal(50.00));
accumulate(feHashMap, "Jetsons", ExpenseType.Grocery, new BigDecimal(55.00));
accumulate(feHashMap, "Johnsons", ExpenseType.Grocery, new BigDecimal(60.00));
accumulate(feHashMap, "Jeffersons",ExpenseType.Transportation, new BigDecimal(15.00));
accumulate(feHashMap, "Jetsons", ExpenseType.Transportation, new BigDecimal(20.00));
accumulate(feHashMap, "Johnsons", ExpenseType.Tran
My biggest concern is whether hashMap is the correct utility for this, or if there is an easier/cleaner way.
FamilyExpenseProcessor.java:
```
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
public class FamilyExpenseProcessor {
enum ExpenseType {
Grocery, Entertainment,Transportation
}
public static void main(String[] args) {
Map feHashMap = new HashMap<>();
accumulate(feHashMap, "Jeffersons",ExpenseType.Entertainment, new BigDecimal(5.00));
accumulate(feHashMap, "Jetsons", ExpenseType.Entertainment, new BigDecimal(10.00));
accumulate(feHashMap, "Johnsons", ExpenseType.Entertainment, new BigDecimal(15.00));
accumulate(feHashMap, "Jeffersons",ExpenseType.Entertainment, new BigDecimal(20.00));
accumulate(feHashMap, "Jetsons", ExpenseType.Entertainment, new BigDecimal(25.00));
accumulate(feHashMap, "Johnsons", ExpenseType.Entertainment, new BigDecimal(30.00));
accumulate(feHashMap, "Jeffersons",ExpenseType.Grocery, new BigDecimal(35.00));
accumulate(feHashMap, "Jetsons", ExpenseType.Grocery, new BigDecimal(40.00));
accumulate(feHashMap, "Johnsons", ExpenseType.Grocery, new BigDecimal(45.00));
accumulate(feHashMap, "Jeffersons",ExpenseType.Grocery, new BigDecimal(50.00));
accumulate(feHashMap, "Jetsons", ExpenseType.Grocery, new BigDecimal(55.00));
accumulate(feHashMap, "Johnsons", ExpenseType.Grocery, new BigDecimal(60.00));
accumulate(feHashMap, "Jeffersons",ExpenseType.Transportation, new BigDecimal(15.00));
accumulate(feHashMap, "Jetsons", ExpenseType.Transportation, new BigDecimal(20.00));
accumulate(feHashMap, "Johnsons", ExpenseType.Tran
Solution
I would suggest adding a method
You can also iterate through the entries using
addExpense(ExpenseType expenseType, BigDecimal amount) to FamilyExpense. That way your accumulate method could look like this:private static void accumulate(Map expenses, String key, ExpenseType expenseType, BigDecimal amount) {
if (expenses.containsKey(key)) {
expenses.get(key).addExpense(expenseType, amount);
return;
}
FamilyExpense expense = new FamilyExpense();
expense.addExpense(expenseType, amount);
expenses.put(key, expense);
}You can also iterate through the entries using
entrySet:for (Map.Entry entry : entries.entrySet()) {
...
}Code Snippets
private static void accumulate(Map<String, FamilyExpense> expenses, String key, ExpenseType expenseType, BigDecimal amount) {
if (expenses.containsKey(key)) {
expenses.get(key).addExpense(expenseType, amount);
return;
}
FamilyExpense expense = new FamilyExpense();
expense.addExpense(expenseType, amount);
expenses.put(key, expense);
}for (Map.Entry<String, FamilyExpense> entry : entries.entrySet()) {
...
}Context
StackExchange Code Review Q#59759, answer score: 2
Revisions (0)
No revisions yet.