Restaurant Menu Management System
題目說明
請撰寫一個餐廳菜單管理程式,程式功能包括允許使用者新增菜品,依照關鍵字判斷菜品類型,
餐廳在收錄菜品時,只會收下菜品類型不為"Other"
且卡路里大於0的菜品,
收錄完成後,會允許用戶根據類型與卡路里範圍列出符合條件的菜品,依照收錄順序排序,
若無符合的菜品,就顯示Not found
。
請利用以下的參考程式碼作為基礎,依照main()
裡面的需求,補上Restaurant
這個類別,需填補的程式碼行數可能在30
行以內。
若修改正確,當程式獲得以下每一組範例輸入時,應該產生該範例的對應輸出結果。
提示,
Restaurant
這個類別應具備以下內容:
- 需要一個_變數_ 存放菜品的列表
- 需要一個_建構函數_ 初始化列表
- 需要一個
add()
_函數_ 允許新增菜品到列表中,新增菜品時,要利用Dish
類別提供的函數,檢查菜品「卡路里大於0」且「菜品類型不為"Other"
」才能列入菜單
- 需要一個
- 需要一個
searchForType()
_函數_ 根據指定菜品類型和卡路里範圍搜尋菜品,返回符合條件的菜品列表
- 需要一個
在測試資料中,至少會有一道合格的菜品,且每一道菜品的名稱至少有一個字。
菜品類型的結果以Dish.getType()
的回傳值為準。
參考程式碼
import java.util.ArrayList;
import java.util.Scanner;
class Dish {
private String name; // name of the dish
private int calories; // calories of the dish
public Dish(String name, int calories) {
this.name = name;
this.calories = calories;
}
public String getName() {
return name;
}
public int getCalories() {
return calories;
}
public String toString() {
return "Name: " + name + ", Calories: " + calories + ", Type: " + getType();
}
public String getType() {
if (name.contains("Salad") || name.contains("Vegetable")) {
return "Healthy";
} else if (name.contains("Burger") || name.contains("Pizza")) {
return "FastFood";
} else if (name.contains("Soup") || name.contains("Rice")) {
return "Comfort";
} else if (name.contains("Cake") || name.contains("Ice")) {
return "Dessert";
} else {
return "Other";
}
}
}
public class MyProgram {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// create a new Restaurant object, where the menu contains some dishes
Restaurant restaurant = new Restaurant();
// Allow the user to add 5 dishes to the restaurant
for (int i = 1; i <= 5; i++) {
// Example input: Caesar Salad;150
// (name: Caesar Salad, calories: 150)
String input = scanner.nextLine();
String name = input.split(";")[0];
int calories = Integer.parseInt(input.split(";")[1]);
// create a new Dish object with the given name and calories
restaurant.add(new Dish(name, calories));
}
// Allow the user to search for dishes in a specific type and calorie range
// Example input: Healthy;100;200
// (type: Healthy, lower calories: 100, upper calories: 200)
String input = scanner.nextLine();
String type = input.split(";")[0];
int lowerCalories = Integer.parseInt(input.split(";")[1]);
int upperCalories = Integer.parseInt(input.split(";")[2]);
ArrayList<Dish> result = restaurant.searchForType(type, lowerCalories, upperCalories);
if (result.isEmpty()) {
System.out.println("Not found");
} else {
for (Dish dish : result) {
System.out.println(dish);
}
}
}
}
輸入
輸入共 6 行,格式如下:
前 5 行:每行輸入一道菜品的資訊,格式為:菜名;卡路里
第 6 行:輸入搜尋條件,格式為:菜品類型;最小卡路里;最大卡路里
輸出
程式會輸出符合下列條件的菜品清單(每行一筆),格式為:Name: <菜名>, Calories: <卡路里>, Type: <菜品類型>
若無任何符合條件的菜品,輸出 Not found
範例輸入#1
Strawberry Cake;280
Cheese Pizza;520
Garden Salad;95
Ice Cream;300
Fish Soup;180
Dessert;250;350
範例輸出#1
Name: Strawberry Cake, Calories: 280, Type: Dessert
Name: Ice Cream, Calories: 300, Type: Dessert
範例輸入#2
Meat Burger;480
Vegetable Salad;110
Tomato Soup;160
Vanilla Cake;400
Fried Rice;320
FastFood;200;300
範例輸出#2
Not found
Comments