Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.*;
- public class Main {
- public static void main(String[] args) throws IOException {
- int counter = 0;
- StringBuilder sb = new StringBuilder();
- BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
- Set<Product> set = new TreeSet<>();
- boolean flag = true;
- while (flag) {
- String[] command = bf.readLine().split(" ");
- switch (command[0]) {
- case "add":
- if (command[1].length() < 3 ||
- command[1].length() > 20 ||
- Double.parseDouble(command[2]) < 0 ||
- Double.parseDouble(command[2]) > 5000 ||
- command[3].length() < 3 ||
- command[3].length() > 20){
- break;
- }
- String name = command[1];
- double price = Double.parseDouble(command[2]);
- String type = command[3];
- if (set.stream().noneMatch(u -> u.getName().equals(name))) {
- set.add(new Product(name, price, type));
- System.out.println("Ok: Item " + name + " added successfully");
- } else {
- System.out.println("Error: Item " + name + " already exists");
- }
- break;
- case "filter":
- switch (command.length) {
- //type from/to price
- case 5:
- if (command[3].equals("from")) {
- String prefix = "";
- sb.append("Ok: ");
- for (Product p : set) {
- if (p.getPrice() > Double.parseDouble(command[4])) {
- sb.append(prefix).append(p.getName()).append("(").append(p.getPrice()).append(")");
- prefix = ", ";
- counter++;
- }
- if (counter == 10) {
- break;
- }
- }
- System.out.println(sb);
- sb.setLength(0);
- counter = 0;
- }
- if (command[3].equals("to")) {
- String prefix = "";
- sb.append("Ok: ");
- for (Product p : set) {
- if (p.getPrice() < Double.parseDouble(command[4])) {
- sb.append(prefix).append(p.getName()).append("(").append(p.getPrice()).append(")");
- prefix = ", ";
- counter++;
- }
- if (counter == 10) {
- break;
- }
- }
- System.out.println(sb);
- sb.setLength(0);
- counter = 0;
- }
- break;
- // type
- case 4:
- String prefix1 = "";
- if (set.stream().noneMatch(u -> u.getType().equals(command[3]))) {
- System.out.println("Error: Type " + command[3] + " does not exists");
- break;
- }
- sb.append("Ok: ");
- for (Product p : set) {
- if (p.getType().equals(command[3])) {
- sb.append(prefix1).append(p.getName()).append("(").append(p.getPrice()).append(")");
- prefix1 = ", ";
- counter++;
- }
- if (counter == 10) {
- break;
- }
- }
- System.out.println(sb);
- sb.setLength(0);
- counter = 0;
- break;
- // from to price
- case 7:
- String prefix2 = "";
- sb.append("Ok: ");
- for (Product p : set) {
- if (p.getPrice() > Double.parseDouble(command[4]) && p.getPrice() < Double.parseDouble(command[6])) {
- sb.append(prefix2).append(p.getName()).append("(").append(p.getPrice()).append(")");
- prefix2 = ", ";
- counter++;
- }
- if (counter == 10) {
- break;
- }
- }
- System.out.println(sb);
- sb.setLength(0);
- counter = 0;
- break;
- }
- break;
- case "end":
- flag = false;
- break;
- }
- }
- }
- private static class Product implements Comparable<Product> {
- private String name;
- private double price;
- private String type;
- public Product(String name, double price, String type) {
- this.name = name;
- this.price = price;
- this.type = type;
- }
- public String getName() {
- return name;
- }
- public double getPrice() {
- return price;
- }
- public String getType() {
- return type;
- }
- @Override
- public int compareTo(Product o) {
- return Comparator.comparing(Product::getPrice).thenComparing(Product::getName).thenComparing(Product::getType).compare(this, o);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement