Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 01639387 Mitar Bursac
- package rbvs;
- import java.util.List;
- import java.util.Scanner;
- import java.util.ArrayList;
- import java.util.Collection;
- import rbvs.product.CompositeProduct;
- import rbvs.product.ExtendedProduct;
- import rbvs.product.IProduct;
- import rbvs.product.Product;
- import rbvs.product.SimpleProduct;
- public class Restaurant {
- private String name;
- private List<Table> tables = new ArrayList<Table>();
- private List<IProduct> productAssortment = new ArrayList<IProduct>();
- private List<Order> orderHistory = new ArrayList<Order>();
- private long uniqueOrderIdentifier;
- public Restaurant(String name) {
- this.name = name;
- }
- public String getName() {
- return this.name;
- }
- public boolean createTable(String tableIdentifier) {
- /*for (Table checkTable : tables)
- if (checkTable.getTableIdentifier().equalsIgnoreCase(tableIdentifier))
- return false;*/
- Table newTable = new Table (tableIdentifier);
- if (tables.contains(newTable))
- return false;
- else {
- tables.add(newTable);
- return true;
- }
- /*Table newTable = new Table (tableIdentifier);
- tables.add(newTable);
- return true;*/
- }
- public List<String> getTableIdentifiers() {
- List<String> ids = new ArrayList<String>();
- for (Table tab : tables)
- ids.add(tab.getTableIdentifier());
- return ids;
- }
- public Table getSpecificTable(String identifier) {
- for (Table tab : tables)
- if (tab.getTableIdentifier().equals(identifier))
- return tab;
- return null;
- }
- public boolean addProduct(IProduct product) throws DuplicateProductException {
- if (product == null)
- return false;
- for (IProduct check : this.getProducts())
- if (check.equals(product)) throw new DuplicateProductException(check);
- this.getProducts().add(product);
- return true;
- }
- public boolean addProduct(Collection<IProduct> products) throws DuplicateProductException {
- for (IProduct check : products) {
- if (check != null)
- {
- if (productAssortment.contains(check))
- throw new DuplicateProductException(check);
- else {
- productAssortment.add(check);
- return true;
- }
- }
- }
- return false;
- /*for (IProduct check : products) {
- if (check != null)
- {
- for (IProduct check1 : this.getProducts())
- {
- if (check.equals(check1)) throw new DuplicateProductException(check);
- else {
- this.getProducts().add(check);
- return true;
- }
- }
- }
- }
- return false;*/
- }
- public List<IProduct> getProducts() {
- return this.productAssortment;
- }
- public boolean orderProductForTable(Table table, IProduct product) {
- if (table != null && product != null && tables.contains(table) && productAssortment.contains(product)) {
- List<IProduct> newProds = new ArrayList<IProduct>();
- newProds.add(product);
- Order newOrder = new Order (generateUniqueIdentifier(), table, newProds);
- orderHistory.add(newOrder);
- return true;
- } else return false;
- }
- public boolean orderProductForTable(Table table, IProduct product, int count) {
- if (table != null && product != null && tables.contains(table) && productAssortment.contains(product)) {
- List<IProduct> newProds = new ArrayList<IProduct>();
- for (int i = 0; i < count; i++)
- newProds.add(product);
- Order newOrder = new Order (generateUniqueIdentifier(), table, newProds);
- orderHistory.add(newOrder);
- return true;
- } else return false;
- }
- public boolean containsProduct(IProduct compareProduct) {
- /*if (productAssortment.contains(compareProduct))
- return true;
- else return false;*/
- if (this.findProduct(compareProduct) != null)
- return true;
- else return false;
- }
- public IProduct findProduct(String productName) {
- for (IProduct check : this.getProducts())
- if (check.getName().equals(productName))
- return check;
- return null;
- }
- private IProduct findProduct(IProduct compareProduct) {
- /*for (IProduct check : productAssortment)
- if (check.equals(compareProduct))
- return check;
- return null;*/
- if (this.getProducts().contains(compareProduct))
- return compareProduct;
- else return null;
- }
- private long generateUniqueIdentifier() {
- return uniqueOrderIdentifier++;
- }
- public static List<IProduct> generateSimpleProducts() {
- List<IProduct> newSpList = new ArrayList<IProduct>();
- SimpleProduct sp1 = new SimpleProduct ("Coca-Cola", (float) 3.25);
- SimpleProduct sp2 = new SimpleProduct ("Juice", (float) 2.75);
- SimpleProduct sp3 = new SimpleProduct ("Water",(float) 2);
- SimpleProduct sp4 = new SimpleProduct ("Tea", (float) 3);
- SimpleProduct sp5 = new SimpleProduct ("Coffee", (float) 3.75);
- newSpList.add(sp1);
- newSpList.add(sp2);
- newSpList.add(sp3);
- newSpList.add(sp4);
- newSpList.add(sp5);
- return newSpList;
- }
- public static List<IProduct> generateCompositeProducts() {
- SimpleProduct p13_1 = new SimpleProduct ("Fish", (float) 14);
- SimpleProduct p13_2 = new SimpleProduct ("Cicken", (float) 11.3);
- SimpleProduct p13_3 = new SimpleProduct ("Pork", (float) 12.5);
- SimpleProduct p23_1 = new SimpleProduct ("Banana", (float) 2.4);
- SimpleProduct p23_2 = new SimpleProduct ("Avocado", (float) 7);
- SimpleProduct p23_3 = new SimpleProduct ("Beans", (float) 5.2);
- SimpleProduct p33_1 = new SimpleProduct ("Toast", (float) 3.5);
- SimpleProduct p33_2 = new SimpleProduct ("Tortilla", (float) 7.5);
- SimpleProduct p33_3 = new SimpleProduct ("Nachos", (float) 7.7);
- Collection<Product> p13 = new ArrayList<Product>();
- p13.add(p13_1);
- p13.add(p13_2);
- p13.add(p13_3);
- Collection<Product> p23 = new ArrayList<Product>();
- p23.add(p23_1);
- p23.add(p23_2);
- p23.add(p23_3);
- Collection<Product> p33 = new ArrayList<Product>();
- p33.add(p33_1);
- p33.add(p33_2);
- p33.add(p33_3);
- CompositeProduct cp13 = new CompositeProduct("Meat", (float) 0.13, p13);
- CompositeProduct cp23 = new CompositeProduct("Fruit", (float) 0.46, p23);
- CompositeProduct cp33 = new CompositeProduct("Brunch", (float) 0.34, p33);
- SimpleProduct p11 = new SimpleProduct ("Apples", (float) 3.1);
- SimpleProduct p12 = new SimpleProduct ("Nuts", (float) 4.9);
- SimpleProduct p21 = new SimpleProduct ("Bread", (float) 2);
- SimpleProduct p22 = new SimpleProduct ("Cake", (float) 5);
- SimpleProduct p31 = new SimpleProduct ("Kiwi", (float) 3.4);
- SimpleProduct p32 = new SimpleProduct ("Pasta", (float) 8.7);
- Collection<Product> p1 = new ArrayList<Product>();
- p1.add(p11);
- p1.add(p12);
- p1.add(cp13);
- Collection<Product> p2 = new ArrayList<Product>();
- p2.add(p21);
- p2.add(p22);
- p2.add(cp23);
- Collection<Product> p3 = new ArrayList<Product>();
- p3.add(p31);
- p3.add(p32);
- p3.add(cp33);
- CompositeProduct cp1 = new CompositeProduct("Combo One", (float) 0.2, p1);
- CompositeProduct cp2 = new CompositeProduct("Combo Two", (float) 0.4, p2);
- CompositeProduct cp3 = new CompositeProduct("Combo Three", (float) 0.35, p3);
- List<IProduct> cps = new ArrayList<IProduct>();
- cps.add(cp1);
- cps.add(cp2);
- cps.add(cp3);
- return cps;
- }
- public static void main(String[] args) {
- SimpleProduct simpProd1 = new SimpleProduct ("Coca-Cola", (float) 3.25);
- SimpleProduct simpProd2 = new SimpleProduct ("Juice", (float) 2.75);
- SimpleProduct simpProd3 = new SimpleProduct ("Water",(float) 2);
- SimpleProduct simpProd4 = new SimpleProduct ("Tea", (float) 3);
- SimpleProduct simpProd5 = new SimpleProduct ("Coffee", (float) 3.75);
- ExtendedProduct extProd1 = new ExtendedProduct ("Potatoes", (float) 4.25);
- ExtendedProduct extProd2 = new ExtendedProduct ("Tomatoes", (float) 5.75);
- ExtendedProduct extProd3 = new ExtendedProduct ("Paprika",(float) 2.3);
- ExtendedProduct extProd4 = new ExtendedProduct ("Kiwi", (float) 5);
- ExtendedProduct extProd5 = new ExtendedProduct ("Milk", (float) 3.45);
- Collection<Product> list1 = new ArrayList<Product>();
- Collection<Product> list2 = new ArrayList<Product>();
- Collection<Product> list3 = new ArrayList<Product>();
- Collection<Product> list4 = new ArrayList<Product>();
- Collection<Product> list5 = new ArrayList<Product>();
- list1.add(simpProd1);
- list2.add(simpProd2);
- list1.add(simpProd3);
- list1.add(simpProd4);
- list1.add(simpProd5);
- list2.add(extProd1);
- list2.add(extProd2);
- list2.add(extProd3);
- list2.add(extProd4);
- list2.add(extProd5);
- CompositeProduct compProd1 = new CompositeProduct("Combination One", (float) 0.23, list1);
- CompositeProduct compProd2 = new CompositeProduct("Combination Two", (float) 0.57, list2);
- list3.add(compProd1);
- list4.add(compProd2);
- CompositeProduct compProd3 = new CompositeProduct("Combination Three", (float) 0.17, list3);
- CompositeProduct compProd4 = new CompositeProduct("Combination Four", (float) 0.81, list4);
- list5.add(compProd3);
- list5.add(compProd4);
- Table table1 = new Table ("Table for two");
- Table table2 = new Table ("Family Table", 6);
- Table table3 = new Table ("Regular Table", 4);
- Restaurant newRestaurant = new Restaurant ("At Mitars");
- newRestaurant.tables.add(table1);
- newRestaurant.tables.add(table2);
- newRestaurant.tables.add(table3);
- newRestaurant.productAssortment.addAll(list1);
- newRestaurant.productAssortment.addAll(list2);
- newRestaurant.productAssortment.addAll(list3);
- newRestaurant.productAssortment.addAll(list4);
- newRestaurant.productAssortment.addAll(list5);
- newRestaurant.orderProductForTable(table1, simpProd1);
- newRestaurant.orderProductForTable(table1, simpProd2, 2);
- newRestaurant.orderProductForTable(table2, compProd3);
- newRestaurant.orderProductForTable(table2, simpProd4, 3);
- newRestaurant.orderProductForTable(table3, extProd1);
- newRestaurant.orderProductForTable(table3, extProd3, 4);
- int end = 1;
- //Scanner input = new Scanner(System.in);
- do {
- System.out.println("At Mitars\n");
- System.out.println("1. Search for a product based on its name.");
- System.out.println("2. Add a new product to the product assortment.");
- System.out.println("0. End.");
- System.out.printf("Choice? ");
- Scanner input = new Scanner(System.in);
- int choice = input.nextInt();
- try {
- //Scanner input = new Scanner(System.in);
- //int choice = input.nextInt();
- switch (choice) {
- case 1:
- System.out.printf("Name: ");
- //Scanner name = new Scanner(System.in);
- String prodName = input.nextLine();
- if (newRestaurant.findProduct(prodName) == null)
- System.out.println("Product not found!");
- else {
- System.out.println("Product successfully found!");
- //System.out.println("Price: " + newRestaurant.findProduct(prodName).getPrice());
- //newRestaurant.findProduct(prodName).toString();
- System.out.println(newRestaurant.findProduct(prodName).toString());
- }
- break;
- case 2:
- //Scanner input = new Scanner(System.in);
- System.out.printf("Name: ");
- String myName = input.nextLine();
- System.out.printf("Price: ");
- float myPrice = input.nextFloat();
- SimpleProduct helpProd = new SimpleProduct (myName, myPrice);
- try {
- if (newRestaurant.addProduct(helpProd) == true)
- System.out.println("Product successfully added!");
- } catch (DuplicateProductException e) {
- e.printStackTrace();
- }
- //input.close();
- break;
- case 0:
- end = 0;
- System.out.println("Program ended!");
- break;
- default:
- System.out.println("Wrong choice");
- }
- } catch (Exception e) {
- System.out.println("Choice has to be a number!");
- } finally {
- input.close();
- }
- System.out.printf("\n");
- } while (end != 0);
- }
- public String toString() {
- return "Restaurant [name=" + name + ", tables=" + tables + ", productAssortment=" + productAssortment
- + ", orderHistory=" + orderHistory + ", uniqueOrderIdentifier=" + uniqueOrderIdentifier + "]";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement