Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package domain;
- import exceptions.OrderStatusException;
- import exceptions.PizzaException;
- import order.OrderStatusListener;
- import order.Order;
- import stock.Stock;
- import java.util.*;
- import java.util.stream.Collectors;
- import java.util.stream.Stream;
- /**
- * Entrypoint to the pizzeria application
- */
- public class Pizzeria {
- private String name;
- private PizzaMenu menu;
- private Map<String, Order> orders = new HashMap<String, Order>();
- private List<OrderStatusListener> orderStatusListeners = new ArrayList<>();
- private Stock stock = new Stock();
- private Map<String, Customer> customers = new HashMap<>();
- /**
- * Create a new pizzeria
- * @param name
- * name for the pizzeria
- */
- public Pizzeria(String name){
- this.name = name;
- menu = new PizzaMenu("Super Menu");
- //addStandardCustomers();
- }
- /**
- * Retrieve the pizza with the given name from the menu
- * @param pizzaName the name of the pizza to get out of the menu
- * @return the requested pizza from the menu
- * @throws PizzaException something went wrong when getting the pizza
- */
- private Pizza getPizza(String pizzaName) throws PizzaException {
- Pizza r = menu.getPizza(pizzaName);
- if (r==null) throw new PizzaException("Unknown pizza: "+pizzaName);
- return r;
- }
- /**
- * Return all pizzas with given filter criteria from the menu. If no filters are given, all pizzas are returned.
- * @param maxPrice Maximum price for the pizza (give null for no filter)
- * @param veggieOnly whether to only return vegetarian pizzas
- * @param containing Ingredients which should be in the pizza (give null for no filter)
- * @return pizzas matching the given criteria
- */
- public Set<Pizza> getPizzas(Double maxPrice, boolean veggieOnly, Ingredient... containing) {
- return menu.getPizzas(maxPrice, veggieOnly, containing);
- }
- /**
- * Add a pizza to the menu
- * @param name name for the pizza
- * @param price price for the pizza
- * @param veggie wether or not it's a vegetarian pizza
- * @param ingredients the list of ingredient names
- * @return the created pizza
- */
- // Note: voor de volgende versie: veggie kan een eigenschap zijn van ingredienten, niet pizza :)
- public Pizza addPizza(String name, double price, boolean veggie, String... ingredients){
- Pizza pizza = new Pizza(name, price, Stream.of(ingredients).map(Ingredient::new).collect(Collectors.toSet()), veggie);
- menu.addPizza(pizza);
- return pizza;
- }
- public void addPizza(Pizza pizza){
- menu.addPizza(pizza);
- }
- /**
- * Update the price of a known pizza
- * @param pizzaName name of the pizza
- * @param price price of the pizza
- * @throws PizzaException something went wrong with creating or setting the pizza
- */
- public void updatePizzaPrice(String pizzaName, double price) throws PizzaException {
- getPizza(pizzaName).setPrice(price);
- }
- /**
- * Update the ingredients of a known pizza
- * @param pizzaName name of the pizza
- * @param ingredients insert as many String ingredients as you wish. updatePizzaIngredients("myPizza","cheese","onion", etc...) will work
- * @throws PizzaException something went wrong with creating or setting the pizza
- */
- public void updatePizzaIngredients(String pizzaName, String... ingredients) throws PizzaException {
- getPizza(pizzaName).setIngredients(Stream.of(ingredients).map(Ingredient::new).collect(Collectors.toSet()));
- }
- public void updatePizzaIngredients(String pizzaName, Set<Ingredient> ingredients) throws PizzaException{
- getPizza(pizzaName).setIngredients(ingredients);
- }
- /**
- * Remove a pizza from the menu
- * @param name pizzaname
- * @throws UnknownPizzaException pizza is not known in the menu
- */
- public void removePizza(String name) throws UnknownPizzaException {
- menu.removePizza(name);
- }
- /**
- * Create a new order
- * @param custNbr customer number the order is for (see {@link #getAllCustomers()})
- * @param pizzas pizzas to place on the order. Pizzas can still be added later using {@link #addToOrder(String, String...)}
- * @return the created order (check {@link Order#getReference()} to find out the reference
- * @throws PizzaException one or more given pizzas is not known to the system
- * @throws UnknownCustomerException the given customer reference is not valid
- */
- public Order placeOrder(String custNbr, String... pizzas) throws PizzaException, UnknownCustomerException {
- Customer customer = customers.get(custNbr);
- if (customer == null) throw new UnknownCustomerException(custNbr);
- String reference = generateUniqueId();
- Order order = new Order(reference, customer, menu, orderStatusListeners);
- try {
- for (String pizza : pizzas) {
- order.addPizza(getPizza(pizza));
- }
- } catch (OrderStatusException e){
- //this cannot happen at this point
- }
- orders.put(reference, order);
- return order;
- }
- /**
- * Cancel an existing order
- * @param reference reference of the order to cancel
- * @throws UnknownOrderException the given reference was not found in the system
- * @throws OrderStatusException the order is not in a cancellable state
- */
- public void cancelOrder(String reference) throws UnknownOrderException, OrderStatusException {
- Order removed = orders.remove(reference);
- if(removed==null) throw new UnknownOrderException(reference);
- removed.cancel();
- }
- /**
- * Add pizzas to an existing order (if still possible)
- * @param reference the reference of the order to modify
- * @param pizzas pizzas to add
- * @throws PizzaException one or more of the given pizzas is not known to the system
- * @throws OrderStatusException the order is not in a state where to can still be modified
- * @throws UnknownOrderException the given order is not known to the system
- */
- public void addToOrder(String reference, String... pizzas)throws PizzaException, OrderStatusException,UnknownOrderException {
- Order order = getOrder(reference);
- for(String pizza:pizzas){
- order.addPizza(getPizza(pizza));
- }
- }
- /**
- * Check whether a given order contains a given pizza
- * @param reference the reference of the order to check
- * @param pizza pizzas to check
- * @return whether the pizza is present in the order at least once
- * @throws PizzaException one or more of the given pizzas is not known to the system
- * @throws UnknownOrderException the given order is not known to the system
- */
- public boolean orderContains(String reference, String pizza) throws PizzaException, UnknownOrderException {
- return getOrder(reference).containsPizza(getPizza(pizza));
- }
- /**
- * Return the order with the given reference
- * @param reference the id of the order
- * @throws UnknownOrderException the given order is not known to the system
- * @return order the order
- */
- public Order getOrder(String reference) throws UnknownOrderException {
- Order r = orders.get(reference);
- if (r==null) throw new UnknownOrderException(reference);
- return r;
- }
- /**
- * @return all orders, current and historic
- */
- public Collection<Order> getOrders(){
- return orders.values();
- }
- /**
- * @return all orders currently open
- */
- public Collection<Order> getOpenOrders(){
- return orders.values().stream()
- .filter(o -> o.isOpen())
- .collect(Collectors.toSet());
- }
- /**
- * @return all orders ready to be delivered
- */
- public Collection<Order> getDeliverableOrders(){
- return orders.values().stream()
- .filter(o -> o.isReadyForDelivery())
- .collect(Collectors.toSet());
- }
- /**
- * @return an overview of all pizzas that are currently ordered
- */
- public Collection<Pizza> getOrderedPizzas(){
- return orders.values().stream()
- .filter(o -> o.isOpen())
- .flatMap(o -> o.getPizzas().stream())
- .collect(Collectors.toList());
- }
- /**
- * @return all orders for the give customer
- * @param customer the customer
- */
- public Collection<Order> getOrdersForCustomer(Customer customer){
- return orders.values().stream()
- .filter(o -> o.getCustomer().equals(customer))
- .collect(Collectors.toSet());
- }
- /**
- * @return all orders for the give customer
- * @param custNbr reference of the customer to retrieve orders for
- */
- public Collection<Order> getOrdersForCustomer(String custNbr){
- return orders.values().stream()
- .filter(o -> o.isOpen())
- .filter(o -> o.getCustomer().getCustNbr().equals(custNbr))
- .collect(Collectors.toSet());
- }
- /**
- * @param pizza the pizza for which you want to retrieve the open orders
- * @return all open orders containing the given pizza
- */
- public Collection<Order> getOrdersForPizza(Pizza pizza){
- return orders.values().stream()
- .filter(o -> o.isOpen())
- .filter(o -> o.containsPizza(pizza))
- .collect(Collectors.toSet());
- }
- /**
- * @return all open orders containing the given pizza
- * @throws PizzaException something went wrong with updating/creating/getting the pizza
- * @param pizza the name of the pizza you want to get the open orders for
- */
- public Collection<Order> getOrdersForPizza(String pizza) throws PizzaException {
- return getOrdersForPizza(getPizza(pizza));
- }
- /**
- * @return the stock status for the given ingredient
- * @param ingredient the name of the ingredient you want to get the stock status of
- */
- public StockStatus getStockStatus(String ingredient){
- return stock.getStatus(new Ingredient(ingredient));
- }
- /**
- * Update the stock status (out of stock, almost out, ok) for the give ingredient
- * @param ingredient the name of the ingredient you want to update the stock status of
- * @param status the new status of the ingredient
- */
- public void updateStockStatus(String ingredient, StockStatus status){
- stock.updateStatus(new Ingredient(ingredient), status);
- }
- /**
- * Set the stock limit of an ingredient.
- * Set to 0 to remove the ingredient completely
- * @param limit the limit to set (0 to remove)
- * @param ingredient then ame of the ingredient
- */
- public void setStockLimit(String ingredient, int limit){
- stock.setLimit(new Ingredient(ingredient), limit);
- }
- /**
- * @return all known ingredients
- */
- public Set<Ingredient> getAllIngredients(){
- return stock.listIngredients(null);
- }
- /**
- * @return all ingredients with a given stock status
- * @param status the stock status you wish to get a list of ingredients for
- */
- public Set<Ingredient> getIngredients(StockStatus status){
- return stock.listIngredients(status);
- }
- /**
- * Indicate the given ingredients are in stock again
- * @param ingredients names of all the ingredients that are in stock again. You can add many!
- */
- public void inStockAgain(String... ingredients){
- stock.setStatus(Stream.of(ingredients).map(Ingredient::new).collect(Collectors.toSet()), StockStatus.OK);
- }
- /**
- * Add a listener to be notified when the status of an order changes
- * @param listener the listener to notify when the status changes
- */
- public void addOrderStatusListener(OrderStatusListener listener){
- orderStatusListeners.add(listener);
- }
- /**
- * Create a new customer
- * @param name customer's name
- * @param surname customer's surname
- * @param street customer's street
- * @param housenbr customer's number
- * @param postcode customer's postal code
- * @param city customer's city
- * @param country customer's country
- * @param phoneNumber customer's phone number
- * @param gender customer's gender ('M' or 'F')
- * @param twitterAccount customer's twitter handle
- * @param email customer's email address
- * @return the reference of the created customer
- */
- public boolean addCustomer(String custnbr, Customer customer){
- customers.put(custnbr, customer);
- return true;
- }
- /**
- * @return an overview of all known customers
- */
- public Set<Customer> getAllCustomers(){
- return new HashSet<>(customers.values());
- }
- /**
- * Remove a customer from the system (by reference)
- * @throws UnknownCustomerException customer is unkown
- * @param custNbr the number of the customer to remove
- */
- public void removeCustomer(String custNbr) throws UnknownCustomerException {
- Customer removed = customers.remove(custNbr);
- if (removed == null) throw new UnknownCustomerException(custNbr);
- }
- private String generateUniqueId() {
- return UUID.randomUUID().toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment