Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedHashMap;
- import java.util.Scanner;
- import java.util.concurrent.atomic.AtomicInteger;
- public class kur {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- LinkedHashMap<String, LinkedHashMap<Double,Integer>> list = new LinkedHashMap<>();
- String input = "";
- AtomicInteger oldQuantity = new AtomicInteger();
- while(!"buy".equals(input = sc.nextLine())){
- String[] data = input.split(" ");
- String product = data[0];
- double price = Double.parseDouble(data[1]);
- int quantity = Integer.parseInt(data[2]);
- if (list.containsKey(product)) {
- list.get(product).entrySet().forEach(e -> {
- oldQuantity.set(e.getValue());
- });
- list.get(product).remove(price);
- list.put(product, new LinkedHashMap<>());
- list.get(product).put(price, oldQuantity.get() + quantity);
- }else {
- list.putIfAbsent(product, new LinkedHashMap<>());
- list.get(product).put(price, quantity);
- }
- }
- list.entrySet().forEach(pair -> {
- System.out.print(pair.getKey() + " -> ");
- pair.getValue().entrySet().forEach(subentry -> {
- System.out.printf("%.2f\n", subentry.getValue() * subentry.getKey());;
- });
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement