模拟肯德基卖餐戡题。有炸鸡腿汉堡戢乐薯戠戢以单个卖也戢以超值套餐炸鸡腿汉堡戢乐各一份顾客排队买餐戢以买炸鸡腿汉堡戢乐薯戠套餐。 类名 成员变量 方戞----------------------------------------------------------------------------------鸡腿D
代码
// 鸡腿类 class Drumstick { private double price; private String color; private String taste; private boolean fried; private String[] toppings;
public Drumstick(double price, String color, String taste) {
this.price = price;
this.color = color;
this.taste = taste;
this.fried = false;
this.toppings = new String[0];
}
public double getPrice() {
return price;
}
public String getColor() {
return color;
}
public String getTaste() {
return taste;
}
public boolean isFried() {
return fried;
}
public void fry() {
this.fried = true;
}
public void addTopping(String topping) {
String[] newToppings = new String[toppings.length + 1];
for (int i = 0; i < toppings.length; i++) {
newToppings[i] = toppings[i];
}
newToppings[toppings.length] = topping;
this.toppings = newToppings;
}
public String[] getToppings() {
return toppings;
}
}
// 汉堡类 class Hamburger { private double price; private double weight; private String[] toppings; private boolean heated; private boolean packaged;
public Hamburger(double price, double weight) {
this.price = price;
this.weight = weight;
this.toppings = new String[0];
this.heated = false;
this.packaged = false;
}
public double getPrice() {
return price;
}
public double getWeight() {
return weight;
}
public void addTopping(String topping) {
String[] newToppings = new String[toppings.length + 1];
for (int i = 0; i < toppings.length; i++) {
newToppings[i] = toppings[i];
}
newToppings[toppings.length] = topping;
this.toppings = newToppings;
}
public String[] getToppings() {
return toppings;
}
public boolean isHeated() {
return heated;
}
public void heat() {
this.heated = true;
}
public boolean isPackaged() {
return packaged;
}
public void package() {
this.packaged = true;
}
}
// 可乐类 class Cola { private double price; private String name; private double volume; private boolean iced; private boolean stirred;
public Cola(double price, String name, double volume) {
this.price = price;
this.name = name;
this.volume = volume;
this.iced = false;
this.stirred = false;
}
public double getPrice() {
return price;
}
public String getName() {
return name;
}
public double getVolume() {
return volume;
}
public boolean isIced() {
return iced;
}
public void ice() {
this.iced = true;
}
public boolean isStirred() {
return stirred;
}
public void stir() {
this.stirred = true;
}
}
// 薯条类 class Chips { private double price; private String color; private double weight; private boolean fried; private boolean cut;
public Chips(double price, String color, double weight) {
this.price = price;
this.color = color;
this.weight = weight;
this.fried = false;
this.cut = false;
}
public double getPrice() {
return price;
}
public String getColor() {
return color;
}
public double getWeight() {
return weight;
}
public boolean isFried() {
return fried;
}
public void fry() {
this.fried = true;
}
public boolean isCut() {
return cut;
}
public void cut() {
this.cut = true;
}
}
// 套餐类 class SetMeal { private double price; private Drumstick drumstick; private Hamburger hamburger; private Cola cola;
public SetMeal(double price, Drumstick drumstick, Hamburger hamburger, Cola cola) {
this.price = price;
this.drumstick = drumstick;
this.hamburger = hamburger;
this.cola = cola;
}
public double getPrice() {
return price;
}
public Drumstick getDrumstick() {
return drumstick;
}
public Hamburger getHamburger() {
return hamburger;
}
public Cola getCola() {
return cola;
}
}
// 顾客类 class Customer { private String name; private double money;
public Customer(String name, double money) {
this.name = name;
this.money = money;
}
public String getName() {
return name;
}
public double getMoney() {
return money;
}
public void buy(Drumstick drumstick) {
if (money >= drumstick.getPrice()) {
money -= drumstick.getPrice();
drumstick.fry();
System.out.println(name + "购买了一份炸鸡腿,价格为" + drumstick.getPrice() + "元。");
} else {
System.out.println(name + "的余额不足,无法购买炸鸡腿。");
}
}
public void buy(Hamburger hamburger) {
if (money >= hamburger.getPrice()) {
money -= hamburger.getPrice();
hamburger.heat();
hamburger.package();
System.out.println(name + "购买了一份汉堡,价格为" + hamburger.getPrice() + "元。");
} else {
System.out.println(name + "的余额不足,无法购买汉堡。");
}
}
public void buy(Cola cola) {
if (money >= cola.getPrice()) {
money -= cola.getPrice();
cola.ice();
cola.stir();
System.out.println(name + "购买了一杯可乐,价格为" + cola.getPrice() + "元。");
} else {
System.out.println(name + "的余额不足,无法购买可乐。");
}
}
public void buy(Chips chips) {
if (money >= chips.getPrice()) {
money -= chips.getPrice();
chips.fry();
chips.cut();
System.out.println(name + "购买了一份薯条,价格为" + chips.getPrice() + "元。");
} else {
System.out.println(name + "的余额不足,无法购买薯条。");
}
}
public void buy(SetMeal setMeal) {
if (money >= setMeal.getPrice()) {
money -= setMeal.getPrice();
Drumstick drumstick = setMeal.getDrumstick();
Hamburger hamburger = setMeal.getHamburger();
Cola cola = setMeal.getCola();
drumstick.fry();
hamburger.heat();
hamburger.package();
cola.ice();
cola.stir();
System.out.println(name + "购买了一份套餐,价格为" + setMeal.getPrice() + "元,包含炸鸡腿、汉堡和可乐。");
} else {
System.out.println(name + "的余额不足,无法购买套餐。");
}
}
}
// 测试代码 public class KFC { public static void main(String[] args) { // 创建食品对象 Drumstick drumstick = new Drumstick(10, "黄色", "香辣"); Hamburger hamburger = new Hamburger(15, 0.2); Cola cola = new Cola(5, "可口可乐", 0.5); Chips chips = new Chips(8, "金黄色", 0.3); SetMeal setMeal = new SetMeal(25, drumstick, hamburger, cola);
// 创建顾客对象
Customer customer1 = new Customer("张三", 30);
Customer customer2 = new Customer("李四", 20);
// 顾客1购买炸鸡腿
customer1.buy(drumstick);
// 顾客2购买汉堡
customer2.buy(hamburger);
// 顾客1购买可乐
customer1.buy(cola);
// 顾客2购买薯条
customer2.buy(chips);
// 顾客1购买套餐
customer1.buy(setMeal);
// 输出顾客余额
System.out.println(customer1.getName() + "的余额为" + customer1.getMoney() + "元。");
System.out.println(customer2.getName() + "的余额为" + customer2.getMoney() + "元。");
}
原文地址: https://www.cveoy.top/t/topic/g0vy 著作权归作者所有。请勿转载和采集!