Advertisement
Tsuki11

Order's

Mar 8th, 2020
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Exercise_4_Orders {
  4.     public static void main(String[] args) {
  5.         Scanner scan = new Scanner(System.in);
  6.  
  7.         Map<String, ArrayList<Double>> products = new LinkedHashMap<>();
  8.  
  9.         String input = scan.nextLine();
  10.         while (!"buy".equals(input)) {
  11.             String[] tokens = input.split("\\s+");
  12.             String name = tokens[0];
  13.             double price = Double.parseDouble(tokens[1]);
  14.             double quantity = Double.parseDouble(tokens[2]);
  15.  
  16.             if (products.containsKey(name)) {
  17.                 products.get(name).set(0, price);
  18.                 double currQuantity = products.get(name).get(1);
  19.                 products.get(name).set(1, quantity + currQuantity);
  20.                 input = scan.nextLine();
  21.                 continue;
  22.             }
  23.  
  24.             products.putIfAbsent(name, new ArrayList<>());
  25.             products.get(name).add(price);
  26.             products.get(name).add(quantity);
  27.  
  28.  
  29.             input = scan.nextLine();
  30.         }
  31.  
  32.         products
  33.                 .forEach((key, value) -> System.out.printf("%s -> %.2f%n", key, value.get(0) * value.get(1)));
  34.  
  35.      
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement