鱼C论坛

 找回密码
 立即注册
查看: 565|回复: 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:

  1. public class Ingredient implements IPrice {
  2.     private String name;
  3.     private double price;

  4.     public Ingredient(String name, double price) {
  5.         this.name = name;
  6.         this.price = price;
  7.     }

  8.     public String getName() {
  9.         return name;
  10.     }

  11.     public double getPrice() {
  12.         return price;
  13.     }
  14. }
复制代码


Meal.java:

  1. import java.util.ArrayList;

  2. public class Meal implements Ingredients, IPrice {
  3.     private String name;
  4.     private ArrayList<Ingredient> ingredients;

  5.     public Meal(String name) {
  6.         this.name = name;
  7.         this.ingredients = new ArrayList<Ingredient>();
  8.     }

  9.     public void addIngredient(Ingredient ingredient) {
  10.         this.ingredients.add(ingredient);
  11.     }

  12.     public ArrayList<Ingredient> getIngredients() {
  13.         return ingredients;
  14.     }

  15.     public double getPrice() {
  16.         double totalPrice = 0;
  17.         for (Ingredient ingredient : ingredients) {
  18.             totalPrice += ingredient.getPrice();
  19.         }
  20.         return totalPrice;
  21.     }

  22.     public String toString() {
  23.         String ingredientsStr = "";
  24.         for (Ingredient ingredient : ingredients) {
  25.             ingredientsStr += ingredient.getName() + " - " + ingredient.getPrice() + ": ";
  26.         }
  27.         return name + " with ingredient: " + ingredientsStr + "Total bill: " + getPrice();
  28.     }
  29. }
复制代码


Main.java:

  1. import java.util.Scanner;

  2. public class Main {
  3.     public static void main(String[] args) {
  4.         Scanner scanner = new Scanner(System.in);
  5.         Meal meal = new Meal("Dinner");
  6.         String input = "";
  7.         while (!input.equals("stop")) {
  8.             System.out.println("Add an ingredient to the meal:");
  9.             String name = scanner.nextLine();
  10.             if (name.equals("stop")) {
  11.                 break;
  12.             }
  13.             System.out.println("Enter the price (double):");
  14.             double price = scanner.nextDouble();
  15.             scanner.nextLine(); // consume the following newline character
  16.             meal.addIngredient(new Ingredient(name, price));
  17.             System.out.println("Do you want to add more ingredients? (enter stop to stop):");
  18.             input = scanner.nextLine();
  19.         }
  20.         System.out.println(meal);
  21.     }
  22. }
复制代码
微信图片_20230415160128.png

最佳答案

回复

使用道具 举报

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-27 16:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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