Advertisement
CR7CR7

orders-LLHashMap

Nov 26th, 2023
849
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.23 KB | None | 0 0
  1. import java.util.*;
  2. public class ProductInformation {
  3. public static void main(String[] args) {
  4. Scanner scanner = new Scanner(System.in);
  5. // Използваме LinkedHashMap да запазим продуктите по ред на въвеждане
  6. Map<String, Double> prices = new LinkedHashMap<>();
  7. Map<String, Integer> quantities = new LinkedHashMap<>();
  8.     String input = scanner.nextLine();
  9.     while (!input.equals("buy")) {
  10.         String[] tokens = input.split(" ");
  11.         String productName = tokens[0];
  12.         double price = Double.parseDouble(tokens[1]);
  13.         int quantity = Integer.parseInt(tokens[2]);
  14.  
  15.         // Използваме  merge method за да ъпдейтнем цените и количествата
  16.         prices.merge(productName, price, (oldPrice, newPrice) -> newPrice);
  17.         quantities.merge(productName, quantity, Integer::sum);
  18.  
  19.         input = scanner.nextLine();
  20.     }
  21.  
  22.     // Ползваме  forEach loop за да принтираме общите цени на продуктите
  23.     prices.forEach((productName, price) -> {
  24.         double totalPrice = price * quantities.get(productName);
  25.         System.out.printf("%s -> %.2f%n", productName, totalPrice);
  26.     });
  27. }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement