1. // 现金收费抽象类
    2. public abstract class CashSuper {
    3. public abstract double acceptCash (Double money);
    4. }
    5. // 此类是正常付款,没有任何折扣返利。。。。
    6. public class CashNormal extends CashSuper {
    7. public double acceptCash(Double money) {
    8. return money;
    9. }
    10. }
    11. // 此类是对各种打折抽象
    12. public class CashRebate extends CashSuper{
    13. private String moneyRebate;
    14. double moneyRebate_d;
    15. public CashRebate(String moneyRebate) {
    16. moneyRebate_d = Double.parseDouble(moneyRebate);
    17. }
    18. public double acceptCash(Double money) {
    19. return money * moneyRebate_d;
    20. }
    21. }
    22. // 满减
    23. public class CashReturn extends CashSuper{
    24. // 300 -100
    25. private double moneyCondition;
    26. private double moenyReturn;
    27. public CashReturn(String moneyCondition, String moenyReturn) {
    28. super();
    29. this.moneyCondition = Double.parseDouble(moneyCondition);
    30. this.moenyReturn = Double.parseDouble(moenyReturn) ;
    31. }
    32. public double acceptCash(Double money) {
    33. if(money>moneyCondition) {
    34. return money-Math.floor(money/moneyCondition)*moenyReturn;
    35. }
    36. return money;
    37. }
    38. }
    39. // 策略模式与工厂模式的相结合
    40. public class CashContext {
    41. CashSuper cashSuper;
    42. private String type;
    43. public CashContext(String type) {
    44. this.type=type;
    45. switch (type) {
    46. case "正常":
    47. cashSuper=new CashNormal();
    48. break;
    49. case "三折":
    50. cashSuper=new CashRebate("0.8");
    51. break;
    52. case "返现金":
    53. cashSuper=new CashReturn("300","100");
    54. break;
    55. default:
    56. break;
    57. }
    58. }
    59. public double getResult(Double money) {
    60. return cashSuper.acceptCash(money);
    61. }
    62. }