用Java写代码:某炸鸡快餐推出一个吉士汉堡套餐CheeseBurgerComboMeal:它由吉士汉堡CheeseBurger+可乐Cola+薯条FrenchFries组成,其中: 吉士汉堡有size(尺寸);price属性; 可乐有volume(容量);price属性; 薯条有type(large, medium, small)和price属性。 CheeseBurgerComboMeal本身也有一个价格属性。 所有数据访问属性均为private。为此需要公开get方法。 CheeseBurgerComboMeal中有一个showMessage方法。 在主函数中,测试创建CheeseBurgerComboMeal对象(先创建吉士汉堡、可乐、薯条对象,并传入CheeseBurgerComboMeal构造方法)并调用CheeseBurgerComboMeal的showMessage输出: 欢迎选用吉士汉堡套餐: 吉士汉堡:5寸;单价15元; 可乐:500ML, 单价8元; 薯条:large,单价8元; 套餐优惠价:22元,为您节省:9元。用5个窗口写
时间: 2024-02-26 19:55:24 浏览: 143
这是用Java写的计算器小程序
窗口1: CheeseBurger 类
```
public class CheeseBurger {
private int size;
private double price;
public CheeseBurger(int size, double price) {
this.size = size;
this.price = price;
}
public int getSize() {
return size;
}
public double getPrice() {
return price;
}
}
```
窗口2: Cola 类
```
public class Cola {
private int volume;
private double price;
public Cola(int volume, double price) {
this.volume = volume;
this.price = price;
}
public int getVolume() {
return volume;
}
public double getPrice() {
return price;
}
}
```
窗口3: FrenchFries 类
```
public class FrenchFries {
private String type;
private double price;
public FrenchFries(String type, double price) {
this.type = type;
this.price = price;
}
public String getType() {
return type;
}
public double getPrice() {
return price;
}
}
```
窗口4: CheeseBurgerComboMeal 类
```
public class CheeseBurgerComboMeal {
private CheeseBurger cheeseBurger;
private Cola cola;
private FrenchFries frenchFries;
private double price;
public CheeseBurgerComboMeal(CheeseBurger cheeseBurger, Cola cola, FrenchFries frenchFries) {
this.cheeseBurger = cheeseBurger;
this.cola = cola;
this.frenchFries = frenchFries;
this.price = cheeseBurger.getPrice() + cola.getPrice() + frenchFries.getPrice() - 9.0;
}
public double getPrice() {
return price;
}
public void showMessage() {
System.out.println("欢迎选用吉士汉堡套餐:");
System.out.println("吉士汉堡:" + cheeseBurger.getSize() + "寸;单价" + cheeseBurger.getPrice() + "元;");
System.out.println("可乐:" + cola.getVolume() + "ML, 单价" + cola.getPrice() + "元;");
System.out.println("薯条:" + frenchFries.getType() + ",单价" + frenchFries.getPrice() + "元;");
System.out.println("套餐优惠价:" + price + "元,为您节省:9元。");
}
}
```
窗口5:主函数
```
public class Main {
public static void main(String[] args) {
CheeseBurger cheeseBurger = new CheeseBurger(5, 15.0);
Cola cola = new Cola(500, 8.0);
FrenchFries frenchFries = new FrenchFries("large", 8.0);
CheeseBurgerComboMeal cheeseBurgerComboMeal = new CheeseBurgerComboMeal(cheeseBurger, cola, frenchFries);
cheeseBurgerComboMeal.showMessage();
}
}
```
阅读全文