2021-06-14 设计模式状态模式

请使用状态模式模拟咖啡自动售货机。
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

//环境Context
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class AutoCoffeeMachine extends JFrame{
	State haveCoffeeNoCoin,haveCoffeeAndCoin,haveNotCaffee;
	State state;
	JButton putIncoin,getCaffee;
	JLabel messShowing;
	int caffeeCount;
	//记录一共有多少杯咖啡
	AutoCoffeeMachine(int caffeeCount){
		this.caffeeCount = caffeeCount;
		haveCoffeeNoCoin = new HaveCoffeeNoCoin(this);
		haveCoffeeAndCoin= new HaveCoffeeAndCoin( this);
		haveNotCaffee = new HaveNotCaffee(this);
		state = haveCoffeeNoCoin;
		putIncoin = new JButton("投币");
		getCaffee = new JButton("取咖啡");
		putIncoin.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent exp){
		if(state == haveCoffeeNoCoin){
		state=haveCoffeeAndCoin;
		messShowing.setText("您投币一元");
		}else if(state==haveCoffeeAndCoin){
		messShowing.setText("您已经投币一元,请取咖啡");
		}else if(state==haveNotCaffee){
		messShowing.setText("没有咖啡,无法投币");
		messShowing. setIcon(new ImageIcon("no.jpg"));
		}}});
		getCaffee=new JButton("取咖啡");
		getCaffee.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				giveAnCupCaffee();
			}});
		messShowing=new JLabel();
		messShowing.setFont(new Font("",Font. BOLD,20));
		JPanel pSouth = new JPanel();
		pSouth.add(putIncoin);
		pSouth.add(getCaffee);
		add(messShowing,BorderLayout.CENTER);
		add(pSouth,BorderLayout. SOUTH);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	public void giveAnCupCaffee(){
		state. giveAnCupCaffee();
		}
	public void showMessage(){
		state. showMessage();
	}
	public void setState( State state){
		this.state= state;
	}
	public State getHaveCoffeeNoCoin(){
		return haveCoffeeNoCoin;
	}
    public State getHaveCoffeeAndCoin(){
		return haveCoffeeAndCoin;
	}
    public State getHaveNotCaffee(){
		return haveNotCaffee;
	}
    public int getCaffeeCount(){
		return caffeeCount;
	}
	public void setCaffeeCount(int n){
		caffeeCount = n;
	}
}

//抽象状态 State
public abstract class State {
	public abstract void giveAnCupCaffee();
	public abstract void showMessage();

}

//具体状态 ConcreteState
public class HaveCoffeeNoCoin extends State{
	AutoCoffeeMachine machine;
	HaveCoffeeNoCoin(AutoCoffeeMachine machine){
	this. machine = machine;
	}
	@Override
	public void giveAnCupCaffee() {
		showMessage();

	}

	@Override
	public void showMessage() {
		machine.messShowing.setText("请投入一枚一元硬币");
	}

}

//具体状态 ConcreteState
public class HaveCoffeeAndCoin extends State{

	private AutoCoffeeMachine machine;

	HaveCoffeeAndCoin(AutoCoffeeMachine machine){
	this.machine=machine;
	}

	@Override
	public void giveAnCupCaffee() {
		int count = machine.getCaffeeCount();
		if(count ==0){
			machine.setState(machine.getHaveNotCaffee());
			machine.messShowing.setText("没有咖啡了");
		}else if(count== 1){
			machine.setCaffeeCount(count-1);
			showMessage();
			machine.setState(machine.getHaveNotCaffee());
		}else{
			machine.setCaffeeCount(count-1);
			showMessage();
			machine. setState(machine. getHaveCoffeeNoCoin());
			}
	}
		
	@Override
	public void showMessage() {
		machine.messShowing. setText("您得到一杯咖啡");
		
	}

}

//具体状态 ConcreteState
public class HaveNotCaffee extends State{
	AutoCoffeeMachine machine;
	HaveNotCaffee(AutoCoffeeMachine machine){
	this.machine = machine;
	}

	@Override
	public void giveAnCupCaffee() {
		machine.putIncoin.setEnabled(false);
		machine. getCaffee.setEnabled(false);
		showMessage();
		
	}

	@Override
	public void showMessage() {
		machine.messShowing. setText("目前机器中没有咖啡");

	}
	

}

public class Application {
	public static void main(String args[]){
		AutoCoffeeMachine autoCoffeeMachine=new AutoCoffeeMachine(3);
		autoCoffeeMachine.setBounds(12, 12, 200, 200);
	}
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值