Advertisement
kacci97

Shop

Oct 24th, 2021
1,182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.20 KB | None | 0 0
  1. package cart;
  2.  
  3. import java.util.*;
  4. class Product {
  5.     protected String name;
  6.     protected String category;
  7.     protected float price;
  8.  
  9.     public  Product(String name,String category, float price){
  10.          this.name=name;
  11.          this.category=category;
  12.          this.price=price;
  13.      }
  14.     public Product() {};
  15.  
  16. }
  17.    
  18. class Cart {
  19.    
  20.     //creating collection of Object from the class Product
  21.     static Collection<Product>collection=new ArrayList<Product>();
  22.    
  23.    
  24.     //adding Objects to the collection
  25.     public static  void addProduct(Product product) {
  26.         collection.add(product);
  27.     }
  28.    
  29.     //printing Objects if they are from the given category
  30.     public static void printByCategory(String category) {
  31.         for(Product p:collection) {
  32.             if(p.category==category) {
  33.                 System.out.println(p.name);
  34.                 }
  35.             }
  36.     }
  37.    
  38.     //finding the total sum of the products
  39.     public static float getTotalPrice() {
  40.         float total=0f;
  41.         for(Product p:collection)
  42.             total+=p.price;
  43.          return total;
  44.     }
  45.    
  46.     //printing the Receipt
  47.     public static void printPaymentReceipt() {
  48.        float total=0f;
  49.        List<Product> list=new ArrayList<>(collection);
  50.        int n=list.size();
  51.        for(int i=0;i<n;i++) {
  52.            int quantity=1;
  53.            for(int j=i+1;j<n;j++) {
  54.                if(list.get(i).name.equals(list.get(j).name)) {
  55.                    if(list.get(i).price==list.get(j).price) {
  56.                        quantity++;
  57.                        list.remove(j);
  58.                        n--;
  59.                     }
  60.                  }
  61.               }
  62.            System.out.println(list.get(i).name+" "+quantity+" "+list.get(i).price+" "+list.get(i).price*quantity+" ");
  63.            total+=list.get(i).price*quantity;
  64.            }
  65.        
  66.        System.out.printf("Total is: %.2f",total+total*0.18);
  67.        System.out.println();
  68.        
  69.     }
  70.    
  71.     //returning list from the n cheapest products
  72.     public static List<Product> getCheapestN(int n){
  73.         List<Product>list=new ArrayList<>(collection);
  74.         List<Product>newlist=new ArrayList<>();
  75.         int m=list.size();
  76.         int k=0;
  77.         while(k<n) {
  78.             for(int i=0;i<m;i++) {
  79.                 float min=99999f;
  80.                 int minIndex=i;
  81.                 if(min<list.get(i).price) {
  82.                     min=list.get(i).price;
  83.                      minIndex=i;}
  84.                 for(int j=i+1;j<m;j++) {
  85.                     if(min<list.get(j).price) {
  86.                         min=list.get(j).price;
  87.                         minIndex=j;}
  88.                     }
  89.                 newlist.add(list.get(minIndex));
  90.                 list.remove(minIndex);
  91.                 m--;
  92.             }
  93.             k++;
  94.         }
  95.         return newlist;}
  96.     }
  97.  
  98. public class Shop {
  99.    
  100.  
  101.     public static void main(String[] args) {
  102.         //adding objects
  103.         Product shirt=new Product("shirt","clothes",14.9f);
  104.         Cart.addProduct(shirt);
  105.         Product skirt=new Product("skirt","clothes",4.0f);
  106.         Cart.addProduct(skirt);
  107.         Product skirt1=new Product("skirt","clothes",20.0f);
  108.         Cart.addProduct(skirt1);
  109.         Product shirt1=new Product("shirt","clothes",1.9f);
  110.         Cart.addProduct(shirt1);
  111.        
  112.         //testing printByCategory method
  113.         Cart.printByCategory("clothes");
  114.        
  115.         //testing getTotalPrice method
  116.         System.out.printf("%.2f",Cart.getTotalPrice());
  117.         System.out.println();
  118.        
  119.         //testing printPaymentReceipt method
  120.         Cart.printPaymentReceipt();
  121.        
  122.         //testing getCheapestN method
  123.         int n=3;
  124.         List<Product>emptylist=Cart.getCheapestN(n);
  125.         for(int i=n;i>0;i--)
  126.             System.out.println(emptylist.get(i).name+" "+emptylist.get(i).price);
  127.            
  128.        
  129.        
  130.     }
  131.  
  132. }
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement