⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test.java

📁 利用栈和队列实现停车场(作业要求
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.awt.*;import java.awt.event.*;import java.util.*;import javax.swing.*;import javax.swing.JOptionPane;class Car          //Car 类定义部分{public	String id;        int number;        Date arrive;        Date change;        Date depart;        String design;        int  type;        Car(String id,int num,String disg,int big)        {                this.id=id;                this.number=num;                this.arrive=new Date();                this.design=disg;                this.type=big;                this.change=null;        }        Car(){	};public void apark(String s){        this.design=s;}public void dpark()        {                this.depart=new Date();        }}class Equeue                 //队列定义部分{        LinkedList mylist=new LinkedList();public void enqueue(Car car)  //入列{        mylist.add(car);}public Car dequeue()          //出列{        if(mylist.size()!=0) return (Car)mylist.removeFirst();        else return null;}public boolean isempty()  //是否为空{        if(mylist.size()==0) return false;        else return true;}}class Sstack  //堆栈定义部分{        LinkedList mylist=new LinkedList();public void push(Car car)//压栈{        mylist.add(car);}public Car pop()  //弹栈{   Car temp=new Car();   if(mylist.size()!=0) {temp=(Car)mylist.getLast(); mylist.removeLast();return (Car)temp;}   else return null;}public boolean empty()//是否为空{        if(mylist.size()==0) return true;        else return false;}}class Win extends JFrame implements ActionListener         //win类定义部分{    int stationlen;    Equeue  queue1=new Equeue();    Equeue  queue2=new Equeue();    Sstack stack1=new Sstack();    Sstack stack2=new Sstack();    MenuBar menubar;    Menu menu1,menu2,menu3,menu4 ,menu5;    MenuItem item1,item2,item3,item4,item5,item6,item7 ,item8,item9,item10;    int b[],c[];    int count1=0;    int count2=0;    int flag=0;    int price1,price2;    JTable table;    Object a[][];    Object name[]={"车牌号","车位号","进站时间","出站时间","备注","车型号"};    Object addname[]={"车牌号","备注"};    JButton button1,button2,button3,button4,button5,button6;    JTextField  text2,text1;    JDialog ss;    Box box1,box2,box3;    Stack stack3;    Choice choice;    Win(String s )              //win类构造函数    {               super(s);               stationlen=5;               price1=2;               price2=1;               b=new int[stationlen];        for(int i=0;i<stationlen;i++)            b[i]=0;            choice=new Choice();            choice.add("小汽车");            choice.add("中型客车");            choice.add("大货车");            choice.add("超大型卡车");            menubar=new MenuBar();            menu1=new Menu("进/出站");            menu2=new Menu("查询");            menu3=new Menu("车辆列表");            menu4=new Menu("设置");            menu5=new Menu("帮助");            item1=new MenuItem("进站");            item2=new MenuItem("出站");            item2.enable(false);            item3=new MenuItem("退出");            item4=new MenuItem("查找汽车") ;            item5=new MenuItem("查询空车位");            item6=new MenuItem("打印");            item7=new MenuItem("设置收费价格");            item8=new MenuItem("设置停车场大小");            item9=new MenuItem("帮助说明");            item10=new MenuItem("查询汽车总数量");            item1.addActionListener(this);            item2.addActionListener(this);            item3.addActionListener(this);            item4.addActionListener(this);            item5.addActionListener(this);            item6.addActionListener(this);            item7.addActionListener(this);            item8.addActionListener(this);            item9.addActionListener(this);            item10.addActionListener(this);            menu1.add(item1);            menu1.add(item2);            menu1.addSeparator();            menu1.add(item3);            menu2.add(item4);            menu2.add(item5);            menu2.add(item10);            menu3.add(item6);            menu4.add(item7);            menu4.add(item8);            menu5.add(item9);            menubar.add(menu1);            menubar.add(menu2);            menubar.add(menu3);            menubar.add(menu4);            menubar.add(menu5);            setMenuBar(menubar);            button1=new JButton("确定");            button1.addActionListener(this);            button2=new JButton("确定");            button2.addActionListener(this);            button3=new JButton("确定");            button3.addActionListener(this);            button4=new JButton("确定");            button4.addActionListener(this);            button5=new JButton("确定");            button5.addActionListener(this);            button6=new JButton("取消");            button6.addActionListener(this);            getContentPane().add(new JScrollPane(table),BorderLayout.CENTER);            setLocation(120,60);            setSize(800,600);            setResizable(false);            setVisible(true);            validate();    }public void  enableitem2(){   int k=0;   for(;k<stationlen;k++)                 {if(b[k]==1) break;}   if(k==stationlen)   item2.enable(false);   else item2.enable(true);}public int checklink(int b[],int big,int len)   //判断是否有足够连续的车位{        int i,j,temp=0;        for(i=0;i<len-big+1;i++)         {  temp=0;            for(j=0;j<big;j++)            {if(b[i+j]==0) ++temp;}            if (temp==big)  return i;         }         return len;}public void  menuable(boolean h)      //设定菜单栏属性{        menu1.enable(h);        menu2.enable(h);        menu3.enable(h);        menu4.enable(h);        menu5.enable(h);}public void removetable(boolean h)  //设置表格在窗体中显示{        if(h)          {            tableset(1,3,addname);                  getContentPane().add(new JScrollPane(table),BorderLayout.CENTER);                  getContentPane().remove(table);          }        else          {                  tableset(1,3,addname);                  getContentPane().remove(table);            getContentPane().add(new JScrollPane(table),BorderLayout.CENTER);          }}public void tableset(int i,int j,Object name1[] )   //设置表格{        a=new Object[i][j];                table=new JTable(a, name1);                getContentPane().removeAll();            getContentPane().add(new JScrollPane(table),BorderLayout.CENTER);            table.repaint();            validate();}public Car checksame(String word )          //判断停车场或者便车道是否有和最近添加相同的车牌号{    Car temp=new Car(),htemp=new Car();    htemp=null;    char cword[]=word.toCharArray();    while(!stack1.empty())         {	temp=stack1.pop();                 stack2.push(temp);                 if(word.equals(temp.id))                    htemp=temp;         }         while(!stack2.empty())                     { temp=stack2.pop();                       stack1.push(temp);                     }          while(queue1.isempty())                 {                   temp=queue1.dequeue();                   queue2.enqueue(temp) ;                   if(word.equals(temp.id))                            htemp=temp;                 }                 while(queue2.isempty())                                 { temp=queue2.dequeue();                                   queue1.enqueue(temp);                                 }         return htemp;}public boolean checkformat(String word)     //判断输入字符是否为数字{         char cword[]=word.toCharArray();         for(int i=0;i<cword.length;i++)                 if(!(Character.isDigit(cword[i]))  )                    return false;           return true;}public void Dialog_ss_action(){    ss.addWindowListener(new WindowAdapter()         {public void windowClosing(WindowEvent e)         {menuable(true);}});}public boolean tranfer(Car temp ,Sstack stack1,int n,int b[])  //便车道车进入 停车场{  int t=0;   if(checklink(b,temp.type,n)!=n)                 {                         Date h=new Date();                         temp.change=new Date();                         temp.number=checklink(b,temp.type,n);                         temp.design=h+"车号"+temp.id+"进入停车场"+String.valueOf(temp.number)+"位";                         for(t=temp.number;t<temp.number+temp.type;t++)                             b[t]=1;                         stack1.push(temp);                         count2--;                         count1++;                         enableitem2();            JOptionPane.showMessageDialog(this, temp.design,"告对话框 ",JOptionPane.WARNING_MESSAGE);             return true;             }         return false;}public int countcost(Car temp,int price1,int price2 ) //计算汽车出停车场费用{        int s1=temp.depart.getSeconds()+temp.depart.getMinutes()*60+temp.depart.getHours()*3600+temp.depart.getDay()*216000;        int s2=temp.arrive.getSeconds()+temp.arrive.getMinutes()*60+temp.arrive.getHours()*3600+temp.arrive.getDay()*216000;        if(temp.change==null)           s1=(s1-s2)*price1*temp.type/60;        else        {           int s3=temp.change.getSeconds()+temp.change.getMinutes()*60+temp.change.getHours()*3600+temp.change.getDay()*216000;           s1=((s3-s2)*price2+(s1-s3)*price1)*temp.type/60;        }        if(s1==0) ++s1;        return s1;}public void   tableprint(Equeue queue1,Equeue queue2,Sstack stakc1,Sstack stack2) //打印停车场{        Car temp=new Car();                  int  i=0,j=6;                  tableset(count1+count2,j,name);             table.setVisible(false);                 while(!stack1.empty())                    {                                    temp=(Car)stack1.pop();                                 a[i][0]=temp.id;                                 a[i][1]=String.valueOf(temp.number)  ;                                 a[i][2]=temp.arrive;                                 a[i][3]=temp.depart;                                 a[i][4]=temp.design;                                 a[i][5]=String.valueOf(temp.type);                                  stack2.push(temp);                                  i++;                         }                 while(!stack2.empty())                 {                         temp=(Car)stack2.pop();                         stack1.push(temp);                 }                 while(queue1.isempty())                 {       temp=queue1.dequeue();                                 a[i][0]=temp.id;                                 a[i][1]=String.valueOf(temp.number);                                 a[i][2]=temp.arrive;                                 a[i][3]=temp.depart;                                 a[i][4]=temp.design;                                 a[i][5]=String.valueOf(temp.type);                                  queue2.enqueue(temp);                                  i++;                 }                 while(queue2.isempty())                 {   temp=queue2.dequeue();                         queue1.enqueue(temp);                 }}public void actionPerformed(ActionEvent e)         //动作监听函数{    if(e.getSource()==item1)   //进站    {        menuable(false);        removetable(true);            ss=new JDialog();            ss.setTitle("进站");            ss.setModal(true);            text1=new JTextField(16);            text1.setText("无");            text2=new JTextField(16);            text2.setText("无");            box1=Box.createVerticalBox();            box1.add(new JLabel("请输入ID"));            box1.add(Box.createVerticalStrut(8));            box1.add(new JLabel("请输入备注"));            box1.add(Box.createVerticalStrut(8));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -