Advertisement
i_graurov

Orders

Mar 17th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1.  
  2. import java.util.LinkedHashMap;
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class orders {
  7. public static void main(String[] args) {
  8. Scanner scanner = new Scanner(System.in);
  9. LinkedHashMap<String, Double> products = new LinkedHashMap<>();
  10. LinkedHashMap<String, Double> current = new LinkedHashMap<>();
  11. String[] input = scanner.nextLine().split(" ");
  12. double totalQuantity = 0;
  13.  
  14.  
  15. while (!input[0].equals("Buy")) {
  16. String type = input[0];
  17. Double price = Double.parseDouble(input[1]);
  18. Double quantity = Double.parseDouble(input[2]);
  19. double finalPrice = price*quantity;
  20. if (products.containsKey(type)){
  21. totalQuantity = current.get(type) + quantity;
  22. finalPrice = totalQuantity*price;
  23. products.put(type,finalPrice);
  24. } else {
  25. current.put(type, quantity);
  26. products.put(type, finalPrice);
  27.  
  28. }
  29.  
  30. input = scanner.nextLine().split(" ");
  31. }
  32. products
  33. .entrySet()
  34. .stream()
  35. .forEach(e -> System.out.printf("%s -> %.2f%n", e.getKey(),e.getValue()));
  36.  
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement