请编写程序完成APP抽奖活动 具体要求如下:
假如每参加一次这个活动要扣除用户50积分,中奖概率是10%
奖品数量固定,抽完就不能抽奖
活动有四个状态:可以抽奖、不能抽奖、发放奖品和奖品领完
活动的四个状态转换关系图(下图)
package com.lm.state;
/**
* 抽象状态类
*/
public abstract class State {
/**
* 扣除积分 - 50
*/
public abstract void deduceMoney();
/**
* 是否抽中奖品
* @return
*/
public abstract boolean raffle();
/**
* 发放奖品
*/
public abstract void dispensePrize();
}
package com.lm.state;
/**
* 不能抽奖的状态
*/
public class NoRaffleState extends State {
// 初始化传入活动引用,扣除积分后改变其状态
RaffleActivity activity;
public NoRaffleState(RaffleActivity activity) {
this.activity = activity;
}
// 当前状态可以扣积分,扣除后,将状态设置成可以抽奖的状态
@Override
public void deduceMoney() {
System.out.println("扣除50积分成功,您可以抽奖了");
activity.setState(activity.getCanRaffleState());
}
// 当前状态不能抽奖
@Override
public boolean raffle() {
System.out.println("扣了积分才能抽奖喔!");
return false;
}
// 当前状态不能发奖品
@Override
public void dispensePrize() {
System.out.println("不能发放奖品");
}
}
package com.lm.state;
import java.util.Random;
/**
* 可以抽奖的状态
*/
public class CanRaffleState extends State {
// 初始化传入活动引用,扣除积分后改变其状态
RaffleActivity activity;
public CanRaffleState(RaffleActivity activity) {
this.activity = activity;
}
// 已经扣除了积分,不能再扣
@Override
public void deduceMoney() {
System.out.println("已经扣取过了积分");
}
// 可以抽奖,抽完奖后,根据实际情况,改成新的状态
@Override
public boolean raffle() {
System.out.println("正在抽奖,请稍等!");
Random r = new Random();
int num = r.nextInt(10);
// 10%中奖机会
if (num == 0) {
// 改变活动状态为发放奖品
activity.setState(activity.getDispenseState());
return true;
} else {
System.out.println("很遗憾没有抽中奖品!");
// 改变状态为不能抽奖
activity.setState(activity.getNoRaffleState());
return false;
}
}
// 不能发放奖品
@Override
public void dispensePrize() {
System.out.println("没中奖,不能发放奖品");
}
}
package com.lm.state;
/**
* 发放奖品的状态
*/
public class DispenseState extends State {
// 初始化传入活动引用,发放奖品后改变其状态
RaffleActivity activity;
public DispenseState(RaffleActivity activity) {
this.activity = activity;
}
@Override
public void deduceMoney() {
System.out.println("不能扣除积分");
}
@Override
public boolean raffle() {
System.out.println("不能抽奖");
return false;
}
// 发放奖品
@Override
public void dispensePrize() {
if (activity.getCount() > 0) {
System.out.println("恭喜中奖了");
// 改变状态为不能抽奖
activity.setState(activity.getNoRaffleState());
} else {
System.out.println("很遗憾,奖品发送完了");
// 改变状态为奖品发送完毕,后面我们就不可以抽奖
// activity.setState(activity.getDispenseOutState());
//或者直接退出程序
System.out.println("抽奖活动结束");
System.exit(0);
}
}
}
package com.lm.state;
/**
* 奖品发放完毕状态
* 说明,当我们activity 改变成 DispenseOutState,抽奖活动结束
*/
public class DispenseOutState extends State {
// 初始化传入活动引用
RaffleActivity activity;
public DispenseOutState(RaffleActivity activity) {
this.activity = activity;
}
@Override
public void deduceMoney() {
System.out.println("奖品发送完了,请下次参加");
}
@Override
public boolean raffle() {
System.out.println("奖品发送完了,请下次参加");
return false;
}
@Override
public void dispensePrize() {
System.out.println("奖品发送完了,请下次参加");
}
}
package com.lm.state;
/**
* 抽奖活动
*/
public class RaffleActivity {
// state 表示活动当前的状态,是变化的
State state = null;
// 奖品数量
int count = 0;
// 四个属性,表示四种状态
NoRaffleState noRaffleState = new NoRaffleState(this);
CanRaffleState canRaffleState = new CanRaffleState(this);
DispenseState dispenseState = new DispenseState(this);
DispenseOutState dispenseOutState = new DispenseOutState(this);
//构造器
//1.初始化当前状态为 noRaffleState(即不能抽奖的状态)
//2.初始化奖品的数量
public RaffleActivity(int count) {
this.state = getNoRaffleState();
this.count = count;
}
// 扣分,调用当前状态的 deduceMoney
public void deduceMoney() {
state.deduceMoney();
}
// 抽奖
public void raffle() {
// 如果当前状态是抽奖成功
if (state.raffle()) {
// 领取奖品
state.dispensePrize();
}
}
public State getState() {
return state;
}
public void setState(State state) {
this.state = state;
}
//这里请大家注意,每领取一次奖品,count--
public int getCount() {
int curCount = count;
count--;
return curCount;
}
public void setCount(int count) {
this.count = count;
}
public NoRaffleState getNoRaffleState() {
return noRaffleState;
}
public void setNoRaffleState(NoRaffleState noRaffleState) {
this.noRaffleState = noRaffleState;
}
public CanRaffleState getCanRaffleState() {
return canRaffleState;
}
public void setCanRaffleState(CanRaffleState canRaffleState) {
this.canRaffleState = canRaffleState;
}
public DispenseState getDispenseState() {
return dispenseState;
}
public void setDispenseState(DispenseState dispenseState) {
this.dispenseState = dispenseState;
}
public DispenseOutState getDispenseOutState() {
return dispenseOutState;
}
public void setDispenseOutState(DispenseOutState dispenseOutState) {
this.dispenseOutState = dispenseOutState;
}
}
package com.lm.state;
/**
* 状态模拟测试
*/
public class Client {
public static void main(String[] args) {
//创建活动对象,奖品池有1个奖品
RaffleActivity activity = new RaffleActivity(1);
// 我们连续抽300次奖
for (int i = 0; i < 30; i++) {
System.out.println("--------第 " + (i+1) + " 次抽奖--------");
// 参加抽奖,第一步点击扣除积分
activity.deduceMoney();
// 第二步抽奖
activity.raffle();
}
}
}
可能输出结果如下:
--------第 1 次抽奖--------
扣除50积分成功,您可以抽奖了
正在抽奖,请稍等!
很遗憾没有抽中奖品!
--------第 2 次抽奖--------
扣除50积分成功,您可以抽奖了
正在抽奖,请稍等!
很遗憾没有抽中奖品!
--------第 3 次抽奖--------
扣除50积分成功,您可以抽奖了
正在抽奖,请稍等!
恭喜中奖了
--------第 4 次抽奖--------
扣除50积分成功,您可以抽奖了
正在抽奖,请稍等!
很遗憾没有抽中奖品!
--------第 5 次抽奖--------
扣除50积分成功,您可以抽奖了
正在抽奖,请稍等!
很遗憾没有抽中奖品!
--------第 6 次抽奖--------
扣除50积分成功,您可以抽奖了
正在抽奖,请稍等!
很遗憾没有抽中奖品!
--------第 7 次抽奖--------
扣除50积分成功,您可以抽奖了
正在抽奖,请稍等!
很遗憾没有抽中奖品!
--------第 8 次抽奖--------
扣除50积分成功,您可以抽奖了
正在抽奖,请稍等!
很遗憾,奖品发送完了
抽奖活动结束
package com.lm.state.money;
/**
* 状态接口
*/
public interface State {
/**
* 电审
* @param context
*/
void checkEvent(Context context);
/**
* 电审失败
* @param context
*/
void checkFailEvent(Context context);
/**
* 定价发布
* @param context
*/
void makePriceEvent(Context context);
/**
* 接单
* @param context
*/
void acceptOrderEvent(Context context);
/**
* 无人接单失效
* @param context
*/
void notPeopleAcceptEvent(Context context);
/**
* 付款
* @param context
*/
void payOrderEvent(Context context);
/**
* 接单有人支付失败
* @param context
*/
void orderFailureEvent(Context context);
/**
* 反馈
* @param context
*/
void feedBackEvent(Context context);
String getCurrentState();
}
package com.lm.state.money;
public enum StateEnum {
//订单生成
GENERATE(1, "GENERATE"),
//已审核
REVIEWED(2, "REVIEWED"),
//已发布
PUBLISHED(3, "PUBLISHED"),
//待付款
NOT_PAY(4, "NOT_PAY"),
//已付款
PAID(5, "PAID"),
//已完结
FEED_BACKED(6, "FEED_BACKED");
private int key;
private String value;
StateEnum(int key, String value) {
this.key = key;
this.value = value;
}
public int getKey() {
return key;
}
public String getValue() {
return value;
}
}
package com.lm.state.money;
public abstract class AbstractState implements State {
protected static final RuntimeException EXCEPTION = new RuntimeException("操作流程不允许");
//抽象类,默认实现 State 接口的所有方法
//该类的所有方法,其子类(具体的状态类),可以有选择的进行重写
@Override
public void checkEvent(Context context) {
throw EXCEPTION;
}
@Override
public void checkFailEvent(Context context) {
throw EXCEPTION;
}
@Override
public void makePriceEvent(Context context) {
throw EXCEPTION;
}
@Override
public void acceptOrderEvent(Context context) {
throw EXCEPTION;
}
@Override
public void notPeopleAcceptEvent(Context context) {
throw EXCEPTION;
}
@Override
public void payOrderEvent(Context context) {
throw EXCEPTION;
}
@Override
public void orderFailureEvent(Context context) {
throw EXCEPTION;
}
@Override
public void feedBackEvent(Context context) {
throw EXCEPTION;
}
}
package com.lm.state.money;
//各种具体状态类
/**
* 反馈
*/
class FeedBackState extends AbstractState {
@Override
public String getCurrentState() {
return StateEnum.FEED_BACKED.getValue();
}
}
/**
* 通用
*/
class GenerateState extends AbstractState {
@Override
public void checkEvent(Context context) {
context.setState(new ReviewState());
}
@Override
public void checkFailEvent(Context context) {
context.setState(new FeedBackState());
}
@Override
public String getCurrentState() {
return StateEnum.GENERATE.getValue();
}
}
/**
* 没付款状态
*/
class NotPayState extends AbstractState {
@Override
public void payOrderEvent(Context context) {
context.setState(new PaidState());
}
@Override
public void feedBackEvent(Context context) {
context.setState(new FeedBackState());
}
@Override
public String getCurrentState() {
return StateEnum.NOT_PAY.getValue();
}
}
/**
* 已付款状态
*/
class PaidState extends AbstractState {
@Override
public void feedBackEvent(Context context) {
context.setState(new FeedBackState());
}
@Override
public String getCurrentState() {
return StateEnum.PAID.getValue();
}
}
/**
* 发布状态
*/
class PublishState extends AbstractState {
@Override
public void acceptOrderEvent(Context context) {
//把当前状态设置为 NotPayState
//至于应该变成哪个状态,有流程图来决定
context.setState(new NotPayState());
}
@Override
public void notPeopleAcceptEvent(Context context) {
context.setState(new FeedBackState());
}
@Override
public String getCurrentState() {
return StateEnum.PUBLISHED.getValue();
}
}
/**
* 回顾状态
*/
class ReviewState extends AbstractState {
@Override
public void makePriceEvent(Context context) {
context.setState(new PublishState());
}
@Override
public String getCurrentState() {
return StateEnum.REVIEWED.getValue();
}
}
package com.lm.state.money;
/**
* 环境上下文
*/
public class Context extends AbstractState {
//当前的状态 state,根据我们的业务流程处理,不停的变化
private State state;
@Override
public void checkEvent(Context context) {
state.checkEvent(this);
getCurrentState();
}
@Override
public void checkFailEvent(Context context) {
state.checkFailEvent(this);
getCurrentState();
}
@Override
public void makePriceEvent(Context context) {
state.makePriceEvent(this);
getCurrentState();
}
@Override
public void acceptOrderEvent(Context context) {
state.acceptOrderEvent(this);
getCurrentState();
}
@Override
public void notPeopleAcceptEvent(Context context) {
state.notPeopleAcceptEvent(this);
getCurrentState();
}
@Override
public void payOrderEvent(Context context) {
state.payOrderEvent(this);
getCurrentState();
}
@Override
public void orderFailureEvent(Context context) {
state.orderFailureEvent(this);
getCurrentState();
}
@Override
public void feedBackEvent(Context context) {
state.feedBackEvent(this);
getCurrentState();
}
public State getState() {
return state;
}
public void setState(State state) {
this.state = state;
}
@Override
public String getCurrentState() {
System.out.println("当前状态是:" + state.getCurrentState());
return state.getCurrentState();
}
}
package com.lm.state.money;
public class Client {
public static void main(String[] args) {
//创建context对象
Context context = new Context();
//将当前状态设置为 PublishState
context.setState(new PublishState());
System.out.println(context.getCurrentState());
context.acceptOrderEvent(context);
context.payOrderEvent(context);
context.checkEvent(context);
}
}
输出结果如下:
当前状态是:PUBLISHED
PUBLISHED
当前状态是:NOT_PAY
当前状态是:PAID
Exception in thread "main" java.lang.RuntimeException: 操作流程不允许
at com.lm.state.money.AbstractState.<clinit>(AbstractState.java:5)
at com.lm.state.money.Client.main(Client.java:7)