Advertisement
Guest User

Untitled

a guest
Apr 12th, 2019
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.24 KB | None | 0 0
  1. package DemoTest;
  2.  
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;
  5. import java.util.Scanner;
  6.  
  7. public class VaporWinterSale {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         String[] input = scanner.nextLine().split(",\\s+");
  12.  
  13.         Map<String, Double> gamesPrice = new LinkedHashMap<>();
  14.         Map<String, String> gamesWithDLC = new LinkedHashMap<>();
  15.  
  16.  
  17.         for (String command : input) {
  18.  
  19.             if (command.contains("-")) {
  20.                 String[] data = command.split("-");
  21.  
  22.  
  23.  
  24.                 String gameName = data[0];
  25.                 double price = Double.parseDouble(data[1]);
  26.                
  27.  
  28.                     gamesPrice.put(gameName, price);
  29.  
  30.             } else if (command.contains(":")) {
  31.                 String[] data = command.split(":");
  32.  
  33.                 String gameName = data[0];
  34.                 String DLC = data[1];
  35.  
  36.                 if (gamesPrice.containsKey(gameName)) {
  37.                     gamesWithDLC.put(gameName, DLC);
  38.                     gamesPrice.put(gameName, gamesPrice.get(gameName) * 1.2);
  39.                 }
  40.  
  41.             }
  42.         }
  43.  
  44.         for (Map.Entry<String, Double> kvp : gamesPrice.entrySet()) {
  45.             if (gamesWithDLC.containsKey(kvp.getKey())) {
  46.                 gamesPrice.put(kvp.getKey(), kvp.getValue() * 0.5);
  47.             } else {
  48.                 gamesPrice.put(kvp.getKey(), kvp.getValue() * 0.8);
  49.             }
  50.         }
  51.  
  52.  
  53.         gamesPrice.entrySet().stream()
  54.                 .sorted((p1, p2) -> Double.compare(p1.getValue(), p2.getValue()))
  55.                 .forEach(e -> {
  56.                         if (gamesWithDLC.containsKey(e.getKey())) {
  57.                             System.out.println(String.format("%s - %s - %.2f", e.getKey(), gamesWithDLC.get(e.getKey()), e.getValue()));
  58.                     }
  59.                 });
  60.         gamesPrice.entrySet().stream()
  61.                 .sorted((p1, p2) -> Double.compare(p2.getValue(), p1.getValue()))
  62.                 .forEach(e -> {
  63.                     if (!gamesWithDLC.containsKey(e.getKey())) {
  64.                         System.out.println(String.format("%s - %.2f", e.getKey(), e.getValue()));
  65.                     }
  66.                 });
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement