Advertisement
desislava_topuzakova

04. Orders

Mar 7th, 2021
2,366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.56 KB | None | 0 0
  1. import com.sun.security.jgss.GSSUtil;
  2.  
  3. import java.util.*;
  4.  
  5. public class demo {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         String input = scanner.nextLine();
  9.         //продукт -> обща цена (бр * ед. цена)
  10.         Map<String, Double> productsPrice = new LinkedHashMap<>(); //продукт -> ед. цена
  11.         Map<String, Integer> productsQuantity = new LinkedHashMap<>(); //продукт -> брой
  12.  
  13.         while(!input.equals("buy")) {
  14.             //"Beer 2.20 100" -> split(" ") -> ["Beer", "2.20", "100"]
  15.             String product = input.split(" ")[0];
  16.             double pricePerProduct = Double.parseDouble(input.split(" ")[1]);
  17.             int quantity = Integer.parseInt(input.split(" ")[2]);
  18.  
  19.             productsPrice.put(product, pricePerProduct);
  20.  
  21.             if(!productsQuantity.containsKey(product)) {
  22.                 productsQuantity.put(product, quantity);
  23.             } else {
  24.                 productsQuantity.put(product, productsQuantity.get(product) + quantity);
  25.             }
  26.  
  27.             input = scanner.nextLine();
  28.         }
  29.  
  30.         for (Map.Entry<String, Double> entry : productsPrice.entrySet()) {
  31.             //key (име на продукта) -> value (цена)
  32.             //цена * количеството
  33.             String productName = entry.getKey();
  34.             double finalSum = entry.getValue() * productsQuantity.get(productName);
  35.             System.out.printf("%s -> %.2f%n", productName, finalSum);
  36.         }
  37.  
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement