package view;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import model.Model;
import model.Passenger;
import model.Plane;
import util.MyUtilHB;
import util.MyutilJP;
public class MainView extends Frame implements ActionListener{
Menu xtMenu,hbMenu,jpMenu;
MenuBar mb;
static JTable tableHB,tableJP;
public static Object arrayHB[][],arrayJP[][];
static Object titleHB[],titleJP[];
static JPanel p;
static int iHB=0,jHB,iiHB;
static int iJP=0,jJP,iiJP;
public static int num=30;
public MainView()
{
super("飞机订票系统");
int w=Toolkit.getDefaultToolkit().getScreenSize().width;
int h=Toolkit.getDefaultToolkit().getScreenSize().height;
this.setBounds(w/2-300, h/2-300, 600, h-250);
mb=new MenuBar();
xtMenu=new Menu("系统管理");
hbMenu=new Menu("航班管理");
jpMenu=new Menu("机票管理");
xtMenu.add("退出系统");
hbMenu.add("显示所有航班");
hbMenu.add("增加航班");
hbMenu.add("删除航班");
jpMenu.add("显示订票记录");
jpMenu.add("订机票");
jpMenu.add("退机票");
mb.add(xtMenu);
mb.add(hbMenu);
mb.add(jpMenu);
this.setMenuBar(mb);
xtMenu.addActionListener(this);
hbMenu.addActionListener(this);
jpMenu.addActionListener(this);
titleHB=new Object[]{"航班号","起始地","目的地","座位总数"};
arrayHB=new Object[num][4];
tableHB=new JTable(arrayHB,titleHB);
tableHB.setEnabled(false);
tableHB.getTableHeader().setReorderingAllowed(false);
tableHB.setPreferredScrollableViewportSize(new Dimension(500,h-350));
p=new JPanel();
p.add(new JScrollPane(tableHB));
add(p,"Center");
titleJP=new Object[]{"姓名","票数","航班号"};
arrayJP=new Object[num][3];
tableJP=new JTable(arrayJP,titleJP);
tableJP.setEnabled(false);
tableJP.getTableHeader().setReorderingAllowed(false);
tableJP.setPreferredScrollableViewportSize(new Dimension(500,h-350));
this.setVisible(true);
}
public boolean handleEvent(Event e)
{
if(e.id==Event.WINDOW_DESTROY)
{
System.exit(0);
}
return true;
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("退出系统"))
{
System.exit(0);
}
if(e.getActionCommand().equals("显示所有航班"))
{
System.out.println("显示所有航班");
showAllPlane();
p.removeAll();
p.add(new JScrollPane(tableHB));
add(p,"Center");
this.setVisible(true);
}
if(e.getActionCommand().equals("增加航班"))
{
System.out.println("增加航班");
addPlaneProcessing();
}
if(e.getActionCommand().equals("删除航班"))
{
System.out.println("删除航班");
deletePlaneProcessing();
}
if(e.getActionCommand().equals("订机票"))
{
System.out.println("订机票");
orderTicketProcessing();
}
if(e.getActionCommand().equals("退机票"))
{
System.out.println("退机票");
cancelTicketProcessing();
}
if(e.getActionCommand().equals("显示订票记录"))
{
System.out.println("显示订票记录");
p.removeAll();
p.add(new JScrollPane(tableJP));
add(p,"Center");
this.setVisible(true);
showTicketRecord();
}
}
//显示订票记录
public static void showTicketRecord()
{
System.err.println("显示订票记录");
java.util.Iterator<Passenger> iter=Plane.getAllPassengers();
iJP=0;//由于iJP是static,每次扫描把它归0
while(iter.hasNext())
{
Passenger p=(Passenger)iter.next();
jJP=0;//每次扫描归0
arrayJP[iJP][jJP]=String.valueOf(p.getName());
tableJP.setValueAt(arrayJP[iJP][jJP], iJP, jJP);jJP++;
arrayJP[iJP][jJP]=String.valueOf(p.getOrderNumOfSeat());
tableJP.setValueAt(arrayJP[iJP][jJP], iJP, jJP);jJP++;
arrayJP[iJP][jJP]=String.valueOf(p.getplaneNo());
tableJP.setValueAt(arrayJP[iJP][jJP], iJP, jJP);
iJP++;
iiJP=iJP;
}
}
public static void main(String[] args)
{
new MainView();
showAllPlane() ;
}
//显示所有航班
public static void showAllPlane()
{
java.util.Iterator<Plane> iter=Model.getAllPlanes();
iHB=0;
while (iter.hasNext())
{
Plane plane = (Plane) iter.next();
jHB=0;
arrayHB[iHB][jHB]=String.valueOf(plane.getPlaneNo());
tableHB.setValueAt(arrayHB[iHB][jHB], iHB, jHB);jHB++;
arrayHB[iHB][jHB]=String.valueOf(plane.getSource());
tableHB.setValueAt(arrayHB[iHB][jHB], iHB, jHB);jHB++;
arrayHB[iHB][jHB]=String.valueOf(plane.getDest());
tableHB.setValueAt(arrayHB[iHB][jHB], iHB, jHB);jHB++;
arrayHB[iHB][jHB]=String.valueOf(plane.getTotalNumOfSeat());
tableHB.setValueAt(arrayHB[iHB][jHB], iHB, jHB);
iHB++;
iiHB=iHB;
}
}
//添加航班
private static void addPlaneProcessing()
{
new MyUtilHB(1);
}
//删除航班
private static void deletePlaneProcessing()
{
new MyUtilHB(-1);
}
//删除订票记录
public static void delTableJP(int planeNo)
{
//判断是否是要删除的项,然后删除
for(int m=0;m<iiJP;m++)
{
if(Integer.parseInt(arrayJP[m][2].toString())==planeNo)
{
for(int n=0;n<=jJP;n++)
{
tableJP.setValueAt("", m, n);
}
}
}
//把表格所有的清空
for(int m=0;m<iiJP;m++)
for(int n=0;n<=jJP;n++)
tableJP.setValueAt("", m, n);
//设置还没删除的项于表格中
for(int m=0;m<iiJP-1;m++)
for(int n=0;n<jJP;n++)
tableJP.setValueAt(arrayJP[m][n], m, n);
iiJP=iiJP-1;//从时间上考虑,减一可以节约程序运行时间
showAllPlane();
}
//删除航班
public static void delTableHB(int planeNo)
{
//判断是否是要删除的项,然后删除
for(int m=0;m<iiHB;m++)
{
if(Integer.parseInt(arrayHB[m][0].toString())==planeNo)
{
for(int n=0;n<=jHB;n++)
{
tableHB.setValueAt("", m, n);
}
}
}
//把表格所有的清空
for(int m=0;m<iiHB;m++)
for(int n=0;n<=jHB;n++)
tableHB.setValueAt("", m, n);
//设置还没删除的项于表格中
for(int m=0;m<iiHB-1;m++)
for(int n=0;n<jHB;n++)
tableHB.setValueAt(arrayHB[m][n], m, n);
iiHB=iiHB-1;//从时间上考虑,减一可以节约程序运行时间
showAllPlane();
}
//订票
private static void orderTicketProcessing()
{
new MyutilJP(1);
}
//退票
private static void cancelTicketProcessing()
{
new MyutilJP(-1);
}
}
评论1