Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.43 KB | None | 0 0
  1. import java.text.DecimalFormat;
  2. import java.util.*;
  3.  
  4. public class OnlineMarket {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.         String line;
  8.         String[] commands;
  9.         DecimalFormat format = new DecimalFormat("#.#######################");
  10.  
  11.         HashSet<String> names = new HashSet<>();
  12.         Hashtable<String, TreeSet<Item>> products = new Hashtable<>();
  13.  
  14.         while (!(line = scanner.nextLine()).equals("end")) {
  15.             commands = line.split(" ");
  16.  
  17.             if (commands[0].equals("add")) {
  18.                 if (names.contains(commands[1])) {
  19.                     System.out.println(String.format("Error: Product %s already exists", commands[1]));
  20.                 } else {
  21.                     names.add(commands[1]);
  22.                     if (!products.containsKey(commands[3])) products.put(commands[3], new TreeSet<>());
  23.                     products.get(commands[3]).add(new Item(commands[1], Double.parseDouble(commands[2])));
  24.                     System.out.println(String.format("Ok: Product %s added successfully", commands[1]));
  25.                 }
  26.  
  27.             } else if (commands[0].equals("filter")) {
  28.                 StringBuilder sb = new StringBuilder();
  29.                 if (commands[2].equals("type")) {
  30.                     if (products.containsKey(commands[3])) {
  31.                         sb.append("Ok: ");
  32.                         products.get(commands[3]).stream().limit(10)
  33.                                 .forEach(item -> {
  34.                                     sb.append(String.format("%s(%s), ", item.name, format.format(item.price)));
  35.                                 });
  36.                         sb.setLength(sb.length() - 2);
  37.                     } else {
  38.                         sb.append(String.format("Error: Type %s does not exists", commands[3]));
  39.                     }
  40.                 } else if (commands[2].equals("price")) {
  41.                     sb.append("Ok: ");
  42.                     if (commands[3].equals("to")) {
  43.                         double maxPrice = Double.parseDouble(commands[4]);
  44.                         products.values().stream().flatMap(Collection::stream)
  45.                                 .filter(item -> item.price < maxPrice)
  46.                                 .sorted().limit(10)
  47.                                 .forEach(item -> sb.append(String.format("%s(%s), ", item.name, format.format(item.price))));
  48.                     }
  49.                     else if(commands[3].equals("from")){
  50.                         if (commands.length > 5){
  51.                             double minPrice = Double.parseDouble(commands[4]);
  52.                             double maxPrice = Double.parseDouble(commands[6]);
  53.                             products.values().stream().flatMap(Collection::stream)
  54.                                     .filter(item -> item.price < maxPrice&& item.price > minPrice)
  55.                                     .sorted().limit(10)
  56.                                     .forEach(item -> {
  57.                                         sb.append(String.format("%s(%s), ", item.name, format.format(item.price)));
  58.                                     });
  59.                         }
  60.                         else{
  61.                             double minPrice = Double.parseDouble(commands[4]);
  62.                             products.values().stream().flatMap(Collection::stream)
  63.                                     .filter(item -> item.price > minPrice)
  64.                                     .sorted().limit(10)
  65.                                     .forEach(item -> {
  66.                                         sb.append(String.format("%s(%s), ", item.name, format.format(item.price)));
  67.                                     });
  68.                         }
  69.                     }
  70.                     if (sb.length() > 6) sb.setLength(sb.length() - 2);
  71.                 }
  72.                 System.out.println(sb.toString());
  73.             }
  74.  
  75.         }
  76.  
  77.     }
  78. }
  79.  
  80. class Item implements Comparable {
  81.     public String name;
  82.     public double price;
  83.  
  84.  
  85.     public Item(String name, double price) {
  86.         this.name = name;
  87.         this.price = price;
  88.     }
  89.  
  90.     @Override
  91.     public int compareTo(Object o) {
  92.         Item item = (Item) o;
  93.         double pr = price - item.price;
  94.  
  95.         if (pr < 0) {
  96.             return -1;
  97.         } else if (pr > 0) {
  98.             return 1;
  99.         } else {
  100.             return name.compareTo(item.name);
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement