招代理网站建设公司,湖北长安建设集团官方网站,wordpress 采集器,谷歌搜索引擎在线任务二#xff1a;使用策略模式设计电影票打折功能
某电影院售标系统为不同类型的用户提供了不同的打折方式#xff08;Discount#xff09;#xff0c;学生凭学生证可享受8折优惠**#xff08;StudentDiscount#xff09;#xff0c;儿童可享受减免10元的优惠#xf…任务二使用策略模式设计电影票打折功能
某电影院售标系统为不同类型的用户提供了不同的打折方式Discount学生凭学生证可享受8折优惠**StudentDiscount儿童可享受减免10元的优惠ChildrenDiscountVIP用户除享受半价优惠外还可以进行积分VIPDiscount**。使用策略模式设计该系统。
UML-class i
1 * ClassName: Ticket * Description: TODO * Author ZSC * Date 2023/12/4 11:32 * Version 1.0 public class Ticket {private double price;private Discount discount;public double getPrice() {return discount.calculate(this.price);}public void setPrice(double price) {this.price price;}public void setDiscount(Discount discount) {this.discount discount;} } 2
public interface Discount {public double calculate(double price);
}3 Overridepublic double calculate(double price) {return price-10;} }public class StuDiscount implements Discount{Overridepublic double calculate(double price) {return price*0.8;} }public class VIPDiscount implements Discount{Overridepublic double calculate(double price) {System.out.println(提示增加积分);return price*0.5;} } 4
public class TicketMain {public static void main(String[] args) {// discoutdouble disPrice;Ticket ticket new Ticket();// set priceticket.setPrice(66.66);// stuDiscount stuDiscount new StuDiscount();ticket.setDiscount(stuDiscount);disPrice ticket.getPrice();System.out.println(stu--折后价格为disPrice);// chilDiscount chilDiscount new ChilDiscount();ticket.setDiscount(chilDiscount);disPrice ticket.getPrice();System.out.println(childen--折后票价为disPrice);System.out.println();// VIPDiscount vipDiscount new VIPDiscount();ticket.setDiscount(vipDiscount);disPrice ticket.getPrice();System.out.println(VIP会员---折后票价为disPrice);}
}reference
【[参考文献]】