Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class ProductInformation {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // Използваме LinkedHashMap да запазим продуктите по ред на въвеждане
- Map<String, Double> prices = new LinkedHashMap<>();
- Map<String, Integer> quantities = new LinkedHashMap<>();
- String input = scanner.nextLine();
- while (!input.equals("buy")) {
- String[] tokens = input.split(" ");
- String productName = tokens[0];
- double price = Double.parseDouble(tokens[1]);
- int quantity = Integer.parseInt(tokens[2]);
- // Използваме merge method за да ъпдейтнем цените и количествата
- prices.merge(productName, price, (oldPrice, newPrice) -> newPrice);
- quantities.merge(productName, quantity, Integer::sum);
- input = scanner.nextLine();
- }
- // Ползваме forEach loop за да принтираме общите цени на продуктите
- prices.forEach((productName, price) -> {
- double totalPrice = price * quantities.get(productName);
- System.out.printf("%s -> %.2f%n", productName, totalPrice);
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement