Advertisement
dzocesrce

[NP] Shopping Cart

Apr 15th, 2025
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.79 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. import java.io.PrintStream;
  5. import java.io.PrintWriter;
  6. import java.util.ArrayList;
  7. import java.util.Comparator;
  8. import java.util.List;
  9.  
  10. abstract class Product implements Comparable<Product> {
  11.     private String id;
  12.     private String name;
  13.     private int price;
  14.  
  15.     public Product(String id, String name, int price) {
  16.         this.id = id;
  17.         this.name = name;
  18.         this.price = price;
  19.     }
  20.  
  21.     public String getId() {
  22.         return id;
  23.     }
  24.  
  25.     public abstract float getTotalPrice();
  26.  
  27.     public String getName() {
  28.         return name;
  29.     }
  30.  
  31.     public int getPrice() {
  32.         return price;
  33.     }
  34. }
  35.  
  36. class ProductPerKilogram extends Product{
  37.     private float quantity;
  38.  
  39.     public ProductPerKilogram(String id, String name, int price, float quantity) {
  40.         super(id, name, price);
  41.         this.quantity= quantity;
  42.     }
  43.  
  44.     public float getTotalPrice(){
  45.         return super.getPrice()*quantity/1000.0f;
  46.     }
  47.  
  48.     @Override
  49.     public int compareTo(Product o) {
  50.         return Float.compare(this.getTotalPrice(),o.getTotalPrice());
  51.     }
  52.  
  53.     @Override
  54.     public String toString() {
  55.         return String.format("%s - %.2f",super.getId(),getTotalPrice());
  56.     }
  57. }
  58.  
  59. class WholeProduct extends Product{
  60.  
  61.     private float quantity;
  62.  
  63.     public WholeProduct(String id, String name, int price, int quantity) {
  64.         super(id, name, price);
  65.         this.quantity= quantity;
  66.     }
  67.  
  68.     public float getTotalPrice(){
  69.         return super.getPrice()*quantity;
  70.     }
  71.  
  72.     @Override
  73.     public int compareTo(Product o) {
  74.         return Float.compare(this.getTotalPrice(),o.getTotalPrice());
  75.     }
  76.     @Override
  77.     public String toString() {
  78.         return String.format("%s - %.2f",super.getId(),getTotalPrice());
  79.     }
  80.  
  81. }
  82.  
  83.  
  84. class InvalidOperationException extends Exception{
  85.     public InvalidOperationException(String message) {
  86.         super(message);
  87.     }
  88. }
  89.  
  90. public class ShoppingTest {
  91.  
  92.     public static void main(String[] args) {
  93.         Scanner sc = new Scanner(System.in);
  94.         ShoppingCart cart = new ShoppingCart();
  95.  
  96.         int items = Integer.parseInt(sc.nextLine());
  97.         for (int i = 0; i < items; i++) {
  98.             try {
  99.                 cart.addItem(sc.nextLine());
  100.             } catch (InvalidOperationException e) {
  101.                 System.out.println(e.getMessage());
  102.             }
  103.         }
  104.  
  105.         List<Integer> discountItems = new ArrayList<>();
  106.         int discountItemsCount = Integer.parseInt(sc.nextLine());
  107.         for (int i = 0; i < discountItemsCount; i++) {
  108.             discountItems.add(Integer.parseInt(sc.nextLine()));
  109.         }
  110.  
  111.         int testCase = Integer.parseInt(sc.nextLine());
  112.         if (testCase == 1) {
  113.             cart.printShoppingCart(System.out);
  114.         } else if (testCase == 2) {
  115.             try {
  116.                 cart.blackFridayOffer(discountItems, System.out);
  117.             } catch (InvalidOperationException e) {
  118.                 System.out.println(e.getMessage());
  119.             }
  120.         } else {
  121.             System.out.println("Invalid test case");
  122.         }
  123.     }
  124. }
  125.  
  126. class ShoppingCart {
  127.     List<Product> products;
  128.  
  129.     public ShoppingCart() {
  130.         this.products = new ArrayList<>();
  131.     }
  132.  
  133.     public void addItem(String s) throws InvalidOperationException {
  134.         String[] parts= s.split(";");
  135.         String id = parts[1];
  136.         String name = parts[2];
  137.         int price = Integer.parseInt(parts[3]);
  138.         float quantity = Float.parseFloat(parts[4]);
  139.         if(quantity==0){
  140.             throw new InvalidOperationException(String.format("The quantity of the product with id %s can not be 0.",id));
  141.         }
  142.         if(parts[0].equals("WS")){
  143.             products.add(new WholeProduct(id,name,price,(int)quantity));
  144.         }
  145.         else{
  146.             products.add(new ProductPerKilogram(id,name,price,quantity));
  147.         }
  148.     }
  149.  
  150.     public void printShoppingCart(PrintStream out) {
  151.  
  152.         PrintWriter printWriter= new PrintWriter(out);
  153.  
  154.         products.stream().sorted(Comparator.reverseOrder()).forEach(i->printWriter.println(i));
  155.  
  156.         printWriter.flush();
  157.     }
  158.  
  159.     public void blackFridayOffer(List<Integer> discountItems, PrintStream out) throws InvalidOperationException {
  160.         PrintWriter printWriter= new PrintWriter(out);
  161.         if(discountItems.size()==0)
  162.             throw new InvalidOperationException("There are no products with discount.");
  163.  
  164.         products.stream()
  165.                 .filter(i->discountItems.contains(Integer.parseInt(i.getId())))
  166.                 .map(i -> i.getId() + " - " + String.format("%.2f", i.getTotalPrice() * 0.1f))
  167.                 .forEach(i -> printWriter.println(i));
  168.         printWriter.flush();
  169.     }
  170. }
  171.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement