Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.*;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import java.util.stream.Collectors;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Map<String, Double> productAndPrice = new LinkedHashMap<>();
- Map<String, Integer> productAndCount = new LinkedHashMap<>();
- String input = "";
- while (!"buy".equals(input = scanner.nextLine())){
- String[] data = input.split(" ");
- String product;
- Double price;
- int count;
- product = data[0];
- price = Double.parseDouble(data[1]);
- count = Integer.parseInt(data[2]);
- if (!productAndCount.containsKey(product)){
- productAndCount.put(product,count);
- productAndPrice.put(product,price);
- }else {
- productAndCount.put(product,productAndCount.get(product) + count);
- productAndPrice.put(product,price);
- }
- }
- productAndCount.entrySet().forEach(e -> {
- String product = e.getKey();
- int count =(e.getValue());
- double price = productAndPrice.get(product);
- double totalPrice = count * price;
- System.out.printf("%s -> %.2f\n",product,totalPrice);
- });
- }
- }
Add Comment
Please, Sign In to add comment