My application needs to read a file, which includes a list of cars from different factories, and calculate the prices (by this I mean mean add up their prices).
I suppose my solution is efficient. Is there any better approach to it?
Company Price other details Benz 10000 x ... BMW 3000 y ... Benz 1000 z ... Toyota 500 w ...
In this case the result should be
Benz 11000 BMW 3000 Toyota 500
Solution
public static final void main(String[] args) {
List<Company> trn = new ArrayList();
Company t = new Company();
t.setName("Benz");
t.setPrice(10000);
trn.add(t);
t = null;
t = new Company();
t.setName("BMW");
t.setPrice(3000);
trn.add(t);
t = null;
t = new Company();
t.setName("Benz");
t.setPrice(1000);
trn.add(t);
t = null;
t = new Company();
t.setName("Toyota");
t.setPrice(500);
trn.add(t);
for (Company c : trn) {
System.err.println(c.toString());
}
Map<String, String> map = new HashMap();
int counter = 0;
for (Company c : trn) {
String name = c.getName();
if (!map.containsKey(name)) {
counter = 0;
for (Company com : trn) {
if (com.getName().equalsIgnoreCase(name)) {
counter += com.getPrice();
}
}
map.put(name, String.valueOf(counter));
}
}
System.err.println(map.get("Benz"));
System.err.println(map.get("BMW"));
System.err.println(map.get("Toyota"));
}
}
Output
Company{name=Benz, price=10000} Company{name=BMW, price=3000} Company{name=Benz, price=1000} Company{name=Toyota, price=500} 11000 3000 500
-
1\$\begingroup\$ sorry but I have rollbacked your latest revision as your code has changed significantly. I think it'll be better if you can mark this answered and then ask a new question with the new code... \$\endgroup\$h.j.k.– h.j.k.2014年07月16日 04:05:57 +00:00Commented Jul 16, 2014 at 4:05
-
\$\begingroup\$ @h.j.k. sorry I am newbie the address of new question is codereview.stackexchange.com/questions/57157/… \$\endgroup\$Jack– Jack2014年07月16日 04:24:30 +00:00Commented Jul 16, 2014 at 4:24
-
\$\begingroup\$ It's all right, glad to help, and hope you'll stay! :) \$\endgroup\$h.j.k.– h.j.k.2014年07月16日 05:41:56 +00:00Commented Jul 16, 2014 at 5:41
-
\$\begingroup\$ Follow-up question \$\endgroup\$200_success– 200_success2014年07月16日 11:39:38 +00:00Commented Jul 16, 2014 at 11:39
1 Answer 1
Your ArrayList
and HashMap
declarations are missing type arguments. Assuming if you're developing on Java 7 then all you're missing is just the diamond operator, e.g. ArrayList<>()
.
Company t = new Company();
t.setName("Benz");
t.setPrice(10000);
trn.add(t);
t = null;
t = new Company();
t.setName("BMW");
t.setPrice(3000);
trn.add(t);
t = null;
t = new Company();
t.setName("Benz");
t.setPrice(1000);
trn.add(t);
t = null;
t = new Company();
t.setName("Toyota");
t.setPrice(500);
trn.add(t);
This is an odd way of adding four objects into your List
. Assuming if your Company
object has the following simple constructor, you should do it as such:
trn.add(new Company("Benz", 10000));
trn.add(new Company("BMW", 3000));
trn.add(new Company("Benz", 1000));
trn.add(new Company("Toyota", 500));
Instead of a nested for
loop and skipping entries that already exist in the result map
, you can consider retrieving the existing calculated sum from the map
, if any, and add the current price to that. Also, why is map
created as Map<String, String>
and not Map<String, Integer>
or Map<Company, Integer>
?
One more thing, you can consider putting this particular comparison as a method in your Company
class: com.getName().equalsIgnoreCase(name)
.
-
\$\begingroup\$ actually I need to change the type of price to BigDecimal to accept the monetary values. Question is updated \$\endgroup\$Jack– Jack2014年07月16日 03:56:01 +00:00Commented Jul 16, 2014 at 3:56
-
\$\begingroup\$ @Jack: Did you just now figure that out? You've just invalidated this answer by modifying the code greatly. \$\endgroup\$Jamal– Jamal2014年07月16日 03:58:32 +00:00Commented Jul 16, 2014 at 3:58
-
\$\begingroup\$ @Jamal sorry I am newbie, the address of new question is codereview.stackexchange.com/questions/57157/… \$\endgroup\$Jack– Jack2014年07月16日 04:27:55 +00:00Commented Jul 16, 2014 at 4:27