
关于面向对象策略模式商场节假日打折的源代码.doc
3页商场节假日打折…00面向对象之策略模式130802031023信管3班 田雪艳1、 父类publicinterface SuperCash {//得到算过后的结账金额publicdouble getcash(double cash);}2、 3个了类1) 平日收费publicclass NormalCash implements SuperCash{publicdouble getcash(double cash) {return cash;}}2) 折扣算法,3・8打折、5 4打折publicclass DiscountCash implements SuperCash{ privatef loatmoneyRebate; //打折的折扣 public DiscountCash(float moneyRebate){ this.moneyRebaoneyRebate;}publicdouble getcash(double cash) { returnmoneyRebate*cash;}}3) 满赠算法,十一特惠publicclass ReturnCash implements SuperCash{privatefloatupcash;// •百五privatefloatdowncash;//71十public ReturnCash(float upcash^float downcash){this•upcash=upcash; this.downcash二downcash;}publicdouble getcash(double cash) { double result二cash;if(cash>=upcash){result=cash-(Math./Loor(cash/upcash)*downcash);return result;}}3、 结账算法选择publicclass CashContext {private SuperCash supercash;public CashContext(String type){//超市收款用多个算法类來进行使用和选择if(”平日收费 H.equals(type)){supercash=new NormalCash();}elseif(type.matches("3.8 妇女节,打・+折”)){float cash=Float.parseFLoat(type.replaceAll("五一劳动节,打(・ + )折”,supercash=new DiscountCash(cash/10);}elseif(type.matches("五一劳动节,打・ +折”)){float cash=Float.parseFLoat(type.replaceAll("五一劳动节,打(・+)折”,supercash=new DiscountCash(cash/10);}elseif(type.matches("国庆节特惠,满・+送・+")){float cashup=Float.parseFLoat(type. replaceAll("国庆节特惠,满(•+)送・+",”$「));float cashdown=Float.parseFLoat(type・replaceAll("国庆节特惠,满・ +送(・+)"_»”$「));supercash=new ReturnCash(cashup,cashdown);}}publicdouble getResult(double cash){returnsupercash.getcash(cash);}}4、 客户端用例测试publicclass Test {publicstaticvoid main(String[] args) {// String type="平日收费”;//正常收费String type=H五一劳动节,打5・1折”;//输出5・1折价格// String type=H3.8妇女节,H 3.8折”;〃输出3・8折价格// String type=H国庆特惠,满150送50”;//输出国庆特惠价CashContext context = new CashContext(type);double cash=context.getResult(500);System.out•println(cash);5、以总金额为500举例,代码结果截图五一应收:254.9IJI C^shContextjava [£| DiscountCashjava [JJ KeturnCash.java LU i est.java 必 s public class Test {//土旦辰転幣坎•、电侈个篡£灵耒电仔殳两岌巳任 public static void main(String[] args) { 〃 String type-"^EaM";//SX^JtString type""五THE・刀5・1癸”;〃輸±5・W 咨 // String type=“3・8気女苇.打3・8护・;//建出3・8$价帝// String type•"支庆-g ®1.5&N50・;//■匕壬庆峥鼻价 CashContext context = new Cashcontext(type); double cash«context.getResult(50G); Syste■・out・p「intln(vosh);丰 Outline W ■B艮&疋。
G. Test0 5 ma!n(String(J): void}type filter text♦ 0・3s0.2KA':j Problems @ Javadoc 曙 Declaration 曰 Console S3












