iLLcheff

Inventory Manager 100%

Jan 18th, 2023
109
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.98 KB | None | 1 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4. public class InventoryManagerSolution {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.         String[] input = scanner.nextLine().split(" ");
  8.         String command = input[0];
  9.         Set<Item> products = new TreeSet<>();
  10.         HashMap<String, List<Item>> hashMap = new HashMap<>();
  11.         while (!command.equals("end")) {
  12.             if (command.equals("add")) {
  13.                 String name = input[1];
  14.                 double price = Double.parseDouble(input[2]);
  15.                 String type = input[3];
  16.                 Item item = new Item(name, price, type);
  17.                 if (products.contains(item)) {
  18.                     System.out.printf("Error: Item %s already exists\n", name);
  19.                 } else {
  20.                     products.add(item);
  21.                     if (!hashMap.containsKey(type)) {
  22.                         hashMap.put(type, new ArrayList<>());
  23.                     }
  24.                     hashMap.get(type).add(item);
  25.                     System.out.printf("Ok: Item %s added successfully\n", name);
  26.                 }
  27.             } else if (command.equals("filter")) {
  28.                 switch (input[2]) {
  29.                     case "type":
  30.                         String type = input[3];
  31.                         List<String> result = new ArrayList<>();
  32.                         if (hashMap.containsKey(type)) {
  33.                             result = hashMap.get(type)
  34.                                     .stream().sorted().limit(10)
  35.                                     .map(Item::toString).collect(Collectors.toList());
  36.                         }
  37.                         if (!result.isEmpty()) {
  38.                             System.out.print(print(result));
  39.                         } else {
  40.                             System.out.printf("Error: Type %s does not exist\n", type);
  41.                         }
  42.                         break;
  43.                     case "price":
  44.                         if (input.length == 7) {
  45.                             double minPrice = Double.parseDouble(input[4]);
  46.                             double maxPrice = Double.parseDouble(input[6]);
  47.                             result = products.stream()
  48.                                     .filter(item1 -> item1.getPrice() <= maxPrice && item1.getPrice() >= minPrice)
  49.                                     .limit(10).map(Item::toString).collect(Collectors.toList());
  50.                             System.out.print(print(result));
  51.                         } else if (input.length == 5 && input[3].equals("from")) {
  52.                             double minPrice = Double.parseDouble(input[4]);
  53.                             result = products.stream()
  54.                                     .filter(item1 -> item1.getPrice() >= minPrice)
  55.                                     .limit(10).map(Item::toString).collect(Collectors.toList());
  56.                             System.out.print(print(result));
  57.                         } else if (input.length == 5 && input[3].equals("to")) {
  58.                             double maxPrice = Double.parseDouble(input[4]);
  59.                             result = products.stream()
  60.                                     .filter(item1 -> item1.getPrice() <= maxPrice)
  61.                                     .limit(10).map(Item::toString).collect(Collectors.toList());
  62.                             System.out.print(print(result));
  63.                         }
  64.                 }
  65.             }
  66.             input = scanner.nextLine().split(" ");
  67.             command = input[0];
  68.         }
  69.     }
  70.  
  71.     public static String print(List<String> result) {
  72.         return String.format("Ok: %s\n", String.join(", ", result));
  73.     }
  74.  
  75.  
  76.     private static class Item implements Comparable<Item> {
  77.         String name;
  78.         double price;
  79.         String type;
  80.  
  81.         public Item(String name, double price, String type) {
  82.             this.name = name;
  83.             this.price = price;
  84.             this.type = type;
  85.         }
  86.  
  87.         public String getName() {
  88.             return name;
  89.         }
  90.  
  91.         public double getPrice() {
  92.             return price;
  93.         }
  94.  
  95.         public String getType() {
  96.             return type;
  97.         }
  98.  
  99.         @Override
  100.         public boolean equals(Object o) {
  101.             if (this == o) return true;
  102.             if (o == null || getClass() != o.getClass()) return false;
  103.             Item item = (Item) o;
  104.             return name.equals(item.name) &&
  105.                     price == item.price &&
  106.                     type.equals(item.type);
  107.         }
  108.  
  109.         @Override
  110.         public String toString() {
  111.             return String.format("%s(%.2f)", this.name, this.price);
  112.         }
  113.  
  114.         @Override
  115.         public int compareTo(Item item) {
  116.             return Comparator.comparing(Item::getPrice)
  117.                     .thenComparing(Item::getName)
  118.                     .thenComparing(Item::getType)
  119.                     .compare(this, item);
  120.         }
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment