Hellboyandso

Pizzeria.java

Dec 15th, 2016
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.20 KB | None | 0 0
  1. package domain;
  2.  
  3. import exceptions.OrderStatusException;
  4. import exceptions.PizzaException;
  5. import order.OrderStatusListener;
  6. import order.Order;
  7. import stock.Stock;
  8.  
  9. import java.util.*;
  10. import java.util.stream.Collectors;
  11. import java.util.stream.Stream;
  12.  
  13. /**
  14.  * Entrypoint to the pizzeria application
  15.  */
  16. public class Pizzeria {
  17.    
  18.     private String name;
  19.     private PizzaMenu menu;
  20.     private Map<String, Order> orders = new HashMap<String, Order>();
  21.     private List<OrderStatusListener> orderStatusListeners = new ArrayList<>();
  22.     private Stock stock = new Stock();
  23.     private Map<String, Customer> customers = new HashMap<>();
  24.  
  25.     /**
  26.      * Create a new pizzeria
  27.      * @param name
  28.      *  name for the pizzeria
  29.      */
  30.     public Pizzeria(String name){
  31.         this.name = name;
  32.         menu = new PizzaMenu("Super Menu");
  33.         //addStandardCustomers();
  34.     }
  35.  
  36.     /**
  37.      * Retrieve the pizza with the given name from the menu
  38.      * @param pizzaName the name of the pizza to get out of the menu
  39.      * @return the requested pizza from the menu
  40.      * @throws PizzaException something went wrong when getting the pizza
  41.      */
  42.     private Pizza getPizza(String pizzaName) throws PizzaException {
  43.         Pizza r = menu.getPizza(pizzaName);
  44.         if (r==null) throw new PizzaException("Unknown pizza: "+pizzaName);
  45.         return r;
  46.     }
  47.  
  48.     /**
  49.      * Return all pizzas with given filter criteria from the menu.  If no filters are given, all pizzas are returned.
  50.      * @param maxPrice Maximum price for the pizza (give null for no filter)
  51.      * @param veggieOnly whether to only return vegetarian pizzas
  52.      * @param containing Ingredients which should be in the pizza (give null for no filter)
  53.      * @return pizzas matching the given criteria
  54.      */
  55.     public Set<Pizza> getPizzas(Double maxPrice, boolean veggieOnly, Ingredient... containing) {
  56.         return menu.getPizzas(maxPrice, veggieOnly, containing);
  57.     }
  58.  
  59.     /**
  60.      * Add a pizza to the menu
  61.      * @param name name for the pizza
  62.      * @param price price for the pizza
  63.      * @param veggie wether or not it's a vegetarian pizza
  64.      * @param ingredients the list of ingredient names
  65.      * @return the created pizza
  66.      */
  67.     // Note: voor de volgende versie: veggie kan een eigenschap zijn van ingredienten, niet pizza :)
  68.     public Pizza addPizza(String name, double price, boolean veggie, String... ingredients){
  69.         Pizza pizza = new Pizza(name, price, Stream.of(ingredients).map(Ingredient::new).collect(Collectors.toSet()), veggie);
  70.         menu.addPizza(pizza);
  71.         return pizza;
  72.     }
  73.    
  74.     public void addPizza(Pizza pizza){
  75.         menu.addPizza(pizza);
  76.     }
  77.  
  78.     /**
  79.      * Update the price of a known pizza
  80.      * @param pizzaName name of the pizza
  81.      * @param price price of the pizza
  82.      * @throws PizzaException something went wrong with creating or setting the pizza
  83.      */
  84.     public void updatePizzaPrice(String pizzaName, double price) throws PizzaException {
  85.         getPizza(pizzaName).setPrice(price);
  86.     }
  87.  
  88.     /**
  89.      * Update the ingredients of a known pizza
  90.      * @param pizzaName name of the pizza
  91.      * @param ingredients insert as many String ingredients as you wish. updatePizzaIngredients("myPizza","cheese","onion", etc...) will work
  92.      * @throws PizzaException something went wrong with creating or setting the pizza
  93.      */
  94.     public void updatePizzaIngredients(String pizzaName, String... ingredients) throws PizzaException {
  95.         getPizza(pizzaName).setIngredients(Stream.of(ingredients).map(Ingredient::new).collect(Collectors.toSet()));
  96.     }
  97.    
  98.     public void updatePizzaIngredients(String pizzaName, Set<Ingredient> ingredients) throws PizzaException{
  99.         getPizza(pizzaName).setIngredients(ingredients);
  100.     }
  101.  
  102.     /**
  103.      * Remove a pizza from the menu
  104.      * @param name pizzaname
  105.      * @throws UnknownPizzaException pizza is not known in the menu
  106.      */
  107.     public void removePizza(String name) throws UnknownPizzaException {
  108.         menu.removePizza(name);
  109.     }
  110.  
  111.     /**
  112.      * Create a new order
  113.      * @param custNbr customer number the order is for (see {@link #getAllCustomers()})
  114.      * @param pizzas pizzas to place on the order.  Pizzas can still be added later using {@link #addToOrder(String, String...)}
  115.      * @return the created order (check {@link Order#getReference()} to find out the reference
  116.      * @throws PizzaException one or more given pizzas is not known to the system
  117.      * @throws UnknownCustomerException the given customer reference is not valid
  118.      */
  119.     public Order placeOrder(String custNbr, String... pizzas) throws PizzaException, UnknownCustomerException {
  120.         Customer customer = customers.get(custNbr);
  121.         if (customer == null) throw new UnknownCustomerException(custNbr);
  122.         String reference = generateUniqueId();
  123.         Order order = new Order(reference, customer, menu, orderStatusListeners);
  124.         try {
  125.             for (String pizza : pizzas) {
  126.                 order.addPizza(getPizza(pizza));
  127.             }
  128.         } catch (OrderStatusException e){
  129.             //this cannot happen at this point
  130.         }
  131.         orders.put(reference, order);
  132.         return order;
  133.     }
  134.  
  135.     /**
  136.      * Cancel an existing order
  137.      * @param reference reference of the order to cancel
  138.      * @throws UnknownOrderException the given reference was not found in the system
  139.      * @throws OrderStatusException the order is not in a cancellable state
  140.      */
  141.     public void cancelOrder(String reference) throws UnknownOrderException, OrderStatusException {
  142.         Order removed = orders.remove(reference);
  143.         if(removed==null) throw new UnknownOrderException(reference);
  144.         removed.cancel();
  145.     }
  146.  
  147.     /**
  148.      * Add pizzas to an existing order (if still possible)
  149.      * @param reference the reference of the order to modify
  150.      * @param pizzas pizzas to add
  151.      * @throws PizzaException one or more of the given pizzas is not known to the system
  152.      * @throws OrderStatusException the order is not in a state where to can still be modified
  153.      * @throws UnknownOrderException the given order is not known to the system
  154.      */
  155.     public void addToOrder(String reference, String... pizzas)throws PizzaException, OrderStatusException,UnknownOrderException {
  156.         Order order = getOrder(reference);
  157.         for(String pizza:pizzas){
  158.             order.addPizza(getPizza(pizza));
  159.         }
  160.     }
  161.  
  162.     /**
  163.      * Check whether a given order contains a given pizza
  164.      * @param reference the reference of the order to check
  165.      * @param pizza pizzas to check
  166.      * @return whether the pizza is present in the order at least once
  167.      * @throws PizzaException one or more of the given pizzas is not known to the system
  168.      * @throws UnknownOrderException the given order is not known to the system
  169.      */
  170.     public boolean orderContains(String reference, String pizza) throws PizzaException, UnknownOrderException {
  171.         return getOrder(reference).containsPizza(getPizza(pizza));
  172.     }
  173.  
  174.     /**
  175.      * Return the order with the given reference
  176.      * @param reference the id of the order
  177.      * @throws UnknownOrderException the given order is not known to the system
  178.      * @return order the order
  179.      */
  180.     public Order getOrder(String reference) throws UnknownOrderException {
  181.         Order r = orders.get(reference);
  182.         if (r==null) throw new UnknownOrderException(reference);
  183.         return r;
  184.     }
  185.  
  186.     /**
  187.      * @return all orders, current and historic
  188.      */
  189.     public Collection<Order> getOrders(){
  190.         return orders.values();
  191.     }
  192.  
  193.     /**
  194.      * @return all orders currently open
  195.      */
  196.     public Collection<Order> getOpenOrders(){
  197.         return orders.values().stream()
  198.                 .filter(o -> o.isOpen())
  199.                 .collect(Collectors.toSet());
  200.     }
  201.  
  202.     /**
  203.      * @return all orders ready to be delivered
  204.      */
  205.     public Collection<Order> getDeliverableOrders(){
  206.         return orders.values().stream()
  207.                 .filter(o -> o.isReadyForDelivery())
  208.                 .collect(Collectors.toSet());
  209.     }
  210.  
  211.     /**
  212.      * @return an overview of all pizzas that are currently ordered
  213.      */
  214.     public Collection<Pizza> getOrderedPizzas(){
  215.         return orders.values().stream()
  216.                 .filter(o -> o.isOpen())
  217.                 .flatMap(o -> o.getPizzas().stream())
  218.                 .collect(Collectors.toList());
  219.     }
  220.  
  221.     /**
  222.      * @return all orders for the give customer
  223.      * @param customer the customer
  224.      */
  225.     public Collection<Order> getOrdersForCustomer(Customer customer){
  226.         return orders.values().stream()
  227.                 .filter(o -> o.getCustomer().equals(customer))
  228.                 .collect(Collectors.toSet());
  229.     }
  230.  
  231.     /**
  232.      * @return all orders for the give customer
  233.      * @param custNbr reference of the customer to retrieve orders for
  234.      */
  235.     public Collection<Order> getOrdersForCustomer(String custNbr){
  236.         return orders.values().stream()
  237.                 .filter(o -> o.isOpen())
  238.                 .filter(o -> o.getCustomer().getCustNbr().equals(custNbr))
  239.                 .collect(Collectors.toSet());
  240.     }
  241.  
  242.     /**
  243.      * @param pizza the pizza for which you want to retrieve the open orders
  244.      * @return all open orders containing the given pizza
  245.      */
  246.     public Collection<Order> getOrdersForPizza(Pizza pizza){
  247.         return orders.values().stream()
  248.                 .filter(o -> o.isOpen())
  249.                 .filter(o -> o.containsPizza(pizza))
  250.                 .collect(Collectors.toSet());
  251.     }
  252.    
  253.     /**
  254.      * @return all open orders containing the given pizza
  255.      * @throws PizzaException something went wrong with updating/creating/getting the pizza
  256.      * @param pizza the name of the pizza you want to get the open orders for
  257.      */
  258.     public Collection<Order> getOrdersForPizza(String pizza) throws PizzaException {
  259.         return getOrdersForPizza(getPizza(pizza));
  260.     }
  261.  
  262.  
  263.     /**
  264.      * @return the stock status for the given ingredient
  265.      * @param ingredient the name of the ingredient you want to get the stock status of
  266.      */
  267.     public StockStatus getStockStatus(String ingredient){
  268.         return stock.getStatus(new Ingredient(ingredient));
  269.     }
  270.  
  271.     /**
  272.      * Update the stock status (out of stock, almost out, ok) for the give ingredient
  273.      * @param ingredient the name of the ingredient you want to update the stock status of
  274.      * @param status the new status of the ingredient
  275.      */
  276.     public void updateStockStatus(String ingredient, StockStatus status){
  277.         stock.updateStatus(new Ingredient(ingredient), status);
  278.     }
  279.  
  280.     /**
  281.      * Set the stock limit of an ingredient.
  282.      * Set to 0 to remove the ingredient completely
  283.      * @param limit the limit to set (0 to remove)
  284.      * @param ingredient then ame of the ingredient
  285.      */
  286.     public void setStockLimit(String ingredient, int limit){
  287.         stock.setLimit(new Ingredient(ingredient), limit);
  288.     }
  289.  
  290.     /**
  291.      * @return all known ingredients
  292.      */
  293.     public Set<Ingredient> getAllIngredients(){
  294.         return stock.listIngredients(null);
  295.     }
  296.  
  297.     /**
  298.      * @return all ingredients with a given stock status
  299.      * @param status the stock status you wish to get a list of ingredients for
  300.      */
  301.     public Set<Ingredient> getIngredients(StockStatus status){
  302.         return stock.listIngredients(status);
  303.     }
  304.  
  305.     /**
  306.      * Indicate the given ingredients are in stock again
  307.      * @param ingredients names of all the ingredients that are in stock again. You can add many!
  308.      */
  309.     public void inStockAgain(String... ingredients){
  310.         stock.setStatus(Stream.of(ingredients).map(Ingredient::new).collect(Collectors.toSet()), StockStatus.OK);
  311.     }
  312.  
  313.     /**
  314.      * Add a listener to be notified when the status of an order changes
  315.      * @param listener the listener to notify when the status changes
  316.      */
  317.     public void addOrderStatusListener(OrderStatusListener listener){
  318.         orderStatusListeners.add(listener);
  319.     }
  320.  
  321.     /**
  322.      * Create a new customer
  323.      * @param name customer's name
  324.      * @param surname customer's surname
  325.      * @param street customer's street
  326.      * @param housenbr customer's number
  327.      * @param postcode customer's postal code
  328.      * @param city customer's city
  329.      * @param country customer's country
  330.      * @param phoneNumber customer's phone number
  331.      * @param gender customer's gender ('M' or 'F')
  332.      * @param twitterAccount customer's twitter handle
  333.      * @param email customer's email address
  334.      * @return the reference of the created customer
  335.      */
  336.     public boolean addCustomer(String custnbr, Customer customer){
  337.         customers.put(custnbr, customer);
  338.         return true;
  339.     }
  340.  
  341.     /**
  342.      * @return an overview of all known customers
  343.      */
  344.     public Set<Customer> getAllCustomers(){
  345.         return new HashSet<>(customers.values());
  346.     }
  347.  
  348.     /**
  349.      * Remove a customer from the system (by reference)
  350.      * @throws UnknownCustomerException customer is unkown
  351.      * @param custNbr the number of the customer to remove
  352.      */
  353.     public void removeCustomer(String custNbr) throws UnknownCustomerException {
  354.         Customer removed = customers.remove(custNbr);
  355.         if (removed == null) throw new UnknownCustomerException(custNbr);
  356.     }
  357.  
  358.     private String generateUniqueId() {
  359.         return UUID.randomUUID().toString();
  360.     }
  361. }
Advertisement
Add Comment
Please, Sign In to add comment