0

How does one return the int values in a situation like this?

import java.util.*;

    public class ShoppingCart {

    private Map<String, Purchase> shoppingCart = new HashMap<String, Purchase>();

    public void add (String product, int price) {
        Purchase purchase = new Purchase(product, 1, price);
        shoppingCart.put(product, purchase);
    }

    public int totalPrice() {
        //How do I accomplish this? I want to return all the prices summed together

        }
    }  
}

The constructor for the purchase method is:

 public Purchase(String product, int amount, int price) {
Mark
  • 57
  • 1
  • 2
  • 7
  • Not quite getting the question. Do you need the methods to be used or the algorithm for the same? – Chetter Hummin Mar 23 '12 at 11:54
  • Is this a homework assignment? If so, please tag it as such - otherwise people might do all the work for you. ;) – raveturned Mar 23 '12 at 12:00
  • Thanks for the help guys, got it now. I tried something similar before and understood the principle, but couldn't quite get it. – Mark Mar 23 '12 at 12:06

6 Answers6

2

You need to iterate over map values sum the total price

This code for instance will work.

 public int totalPrice() {
      int sum = 0;
       for(Purchase p:shoppingCart.values()){
            sum+=p.getPrice();
       }
       return sum;
  } 
narek.gevorgyan
  • 4,165
  • 5
  • 32
  • 52
1

You loop through the values in the Map (Javadoc). As this is most likely homework, I'll let you figure out the rest.

ftr
  • 2,105
  • 16
  • 29
0

Iterate through map and sum the price of each purchase

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
0
public int totalPrice() {
    int i = 0;
    for(Purchase purchase : shoppingCart.values()) {
      i += purchase.getPrice();
    }
    return i;
}
Kartoch
  • 7,610
  • 9
  • 40
  • 68
0
   Map<String, Purchase>  map = new HashMap<String, Purchase> ();

    for(String value : map.values()) {

    }
HamoriZ
  • 2,370
  • 18
  • 38
0

In below code, you do not need to run for loop again...
import java.util.*;

    public class ShoppingCart {

    private int sum = 0;    

    private Map<String, Purchase> shoppingCart = new HashMap<String, Purchase>();

    public void add (String product, int price) {
        Purchase purchase = new Purchase(product, 1, price);
        if(!shoppingCart.contains(product)){
             shoppingCart.put(product, purchase);
             sum += price;
        }
    }

    public int totalPrice() {
        return price;
        }
    }  
}
kandarp
  • 4,979
  • 11
  • 34
  • 43
  • in map, key must be unique. And if you add product second time in the hashmap, it will overwrite existing value with new value.. And I have updated the code, if any product already added in the hashmap, it will not be added second time – kandarp Mar 23 '12 at 12:11
  • Indeed, you will override the amount of the price and add the new price to the sum, but you won't remove the old one. EDIT: I don't think that's a good design either. If the caller is not allowed to add the same product twice, I'd throw an exception. The way it is now, the code pretends that an add-call was successful when nothing actually happened. – ftr Mar 23 '12 at 12:14
  • if any product is already added in the hashmap, it will not be added in the hashmap again as well as price will also not added in the total sum. I have put ! in if condition.. – kandarp Mar 23 '12 at 12:17