Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class MapsLambdaStreamAPI {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- Map<String, ArrayList<Double>> orders = new LinkedHashMap<>();
- while(!input.equals("buy")) {
- String[] data = input.split("\\s+");
- String product = data[0];
- double price = Double.parseDouble(data[1]);
- Double quantity = Double.parseDouble(data[2]);
- if (!orders.containsKey(product)) {
- orders.put(product, new ArrayList<>());
- orders.get(product).add(price);
- orders.get(product).add(quantity);
- } else {
- orders.get(product).remove(0);
- orders.get(product).add(0, price);
- orders.get(product).add(orders.get(product).get(1) + quantity);
- orders.get(product).remove(1);
- }
- input = scanner.nextLine();
- }
- Map<String, Double> finalPrice = new LinkedHashMap<>();
- orders.entrySet()
- .forEach(e -> {
- double priceQuantity = e.getValue().get(0)
- * e.getValue().get(1);
- finalPrice.put(e.getKey(), priceQuantity);
- });
- finalPrice.entrySet()
- .forEach(e -> {
- System.out.println(String.format("%s -> %.2f",
- e.getKey(), e.getValue()));
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement