import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.*;
public class ASM implements ActionListener {
/**
* @param args
*/
JPanel jp;
Drink k1,k2,k3,k4,k5,k6;//饮料
OpeModel ope;
Admin adm;
int k1amt,k2amt, k3amt, k4amt, k5amt, k6amt;
double cost;//花费总额
public static void main(String args[]){
ASM a=new ASM();
a.gosale();
JOptionPane.showMessageDialog(null, "首次启动设备,请先添加饮料");
a.goadmin();
try {
while(true){
System.out.println("please input your password:");
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));
String password = null;
password = reader.readLine();
if(password.equals("admin"))
a.goadmin();
else
System.out.println("请正确输入密码,进入管理界面!");
}
}catch (IOException ex) {
JOptionPane.showMessageDialog(null, "请正确输入密码,进入管理界面");
}
}
//进入管理员界面
public void goadmin(){
adm=new Admin();
//显示饮料现有数量
adm.jlamt1.setText(Integer.toString(k1amt));
adm.jlamt2.setText(Integer.toString(k2amt));
adm.jlamt3.setText(Integer.toString(k3amt));
adm.jlamt4.setText(Integer.toString(k4amt));
adm.jlamt5.setText(Integer.toString(k5amt));
adm.jlamt6.setText(Integer.toString(k6amt));
//给添加按钮添加事
adm.jbadd1.addActionListener(this);
adm.jbadd2.addActionListener(this);
adm.jbadd3.addActionListener(this);
adm.jbadd4.addActionListener(this);
adm.jbadd5.addActionListener(this);
adm.jbadd6.addActionListener(this);
}
//进入售货界面
public void gosale() {
// TODO Auto-generated method stub
//定义顶层的框架和顶层的容器
JFrame jf=new JFrame("饮料售货机");
JPanel contentpane=new JPanel();
//定义顶层容器的布局
contentpane.setLayout(new BorderLayout());
//开始展示的饮料GUI
jp=new JPanel();
k1=new Drink("pic/1.jpg","可口可乐",2);
k1.p.setVisible(false);
k1.b.addActionListener(this);
k2=new Drink("pic/2.jpg","雪碧",2);
k2.p.setVisible(false);
k2.b.addActionListener(this);
k3=new Drink("pic/3.jpg","王老吉",3.5);
k3.p.setVisible(false);
k3.b.addActionListener(this);
k4=new Drink("pic/4.jpg","雀巢咖啡",2.5);
k4.p.setVisible(false);
k4.b.addActionListener(this);
k5=new Drink("pic/5.jpg","百事可乐",2);
k5.p.setVisible(false);
k5.b.addActionListener(this);
k6=new Drink("pic/6.jpg","红牛",5.5);
k6.p.setVisible(false);
k6.b.addActionListener(this);
jp.setLayout(new GridLayout(2,3));
jp.add(k1.p);
jp.add(k2.p);
jp.add(k3.p);
jp.add(k4.p);
jp.add(k5.p);
jp.add(k6.p);
jp.setSize(600, 330);
//结束展示的饮料GUI
//开始操作GUI
ope=new OpeModel();
ope.getDrinkButton.addActionListener(this);
ope.getChangeButton.addActionListener(this);
ope.heatButton.addActionListener(this);
ope.coolButton.addActionListener(this);
//结束操作GUI
//开始向顶层窗口添加饮料界面和操作界面
contentpane.add(jp,BorderLayout.CENTER);
contentpane.add(ope.opePanel,BorderLayout.EAST);
//结束向顶层窗口添加饮料界面和操作界面
jf.setContentPane(contentpane);
jf.setSize(1000,350);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//事件响应方法
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//-------------------售货界面的事件开始---------------------------------
//改变显示的饮料数量
if(e.getSource()==k1.b|e.getSource()==k2.b|e.getSource()==k3.b|e.getSource()==k4.b|e.getSource()==k5.b|e.getSource()==k6.b)
amount(e);
//显示花费的钱数
ope.costTex.setText(Double.toString(cost));
//求投入的钱数
String str=ope.moneyTex.getText().toString();
double money=verMoney(str);
//硬币无法识别
if(money<0)
{
ope.warningLab.setText("硬币无法识别,请重投");
ope.moneyTex.setText("");
}
//已投入硬币
else if(money>0){
funcs(e,money);
}
//末投入硬币
else
{
ope.coolButton.disable();
ope.heatButton.disable();
ope.getChangeButton.disable();
ope.getDrinkButton.disable();
ope.warningLab.setText("请先投入硬币");
}
//-------------------售货界面的事件结束---------------------------------
//-------------------管理界面事件开始---------------------------
if(e.getSource()==adm.jbadd1){
int addamt=Integer.parseInt(adm.jtadd1.getText().toString());
k1amt=k1amt+addamt;
if(k1amt==0)k1.p.setVisible(false);
adm.jlamt1.setText(Integer.toString(k1amt));
if(k1amt>0) k1.p.setVisible(true);
adm.jtadd1.setText("0");
}
if(e.getSource()==adm.jbadd2){
int addamt=Integer.parseInt(adm.jtadd2.getText().toString());
k2amt=k2amt+addamt;
if(k2amt==0)k2.p.setVisible(false);
adm.jlamt2.setText(Integer.toString(k2amt));
if(k2amt>0) k2.p.setVisible(true);
adm.jtadd2.setText("0");
}
if(e.getSource()==adm.jbadd3){
int addamt=Integer.parseInt(adm.jtadd3.getText().toString());
k3amt=k3amt+addamt;
if(k3amt==0)k3.p.setVisible(false);
adm.jlamt3.setText(Integer.toString(k3amt));
if(k3amt>0) k3.p.setVisible(true);
adm.jtadd3.setText("0");
}
if(e.getSource()==adm.jbadd4){
int addamt=Integer.parseInt(adm.jtadd4.getText().toString());
k4amt=k4amt+addamt;
if(k4amt==0)k4.p.setVisible(false);
adm.jlamt4.setText(Integer.toString(k4amt));
if(k4amt>0) k4.p.setVisible(true);
adm.jtadd4.setText("0");
}
if(e.getSource()==adm.jbadd5){
int addamt=Integer.parseInt(adm.jtadd5.getText().toString());
k5amt=k5amt+addamt;
if(k5amt==0)k5.p.setVisible(false);
adm.jlamt5.setText(Integer.toString(k5amt));
if(k5amt>0) k5.p.setVisible(true);
adm.jtadd5.setText("0");
}
if(e.getSource()==adm.jbadd6){
int addamt=Integer.parseInt(adm.jtadd6.getText().toString());
k6amt=k6amt+addamt;
if(k6amt==0)k6.p.setVisible(false);
adm.jlamt6.setText(Integer.toString(k6amt));
if(k6amt>0) k6.p.setVisible(true);
adm.jtadd6.setText("0");
}
//-------------------管理界面事件结束---------------------------
}
private double verMoney(String str) {
// TODO Auto-generated method stub
double money=-1;
try
{
money=Double.parseDouble(str);
}
catch(Exception ex){
ope.warningLab.setText("硬币无法识别,请重投");
//JOptionPane.showMessageDialog(null,"硬币无法识别,请重投");
}
return money;
}
private void amount(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==k1.b){
if(k1.amout<k1amt)
k1.t.setText(++k1.amout+"瓶");
else
k1.lname.setText("只有"+k1.amout+"瓶");
}
if(e.getSource()==k2.b){
if(k2.amout<k2amt)
k2.t.setText(++k2.amout+"瓶");
else
k2.lname.setText("只有"+k2.amout+"瓶");
}
if(e.getSource()==k3.b){
if(k3.amout<k3amt)
k3.t.setText(++k3.amout+"瓶");
else
k3.lname.setText("只有"+k3.amout+"瓶");
}
if(e.getSource()==k4.b){
if(k4.amout<k4amt)
k4.t.setText(++k4.amout+"瓶");
else
k4.lname.setText("只有"+k4.amout+"瓶");
}
if(e.getSource()==k5.b){
if(k5.amout<k5amt)
k5.t.setText(++k5.amout+"瓶");
else
k5.lname.setText("只有"+k5.amout+"瓶");
}
if(e.getSource()==k6.b){
if(k6.amout<k6amt)
k6.t.setText(++k6.amout+"瓶");
else
k6.lname.setText("只有"+k6.amout+"瓶");
}
calCost();
}
//加热,制冷,找零,取饮料的处理函数
@SuppressWarnings("deprecation")
private void funcs(ActionEvent e,double money){
wmh3090
- 粉丝: 8
- 资源: 2