Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.time.LocalDateTime;
- import java.util.*;
- import java.util.stream.Collectors;
- import java.util.stream.IntStream;
- import java.time.LocalDateTime;
- import java.util.*;
- import java.util.stream.Collectors;
- import java.util.Comparator;
- class Product {
- final static Comparator<Product> OLDEST_FIRST = Comparator.comparing(Product::getCreatedAt);
- final static Comparator<Product> LOWEST_PRICE_FIRST = Comparator.comparing(Product::getPrice);
- final static Comparator<Product> LEAST_SOLD_FIRST = Comparator.comparing(Product::getQuantitySold);
- String category;
- String id;
- String name;
- LocalDateTime createdAt;
- double price;
- int quantitySold;
- public Product(String category, String id, String name, LocalDateTime createdAt, double price) {
- this.category = category;
- this.id = id;
- this.name = name;
- this.createdAt = createdAt;
- this.price = price;
- this.quantitySold= 0;
- }
- public int getQuantitySold() {
- return quantitySold;
- }
- public String getCategory() {
- return category;
- }
- public String getId() {
- return id;
- }
- public String getName() {
- return name;
- }
- public LocalDateTime getCreatedAt() {
- return createdAt;
- }
- public double getPrice() {
- return price;
- }
- public double getTransaction(int quantity){
- quantitySold+=quantity;
- return price*quantitySold;
- }
- @Override
- public String toString() {
- return "Product{" +
- "id='" + id + '\'' +
- ", name='" + name + '\'' +
- ", createdAt=" + createdAt +
- ", price=" + price +
- ", quantitySold=" + quantitySold +
- '}';
- }
- }
- class OnlineShop {
- Map<String,Product> products;
- OnlineShop() {
- products= new HashMap<>();
- }
- void addProduct(String category, String id, String name, LocalDateTime createdAt, double price){
- products.putIfAbsent(id,new Product(category,id,name,createdAt,price));
- }
- double buyProduct(String id, int quantity) throws ProductNotFoundException{
- if(products.get(id)==null)
- throw new ProductNotFoundException(id);
- return products.get(id).getTransaction(quantity);
- //return 0.0;
- }
- List<List<Product>> listProducts(String category, ComparatorType comparatorType, int pageSize) {
- List<List<Product>> result = new ArrayList<>();
- Comparator<Product> comparator;
- if(comparatorType.equals(ComparatorType.HIGHEST_PRICE_FIRST))
- comparator= Product.LOWEST_PRICE_FIRST.reversed();
- else if(comparatorType.equals(ComparatorType.MOST_SOLD_FIRST))
- comparator= Product.LEAST_SOLD_FIRST.reversed();
- else if(comparatorType.equals(ComparatorType.LEAST_SOLD_FIRST))
- comparator= Product.LEAST_SOLD_FIRST;
- else if(comparatorType.equals(ComparatorType.LOWEST_PRICE_FIRST))
- comparator= Product.LOWEST_PRICE_FIRST;
- else if(comparatorType.equals(ComparatorType.NEWEST_FIRST))
- comparator= Product.OLDEST_FIRST.reversed();
- else
- comparator= Product.OLDEST_FIRST;
- List<Product> productsFromCategory = products.values().stream().filter(i->i.getCategory()
- .equals(category))
- .sorted(comparator)
- .collect(Collectors.toList());
- if(productsFromCategory.size()==0)
- productsFromCategory= products.values().stream().sorted(comparator).collect(Collectors.toList());
- for(int i=0;i<productsFromCategory.size();i+=pageSize){
- List<Product> page = new ArrayList<>();
- for(int j=0;j<pageSize;j++){
- if(i+j==productsFromCategory.size())
- break;
- page.add(productsFromCategory.get(i+j));
- }
- result.add(page);
- }
- return result;
- }
- }
- enum ComparatorType {
- NEWEST_FIRST,
- OLDEST_FIRST,
- LOWEST_PRICE_FIRST,
- HIGHEST_PRICE_FIRST,
- MOST_SOLD_FIRST,
- LEAST_SOLD_FIRST
- }
- class ProductNotFoundException extends Exception {
- ProductNotFoundException(String id) {
- super(String.format("Product with id %s does not exist in the online shop!",id));
- }
- }
- public class OnlineShopTest {
- public static void main(String[] args) {
- OnlineShop onlineShop = new OnlineShop();
- double totalAmount = 0.0;
- Scanner sc = new Scanner(System.in);
- String line;
- while (sc.hasNextLine()) {
- line = sc.nextLine();
- String[] parts = line.split("\\s+");
- if (parts[0].equalsIgnoreCase("addproduct")) {
- String category = parts[1];
- String id = parts[2];
- String name = parts[3];
- LocalDateTime createdAt = LocalDateTime.parse(parts[4]);
- double price = Double.parseDouble(parts[5]);
- onlineShop.addProduct(category, id, name, createdAt, price);
- } else if (parts[0].equalsIgnoreCase("buyproduct")) {
- String id = parts[1];
- int quantity = Integer.parseInt(parts[2]);
- try {
- totalAmount += onlineShop.buyProduct(id, quantity);
- } catch (ProductNotFoundException e) {
- System.out.println(e.getMessage());
- }
- } else {
- String category = parts[1];
- if (category.equalsIgnoreCase("null"))
- category=null;
- String comparatorString = parts[2];
- int pageSize = Integer.parseInt(parts[3]);
- ComparatorType comparatorType = ComparatorType.valueOf(comparatorString);
- printPages(onlineShop.listProducts(category, comparatorType, pageSize));
- }
- }
- System.out.println("Total revenue of the online shop is: " + totalAmount);
- }
- private static void printPages(List<List<Product>> listProducts) {
- for (int i = 0; i < listProducts.size(); i++) {
- System.out.println("PAGE " + (i + 1));
- listProducts.get(i).forEach(System.out::println);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement