Advertisement
petur_stoqnov

Java

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