Advertisement
mstoyanova

Untitled

Jun 24th, 2021
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.60 KB | None | 0 0
  1. package com.telerikacademy.oop.setmap;
  2.  
  3. import java.math.BigDecimal;
  4. import java.util.*;
  5.  
  6. public class InventoryManager {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.         String[] commands = scanner.nextLine().split(" ");
  10.         Map<String, Item> items = new HashMap<>();
  11. //        Map<String, Double> itemsPrice = new HashMap<>();
  12.         StringBuilder sb = new StringBuilder("Ok: ");
  13.  
  14.  
  15.         while (!commands[0].equalsIgnoreCase("end")) {
  16.             if (commands[0].equalsIgnoreCase("add")) {
  17.                 if (items.containsKey(commands[1])) {
  18. //                    sb.append("Item " + commands[1] + " already exists");
  19.                     System.out.println("Item " + commands[1] + " already exists");
  20.                 }
  21.                 Item item = new Item(commands[1], Double.parseDouble(commands[2]), commands[3]);
  22.                 items.put(commands[1], item);
  23. //                sb.append("Ok: Item " + commands[1] + " added successfully");
  24.                 System.out.println("Ok: Item " + commands[1] + " added successfully");
  25.             } else if (commands[0].equalsIgnoreCase("filter")) {
  26.                 if (commands[2].equalsIgnoreCase("type")) {
  27.                     if (!items.containsKey(commands[1])) {
  28. //                        sb.append("Error: type " + commands[1] + "does not exist");
  29.                         System.out.println("Error: type " + commands[1] + "does not exist");
  30.                     }
  31.                     String commandType = commands[3];
  32.                     items.values()
  33.                             .stream()
  34.                             .sorted()
  35.                             .limit(10)
  36.                             .filter(item -> item.getType().equals(commandType))
  37.                             .forEach(x -> sb.append(x));
  38.                     if (sb.length() > 4) {
  39.                         sb.deleteCharAt(sb.length() - 1);
  40.                         sb.deleteCharAt(sb.length() - 1);
  41.                     }
  42.                     System.out.println(sb);
  43.                 }
  44.                 else if (commands[2].equalsIgnoreCase("price")) {
  45.                     if (commands.length == 7) {
  46.                         double lowRange = Double.parseDouble(commands[4]);
  47.                         double upperRange = Double.parseDouble(commands[6]);
  48.                         items.values()
  49.                                 .stream()
  50.                                 .sorted()
  51.                                 .filter(item -> item.getPrice() <= lowRange &&
  52.                                         item.getPrice() <= upperRange)
  53.                                 .forEach(x -> sb.append(x));
  54.                         if (sb.length() > 4) {
  55.                             sb.deleteCharAt(sb.length() - 1);
  56.                             sb.deleteCharAt(sb.length() - 1);
  57.                         }
  58.                         System.out.println(sb);
  59.                     } else if (commands.length == 5) {
  60.                         if (commands[3].equalsIgnoreCase("from")) {
  61.                             double lowRange = Double.parseDouble(commands[4]);
  62.                             items.values()
  63.                                     .stream()
  64.                                     .sorted()
  65.                                     .filter(item -> item.getPrice() >= lowRange)
  66.                                     .forEach(x -> sb.append(x));
  67.                             if (sb.length() > 4) {
  68.                                 sb.deleteCharAt(sb.length() - 1);
  69.                                 sb.deleteCharAt(sb.length() - 1);
  70.                             }
  71.                             System.out.println(sb);
  72.                         } else if (commands[3].equalsIgnoreCase("to")) {
  73.                             double highRange = Double.parseDouble(commands[4]);
  74.                             items.values()
  75.                                     .stream()
  76.                                     .sorted()
  77.                                     .filter(item -> item.getPrice() <= highRange)
  78.                                     .forEach(x -> sb.append(x));
  79.                             if (sb.length() > 4) {
  80.                                 sb.deleteCharAt(sb.length() - 1);
  81.                                 sb.deleteCharAt(sb.length() - 1);
  82.                             }
  83.                             System.out.println(sb);
  84.                         }
  85.                     }
  86.                 }
  87.             }
  88. //            System.out.println(sb);
  89.             commands = scanner.nextLine().split(" ");
  90.         }
  91.  
  92.  
  93.  
  94.     }
  95.  
  96.     public static class Item implements Comparable<Item> {
  97.         private String itemName;
  98.         private String itemType;
  99.         private double itemPrice;
  100.  
  101.         public Item(String name, double price, String type) {
  102.             this.itemName = name;
  103.             this.itemType = type;
  104.             this.itemPrice = price;
  105.         }
  106.  
  107.         public String getName() {
  108.             return itemName;
  109.         }
  110.  
  111.         public String getType() {
  112.             return itemType;
  113.         }
  114.  
  115.         public double getPrice() {
  116.             return itemPrice;
  117.         }
  118.  
  119.         @Override
  120.         public int compareTo(Item item) {
  121.             return Comparator.comparing(Item::getPrice)
  122.                     .thenComparing(Item::getName)
  123.                     .thenComparing(Item::getType)
  124.                     .compare(this, item);
  125.         }
  126.  
  127.         @Override
  128.         public String toString() {
  129.             BigDecimal price = BigDecimal.valueOf(itemPrice).stripTrailingZeros();
  130.             return String.format("%s(%s), ", itemType, price);
  131.         }
  132.     }
  133.  
  134.  
  135. }
  136.  
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement