import java.io.*;
import java.util.*;
public class Flight
{
private static int INCREMENT = 5;
private FlightData[] flightList = new FlightData[10]; //航班
private String flightName = null; //当前处理航班
private int row =0; //当前航班的row
private int rowLength = 0; //当前航班的rowLength
private FlightData flight = null; //当前航班的引用
String cmdString = null; //命令
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public Flight()
{
}
/**
*commandShell
*/
private void commandShell()
{
System.out.println("\n\n=============Welcome=============");
System.out.println("Command Shell V1.0");
System.out.println("Type \"exit\" command to exit");
System.out.println("Type \"help\" command to help");
while(true)
{
readCommand();
processCommand();
}
}
/**
*readCommand 读取命令
*/
private void readCommand()
{
try
{
cmdString=br.readLine().trim();
}catch(IOException e)
{
System.out.println("Read Command Error!");
cmdString = null;
}
}
/**
*processCommand 处理
*/
private void processCommand()
{
String[] cmds;
String cmd;
if(cmdString!=null)
{
cmds = command();
if(cmds != null)
{
cmd = cmds[0].toLowerCase();
if(cmd.equals("help"))
{
helpCommand();
}
else if(cmd.equals("exit"))
{
System.out.println("Thanks for using!");
System.exit(0);
}
else if(cmd.equals("create"))
{
createCommand(cmds);
}
else if(cmd.equals("reserve"))
{
reserveCommand(cmds);
}
else if(cmd.equals("list"))
{
listCommand(cmds);
}
else if(cmd.equals("cancel"))
{
cancelCommand(cmds);
}
else if(cmd.equals("quit"))
{
System.out.println("退 出 航 班 :"+flightName+"!");
flightName = null;
}
else if(cmd.equals("listline"))
{
listLine();
}
else if(cmd.equals("locateline"))
{
locateLine(cmds);
}
else
{
System.out.println("Invalid Command!");
}
}
}
}
/**
*command()分离
*/
private String[] command()
{
int cc=0;
String[] cmds;
StringTokenizer st = new StringTokenizer(cmdString);
cc = st.countTokens();
if(cc == 0)
return null;
cmds = new String[cc];
for(int i=0;i<cc;i++)
{
cmds[i] = new String(st.nextToken().trim());
}
return cmds;
}
////////////
//航班业务//
////////////
/**
*创建
*/
private void createCommand(String[] cmds)
{
if(cmds.length != 4)
{
System.out.println("invalid create command!");
return;
}
if(Integer.parseInt(cmds[2])<=0 || Integer.parseInt(cmds[3])<=0)
{
System.out.println("create command parameter error!");
return;
}
try
{
flightName = cmds[1];
row = Integer.parseInt(cmds[2]);
rowLength = Integer.parseInt(cmds[3]);
flight = new FlightData(cmds[1],row,rowLength);
if(isfLFull())
{
if(!increase())
return;
}
for(int i=0;i<flightList.length;i++)
{
if(flightList[i]==null)
{
flightList[i] = flight;
break;
}
}
}catch(Exception e)
{
System.out.println(e);
}
}
/**
*increase
*/
private boolean increase()
{
FlightData[] temp = new FlightData[flightList.length];
temp = flightList;
try
{
flightList = new FlightData[temp.length+INCREMENT];
for(int i=0;i<temp.length;i++)
{
flightList[i]=temp[i];
}
}catch(Exception e)
{
System.out.println(e);
return false;
}
return true;
}
/**
*预订
*/
private void reserveCommand(String[] cmds)
{
if(cmds.length<=1)
{
System.out.println("invalid reserve command!");
return;
}
if(flight == null)
{
System.out.println("No line selected!Please locate a line!");
return;
}
else
{
String[] names = new String[cmds.length-1];
for(int i=0;i<names.length;i++)
{
names[i] = cmds[i+1];
}
int[] bn = flight.reserve(names);
if(bn[0]!=-1)
{
for(int i=0;i<bn.length;i++)
{
System.out.println(names[i]+"\'s BookingNumber is:"+bn[i]);
}
}
else
{
System.out.println("No such seat!");
}
}
}
/**
*打印
*/
private void listCommand(String[] cmds)
{
if(cmds.length != 1)
{
System.out.println("invalid list command!");
}
else
{
if(flight == null)
{
System.out.println("No line selected!Please locate one line!");
return;
}
else
{
System.out.println("Passenger name Booking number Seat<row position col position>");
Passenger[] passengerList = flight.getPassengerList();
for(int i=0;i<row*rowLength;i++)
{
if(passengerList[i]!=null)
{
System.out.println(formatStr(""+passengerList[i].getName(),18)
+formatStr(""+passengerList[i].getBookingNumber(),18)
+formatStr(""+passengerList[i].getRow(),18)
+formatStr(""+passengerList[i].getSeatPosition(),18));
}
}
}
}
}
/**
*取消预订
*/
private void cancelCommand(String[] cmds)
{
if(cmds.length<=1)
{
System.out.println("invalid cancel command!");
}
else
{
if(isfLEmpty())
{
System.out.println("航班记录为空!");
return;
}
else
{
String names[]=new String[cmds.length-1];
for(int i=0;i<names.length;i++)
{
names[i]=cmds[i+1];
}
for(int j=0;j<names.length;j++)
{
for(int k=0;k<flightList.length;k++)
{
if(flightList[k].getFlightName().equals(names[j]))
{
flightList[k]=null;
if(flight==flightList[k])
{
flight=null;
}
}
}
}
}
}
}
/**
*帮助
*/
private void helpCommand()
{
System.out.println("\"create\" command for create. format:create 航班名 坐位排数 每排坐位数<CR>");
System.out.println("\"reserve\" command for reserve. format:reserve 乘客名1< 乘客名2...><CR>");
System.out.println("\"cancel\" command for cancel. format:cancel 流水号<CR>");
System.out.println("\"list\" command for list. format:list<CR>");
}
/**
*listLine
*/
private void listLine()
{
if(isfLEmpty())
{
System.out.println("航班记录为空!");
return;
}
System.out.println("Airline Information.\nAirLine Name row rowLength");
for(int i = 0; i<flightList.length;i++)
{
if(flightList[i]!=null)
System.out.println(formatStr(flightList[i].getFlightName(),12)+" "+flightList[i].getRow()+" rows and "+flightList[i].getRowLength()+" cols");
}
}
private void locateLine(String[] cmds)
{
if(isfLEmpty())
{
System.out.println("航班记录为空!");
return;
}
for(int i=0;i<flightList.length;i++)
{
if(flightList[i]!=null && flightList[i].getFlightName().equals(cmds[1]))
{
flight = flightList[i];
System.out.println(flight.getFlightName());
break;
}
}
}
/**
*判断flightList是否为空
*/
private boolean isfLEmpty()
{
for(int i=0;i<flightList.lengt
评论7