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

📄 flight.java

📁 航班订座系统~java的作业
💻 JAVA
字号:
import java.io.*;
import java.util.*;
import java.util.Date;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;

public class Flight
{
 private Seat seats [];
 private String flightNumber;
 private String departure;
 private String arrival;
 private String date;
 private boolean checkingIn = false;
 private boolean boarding = false;
 private boolean closed = false;
 private boolean full = false;
 private String statusMessage = "Seats available";;
 private int freeSeats;
 private int reservedSeats;
 private int bookedSeats;
 private int rows;
 private int cols;

 public Flight (int rows, int cols)
 {
  seats = new Seat[rows*cols];
  Date today = new Date();
  int i = 0;
  this.rows = rows;
  this.cols = cols;

  for (int row=1; row<=rows; row++)
  for(int col=1; col<=cols; col++)
  {
   seats[i] = new Seat(row, col);
   i++;
  }
  freeSeats = rows*cols;
  reservedSeats = 0;
  bookedSeats = 0;
  today = new Date();
  SimpleDateFormat df1 = new SimpleDateFormat( "dd-MM-yy" );
  date = df1.format(today);
 }

 public boolean isCheckingIn()
 {
  return checkingIn;
 }

 public boolean isBoarding()
 {
  return boarding;
 }

 public boolean isClosed()
 {
  return closed;
 }

 public boolean isFull()
 {
  return full;
 }

 public String getStatusMessage()
 {
  return statusMessage;
 }

 public void setFlightDetails(String fltNo, String dep, String arr)
 {
  flightNumber = fltNo;
  departure = dep;
  arrival = arr;
 }

 public String getDeparture()
 {
  return departure;
 }

 public String getArrival()
 {
  return arrival;
 }

 public void setFlightStatus(char statusCode)
 {
  switch (statusCode)
  {
   case '1' : checkingIn = true;
    statusMessage = "Checking in";
   break;
   case '2' : boarding = true;
    statusMessage = "Boarding";
   break;
   case '3' : closed = true;
    statusMessage = "Flight closed";
   break;
  } 
 }

 public int getFreeSeats()
 {
  return freeSeats;
 }

 public int getReservedSeats()
 {
  return reservedSeats;
 }

 public int getBookedSeats()
 {
  return bookedSeats;
 }

 public String getDate()
 {
  return date;
 }

 public void viewSeat (String seatNo)
 {
  Seat temp;
  temp = getSeat(seatNo);
  if (temp != null)
  temp.displaySeatInfo();
 }

 public void displayFlightInfo()
 {
  DecimalFormat df1 = new DecimalFormat("###0.00");
  System.out.print("\n\t\tFlight: "+flightNumber);
  System.out.print("\tDate: "+date);
  System.out.print("\n\t\tDep: "+departure);
  System.out.println(" Arr: "+arrival);
  System.out.println("\t\tFlight Status: "+statusMessage);
  System.out.println("\t\tTakings: "+df1.format(getTakings()));
  System.out.print("\t\tfree: "+getFreeSeats());
  System.out.print(" reserved: "+getReservedSeats());
  System.out.println(" booked: "+getBookedSeats());
 }

 public void displaySeatingPlan()
 {
  int i = 0;
  int row = 1;

  System.out.println("\n\t\t\t AB CD");
  System.out.println("\t\t\t -----");
  for (int j=1; j<=rows; j++) 
  {
   System.out.print("\t\t\t"+row+" - ");
   row++;
   for (int k=1; k <=cols; k++)
   {
    if (k == 3)
     System.out.print(" ");
    if (k % 4 == 0)
     System.out.println(seats[i].getStatus());
    else
     System.out.print(seats[i].getStatus());
    i++;
   }
  }
 }

 public void updateSeat (String seatNo, String pName, int status)
 {
  Seat temp;
  temp = getSeat(seatNo);
  if (temp != null) 
  {
   temp.changeSeatStatus(status, pName);
   if (temp.updated() != 0) 
   {
    switch (temp.updated())
    {
     case 1 : freeSeats++; 
     reservedSeats--;
     if (!boarding || !closed)
     {
      statusMessage = "Seats available";
      full = false;
     }
     break;
     case 2 : freeSeats++; 
     bookedSeats--;
     if (!boarding || !closed)
     {
      statusMessage = "Seats available";
      full = false;
     }
     break;
     case 3 : freeSeats--; 
     reservedSeats++;
     break;
     case 4 : freeSeats--; 
     bookedSeats++;
     break;
     case 5 : reservedSeats--;
     bookedSeats++;
     break;
    } 
   } 
  } 
  if ( freeSeats == 0 ) 
  {
   full = true;
   statusMessage = "Flight full";
  }
 }

 public void setTakings (String seatNo, float takings)
 {
  Seat temp;
  temp = getSeat(seatNo);
  if (temp != null) 
  {
   temp.setTakings(takings);
  } 
 }

//----- file handling methods ---------------

 public void loadFromFile(String filename)
 {
  String textLine = "";
  FileReader fr;
  BufferedReader br;
  Seat temp;
  String seatNo = "";
  String pName = "";
  char pType = 'X';
  String pInfo = "";
  int status;
  float takings;

  try 
  {
   fr = new FileReader (filename);
   br = new BufferedReader (fr);
   while ((textLine = br.readLine()) != null) 
   {
    StringTokenizer st = new StringTokenizer (textLine, ",");
    while (st.hasMoreTokens()) 
    {
     seatNo = st.nextToken();
     pName = st.nextToken();
     pType =st.nextToken().charAt(0);
     pInfo = st.nextToken();
     status = Integer.parseInt(st.nextToken());
     takings = Float.parseFloat(st.nextToken());
     temp = getSeat(seatNo);
     temp.changeSeatStatus(status, pName, pType, pInfo);
     setTakings (seatNo, takings);
     if (status == 2)
       bookedSeats++;
     else
       reservedSeats++;
     freeSeats--;
    }
   } 
   fr.close();
   br.close();
  }
  catch (IOException e) 
  {
   System.out.println("File not found");}
  }

  public void saveToFile(String filename)
  {
   FileWriter fw;
   PrintWriter pw;
   String textLine;

   try 
   {
    fw = new FileWriter (filename);
    pw = new PrintWriter (fw);
    for (int i=0; i<24; i++)
    {
     if (seats[i].getStatus() != 0) 
     {
      textLine = seats[i].getNo() + ",";
      textLine += seats[i].getPassDetails()+",";
      textLine += seats[i].getStatus()+",";
      textLine += seats[i].getTakings();
      pw.println(textLine);
     }
    } 
    fw.close();
    pw.close();
   }
   catch (IOException e) 
   {
    System.out.println("Unable to open file");
   }
 }

//---------- helper methods -----------

 private Seat getSeat(String searchValue)
 {
  int index = 0;
  boolean found = true;
  try
  {
   while (!(seats[index].getNo().equalsIgnoreCase(searchValue)))
   {
    index++;
   } 
  }
  catch (ArrayIndexOutOfBoundsException e)
  {
   System.out.println("\n\t\tERROR - invalid seat number entered");
   found = false;
  }
  if (found)
   return seats[index];
  else
   return null;
 }

 private float getTakings()
 {
  float takings = 0.0F;
  for (int i=0; i<rows*cols; i++)
   takings += seats[i].getTakings();
  return takings;
 }
} // end of Flight class

⌨️ 快捷键说明

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