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

📄 ag2.java

📁 AirGourmet:空中美食管理系统。国外一个学习软件工程的经典例子。
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
//
// This is an implementation of the Air Gourmet product written in Java
//
// This application was developed using Borland JBuilder 2.0 (JDK 1.1)
//
package Air;

import java.io.*;
import java.util.*;
import java.text.*;

//---------------------------------------------------------------------------------------------------------------------------------------------------

class CPassenger implements java.io.Serializable
{
  //
  // data members
  //
  private String        passengerID;  // ID of passenger (9 digits)
  private String        firstName;  // first name of passenger (15 chars)
  private char        middleInit;  // middle initial of passenger (1 char)
  private String        lastName;  // last name of passenger (15 chars)
  private String        suffix;    // suffix of passenger (5 chars)
  private String        address1;  // first line of passenger address (25 chars)
  private String        address2;  // second line of pass. address (25 chars)
  private String        city;    // city in which passenger lives (14 chars)
  private String        state;    // state in which passenger lives (14 chars)
  private String        postalCode;  // postal code of passenger (10 chars)
  private String        country;  // country in which pass. lives (20 chars)

  //
  // accessor methods
  //
  public String  getPassengerID ()      { return passengerID; }
  public String  getFirstName ()      { return firstName; }
  public char  getMiddleInit ()        { return middleInit; }
  public String  getLastName ()      { return lastName; }
  public String  getSuffix ()          { return suffix; }
  public String  getAddress1 ()        { return address1; }
  public String  getAddress2 ()        { return address2; }
  public String  getCity ()          { return city; }
  public String  getState ()          { return state; }
  public String  getPostalCode ()      { return postalCode; }
  public String  getCountry ()        { return country; }

  //
  // mutator methods
  //
  public void  setPassengerID (String s)    { passengerID = s.toUpperCase (); }
  public void  setFirstName (String f)    { firstName = f.toUpperCase (); }
  public void  setMiddleInit (char m)    { middleInit = m; }
  public void  setLastName (String l)    { lastName = l.toUpperCase (); }
  public void  setSuffix (String s)      { suffix = s.toUpperCase (); }
  public void  setAddress1 (String a1)    { address1 = a1.toUpperCase (); }
  public void  setAddress2 (String a2)    { address2 = a2.toUpperCase (); }
  public void  setCity (String c)      { city = c.toUpperCase (); }
  public void  setState (String s)      { state = s.toUpperCase (); }
  public void  setPostalCode (String p)    { postalCode = p.toUpperCase (); }
  public void  setCountry (String c)    { country = c.toUpperCase (); }

  //
  // public methods
  //

  public synchronized String toString ()
  //
  // toString composes a string representation of the passenger object
  //
  {
    return passengerID + "\n"+ firstName + " " + middleInit + " " + lastName + " " + suffix + "\n"
        +  address1 + "\n" + address2 + "\n" +  city + " " + state + "  " + postalCode + "  "
        + country;
  }

  public void Copy (CPassenger tempPassenger)
  //
  // Copy  makes a copy of tempPassenger into the current object
  //
  {
    this.passengerID = tempPassenger.getPassengerID ();
    this.firstName = tempPassenger.getFirstName ();
    this.middleInit = tempPassenger.getMiddleInit ();
    this.lastName = tempPassenger.getLastName ();
    this.suffix = tempPassenger.getSuffix ();
    this.address1 = tempPassenger.getAddress1 ();
    this.address2 = tempPassenger.getAddress2 ();
    this.city = tempPassenger.getCity ();
    this.state = tempPassenger.getState ();
    this.postalCode = tempPassenger.getPostalCode ();
    this.country = tempPassenger.getCountry ();
  }

  public boolean getPassenger (String searchID)
  //
  // getPassenger loads the passenger from the file that has passengerID equal to searchID
  // Returns true if the passenger was found and loaded
  //
  {
    boolean        found = false;  // indicates if passenger already exists
    File          fileExists = new File ("passenger.dat");
                      // used to test if file exists
    CPassenger      tempPassenger;  // temporary object used to determine if
                      // object already exists
    boolean        EOF = false;

    if (!fileExists.exists ())
      return false;

    try
    {
      ObjectInputStream in = new ObjectInputStream (new FileInputStream 
          ("passenger.dat"));

      while (!EOF)
      {
        try
        {
          //
          // determine if the passenger object already exists
          //
          tempPassenger = (CPassenger)in.readObject ();

          //
      // check if there is a match with searchID
          //
          if (tempPassenger.getPassengerID ().toLowerCase ().compareTo
              (searchID.toLowerCase ()) == 0)
            {
              found = true;
              this.Copy (tempPassenger);
              break;
            }
        } // try
        catch (EOFException e)
        {
          EOF = true;
        }

      } // while (!EOF)

      in.close ();
    } // try
    catch (Exception e)
    {
      e.printStackTrace (System.out);
    }

    return found;

  } // getPassenger


  public void getDescription ()
  //
  // getDescription retrieves passenger information
  //
  {
    AirGourmetUtilities.clearScreen ();

    System.out.println ("Please enter the following information about the passenger.\n\n");

    System.out.println ("Enter the PASSENGER ID assigned to this passenger");
    System.out.print (" (9 numbers only - no spaces or dashes): ");
    passengerID = AirGourmetUtilities.readString ();

    if (!alreadyExists () )
    {
      System.out.print ("Enter the FIRST name of the passenger: ");
      firstName = AirGourmetUtilities.readString ();

      System.out.print ("Enter the MIDDLE INITIAL of the passenger: ");
      middleInit = AirGourmetUtilities.getChar ();

      System.out.print ("Enter the LAST name of the passenger: ");
      lastName = AirGourmetUtilities.readString ();

      System.out.print ("Enter the SUFFIX used by the passenger: ");
      suffix = AirGourmetUtilities.readString ();

      System.out.print ("Enter the ADDRESS (first line) of the passenger: ");
      address1 = AirGourmetUtilities.readString ();

      System.out.print ("Enter the ADDRESS (second line) of the passenger: ");
      address2 = AirGourmetUtilities.readString ();

      System.out.print ("Enter the CITY where the passenger lives: ");
      city = AirGourmetUtilities.readString ();

      System.out.print ("Enter the STATE where the passenger lives: ");
      state = AirGourmetUtilities.readString ();

      System.out.print ("Enter the POSTAL CODE where the passenger lives: ");
      postalCode = AirGourmetUtilities.readString ();

      System.out.print ("Enter the COUNTRY where the passenger lives: ");
      country = AirGourmetUtilities.readString ();
    }
  } // getDescription


  public void insert ()
  //
  // insert inserts a passenger object in the proper place
  //
  {
    boolean        found = false;  // indicates if object insertion point found
    File          fileExists = new File ("passenger.dat");
                      // used to test if file exists
    CPassenger      tempPassenger;  // temporary object used for file copying
    boolean        EOF = false;

    try
    {
      ObjectOutputStream out = new ObjectOutputStream (new FileOutputStream
          ("tempP.dat"));

      if (fileExists.exists ())
      {
        ObjectInputStream in = new ObjectInputStream (new FileInputStream
            ("passenger.dat"));

        while (!EOF)
        {
          try
          {
            // read/write temporary object from the passenger file
            tempPassenger = (CPassenger)in.readObject ();
            out.writeObject (tempPassenger);
          }
          catch (EOFException e)
          {
            EOF = true;
          }

        } // while (!EOF)

        in.close ();
      } // if (fileExists.exists ())
      else
        out.writeObject (this);

      out.close ();
    } // try

    catch (Exception e)
    {
      e.printStackTrace (System.out);
    }

    EOF = false;

    try
    {
      ObjectInputStream in = new ObjectInputStream (new FileInputStream ("tempP.dat"));
      ObjectOutputStream out = new ObjectOutputStream (new FileOutputStream
          ("passenger.dat"));

      while (!EOF)
      {
        try
        {
          tempPassenger = (CPassenger)in.readObject ();

          //
          // check if there is already a passenger with the current ID.
          // If one exists, it means a change of address so write the new
          // passenger object into the file
          //
          if (passengerID.compareTo (tempPassenger.getPassengerID ().
              toLowerCase ()) == 0)
          {
            out.writeObject (this);
            found = true;
          }
          else
            out.writeObject (tempPassenger);

        } // try

        catch (EOFException e)
        {
          if (!found)
            out.writeObject (this);

          EOF = true;
        }

      } // while (!EOF)

      in.close ();
      out.close ();
    } // try

    catch (Exception e)
    {
      e.printStackTrace (System.out);
    }

  } // insert

  //
  // private method
  //

  private boolean alreadyExists ()
  //
  // alreadyExists determines if the passengerID of the current object already exists in the file
  // if the ID exists, then the user is asked if the values stored in the file
  // are to be used
  //
  {
    char          ch;    // holds user response to Y/N question
    boolean        found = false;  // indicates if passenger already exists
    String          searchID;  // the passengerID for which to search
    File          fileExists = new File ("passenger.dat");
                      // used to test if file exists
    CPassenger      tempPassenger;  // temporary object used to determine if
                      // object already exists
    boolean        EOF = false;

    if (!fileExists.exists ())
      return false;

    searchID = passengerID;

    try
    {
      ObjectInputStream in = new ObjectInputStream (new FileInputStream
          ("passenger.dat"));

      while (!EOF)
      {
        try
        {
          //
          // determine if the passenger object already exists
          //
          tempPassenger = (CPassenger)in.readObject ();

          if (tempPassenger.getPassengerID ().toLowerCase ().compareTo
              (searchID.toLowerCase ()) == 0)
          {
            found = true;
            this.Copy (tempPassenger);
            break;
          }
        } // try

        catch (EOFException e)
        {
          EOF = true;
        }

      } // while

      in.close ();
    } // try

    catch (Exception e)
    {
      e.printStackTrace (System.out);
    }

    //
    // A record was found that has the same passengerID.
    // Ask the users if they want to use this record in the current
    // reservation (if the user answers N, then a new name/address may be
    // given to this passengerID)
    //
    if (found)
    {
      System.out.println ("\n\n");
      System.out.println ("The following passenger exists: \n\n");
      System.out.println (this.toString () + "\n");

      System.out.println ("Do you want to use this name and address to make a ");
      System.out.print ("reservation for this passenger (Y/N)? ");

      ch = AirGourmetUtilities.getChar ();
      System.out.println ("\n");

      found = false;

      if (Character.toUpperCase (ch) == 'Y')
        found = true;
    }
    passengerID = searchID;
    return found;

  } // alreadyExists

} // class CPassenger


//---------------------------------------------------------------------------------------------------------------------------------------------------

class CFlightRecord implements java.io.Serializable
{
  //
  // mealTypeValues represents an array containing the different kind of meal types
  //
  public static String mealTypeValues[] =  {"Child          ", "Diabetic       ",
      "Halaal         ", "Kosher         ", "Lactose Free   ", "Low Calorie    ",
      "Low Cholesterol", "Low Fat        ", "Low Protein    ", "Low Sodium     ",
      "Sea Food       ", "Vegan          ", "Vegetarian     "};

  //
  // data members
  //

  private String        passengerID;  // ID of passenger (9 digits)
  private String        reservationID;  // reservationID of flight (6 uppercase)
  private String        flightNum;  // flight number (3 digits, right justified)
  private Date        flightDate;  // date of flight
  private String        seatNum;  // seat number (3 digits + char, right justified)
  private char        mealType;  // special meal type
  private short        perceivedQuality;  // perceived meal quality (1 through 5)
  private boolean      checkedIn;  // indicates if passenger has checked in
  private boolean      mealLoaded;  // indicates if a passengers meal was loaded

  //
  // accessor methods
  //

  public String  getPassengerID ()      { return passengerID; }
  public String  getReservationID ()    { return reservationID; }
  public String  getFlightNum ()      { return flightNum; }
  public Date  getFlightDate ()      { return flightDate; }
  public String  getSeatNum ()        { return seatNum; }
  public char  getMealType ()        { return mealType; }
  public short  getPerceivedQuality ()    { return perceivedQuality; }
  public boolean  getCheckedIn ()    { return checkedIn; }
  public boolean  getMealLoaded ()    { return mealLoaded; }

⌨️ 快捷键说明

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