Didart

Orders - Associative Arrays

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