Advertisement
Guest User

04. Orders

a guest
Nov 17th, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class AssArrays {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         Map<String, Double> map = new LinkedHashMap<>();
  10.  
  11.         Map<String, Double> testQuantity = new LinkedHashMap<>();
  12.  
  13.         String input = scanner.nextLine();
  14.  
  15.         while(!input.equals("buy")){
  16.  
  17.             String [] tokens =  input.split(" ");
  18.             String productName = tokens[0];
  19.             Double price = Double.parseDouble(tokens[1]);
  20.             Double quantity = Double.parseDouble(tokens[2]);
  21.             Double sum = price*quantity;
  22.  
  23.  
  24.             if(!map.containsKey(productName)){
  25.                 map.put(productName,sum);
  26.             }
  27.             else{
  28.                 double sumQauntity = testQuantity.get(productName) + quantity;
  29.  
  30.                     sum = sumQauntity * price;
  31.                     map.put(productName,sum);
  32.  
  33.             }
  34.  
  35.  
  36.             Double currentQuantity = quantity;
  37.             testQuantity.put(productName,currentQuantity);
  38.  
  39.  
  40.               input = scanner.nextLine();
  41.         }
  42.         map
  43.                 .forEach((key,value) -> System.out.printf("%s -> %.2f%n",key,value));
  44.  
  45.  
  46.  
  47.     }
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement