ilianrusev

Maps, Lambda and Stream API - Exercise - Orders

Mar 19th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.42 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.*;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6. import java.util.stream.Collectors;
  7.  
  8. public class Main {
  9.  
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.  
  13.         Map<String, Double> productAndPrice = new LinkedHashMap<>();
  14.         Map<String, Integer> productAndCount = new LinkedHashMap<>();
  15.  
  16.  
  17.         String input = "";
  18.  
  19.         while (!"buy".equals(input = scanner.nextLine())){
  20.             String[] data = input.split(" ");
  21.             String product;
  22.             Double price;
  23.             int count;
  24.  
  25.             product = data[0];
  26.             price = Double.parseDouble(data[1]);
  27.             count = Integer.parseInt(data[2]);
  28.  
  29.             if (!productAndCount.containsKey(product)){
  30.                 productAndCount.put(product,count);
  31.                 productAndPrice.put(product,price);
  32.             }else {
  33.                 productAndCount.put(product,productAndCount.get(product) + count);
  34.                 productAndPrice.put(product,price);
  35.             }
  36.  
  37.         }
  38.  
  39.         productAndCount.entrySet().forEach(e -> {
  40.             String product = e.getKey();
  41.             int count =(e.getValue());
  42.             double price = productAndPrice.get(product);
  43.             double totalPrice = count * price;
  44.             System.out.printf("%s -> %.2f\n",product,totalPrice);
  45.         });
  46.  
  47.  
  48.     }
  49. }
Add Comment
Please, Sign In to add comment