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
HashMapto look up a price by item name. - Store item names in an
ArrayList. - Reject invalid input and unknown items.
- Return
double,boolean, andStringvalues from instance methods.
Program Rules¶
- The constructor receives a customer name. Create an empty
HashMap<String, Double> menu, an emptyArrayList<String> itemNames, and set the discount to0.0. addMenuItem(String itemName, double price)stores the price inmenuunder that item name.addItem(String itemName)adds the name toitemNamesonly when the item exists inmenu(usecontainsKey).applyDiscount(double percent)stores the discount percent only whenpercentis greater than0and less than100.itemCount()returns the number of ordered items.totalSpent()adds the menu price of every ordered item, then applies the discount.averageItemPrice()returns0.0when nothing has been ordered; otherwise, return the total spent divided by the number of items.qualifiesForLoyalty()returnstruewhenitemCount()is at least4andtotalSpent()is at least20.0.getSummary()must return this exact style of sentence:Mina: 4 items, $23.85 totalFormat 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)inaddItemso unknown items are ignored. - In
totalSpent(), look up each ordered item's price withmenu.get(name). - In
averageItemPrice(), check thatitemCount()is greater than0before dividing. - Use
&&inqualifiesForLoyalty()because both requirements must be satisfied. - Use
String.format("%.2f", totalSpent())insidegetSummary().