请使用状态模式模拟咖啡自动售货机。
//环境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);
}
}