strategymode.java
package org.ustc.strategy;
public class StrategyMode {
private CashSuper cs = null ;
public CashSuper getCs() {
return cs;
}
public void setCs(CashSuper cs) {
this.cs = cs;
}
public StrategyMode(String cash) {
// TODO Auto-generated constructor stub
char letter = cash.charAt(0);
switch(letter){
case '正':
cs = new CashNormal();
break;
case '满':
cs = new CashReturn(300,100);
break;
case '打':
cs = new CashDazhe(0.8);
break;
}
}
public double getResult(int money){
return cs.acceptCash(money);
}
}
package org.ustc.strategy;
public interface CashSuper {
public double acceptCash(int money);
}
实现类
package org.ustc.strategy;
public class CashDazhe implements CashSuper {
private double zhekou ;
//打折
public CashDazhe(double zhekou) {
super();
this.zhekou = zhekou;
}
public double getZhekou() {
return zhekou;
}
public void setZhekou(double zhekou) {
this.zhekou = zhekou;
}
@Override
public double acceptCash(int money) {
// TODO Auto-generated method stub
return money*zhekou;
}
}
package org.ustc.strategy;
public class CashNormal implements CashSuper {
@Override
public double acceptCash(int money) {
// TODO Auto-generated method stub
return money;
}
}
package org.ustc.strategy;
public class CashReturn implements CashSuper {
private int man;
private int huan;
//满?还?
public CashReturn(int man, int huan) {
super();
this.man = man;
this.huan = huan;
}
public int getMan() {
return man;
}
public void setMan(int man) {
this.man = man;
}
public int getHuan() {
return huan;
}
public void setHuan(int huan) {
this.huan = huan;
}
@Override
public double acceptCash(int money) {
// TODO Auto-generated method stub
return money - money/man*huan;
}
}
main.java
package org.ustc.strategy;
public class Main {
public static void main(String[] args) {
StrategyMode str = new StrategyMode("满300反100");
double cash = str.getResult(1000);
System.out.println("应付款项"+cash);
}
}
UML