Advertisement
BojidarDosev

Maps_ex3

Mar 2nd, 2025
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class exercises_Map2 {
  4. public static void main(String[] args) {
  5. Scanner scan = new Scanner(System.in);
  6.  
  7. Map<String, Double> products = new LinkedHashMap<>();
  8. Map<String, Integer> productsQuanity = new LinkedHashMap<>();
  9.  
  10. while(true){
  11. List<String> prodInfo = Arrays.asList(scan.nextLine().split(" "));
  12. if(prodInfo.contains("buy")){
  13. break;
  14. }
  15.  
  16. String prodName = prodInfo.get(0);
  17. double price = Double.parseDouble(prodInfo.get(1));
  18. int quantity = Integer.parseInt(prodInfo.get(2));
  19.  
  20. if(!products.containsKey(prodName)){
  21. productsQuanity.put(prodName, quantity);
  22. products.put(prodName,price*quantity);
  23. }
  24. else{
  25. int totalQuantityTillNow = productsQuanity.get(prodName);
  26. int nextQuantity = totalQuantityTillNow + quantity;
  27. products.put(prodName, price*nextQuantity);
  28. productsQuanity.put(prodName,nextQuantity);
  29. }
  30. }
  31.  
  32. for (Map.Entry<String, Double> entry : products.entrySet()) {
  33. System.out.printf("%s -> %.2f%n" , entry.getKey(), entry.getValue());
  34. }
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement