今天是第二章策略模式:首先要明白策略模式的定义是什么?
定义:策略模式:定义了算法家族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化,不会影响到使用算法的客户。
今天就用Java写了一个简单版的商场收银软件v1.0
public class cashier {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double total = 0d; //总计
String product;
double price; //单价
int number; // 数量
String flag = "2";
while (true) {
System.out.println("请输入商品:");
product = sc.next();
System.out.println("请输入单价:");
price = sc.nextDouble();
System.out.println("请输入数量:");
number = sc.nextInt();
total = getTotal(product, price, number) + total;
System.out.println( "继续输入请按1: 结束请输入2");
if(sc.next().equals(flag)){
break;
}
}
System.out.println("总计是:" + total );
}
public static double getTotal(String product, double price, int number) {
double total = 0d;
total = price * number;
System.out.println("商品:" + product + " 总计是:" + total + "," + "单价是:" + price + ",数量是:" + number);
return total;
}
}



