咖啡店需要售卖各种各样的咖啡:黑咖啡,加糖,加冰,加奶,加巧克力,蒸汽咖啡,加压咖啡等等。顾客要买咖啡,他可以往咖啡里加任意一种或几种产品。请设计一个Java代码,对该问题采用面向对象思想进行建模,完成客户想要的咖啡,并可方便计算价钱。
时间: 2024-05-22 22:16:26 浏览: 13
以下是一个可能的Java代码实现:
// 咖啡类
public abstract class Coffee {
protected String description;
public String getDescription() {
return description;
}
public abstract double cost();
}
// 黑咖啡类
public class BlackCoffee extends Coffee {
public BlackCoffee() {
description = "Black Coffee";
}
public double cost() {
return 1.0;
}
}
// 加糖类
public class Sugar extends Coffee {
private Coffee coffee;
public Sugar(Coffee coffee) {
this.coffee = coffee;
}
public String getDescription() {
return coffee.getDescription() + ", Sugar";
}
public double cost() {
return 0.2 + coffee.cost();
}
}
// 加冰类
public class Ice extends Coffee {
private Coffee coffee;
public Ice(Coffee coffee) {
this.coffee = coffee;
}
public String getDescription() {
return coffee.getDescription() + ", Ice";
}
public double cost() {
return 0.3 + coffee.cost();
}
}
// 加奶类
public class Milk extends Coffee {
private Coffee coffee;
public Milk(Coffee coffee) {
this.coffee = coffee;
}
public String getDescription() {
return coffee.getDescription() + ", Milk";
}
public double cost() {
return 0.5 + coffee.cost();
}
}
// 加巧克力类
public class Chocolate extends Coffee {
private Coffee coffee;
public Chocolate(Coffee coffee) {
this.coffee = coffee;
}
public String getDescription() {
return coffee.getDescription() + ", Chocolate";
}
public double cost() {
return 0.4 + coffee.cost();
}
}
// 蒸汽咖啡类
public class SteamCoffee extends Coffee {
public SteamCoffee() {
description = "Steam Coffee";
}
public double cost() {
return 1.5;
}
}
// 加压咖啡类
public class PressureCoffee extends Coffee {
public PressureCoffee() {
description = "Pressure Coffee";
}
public double cost() {
return 2.0;
}
}
// 客户类
public class Customer {
public static void main(String[] args) {
Coffee coffee = new BlackCoffee();
coffee = new Sugar(coffee);
coffee = new Ice(coffee);
coffee = new Milk(coffee);
System.out.println(coffee.getDescription() + " $" + coffee.cost());
coffee = new SteamCoffee();
coffee = new Chocolate(coffee);
System.out.println(coffee.getDescription() + " $" + coffee.cost());
}
}
// 输出结果
// Black Coffee, Sugar, Ice, Milk $2.0
// Steam Coffee, Chocolate $1.9
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)