Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class _04_Orders {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Map<String, ArrayList<Double>> productsByPriceAndQ = new LinkedHashMap<>();
- List<Double> values = new ArrayList<>();
- String item = "";
- String input = "";
- while (!"buy".equals(input = scanner.nextLine())) {
- String[] line = input.split("\\s+");
- item = line[0];
- double price = Double.parseDouble(line[1]);
- double q = Double.parseDouble(line[2]);
- productsByPriceAndQ.putIfAbsent(item, new ArrayList<>());
- values = productsByPriceAndQ.get(item);
- if (values.isEmpty()) {
- values.add(price);
- values.add(q);
- } else {
- if (price != productsByPriceAndQ.get(item).get(0)) {
- values.set(0, Double.parseDouble(line[1]));
- values.set(1, productsByPriceAndQ.get(item).get(1)+q);
- }
- }
- }
- productsByPriceAndQ.entrySet()
- .stream()
- .forEach(entry -> {
- System.out.println(String.format("%s -> %.2f",
- entry.getKey(), entry.getValue().get(0) * entry.getValue().get(1)));
- });
- }
- }
Add Comment
Please, Sign In to add comment