Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.23 KB | None | 0 0
  1. // 01639387 Mitar Bursac
  2.  
  3. package rbvs;
  4.  
  5. import java.util.List;
  6. import java.util.Scanner;
  7. import java.util.ArrayList;
  8. import java.util.Collection;
  9.  
  10. import rbvs.product.CompositeProduct;
  11. import rbvs.product.ExtendedProduct;
  12. import rbvs.product.IProduct;
  13. import rbvs.product.Product;
  14. import rbvs.product.SimpleProduct;
  15.  
  16. public class Restaurant {
  17.    
  18.     private String name;
  19.     private List<Table> tables = new ArrayList<Table>();
  20.     private List<IProduct> productAssortment = new ArrayList<IProduct>();
  21.     private List<Order> orderHistory = new ArrayList<Order>();
  22.     private long uniqueOrderIdentifier;
  23.    
  24.     public Restaurant(String name) {
  25.         this.name = name;
  26.     }
  27.    
  28.     public String getName() {
  29.         return this.name;
  30.     }
  31.    
  32.     public boolean createTable(String tableIdentifier) {
  33.        
  34.         /*for (Table checkTable : tables)
  35.             if (checkTable.getTableIdentifier().equalsIgnoreCase(tableIdentifier))
  36.                 return false;*/
  37.        
  38.         Table newTable = new Table (tableIdentifier);
  39.        
  40.         if (tables.contains(newTable))
  41.             return false;
  42.         else {
  43.             tables.add(newTable);
  44.             return true;
  45.         }
  46.        
  47.        
  48.             /*Table newTable = new Table (tableIdentifier);
  49.             tables.add(newTable);
  50.             return true;*/
  51.     }
  52.    
  53.    
  54.     public List<String> getTableIdentifiers() {
  55.        
  56.         List<String> ids = new ArrayList<String>();
  57.         for (Table tab : tables)
  58.             ids.add(tab.getTableIdentifier());
  59.        
  60.         return ids;
  61.     }
  62.    
  63.    
  64.    
  65.     public Table getSpecificTable(String identifier) {
  66.        
  67.         for (Table tab : tables)
  68.             if (tab.getTableIdentifier().equals(identifier))
  69.                 return tab;
  70.        
  71.         return null;
  72.     }
  73.    
  74.     public boolean addProduct(IProduct product) throws DuplicateProductException {
  75.        
  76.         if (product == null)
  77.             return false;
  78.        
  79.         for (IProduct check : this.getProducts())
  80.             if (check.equals(product)) throw new DuplicateProductException(check);
  81.        
  82.         this.getProducts().add(product);
  83.         return true;
  84.     }
  85.    
  86.    
  87.     public boolean addProduct(Collection<IProduct> products) throws DuplicateProductException {
  88.        
  89.         for (IProduct check : products) {
  90.             if (check != null)
  91.             {
  92.                 if (productAssortment.contains(check))
  93.                     throw new DuplicateProductException(check);
  94.                 else {
  95.                     productAssortment.add(check);
  96.                     return true;
  97.                 }
  98.             }
  99.         }
  100.         return false;  
  101.        
  102.         /*for (IProduct check : products) {
  103.             if (check != null)
  104.             {
  105.                 for (IProduct check1 : this.getProducts())
  106.                 {
  107.                     if (check.equals(check1)) throw new DuplicateProductException(check);
  108.                     else {
  109.                         this.getProducts().add(check);
  110.                         return true;
  111.                     }
  112.                 }
  113.             }
  114.         }
  115.         return false;*/
  116.        
  117.     }
  118.    
  119.    
  120.    
  121.     public List<IProduct> getProducts() {
  122.         return this.productAssortment;
  123.     }
  124.    
  125.     public boolean orderProductForTable(Table table, IProduct product) {
  126.        
  127.         if (table != null && product != null && tables.contains(table) && productAssortment.contains(product)) {
  128.             List<IProduct> newProds = new ArrayList<IProduct>();
  129.             newProds.add(product);
  130.             Order newOrder = new Order (generateUniqueIdentifier(), table, newProds);
  131.             orderHistory.add(newOrder);
  132.             return true;
  133.         } else return false;
  134.     }
  135.    
  136.    
  137.     public boolean orderProductForTable(Table table, IProduct product, int count) {
  138.        
  139.         if (table != null && product != null && tables.contains(table) && productAssortment.contains(product)) {
  140.             List<IProduct> newProds = new ArrayList<IProduct>();
  141.             for (int i = 0; i < count; i++)
  142.                 newProds.add(product);
  143.             Order newOrder = new Order (generateUniqueIdentifier(), table, newProds);
  144.             orderHistory.add(newOrder);
  145.             return true;
  146.         } else return false;   
  147.     }
  148.    
  149.    
  150.     public boolean containsProduct(IProduct compareProduct) {
  151.         /*if (productAssortment.contains(compareProduct))
  152.             return true;
  153.         else return false;*/
  154.        
  155.         if (this.findProduct(compareProduct) != null)
  156.             return true;
  157.         else return false;
  158.     }
  159.    
  160.     public IProduct findProduct(String productName) {
  161.         for (IProduct check : this.getProducts())
  162.             if (check.getName().equals(productName))
  163.                 return check;
  164.        
  165.         return null;
  166.     }
  167.    
  168.    
  169.     private IProduct findProduct(IProduct compareProduct) {
  170.         /*for (IProduct check : productAssortment)
  171.             if (check.equals(compareProduct))
  172.                 return check;
  173.        
  174.         return null;*/
  175.        
  176.         if (this.getProducts().contains(compareProduct))
  177.             return compareProduct;
  178.         else return null;
  179.     }
  180.    
  181.    
  182.     private long generateUniqueIdentifier() {
  183.         return uniqueOrderIdentifier++;
  184.     }
  185.    
  186.     public static List<IProduct> generateSimpleProducts() {
  187.        
  188.         List<IProduct> newSpList = new ArrayList<IProduct>();
  189.        
  190.         SimpleProduct sp1 = new SimpleProduct ("Coca-Cola", (float) 3.25);
  191.         SimpleProduct sp2 = new SimpleProduct ("Juice", (float) 2.75);
  192.         SimpleProduct sp3 = new SimpleProduct ("Water",(float) 2);
  193.         SimpleProduct sp4 = new SimpleProduct ("Tea", (float) 3);
  194.         SimpleProduct sp5 = new SimpleProduct ("Coffee", (float) 3.75);
  195.        
  196.        
  197.         newSpList.add(sp1);
  198.         newSpList.add(sp2);
  199.         newSpList.add(sp3);
  200.         newSpList.add(sp4);
  201.         newSpList.add(sp5);
  202.        
  203.         return newSpList;
  204.     }
  205.    
  206.    
  207.     public static List<IProduct> generateCompositeProducts() {
  208.        
  209.         SimpleProduct p13_1 = new SimpleProduct ("Fish", (float) 14);
  210.         SimpleProduct p13_2 = new SimpleProduct ("Cicken", (float) 11.3);
  211.         SimpleProduct p13_3 = new SimpleProduct ("Pork", (float) 12.5);
  212.        
  213.         SimpleProduct p23_1 = new SimpleProduct ("Banana", (float) 2.4);
  214.         SimpleProduct p23_2 = new SimpleProduct ("Avocado", (float) 7);
  215.         SimpleProduct p23_3 = new SimpleProduct ("Beans", (float) 5.2);
  216.        
  217.         SimpleProduct p33_1 = new SimpleProduct ("Toast", (float) 3.5);
  218.         SimpleProduct p33_2 = new SimpleProduct ("Tortilla", (float) 7.5);
  219.         SimpleProduct p33_3 = new SimpleProduct ("Nachos", (float) 7.7);
  220.        
  221.         Collection<Product> p13 = new ArrayList<Product>();
  222.         p13.add(p13_1);
  223.         p13.add(p13_2);
  224.         p13.add(p13_3);
  225.        
  226.         Collection<Product> p23 = new ArrayList<Product>();
  227.         p23.add(p23_1);
  228.         p23.add(p23_2);
  229.         p23.add(p23_3);
  230.        
  231.         Collection<Product> p33 = new ArrayList<Product>();
  232.         p33.add(p33_1);
  233.         p33.add(p33_2);
  234.         p33.add(p33_3);
  235.        
  236.        
  237.         CompositeProduct cp13 = new CompositeProduct("Meat", (float) 0.13, p13);
  238.         CompositeProduct cp23 = new CompositeProduct("Fruit", (float) 0.46, p23);
  239.         CompositeProduct cp33 = new CompositeProduct("Brunch", (float) 0.34, p33);
  240.        
  241.         SimpleProduct p11 = new SimpleProduct ("Apples", (float) 3.1);
  242.         SimpleProduct p12 = new SimpleProduct ("Nuts", (float) 4.9);
  243.        
  244.         SimpleProduct p21 = new SimpleProduct ("Bread", (float) 2);
  245.         SimpleProduct p22 = new SimpleProduct ("Cake", (float) 5);
  246.        
  247.         SimpleProduct p31 = new SimpleProduct ("Kiwi", (float) 3.4);
  248.         SimpleProduct p32 = new SimpleProduct ("Pasta", (float) 8.7);
  249.        
  250.         Collection<Product> p1 = new ArrayList<Product>();
  251.         p1.add(p11);
  252.         p1.add(p12);
  253.         p1.add(cp13);
  254.        
  255.         Collection<Product> p2 = new ArrayList<Product>();
  256.         p2.add(p21);
  257.         p2.add(p22);
  258.         p2.add(cp23);
  259.        
  260.        
  261.        
  262.         Collection<Product> p3 = new ArrayList<Product>();
  263.         p3.add(p31);
  264.         p3.add(p32);
  265.         p3.add(cp33);
  266.        
  267.        
  268.         CompositeProduct cp1 = new CompositeProduct("Combo One", (float) 0.2, p1);
  269.         CompositeProduct cp2 = new CompositeProduct("Combo Two", (float) 0.4, p2);
  270.         CompositeProduct cp3 = new CompositeProduct("Combo Three", (float) 0.35, p3);
  271.        
  272.         List<IProduct> cps = new ArrayList<IProduct>();
  273.         cps.add(cp1);
  274.         cps.add(cp2);
  275.         cps.add(cp3);
  276.        
  277.         return cps;
  278.        
  279.     }
  280.    
  281.    
  282.     public static void main(String[] args) {
  283.        
  284.         SimpleProduct simpProd1 = new SimpleProduct ("Coca-Cola", (float) 3.25);
  285.         SimpleProduct simpProd2 = new SimpleProduct ("Juice", (float) 2.75);
  286.         SimpleProduct simpProd3 = new SimpleProduct ("Water",(float) 2);
  287.         SimpleProduct simpProd4 = new SimpleProduct ("Tea", (float) 3);
  288.         SimpleProduct simpProd5 = new SimpleProduct ("Coffee", (float) 3.75);
  289.        
  290.         ExtendedProduct extProd1 = new ExtendedProduct ("Potatoes", (float) 4.25);
  291.         ExtendedProduct extProd2 = new ExtendedProduct ("Tomatoes", (float) 5.75);
  292.         ExtendedProduct extProd3 = new ExtendedProduct ("Paprika",(float) 2.3);
  293.         ExtendedProduct extProd4 = new ExtendedProduct ("Kiwi", (float) 5);
  294.         ExtendedProduct extProd5 = new ExtendedProduct ("Milk", (float) 3.45);
  295.        
  296.        
  297.        
  298.         Collection<Product> list1 = new ArrayList<Product>();
  299.         Collection<Product> list2 = new ArrayList<Product>();
  300.         Collection<Product> list3 = new ArrayList<Product>();
  301.         Collection<Product> list4 = new ArrayList<Product>();
  302.         Collection<Product> list5 = new ArrayList<Product>();
  303.        
  304.        
  305.         list1.add(simpProd1);
  306.         list2.add(simpProd2);
  307.         list1.add(simpProd3);
  308.         list1.add(simpProd4);
  309.         list1.add(simpProd5);
  310.        
  311.         list2.add(extProd1);
  312.         list2.add(extProd2);
  313.         list2.add(extProd3);
  314.         list2.add(extProd4);
  315.         list2.add(extProd5);
  316.        
  317.        
  318.         CompositeProduct compProd1 = new CompositeProduct("Combination One", (float) 0.23, list1);
  319.         CompositeProduct compProd2 = new CompositeProduct("Combination Two", (float) 0.57, list2);
  320.        
  321.         list3.add(compProd1);
  322.         list4.add(compProd2);
  323.        
  324.         CompositeProduct compProd3 = new CompositeProduct("Combination Three", (float) 0.17, list3);
  325.         CompositeProduct compProd4 = new CompositeProduct("Combination Four", (float) 0.81, list4);
  326.        
  327.         list5.add(compProd3);
  328.         list5.add(compProd4);
  329.        
  330.        
  331.        
  332.         Table table1 = new Table ("Table for two");
  333.         Table table2 = new Table ("Family Table", 6);
  334.         Table table3 = new Table ("Regular Table", 4);
  335.        
  336.        
  337.        
  338.         Restaurant newRestaurant = new Restaurant ("At Mitars");
  339.        
  340.        
  341.         newRestaurant.tables.add(table1);
  342.         newRestaurant.tables.add(table2);
  343.         newRestaurant.tables.add(table3);
  344.        
  345.        
  346.         newRestaurant.productAssortment.addAll(list1);
  347.         newRestaurant.productAssortment.addAll(list2);
  348.         newRestaurant.productAssortment.addAll(list3);
  349.         newRestaurant.productAssortment.addAll(list4);
  350.         newRestaurant.productAssortment.addAll(list5);
  351.        
  352.        
  353.         newRestaurant.orderProductForTable(table1, simpProd1);
  354.         newRestaurant.orderProductForTable(table1, simpProd2, 2);
  355.        
  356.         newRestaurant.orderProductForTable(table2, compProd3);
  357.         newRestaurant.orderProductForTable(table2, simpProd4, 3);
  358.        
  359.         newRestaurant.orderProductForTable(table3, extProd1);
  360.         newRestaurant.orderProductForTable(table3, extProd3, 4);
  361.        
  362.        
  363.         int end = 1;
  364.         //Scanner input = new Scanner(System.in);
  365.        
  366.         do {
  367.             System.out.println("At Mitars\n");
  368.             System.out.println("1. Search for a product based on its name.");
  369.             System.out.println("2. Add a new product to the product assortment.");
  370.             System.out.println("0. End.");
  371.             System.out.printf("Choice? ");
  372.            
  373.             Scanner input = new Scanner(System.in);
  374.             int choice = input.nextInt();
  375.            
  376.             try {
  377.                
  378.                 //Scanner input = new Scanner(System.in);
  379.                 //int choice = input.nextInt();
  380.                
  381.                 switch (choice) {
  382.                
  383.                 case 1:
  384.                     System.out.printf("Name: ");
  385.                     //Scanner name = new Scanner(System.in);
  386.                     String prodName = input.nextLine();
  387.                     if (newRestaurant.findProduct(prodName) == null)
  388.                         System.out.println("Product not found!");
  389.                     else {
  390.                         System.out.println("Product successfully found!");
  391.                         //System.out.println("Price: " + newRestaurant.findProduct(prodName).getPrice());
  392.                         //newRestaurant.findProduct(prodName).toString();
  393.                         System.out.println(newRestaurant.findProduct(prodName).toString());
  394.                     }
  395.                     break;
  396.                    
  397.                 case 2:
  398.                     //Scanner input = new Scanner(System.in);
  399.                     System.out.printf("Name: ");
  400.                     String myName = input.nextLine();
  401.                     System.out.printf("Price: ");
  402.                     float myPrice =  input.nextFloat();
  403.                    
  404.                     SimpleProduct helpProd = new SimpleProduct (myName, myPrice);
  405.                    
  406.                     try {
  407.                         if (newRestaurant.addProduct(helpProd) == true)
  408.                             System.out.println("Product successfully added!");
  409.                            
  410.                     } catch (DuplicateProductException e) {
  411.                         e.printStackTrace();
  412.                     }
  413.                     //input.close();
  414.                     break;
  415.                    
  416.                    
  417.                    
  418.                 case 0:
  419.                     end = 0;
  420.                     System.out.println("Program ended!");
  421.                     break;
  422.                    
  423.                 default:
  424.                     System.out.println("Wrong choice");
  425.                 }
  426.            
  427.             } catch (Exception e) {
  428.                 System.out.println("Choice has to be a number!");
  429.             } finally {
  430.                 input.close();
  431.             }
  432.            
  433.             System.out.printf("\n");
  434.            
  435.            
  436.         } while (end != 0);
  437.        
  438.     }
  439.  
  440.    
  441.     public String toString() {
  442.         return "Restaurant [name=" + name + ", tables=" + tables + ", productAssortment=" + productAssortment
  443.                 + ", orderHistory=" + orderHistory + ", uniqueOrderIdentifier=" + uniqueOrderIdentifier + "]";
  444.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement