Advertisement
Guest User

Untitled

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