Advertisement
kacci97

Prodanvica

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