本帖最后由 匿名 于 2023-4-16 12:58 编辑
要求如下
和运行结果再图片上
Exercises 1
Given the Ingredients Interface class as follows:
public interface IPrice {
public double getPrice();
}
Given a UML diagram as follows, write the Java code to implement the class Ingredient that implements the interface IPrice. An ingredient has a name and a price and a method to get the name of the ingredient and the price. The constructor of the class receives the name and the price of the ingredient as parameters. 
Exercises 2
Given the Ingredients Interface class as follows:
public interface Ingredients {
public void addIngredient(Ingredient ingredient);
public ArrayList<Ingredient> getIngredients();
}
Implement the class Meal. A Meal has a name and a list of ingredients and a price. The class implements the Ingredients and IPrice interfaces. The price of the meal is calculated by adding the prices of all the ingredients. The Ingredient class is the one implemented in question 2 and the link between Ingredient and IPrice has been omitted in this diagram.
Exercises 3
Write one main class Main.java to use Meal and Ingredient based on the UML above. The requirements as below:
Create one meal instance with name “Dinner”, ask user to add Ingredients to the meal until the input is “stop”. The ingredients can be one or more than one.
Print out the meal information and calculate the total bill after the user added the Ingredients.
本帖最后由 歌者文明清理员 于 2023-4-15 16:16 编辑
Ingredient.java:
- public class Ingredient implements IPrice {
- private String name;
- private double price;
- public Ingredient(String name, double price) {
- this.name = name;
- this.price = price;
- }
- public String getName() {
- return name;
- }
- public double getPrice() {
- return price;
- }
- }
复制代码
Meal.java:
- import java.util.ArrayList;
- public class Meal implements Ingredients, IPrice {
- private String name;
- private ArrayList<Ingredient> ingredients;
- public Meal(String name) {
- this.name = name;
- this.ingredients = new ArrayList<Ingredient>();
- }
- public void addIngredient(Ingredient ingredient) {
- this.ingredients.add(ingredient);
- }
- public ArrayList<Ingredient> getIngredients() {
- return ingredients;
- }
- public double getPrice() {
- double totalPrice = 0;
- for (Ingredient ingredient : ingredients) {
- totalPrice += ingredient.getPrice();
- }
- return totalPrice;
- }
- public String toString() {
- String ingredientsStr = "";
- for (Ingredient ingredient : ingredients) {
- ingredientsStr += ingredient.getName() + " - " + ingredient.getPrice() + ": ";
- }
- return name + " with ingredient: " + ingredientsStr + "Total bill: " + getPrice();
- }
- }
复制代码
Main.java:
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Meal meal = new Meal("Dinner");
- String input = "";
- while (!input.equals("stop")) {
- System.out.println("Add an ingredient to the meal:");
- String name = scanner.nextLine();
- if (name.equals("stop")) {
- break;
- }
- System.out.println("Enter the price (double):");
- double price = scanner.nextDouble();
- scanner.nextLine(); // consume the following newline character
- meal.addIngredient(new Ingredient(name, price));
- System.out.println("Do you want to add more ingredients? (enter stop to stop):");
- input = scanner.nextLine();
- }
- System.out.println(meal);
- }
- }
复制代码
|