鱼C论坛

 找回密码
 立即注册
查看: 791|回复: 1

[已解决]练习

[复制链接]
匿名鱼油
匿名鱼油  发表于 2023-4-15 16:05:35 |阅读模式
30鱼币
本帖最后由 匿名 于 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:
&#61623;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.
&#61623;Print out the meal information and calculate the total bill after the user added the Ingredients.
最佳答案
2023-4-15 16:05:36
本帖最后由 歌者文明清理员 于 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);
    }
}
微信图片_20230415160128.png

最佳答案

回复

使用道具 举报

发表于 2023-4-15 16:05:36 | 显示全部楼层    本楼为最佳答案   
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-21 19:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表