patternjavaMinor
Calculating company-wise total expenses
Viewed 0 times
totalcompanywisecalculatingexpenses
Problem
I have a variable List. Its declaration is like :
Sample data in it:
[{total-expense=228, company=HiHard, year=2009}, {total-expense=2936, company=POTENT, year=2009}, {total-expense=1362412.65, company=HiHard, year=2010}, {total-expense=9007.96, company=POTENT, year=2010}, {total-expense=240427.43, company=HiHard, year=2011}, {total-expense=1956.11, company=POTENT, year=2011}]
I have written a program to fetch the below result:
{"HiHard":[[228,1362412.65,240427.43]],"POTENT":[[2936,9007.96,1956.11]]}
i.e. to fetch company-wise total expense incurred for financial years 2009, 2010, 2011.
Current program logic:
1) getDistinctCompanies from the list
2) Iterate through the distinct companies[from step 1] and get the expense incurred by passing in company and list.
Complete program:
```
public class JSONConstruction {
public static void main(String[] args) {
List> list = new ArrayList>();
... ASSUME list has the SAMPLE data as described before.
List companyList = getDistinctCompanies(list);
Iterator iterCompany = companyList.iterator();
String company;
JSONObject obj = new JSONObject();
while(iterCompany.hasNext()){
company = iterCompany.next();
JSONArray jsonList = new JSONArray();
jsonList.add(getExpenseList(list, company));
obj.put(company, jsonList);
}
System.out.print(obj);
}
private static List getDistinctCompanies(List> list) {
Iterator> iterList = list.iterator();
String company;
List companyList = new ArrayList();
while (iterList.hasNext()) {
HashMap map = iterList.next();
company = (String) map.get("company");
if(companyList==null || (!companyList.contains(company))) {
companyList.add(company);
}
}
return companyList;
}
private static List getExpenseList(List> list, String comp
List>Sample data in it:
[{total-expense=228, company=HiHard, year=2009}, {total-expense=2936, company=POTENT, year=2009}, {total-expense=1362412.65, company=HiHard, year=2010}, {total-expense=9007.96, company=POTENT, year=2010}, {total-expense=240427.43, company=HiHard, year=2011}, {total-expense=1956.11, company=POTENT, year=2011}]
I have written a program to fetch the below result:
{"HiHard":[[228,1362412.65,240427.43]],"POTENT":[[2936,9007.96,1956.11]]}
i.e. to fetch company-wise total expense incurred for financial years 2009, 2010, 2011.
Current program logic:
1) getDistinctCompanies from the list
2) Iterate through the distinct companies[from step 1] and get the expense incurred by passing in company and list.
Complete program:
```
public class JSONConstruction {
public static void main(String[] args) {
List> list = new ArrayList>();
... ASSUME list has the SAMPLE data as described before.
List companyList = getDistinctCompanies(list);
Iterator iterCompany = companyList.iterator();
String company;
JSONObject obj = new JSONObject();
while(iterCompany.hasNext()){
company = iterCompany.next();
JSONArray jsonList = new JSONArray();
jsonList.add(getExpenseList(list, company));
obj.put(company, jsonList);
}
System.out.print(obj);
}
private static List getDistinctCompanies(List> list) {
Iterator> iterList = list.iterator();
String company;
List companyList = new ArrayList();
while (iterList.hasNext()) {
HashMap map = iterList.next();
company = (String) map.get("company");
if(companyList==null || (!companyList.contains(company))) {
companyList.add(company);
}
}
return companyList;
}
private static List getExpenseList(List> list, String comp
Solution
private static List getDistinctCompanies(List> list) {
Iterator> iterList = list.iterator();
String company;
List companyList = new ArrayList();
while (iterList.hasNext()) {
HashMap map = iterList.next();
company = (String) map.get("company");
if(companyList==null || (!companyList.contains(company))) {
companyList.add(company);
}
}
return companyList;
}-
There is no need to check if
companyList==null because that won't ever happen because 4 lines prior you create the list. -
The scope of
map is defined to be inside the while loop, but company is defined outside whereas it is only used in the while loop as well. -
You should name your variables proper. Shortening names doesn't add any value. Instead of
iterList a simple iterator or listIterator would be much better. -
You should leave your variables and operators some space to breathe. This helps to make your code more readable.
companyList==null vs companyList == null.Code Snippets
private static List<String> getDistinctCompanies(List<HashMap<String, Object>> list) {
Iterator<HashMap<String, Object>> iterList = list.iterator();
String company;
List<String> companyList = new ArrayList<String>();
while (iterList.hasNext()) {
HashMap<String, Object> map = iterList.next();
company = (String) map.get("company");
if(companyList==null || (!companyList.contains(company))) {
companyList.add(company);
}
}
return companyList;
}Context
StackExchange Code Review Q#112311, answer score: 8
Revisions (0)
No revisions yet.