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