damesova

Orders [Mimi]

Mar 29th, 2019
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.37 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class _04_Orders {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         Map<String, ArrayList<Double>> productsByPriceAndQ = new LinkedHashMap<>();
  8.         List<Double> values = new ArrayList<>();
  9.  
  10.         String item = "";
  11.         String input = "";
  12.  
  13.         while (!"buy".equals(input = scanner.nextLine())) {
  14.             String[] line = input.split("\\s+");
  15.  
  16.             item = line[0];
  17.             double price = Double.parseDouble(line[1]);
  18.             double q = Double.parseDouble(line[2]);
  19.  
  20.             productsByPriceAndQ.putIfAbsent(item, new ArrayList<>());
  21.             values = productsByPriceAndQ.get(item);
  22.             if (values.isEmpty()) {
  23.                 values.add(price);
  24.                 values.add(q);
  25.             } else {
  26.                 if (price != productsByPriceAndQ.get(item).get(0)) {
  27.                     values.set(0, Double.parseDouble(line[1]));
  28.                     values.set(1, productsByPriceAndQ.get(item).get(1)+q);
  29.                 }
  30.             }
  31.         }
  32.  
  33.         productsByPriceAndQ.entrySet()
  34.                 .stream()
  35.                 .forEach(entry -> {
  36.                     System.out.println(String.format("%s -> %.2f",
  37.                             entry.getKey(), entry.getValue().get(0) * entry.getValue().get(1)));
  38.                 });
  39.     }
  40. }
Add Comment
Please, Sign In to add comment