mstoyanova

Untitled

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