Advertisement
dzocesrce

[NP] Online Shop

Apr 23rd, 2025
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.33 KB | None | 0 0
  1. import java.time.LocalDateTime;
  2. import java.util.*;
  3. import java.util.stream.Collectors;
  4. import java.util.stream.IntStream;
  5. import java.time.LocalDateTime;
  6. import java.util.*;
  7. import java.util.stream.Collectors;
  8. import java.util.Comparator;
  9.  
  10. class Product {
  11.     final static Comparator<Product> OLDEST_FIRST = Comparator.comparing(Product::getCreatedAt);
  12.     final static Comparator<Product> LOWEST_PRICE_FIRST = Comparator.comparing(Product::getPrice);
  13.     final static Comparator<Product> LEAST_SOLD_FIRST = Comparator.comparing(Product::getQuantitySold);
  14.  
  15.     String category;
  16.     String id;
  17.     String name;
  18.     LocalDateTime createdAt;
  19.     double price;
  20.     int quantitySold;
  21.  
  22.  
  23.     public Product(String category, String id, String name, LocalDateTime createdAt, double price) {
  24.         this.category = category;
  25.         this.id = id;
  26.         this.name = name;
  27.         this.createdAt = createdAt;
  28.         this.price = price;
  29.         this.quantitySold= 0;
  30.     }
  31.  
  32.     public int getQuantitySold() {
  33.         return quantitySold;
  34.     }
  35.  
  36.     public String getCategory() {
  37.         return category;
  38.     }
  39.  
  40.     public String getId() {
  41.         return id;
  42.     }
  43.  
  44.     public String getName() {
  45.         return name;
  46.     }
  47.  
  48.     public LocalDateTime getCreatedAt() {
  49.         return createdAt;
  50.     }
  51.  
  52.     public double getPrice() {
  53.         return price;
  54.     }
  55.  
  56.     public double getTransaction(int quantity){
  57.         quantitySold+=quantity;
  58.         return price*quantitySold;
  59.     }
  60.  
  61.     @Override
  62.     public String toString() {
  63.         return "Product{" +
  64.              
  65.                 "id='" + id + '\'' +
  66.                 ", name='" + name + '\'' +
  67.                 ", createdAt=" + createdAt +
  68.                 ", price=" + price +
  69.                 ", quantitySold=" + quantitySold +
  70.                 '}';
  71.     }
  72. }
  73. class OnlineShop {
  74.     Map<String,Product> products;
  75.  
  76.  
  77.     OnlineShop() {
  78.     products= new HashMap<>();
  79.     }
  80.  
  81.     void addProduct(String category, String id, String name, LocalDateTime createdAt, double price){
  82.         products.putIfAbsent(id,new Product(category,id,name,createdAt,price));
  83.     }
  84.  
  85.     double buyProduct(String id, int quantity) throws ProductNotFoundException{
  86.         if(products.get(id)==null)
  87.             throw new ProductNotFoundException(id);
  88.         return products.get(id).getTransaction(quantity);
  89.         //return 0.0;
  90.     }
  91.  
  92.     List<List<Product>> listProducts(String category, ComparatorType comparatorType, int pageSize) {
  93.         List<List<Product>> result = new ArrayList<>();
  94.         Comparator<Product> comparator;
  95.         if(comparatorType.equals(ComparatorType.HIGHEST_PRICE_FIRST))
  96.             comparator= Product.LOWEST_PRICE_FIRST.reversed();
  97.         else if(comparatorType.equals(ComparatorType.MOST_SOLD_FIRST))
  98.             comparator= Product.LEAST_SOLD_FIRST.reversed();
  99.         else if(comparatorType.equals(ComparatorType.LEAST_SOLD_FIRST))
  100.             comparator= Product.LEAST_SOLD_FIRST;
  101.         else if(comparatorType.equals(ComparatorType.LOWEST_PRICE_FIRST))
  102.             comparator= Product.LOWEST_PRICE_FIRST;
  103.         else if(comparatorType.equals(ComparatorType.NEWEST_FIRST))
  104.             comparator= Product.OLDEST_FIRST.reversed();
  105.         else
  106.             comparator= Product.OLDEST_FIRST;
  107.  
  108.         List<Product> productsFromCategory = products.values().stream().filter(i->i.getCategory()
  109.                 .equals(category))
  110.                 .sorted(comparator)
  111.                 .collect(Collectors.toList());
  112.         if(productsFromCategory.size()==0)
  113.             productsFromCategory= products.values().stream().sorted(comparator).collect(Collectors.toList());
  114.         for(int i=0;i<productsFromCategory.size();i+=pageSize){
  115.             List<Product> page = new ArrayList<>();
  116.             for(int j=0;j<pageSize;j++){
  117.                 if(i+j==productsFromCategory.size())
  118.                     break;
  119.                 page.add(productsFromCategory.get(i+j));
  120.             }
  121.  
  122.             result.add(page);
  123.         }
  124.         return result;
  125.     }
  126.  
  127. }
  128. enum ComparatorType {
  129.     NEWEST_FIRST,
  130.     OLDEST_FIRST,
  131.     LOWEST_PRICE_FIRST,
  132.     HIGHEST_PRICE_FIRST,
  133.     MOST_SOLD_FIRST,
  134.     LEAST_SOLD_FIRST
  135. }
  136.  
  137. class ProductNotFoundException extends Exception {
  138.     ProductNotFoundException(String id) {
  139.         super(String.format("Product with id %s does not exist in the online shop!",id));
  140.     }
  141. }
  142.  
  143.  
  144.  
  145. public class OnlineShopTest {
  146.  
  147.     public static void main(String[] args) {
  148.         OnlineShop onlineShop = new OnlineShop();
  149.         double totalAmount = 0.0;
  150.         Scanner sc = new Scanner(System.in);
  151.         String line;
  152.         while (sc.hasNextLine()) {
  153.             line = sc.nextLine();
  154.             String[] parts = line.split("\\s+");
  155.             if (parts[0].equalsIgnoreCase("addproduct")) {
  156.                 String category = parts[1];
  157.                 String id = parts[2];
  158.                 String name = parts[3];
  159.                 LocalDateTime createdAt = LocalDateTime.parse(parts[4]);
  160.                 double price = Double.parseDouble(parts[5]);
  161.                 onlineShop.addProduct(category, id, name, createdAt, price);
  162.             } else if (parts[0].equalsIgnoreCase("buyproduct")) {
  163.                 String id = parts[1];
  164.                 int quantity = Integer.parseInt(parts[2]);
  165.                 try {
  166.                     totalAmount += onlineShop.buyProduct(id, quantity);
  167.                 } catch (ProductNotFoundException e) {
  168.                     System.out.println(e.getMessage());
  169.                 }
  170.             } else {
  171.                 String category = parts[1];
  172.                 if (category.equalsIgnoreCase("null"))
  173.                     category=null;
  174.                 String comparatorString = parts[2];
  175.                 int pageSize = Integer.parseInt(parts[3]);
  176.                 ComparatorType comparatorType = ComparatorType.valueOf(comparatorString);
  177.                 printPages(onlineShop.listProducts(category, comparatorType, pageSize));
  178.             }
  179.         }
  180.         System.out.println("Total revenue of the online shop is: " + totalAmount);
  181.  
  182.     }
  183.  
  184.     private static void printPages(List<List<Product>> listProducts) {
  185.         for (int i = 0; i < listProducts.size(); i++) {
  186.             System.out.println("PAGE " + (i + 1));
  187.             listProducts.get(i).forEach(System.out::println);
  188.         }
  189.     }
  190. }
  191.  
  192.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement