Advertisement
kacci97

Sorted

Nov 3rd, 2021
1,026
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.38 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collection;
  3. import java.util.List;
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12. class Product  {
  13.  
  14.     protected String name;
  15.     protected float price;
  16.     protected boolean domestic;
  17.     protected float weight;
  18.     protected String desc;
  19.  
  20.  
  21.     public Product(String name, float price, boolean domestic, float weight, String desc) {
  22.         this.name=name;
  23.         this.price=price;
  24.         this.domestic=domestic;
  25.         this.weight=weight;
  26.         this.desc=desc;
  27.  
  28.     }
  29.     public Product(){}
  30.     public void copy(Product p){
  31.         this.setName(p.getName());
  32.         this.setPrice(p.getPrice());
  33.         this.setdomestic(p.getDomestic());
  34.         this.setWeight(p.getWeight());
  35.         this.setDesc(p.getDesc());
  36.     }
  37.  
  38.     public void setName(String name){
  39.         this.name=name;
  40.     }
  41.     public void setPrice(Float price){
  42.         this.price=price;
  43.     }
  44.     public void setdomestic(boolean domestic){
  45.         this.domestic=domestic;
  46.     }
  47.     public void setWeight(float weight){
  48.         this.weight=weight;
  49.     }
  50.     public void setDesc(String desc){
  51.         this.desc=desc;
  52.     }
  53.     public String getName(){
  54.         return name;
  55.     }
  56.     public String getDesc(){
  57.         return desc;
  58.     }
  59.     public float getPrice(){
  60.         return price;
  61.     }
  62.     public float getWeight(){
  63.         return weight;
  64.     }
  65.     public boolean getDomestic(){
  66.         return domestic;
  67.     }
  68.  
  69.  
  70.  
  71.  
  72. }
  73.  
  74. class Cart {
  75.  
  76.     static Collection<Product>collection=new ArrayList<>();
  77.  
  78.     public static  void addProduct(Product product) {
  79.         collection.add(product);
  80.     }
  81.  
  82.  
  83.  
  84.  
  85.     public static void sortList(List<Product> list, int n) {
  86.  
  87.        
  88.         for(int i=0;i<n-1;i++){
  89.         Product smallest=new Product();
  90.         smallest.copy(list.get(i));
  91.          for(int j=i+1;j<n;j++){
  92.             Product smaller=new Product();
  93.             int compare=list.get(i).name.compareTo(list.get(j).name);
  94.             if(compare<0){
  95.                  smaller.copy(list.get(i));}
  96.             else{
  97.                 smaller.copy(list.get(j));}
  98.  
  99.             if (smaller.name.compareTo(smallest.name)<0){
  100.                     smallest.copy(smaller);}
  101.  
  102.  
  103.  
  104.             }
  105.          list.add(i,smallest);
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.     }}
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.     public static void printReceipt() {
  123.  
  124.  
  125.  
  126.         List<Product> list=new ArrayList<>(collection);
  127.  
  128.         int n=list.size();
  129.         int domesticProduct=0;
  130.         float domesticPrice=0f;
  131.         int importedProduct=0;
  132.         float importedPrice=0f;
  133.         for(int i=0;i<n;i++) {
  134.             if(list.get(i).domestic) {
  135.                 domesticProduct++;
  136.                 domesticPrice+=list.get(i).price;
  137.  
  138.             }
  139.             else {
  140.  
  141.                 importedProduct++;
  142.                 importedPrice+=list.get(i).price;
  143.  
  144.  
  145.             }
  146.  
  147.         }
  148.  
  149.         List<Product>domestic=new ArrayList<>();
  150.         List<Product>imported=new ArrayList<>();
  151.  
  152.         for(int i=0;i<n;i++) {
  153.             if(list.get(i).domestic)
  154.                 domestic.add(list.get(i));
  155.  
  156.  
  157.             else {
  158.                 imported.add(list.get(i));
  159.             }}
  160.  
  161.         int numD=domestic.size();
  162.         int numI=imported.size();
  163.  
  164.         sortList(domestic,numD);
  165.         sortList(imported,numI);
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.         System.out.println(". Domestic");
  173.  
  174.         for(int i=0;i<numD;i++) {
  175.             System.out.println("... "+domestic.get(i).name);
  176.             System.out.println("    Price $: "+domestic.get(i).price);
  177.             System.out.println("    "+domestic.get(i).desc.substring(0,10));
  178.             System.out.println("    Weight: "+domestic.get(i).weight);
  179.  
  180.  
  181.         }
  182.         System.out.println(". Imported");
  183.         for(int i=0;i<numI;i++) {
  184.             System.out.println("... "+imported.get(i).name);
  185.             System.out.println("    Price $: "+imported.get(i).price);
  186.             System.out.println("    "+imported.get(i).desc.substring(0,10));
  187.             System.out.println("    Weight: "+imported.get(i).weight);
  188.  
  189.  
  190.         }
  191.  
  192.  
  193.  
  194.  
  195.  
  196.         System.out.println("Domestic cost: $"+domesticPrice);
  197.         System.out.println("Imported cost: $"+importedPrice);
  198.         System.out.println("Domestic count: "+domesticProduct);
  199.         System.out.println("Imported count: "+importedProduct);
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.     }
  221.  
  222. }
  223.  
  224.  
  225.  
  226.  
  227.  
  228. public class shop{
  229.  
  230.     public static void main(String[] args) {
  231.         Product tomato=new Product("Tomato",30.5f,true,150,"The species originated in western South America and Central America. The Nahuatl (the language used by the Aztecs) word tomatl gave rise to the Spanish word tomate, from which the English word tomato derived");
  232.         Product apple=new Product("Apple",14.5f,true,20,"Apple trees are cultivated worldwide and are the most widely grown species in the genus Malus. The tree originated in Central Asia, where its wild ancestor, Malus sieversii, is still found today");
  233.         Product banana= new Product("Banana",22.0f,false,20,"A banana is an elongated, edible fruit – botanically a berry – produced by several kinds of large herbaceous flowering plants in the genus Musa. In some countries, bananas used for cooking may be called plantains, distinguishing them from dessert bananas");
  234.  
  235.         Cart.addProduct(tomato);
  236.         Cart.addProduct(apple);
  237.         Cart.addProduct(banana);
  238.  
  239.         Cart.printReceipt();
  240.     }
  241.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement