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