Advertisement
svephoto

Orders

Nov 17th, 2019
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. import java.util.LinkedHashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4.  
  5. public class Orders {
  6. public static void main(String[] args) {
  7.  
  8. Scanner scanner = new Scanner(System.in);
  9.  
  10. Map<String, Double> map = new LinkedHashMap<>();
  11. Map<String, Integer> testQuantity = new LinkedHashMap<>();
  12.  
  13. String input = scanner.nextLine();
  14.  
  15. while (!input.equals("buy")) {
  16. String[] tokens = input.split(" ");
  17.  
  18. String productName = tokens[0];
  19. double price = Double.parseDouble(tokens[1]);
  20. int quantity = Integer.parseInt(tokens[2]);
  21. double sum = price * quantity;
  22.  
  23. if (!map.containsKey(productName)) {
  24. map.put(productName, sum);
  25. testQuantity.put(productName, quantity);
  26. } else {
  27. testQuantity.put(productName, testQuantity.get(productName) + quantity);
  28. map.put(productName, testQuantity.get(productName) * price);
  29. }
  30.  
  31. input = scanner.nextLine();
  32. }
  33.  
  34. map.forEach((key, value) -> System.out.printf("%s -> %.2f%n", key, value));
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement