Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Exercise_4_Orders {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- Map<String, ArrayList<Double>> products = new LinkedHashMap<>();
- String input = scan.nextLine();
- while (!"buy".equals(input)) {
- String[] tokens = input.split("\\s+");
- String name = tokens[0];
- double price = Double.parseDouble(tokens[1]);
- double quantity = Double.parseDouble(tokens[2]);
- if (products.containsKey(name)) {
- products.get(name).set(0, price);
- double currQuantity = products.get(name).get(1);
- products.get(name).set(1, quantity + currQuantity);
- input = scan.nextLine();
- continue;
- }
- products.putIfAbsent(name, new ArrayList<>());
- products.get(name).add(price);
- products.get(name).add(quantity);
- input = scan.nextLine();
- }
- products
- .forEach((key, value) -> System.out.printf("%s -> %.2f%n", key, value.get(0) * value.get(1)));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement