欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

策略模式+简单工厂之旅游出行策略与门票折扣案例

程序员文章站 2024-03-23 18:16:40
...

策略模式+简单工厂

(1)旅游出行策略(飞机,高铁,大巴,骑行,徒步至少两种出行方式)

出行类:

package gsg.gao4;
class AirplaneStrategy implements  TravelStrategy
{	
	public void travelMethod()
	{
		System.out.println("飞机游!");
	}
}

class TrainStrategy implements  TravelStrategy
{
	public void travelMethod()
	{
		System.out.println("火车游!");
	}
}

class BicycleTravelStrategy implements  TravelStrategy
{
	public void travelMethod()
	{
		System.out.println("大巴游!");
	}
}
class buxing implements  TravelStrategy
{
	public void travelMethod()
	{
		System.out.println("步行!");
	}
}

MyContext类:

package gsg.gao4;

public class MyContext
{
	private TravelStrategy ts;
	public MyContext(TravelStrategy ts)
	{
		this.ts=ts;
	}
	public void travelMethod()
	{
		ts.travelMethod();
	}
}
TravelStrategy类:
package gsg.gao4;

interface TravelStrategy
{
	
	public void travelMethod();

}
测试类;
package gsg.gao4;

import java.util.Scanner;

public class Client {
	public static void main(String args[])
	{
		System.out.println("请输入你的出行方式  1,飞机  2.火车  3.大巴 4.步行");
		
		//调用scoreInput方法,定义score接收用户输入值
        Scanner scoreInput = new Scanner(System.in);

		int score = scoreInput.nextInt();
		if (score==1) {
			MyContext mc=new MyContext(new AirplaneStrategy());
			mc.travelMethod();
		}else if (score==2) {
			MyContext mc=new MyContext(new TrainStrategy());
			mc.travelMethod();
		}else if (score==3) {
			MyContext mc=new MyContext(new BicycleTravelStrategy());
			mc.travelMethod();
		}else if (score==4) {
			MyContext mc=new MyContext(new buxing());
			mc.travelMethod();
		}
		
	}
}

结果:
策略模式+简单工厂之旅游出行策略与门票折扣案例

(2)门票折扣

AgedDiscount类:

package gsg.gao1;
public class AgedDiscount implements Discount {
	private final double DISCOUNT = 0.5;
	public double calculate(double price) {
		System.out.println("老人票:");
		return price * DISCOUNT;
	}
}
 ChildrenDiscount类;
package gsg.gao1;
public class ChildrenDiscount implements Discount {
	public final double DISCOUNT =0;
	public double calculate(double price) {
		System.out.println("儿童票:");
		if(price>=20) {
			return price*DISCOUNT;
		}
		else {
			return price;
		}
	}
}
Discount类:
package gsg.gao1;

public interface Discount {
	public double calculate(double price);

}

StudentDiscount类;

package gsg.gao1;
public class StudentDiscount implements Discount {
	private final double DISCOUNT = 0.8;
	public double calculate(double price) {
		System.out.println("学生票:");
		return price * DISCOUNT;
	}
}
Ticket类:
package gsg.gao1;
public class Ticket {
	private  double price;
	private Discount discount;
	public void setPrice(double price) {
		this.price = price;
	}
	public void setDiscount(Discount discount) {
		this.discount = discount;
	}
	public double getPrice() {
		return discount.calculate(this.price);
	}

}

测试类;

package gsg.gao1;

import java.util.Scanner;

public class Client {
	public static void main(String args[]) {
		Ticket tt = new Ticket();
		double orginalPrice=210.0;
		double currentPrice;
		tt.setPrice(orginalPrice);
		System.out.println("原始价为:" + orginalPrice);
		System.out.println("-------------");
		System.out.println("请输入你的票  1,老人票  2.学生票  3.儿童票");
	
		//调用scoreInput方法,定义score接收用户输入值
        Scanner scoreInput = new Scanner(System.in);

		int score = scoreInput.nextInt();
		if (score==1) {

		Discount discount;
		discount = new AgedDiscount();
		tt.setDiscount(discount);
		currentPrice = tt.getPrice();
		System.out.println("折后价为:"+currentPrice);
		}else if(score==2){
			Discount discount;
			discount = new StudentDiscount();
			tt.setDiscount(discount);
			currentPrice = tt.getPrice();
			System.out.println("折后价为:"+currentPrice);
		}else if (score==3) {
			Discount discount;
			discount = new ChildrenDiscount();
			tt.setDiscount(discount);
			currentPrice = tt.getPrice();
			System.out.println("折后价为:"+currentPrice);
		} 	}}

结果:
策略模式+简单工厂之旅游出行策略与门票折扣案例