Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.DecimalFormat;
- import java.util.*;
- public class OnlineMarket {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String line;
- String[] commands;
- DecimalFormat format = new DecimalFormat("#.#######################");
- HashSet<String> names = new HashSet<>();
- Hashtable<String, TreeSet<Item>> products = new Hashtable<>();
- while (!(line = scanner.nextLine()).equals("end")) {
- commands = line.split(" ");
- if (commands[0].equals("add")) {
- if (names.contains(commands[1])) {
- System.out.println(String.format("Error: Product %s already exists", commands[1]));
- } else {
- names.add(commands[1]);
- if (!products.containsKey(commands[3])) products.put(commands[3], new TreeSet<>());
- products.get(commands[3]).add(new Item(commands[1], Double.parseDouble(commands[2])));
- System.out.println(String.format("Ok: Product %s added successfully", commands[1]));
- }
- } else if (commands[0].equals("filter")) {
- StringBuilder sb = new StringBuilder();
- if (commands[2].equals("type")) {
- if (products.containsKey(commands[3])) {
- sb.append("Ok: ");
- products.get(commands[3]).stream().limit(10)
- .forEach(item -> {
- sb.append(String.format("%s(%s), ", item.name, format.format(item.price)));
- });
- sb.setLength(sb.length() - 2);
- } else {
- sb.append(String.format("Error: Type %s does not exists", commands[3]));
- }
- } else if (commands[2].equals("price")) {
- sb.append("Ok: ");
- if (commands[3].equals("to")) {
- double maxPrice = Double.parseDouble(commands[4]);
- products.values().stream().flatMap(Collection::stream)
- .filter(item -> item.price < maxPrice)
- .sorted().limit(10)
- .forEach(item -> sb.append(String.format("%s(%s), ", item.name, format.format(item.price))));
- }
- else if(commands[3].equals("from")){
- if (commands.length > 5){
- double minPrice = Double.parseDouble(commands[4]);
- double maxPrice = Double.parseDouble(commands[6]);
- products.values().stream().flatMap(Collection::stream)
- .filter(item -> item.price < maxPrice&& item.price > minPrice)
- .sorted().limit(10)
- .forEach(item -> {
- sb.append(String.format("%s(%s), ", item.name, format.format(item.price)));
- });
- }
- else{
- double minPrice = Double.parseDouble(commands[4]);
- products.values().stream().flatMap(Collection::stream)
- .filter(item -> item.price > minPrice)
- .sorted().limit(10)
- .forEach(item -> {
- sb.append(String.format("%s(%s), ", item.name, format.format(item.price)));
- });
- }
- }
- if (sb.length() > 6) sb.setLength(sb.length() - 2);
- }
- System.out.println(sb.toString());
- }
- }
- }
- }
- class Item implements Comparable {
- public String name;
- public double price;
- public Item(String name, double price) {
- this.name = name;
- this.price = price;
- }
- @Override
- public int compareTo(Object o) {
- Item item = (Item) o;
- double pr = price - item.price;
- if (pr < 0) {
- return -1;
- } else if (pr > 0) {
- return 1;
- } else {
- return name.compareTo(item.name);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement