import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Bag extends JFrame implements ActionListener{
private final KeyPanel keypad;
private final ChestPanel chest;
private JLabel message = new JLabel();
private JTextField num = new JTextField(9);
private int arr[] = new int[40];//用来存放生成的条形码,最多存放50条
int i;
int target;//生成的条形码数字
int n;

public Bag(){
setTitle("超市顾客包裹自动存取系统");
setLocation(200,200);
setSize(400, 400);
setLayout(new FlowLayout());
keypad = new KeyPanel(this);
chest = new ChestPanel(this);

getContentPane().add(num);
getContentPane().add(message);
getContentPane().add(keypad);
getContentPane().add(chest);
setDefaultCloseOperation(EXIT_ON_CLOSE);//窗口事件
}

public void actionPerformed(ActionEvent e) {
String number =new String(num.getText());
num.setText("");
}

public static void main(String[] args) {
Bag b = new Bag();
b.setVisible(true);
}

public void setOwner1(String s) {
if (s.equals("重填")) {
num.setText("");
}
else if(s.equals("存放")){
for (i=0;i<arr.length;i++){
if(i<=arr.length && arr[i] == 0){
target=100000000+(int)(Math.random()*99999999);//随机生成一串9位数的条形码
message.setText((i+1)+"号柜已打开,请记住取走条形码");
arr[i]=target;
chest.buttons[i].setBackground(Color.lightGray);
System.out.println("arr["+i+"]="+arr[i]);//窗口输出条形码,便于程序员或测试员测试程序
JOptionPane.showMessageDialog(null,"条形码是:"+target
+"\n"+"条形码只能使用一次!");
return;//跳出for循环
}
else{
message.setText("本柜已满,请存到其他储物柜");
}
}
}
else if(s.equals("取出")){
try{
n = Integer.parseInt(num.getText());
for(int j = 0; j<=arr.length; j++){
if(arr[j]==n){
arr[j] = 0;
message.setText((j+1)+"号柜,取走东西后请关上柜门");
chest.buttons[j].setBackground(Color.lightGray);
//i=j;
i--;
num.setText("");
return;
}
}
}
catch(NumberFormatException ex){
message.setText("条形码错误或未在此存放物品");
num.setText("");
}
catch(ArrayIndexOutOfBoundsException ex){
message.setText("条形码错误或未在此存放物品");
num.setText("");
}
}
else{
num.setText(num.getText() + s);
}
}
public void setOwner2(String ss){
int k;
k = Integer.parseInt(ss);
chest.buttons[k-1].setBackground(null);
message.setText(k+"号柜已关好,祝购物愉快");
}
}

class KeyPanel extends JPanel implements ActionListener {
private final static int NBUTTONS = 13;
private JButton[] buttons;
private Bag bag;
private String[] labels = {
"1","2","3",
"4","5","6",
"7","8","9",
"存放","0","重填",
"取出"
};
KeyPanel(Bag b) {
this.setLayout(new GridLayout(5,3));
buttons = new JButton[NBUTTONS];
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton(labels[i]);
this.add(buttons[i]);
buttons[i].addActionListener(this);
}
this.bag = b;
}
public void actionPerformed(ActionEvent e) {
String content = ((JButton)e.getSource()).getText();
bag.setOwner1(content);
}
}
//柜子类
class ChestPanel extends JPanel implements ActionListener{
private final static int NBUTTONS = 40;
public JButton[] buttons;
private Bag bag;
private String[] labels = {
"1","2","3","4","5","6","7","8",
"9","10","11","12","13","14","15","16",
"17","18","19","20","21","22","23","24",
"25","26","27","28","29","30","31","32",
"33","34","35","36","37","38","39","40"
};
ChestPanel(Bag b) {
this.setLayout(new GridLayout(5,8));
buttons();
this.bag = b;
}
public void buttons(){
buttons = new JButton[NBUTTONS];
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton(labels[i]);
this.add(buttons[i]);
buttons[i].addActionListener(this);
}
}
public void actionPerformed(ActionEvent e) {
String content1 = ((JButton)e.getSource()).getText();
bag.setOwner2(content1);
}