jeudi 3 décembre 2020

Decorator Pattern using HashMap

I am faily new to Java in itself, I have been tasked with adding a Decorator Pattern within the application posted below.

I have no idea how to do this, I have however done the Decorator pattern before just not with a HashMap.


public class Food {
  private final String name;
  private final BigDecimal cost;

  public Food(String name, Double cost)
  {
    this.name = name;
    this.cost = new BigDecimal(cost);
  }
  public BigDecimal getCost()
  { 
    return this.cost;
  }

  public String getName()
  { 
    return this.name;
  }
}

The reason for BigDecimal is it is more reliable than a double, I just need to convert it to 2 decimal spaces.

public class Order {

    private Map<Food, Integer> foods = new HashMap<Food, Integer>();

    public BigDecimal getTotalFood () {
        BigDecimal totalFood = BigDecimal.ZERO;

        for (Food food : this.foods.keySet()) {
            BigDecimal quantity = new BigDecimal(this.foods.get(food));
            totalFood = totalFood.add(food.getCost().multiply(quantity));
            System.out.println("Item: "+food.getName() +" "+ "Cost of item: " + food.getCost() +
                    " " + "Quantity ordered: " + quantity );
        }
        return totalFood;
    }

    public void add(Food food, Integer quantity) {
        //Store the quantity against the beverage.
        this.foods.put(food, quantity);
    }
public class Restaurant {

    public static void main(String[] args) {

        Map<String, Food> food = new HashMap<String, Food>();
        food.put("Cheese Burger", new Food("Cheese Burger", 3.0));
        food.put("Cheese Burger with Bacon", new Food("Cheese Burger with Bacon", 3.5));
        food.put("Fried Rice", new Food("Fried Rice", 5.5));
        food.put("Chicken Rice", new Food("Chicken Rice", 5.0));
        food.put("Toast Bread", new Food("Toast Bread", 2.0));
        food.put("Mixed Rice", new Food("Mixed Rice", 3.8));
        food.put("Fanta", new Food("Fanta", 5.50));
        
        
        //Create an order and add items from the menu to it.
        Order order1 = new Order();
        order1.add(food.get("Fried Rice"), 2);
        order1.add(food.get("Toast Bread"), 1);
        order1.add(food.get("Mixed Rice"), 1);
        order1.add(food.get("Fanta"), 1);
        order1.add(food.get("Cheese Burger with Bacon"), 1);
        System.out.println();
        System.out.println("Total for order 1: "  + order1.getTotalFood());
        System.out.println();

The current code above works, prints out items, price and quantity.

The decorator should ask the user if they want any condiments such as different dips, cutlery etc before the application prints out the order.

My idea for the CondimentDecorator should create a HashMap of items, price. If the user selects one or more then the selected key value pairs will be added to the Food HashMap.

I have no clue how to implement this into the current application, any ideas?

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire