3
\$\begingroup\$

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
h.j.k.
19.3k3 gold badges37 silver badges93 bronze badges
asked Jul 16, 2014 at 2:08
\$\endgroup\$
4
  • 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\$ Commented 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\$ Commented Jul 16, 2014 at 4:24
  • \$\begingroup\$ It's all right, glad to help, and hope you'll stay! :) \$\endgroup\$ Commented Jul 16, 2014 at 5:41
  • \$\begingroup\$ Follow-up question \$\endgroup\$ Commented Jul 16, 2014 at 11:39

1 Answer 1

1
\$\begingroup\$

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).

answered Jul 16, 2014 at 3:05
\$\endgroup\$
3
  • \$\begingroup\$ actually I need to change the type of price to BigDecimal to accept the monetary values. Question is updated \$\endgroup\$ Commented 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\$ Commented Jul 16, 2014 at 3:58
  • \$\begingroup\$ @Jamal sorry I am newbie, the address of new question is codereview.stackexchange.com/questions/57157/… \$\endgroup\$ Commented Jul 16, 2014 at 4:27

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.