Advertisement
meteor4o

JF-Maps-Exercise-04.Orders

Jul 14th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.LinkedHashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Scanner;
  7.  
  8. public class Orders {
  9.     public static void main(String[] args) {
  10.         Scanner sc = new Scanner(System.in);
  11.  
  12.         LinkedHashMap<String, Integer> productsQuantity = new LinkedHashMap<>();
  13.         LinkedHashMap<String, Double> productsPrice = new LinkedHashMap<>();
  14.  
  15.         String input = sc.nextLine();
  16.  
  17.         while (!input.equals("buy")) {
  18.  
  19.             String[] tokens = input.split("\\s+");
  20.             String product = tokens[0];
  21.             double price = Double.parseDouble(tokens[1]);
  22.             int quantity = Integer.parseInt(tokens[2]);
  23.  
  24.             if (!productsQuantity.containsKey(product)) {
  25.                 productsQuantity.put(product, quantity);
  26.                 productsPrice.put(product, price);
  27.             } else {
  28.                 int currQuantity = productsQuantity.get(product);
  29.                 productsQuantity.put(product, currQuantity + quantity);
  30.  
  31.                 if (productsPrice.get(product) - price !=0) {
  32.                     productsPrice.put(product, price);
  33.                 }
  34.             }
  35.  
  36.             input= sc.nextLine();
  37.         }
  38.         for (Map.Entry<String, Integer> entry : productsQuantity.entrySet()) {
  39.             double price = productsPrice.get(entry.getKey());
  40.             System.out.printf("%s -> %.2f%n", entry.getKey(), entry.getValue() * price );
  41.  
  42.         }
  43.  
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement