Advertisement
Guest User

Orders

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