콘텐츠로 이동

Mini Project Set 6: Cafe Order Manager

Name: ________

Time: 40 minutes

Build a small Java program that tracks a customer's order at a cafe. The CafeOrder class keeps the menu in a HashMap and the customer's items in an ArrayList. Complete the class, then run Main to test it.

Learning Goals

  • Initialize collections and instance variables in a constructor.
  • Use a HashMap to look up a price by item name.
  • Store item names in an ArrayList.
  • Reject invalid input and unknown items.
  • Return double, boolean, and String values from instance methods.

Program Rules

  • The constructor receives a customer name. Create an empty HashMap<String, Double> menu, an empty ArrayList<String> itemNames, and set the discount to 0.0.
  • addMenuItem(String itemName, double price) stores the price in menu under that item name.
  • addItem(String itemName) adds the name to itemNames only when the item exists in menu (use containsKey).
  • applyDiscount(double percent) stores the discount percent only when percent is greater than 0 and less than 100.
  • itemCount() returns the number of ordered items.
  • totalSpent() adds the menu price of every ordered item, then applies the discount.
  • averageItemPrice() returns 0.0 when nothing has been ordered; otherwise, return the total spent divided by the number of items.
  • qualifiesForLoyalty() returns true when itemCount() is at least 4 and totalSpent() is at least 20.0.
  • getSummary() must return this exact style of sentence: Mina: 4 items, $23.85 total Format the total with exactly two decimal places.
  • Do not change the method headers or the code in Main.

Skeleton Code

import java.util.ArrayList;
import java.util.HashMap;

class CafeOrder {
    private String customerName;
    private HashMap<String, Double> menu;
    private ArrayList<String> itemNames;
    private double discountPercent;

    public CafeOrder(String customerName) {
        // TODO
    }

    public void addMenuItem(String itemName, double price) {
        // TODO
    }

    public void addItem(String itemName) {
        // TODO
    }

    public void applyDiscount(double percent) {
        // TODO
    }

    public int itemCount() {
        // TODO
        return 0;
    }

    public double totalSpent() {
        // TODO
        return 0.0;
    }

    public double averageItemPrice() {
        // TODO
        return 0.0;
    }

    public boolean qualifiesForLoyalty() {
        // TODO
        return false;
    }

    public String getSummary() {
        // TODO
        return "";
    }
}

public class Main {
    public static void main(String[] args) {
        CafeOrder order = new CafeOrder("Mina");

        order.addMenuItem("Americano", 5.50);
        order.addMenuItem("Sandwich", 8.00);
        order.addMenuItem("Cake", 12.00);
        order.addMenuItem("Water", 1.00);

        order.addItem("Americano");
        order.addItem("Sandwich");
        order.addItem("Unknown Item"); // This item must be ignored.
        order.addItem("Cake");
        order.addItem("Water");
        order.applyDiscount(10.0);

        System.out.printf("%.2f%n", order.averageItemPrice());
        System.out.println(order.qualifiesForLoyalty());
        System.out.println(order.getSummary());
    }
}

Expected Output

5.96
true
Mina: 4 items, $23.85 total

Before You Submit

  • Initialize every field in the constructor, including both collections.
  • Use menu.containsKey(itemName) in addItem so unknown items are ignored.
  • In totalSpent(), look up each ordered item's price with menu.get(name).
  • In averageItemPrice(), check that itemCount() is greater than 0 before dividing.
  • Use && in qualifiesForLoyalty() because both requirements must be satisfied.
  • Use String.format("%.2f", totalSpent()) inside getSummary().