好文档就是一把金锄头!
欢迎来到金锄头文库![会员中心]
电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本

关于面向对象策略模式商场节假日打折的源代码.doc

3页
  • 卖家[上传人]:gg****m
  • 文档编号:286361543
  • 上传时间:2022-05-01
  • 文档格式:DOC
  • 文档大小:117.44KB
  • / 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 Test [Java Application] E:\Xhomi\Common\biMry\com.S5・j3b・jdlGwin32・x86-l・6・0・013\bin\j3bw・exe (2016-3-11 下去:45:09)254.99999523162842WritableSmart Insert国庆特惠价,支付350ontextjava 山 DiscountCash.java 』ReturnCash.java [£ estjava 必ic class Test {”/出农是总寺V®參个*8沏黑W工战乂二冬 xiblic static void aain(String[] args) {String type=M== CashContext context = new CashContext(type); double cash-context.getResult(500);System.out.pfintlnCcash);type :ei tx 第I 昌 aHRBHI s ▼ c ▼350.0。

      点击阅读更多内容
      关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
      手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
      ©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.