Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cart;
- import java.util.*;
- class Product {
- protected String name;
- protected String category;
- protected float price;
- public Product(String name,String category, float price){
- this.name=name;
- this.category=category;
- this.price=price;
- }
- public Product() {};
- }
- class Cart {
- //creating collection of Object from the class Product
- static Collection<Product>collection=new ArrayList<Product>();
- //adding Objects to the collection
- public static void addProduct(Product product) {
- collection.add(product);
- }
- //printing Objects if they are from the given category
- public static void printByCategory(String category) {
- for(Product p:collection) {
- if(p.category==category) {
- System.out.println(p.name);
- }
- }
- }
- //finding the total sum of the products
- public static float getTotalPrice() {
- float total=0f;
- for(Product p:collection)
- total+=p.price;
- return total;
- }
- //printing the Receipt
- public static void printPaymentReceipt() {
- float total=0f;
- List<Product> list=new ArrayList<>(collection);
- int n=list.size();
- for(int i=0;i<n;i++) {
- int quantity=1;
- for(int j=i+1;j<n;j++) {
- if(list.get(i).name.equals(list.get(j).name)) {
- if(list.get(i).price==list.get(j).price) {
- quantity++;
- list.remove(j);
- n--;
- }
- }
- }
- System.out.println(list.get(i).name+" "+quantity+" "+list.get(i).price+" "+list.get(i).price*quantity+" ");
- total+=list.get(i).price*quantity;
- }
- System.out.printf("Total is: %.2f",total+total*0.18);
- System.out.println();
- }
- //returning list from the n cheapest products
- public static List<Product> getCheapestN(int n){
- List<Product>list=new ArrayList<>(collection);
- List<Product>newlist=new ArrayList<>();
- int m=list.size();
- int k=0;
- while(k<n) {
- for(int i=0;i<m;i++) {
- float min=99999f;
- int minIndex=i;
- if(min<list.get(i).price) {
- min=list.get(i).price;
- minIndex=i;}
- for(int j=i+1;j<m;j++) {
- if(min<list.get(j).price) {
- min=list.get(j).price;
- minIndex=j;}
- }
- newlist.add(list.get(minIndex));
- list.remove(minIndex);
- m--;
- }
- k++;
- }
- return newlist;}
- }
- public class Shop {
- public static void main(String[] args) {
- //adding objects
- Product shirt=new Product("shirt","clothes",14.9f);
- Cart.addProduct(shirt);
- Product skirt=new Product("skirt","clothes",4.0f);
- Cart.addProduct(skirt);
- Product skirt1=new Product("skirt","clothes",20.0f);
- Cart.addProduct(skirt1);
- Product shirt1=new Product("shirt","clothes",1.9f);
- Cart.addProduct(shirt1);
- //testing printByCategory method
- Cart.printByCategory("clothes");
- //testing getTotalPrice method
- System.out.printf("%.2f",Cart.getTotalPrice());
- System.out.println();
- //testing printPaymentReceipt method
- Cart.printPaymentReceipt();
- //testing getCheapestN method
- int n=3;
- List<Product>emptylist=Cart.getCheapestN(n);
- for(int i=n;i>0;i--)
- System.out.println(emptylist.get(i).name+" "+emptylist.get(i).price);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement