Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedHashMap;
- import java.util.Map;
- import java.util.Scanner;
- public class Orders {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Map<String, Double> productsPrice = new LinkedHashMap<>();
- Map<String, Integer> productsQuantity = new LinkedHashMap<>();
- String command = scanner.nextLine();
- while (!command.equals("buy")) {
- String product = command.split(" ")[0];
- double pricePerProduct = Double.parseDouble(command.split(" ")[1]);
- int quantity = Integer.parseInt(command.split(" ")[2]);
- productsPrice.put(product, pricePerProduct);
- if (!productsQuantity.containsKey(product)) {
- productsQuantity.put(product, quantity);
- } else {
- productsQuantity.put(product, productsQuantity.get(product) + quantity);
- }
- command = scanner.nextLine();
- }
- for (Map.Entry<String, Double> entry : productsPrice.entrySet()) {
- String productName = entry.getKey();
- double finalSum = entry.getValue() * productsQuantity.get(productName);
- System.out.printf("%s -> %.2f%n", productName, finalSum);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment