Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.ByteArrayInputStream;
- import java.io.InputStreamReader;
- import java.text.DecimalFormat;
- import java.util.HashMap;
- import java.util.List;
- import java.util.TreeSet;
- import java.util.stream.Collectors;
- public class OnlineMarket {
- public static void testInput() {
- String result = "add Milk 1.90 dairy\n" +
- "add Yogurt 1.90 dairy\n" +
- "add Notebook 1111.90 technology\n" +
- "add Orbit 0.90 food\n" +
- "add Rakia 11.90 drinks\n" +
- "add Dress 121.90 clothes\n" +
- "add Jacket 49.90 clothes\n" +
- "add Milk 1.90 dairy\n" +
- "add Eggs 2.34 food\n" +
- "add Cheese 5.55 dairy\n" +
- "filter by type clothes\n" +
- "filter by price from 1.00 to 2.00\n" +
- "add CappyOrange 1.99 juice \n" +
- "add Nestey 2.7 juice \n" +
- "filter by price from 1200\n" +
- "add Socks 2.90 clothes\n" +
- "filter by type fruits\n" +
- "nextpls\n" +
- "add MacBookPro 1700.1234 technology\n" +
- "filter by price from 1200\n" +
- "filter by price from 1.50\n" +
- "filter by price to 2.00\n" +
- "filter by type clothes\n" +
- "end";
- System.setIn(new ByteArrayInputStream(result.getBytes()));
- }
- static class Product implements Comparable {
- String productName;
- double productPrice;
- String productType;
- public Product(String productName, double productPrice, String productType) {
- this.productName = productName;
- this.productPrice = productPrice;
- this.productType = productType;
- }
- DecimalFormat df = new DecimalFormat("#.####");
- @Override
- public String toString() {
- return String.format("%s(%s)", productName, df.format(productPrice));
- }
- // @Override
- // public String toString() {
- // String str = productName + "(" + productPrice + ")";
- // return str;
- // }
- @Override
- public int compareTo(Object o) {
- Product product = (Product) o;
- int priceToCompare = Double.compare(product.productPrice, this.productPrice);
- if (priceToCompare != 0) {
- return -priceToCompare;
- }
- return 1;
- }
- }
- private static StringBuilder strBuilder = new StringBuilder();
- private static HashMap<String, Product> productMap = new HashMap<>();
- private static HashMap<String, TreeSet<Product>> productsFirstByType = new HashMap<>();
- private static TreeSet<Product> productsOrderedByPrice = new TreeSet<>();
- public static void main(String[] args) throws Exception {
- //testInput();
- BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
- while (true) {
- String inputReader = input.readLine();
- String[] parameters = inputReader.split(" ");
- String command = parameters[0];
- switch (command) {
- case "add":
- addProduct(parameters);
- break;
- case "filter":
- if (parameters.length == 4) {
- filterByType(parameters);
- } else if (parameters.length == 7) {
- filterMinToMax(parameters);
- } else if (parameters.length == 5) {
- if (parameters[3].equals("from")) {
- filterMin(parameters);
- } else if (parameters[3].equals("to")) {
- filterMax(parameters);
- }
- }
- break;
- case "end":
- System.out.println(strBuilder);
- return;
- }
- }
- }
- private static void filterMax(String[] parameters) {
- Double value = Double.parseDouble(parameters[4]);
- strBuilder.append("OK: ");
- List<Product> showList = productsOrderedByPrice.stream()
- .filter(product -> product.productPrice < value)
- .collect(Collectors.toList());
- for (int i = 0; i < showList.size(); i++) {
- if (i<10){
- strBuilder.append(showList.get(i) + ", ");
- }
- }
- if (showList.size() != 0) {
- strBuilder.deleteCharAt(strBuilder.length() - 1);
- strBuilder.deleteCharAt(strBuilder.length() - 1);
- }
- strBuilder.append("\n");
- }
- private static void filterMin(String[] parameters) {
- Double value = Double.parseDouble(parameters[4]);
- strBuilder.append("Ok: ");
- List<Product> showList = productsOrderedByPrice.stream()
- .filter(product -> product.productPrice > value)
- .collect(Collectors.toList());
- for (int i = 0; i < showList.size(); i++) {
- if (i<10){
- strBuilder.append(showList.get(i) + ", ");
- }
- }
- if (showList.size() != 0) {
- strBuilder.deleteCharAt(strBuilder.length() - 1);
- strBuilder.deleteCharAt(strBuilder.length() - 1);
- }
- strBuilder.append("\n");
- // for (int i = 0; i < ; i++) {
- //
- // }
- }
- private static void filterMinToMax(String[] parameters) {
- Double minValue = Double.parseDouble(parameters[4]);
- Double maxValue = Double.parseDouble(parameters[6]);
- strBuilder.append("Ok: ");
- List<Product> showList = productsOrderedByPrice.stream()
- .filter(product -> product.productPrice > minValue && product.productPrice < maxValue)
- .collect(Collectors.toList());
- for (int i = 0; i < showList.size(); i++) {
- if (i<10){
- strBuilder.append(showList.get(i) + ", ");
- }
- }
- strBuilder.deleteCharAt(strBuilder.length() - 1);
- strBuilder.deleteCharAt(strBuilder.length() - 1);
- strBuilder.append("\n");
- }
- private static void filterByType(String[] parameters) {
- if (productsFirstByType.containsKey(parameters[3])) {
- strBuilder.append("Ok: ");
- strBuilder.append(productsFirstByType.get(parameters[3]).stream().
- limit(10).map(x -> x.toString()).collect(Collectors.joining(", ")));
- strBuilder.append("\n");
- } else {
- strBuilder.append(String.format("Error: Type %s does not exists\n", parameters[3]));
- }
- }
- private static void addProduct(String[] parameters) {
- if (!productMap.containsKey(parameters[1])) {
- Product newProduct = new Product(parameters[1], Double.parseDouble(parameters[2]), parameters[3]);
- productMap.put(parameters[1], newProduct);
- if (!productsFirstByType.containsKey(parameters[3])) {
- productsFirstByType.put(parameters[3], new TreeSet<>());
- }
- TreeSet<Product> tempSet = productsFirstByType.get(parameters[3]);
- tempSet.add(newProduct);
- productsOrderedByPrice.add(newProduct);
- strBuilder.append(String.format("Ok: Product %s added successfully\n", parameters[1]));
- } else {
- strBuilder.append(String.format("Error: Product %s already exists\n", parameters[1]));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement