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

📄 ag2.java

📁 AirGourmet:空中美食管理系统。国外一个学习软件工程的经典例子。
💻 JAVA
📖 第 1 页 / 共 5 页
字号:

      tempString = AirGourmetUtilities.readString ();
      perceivedQuality = (short)Integer.parseInt (tempString);

      insert ();

      System.out.println ("\n\n\tThe passenger record has been updated.\n");

      System.out.println ("\n\nPress <ENTER> to return to main menu...");
      AirGourmetUtilities.pressEnter ();
    }
    else
    {
      System.out.println ("\n\n\tThere is no reservation with this ID...");

      System.out.println ("\n\nPress <ENTER> to return to main menu...");
      AirGourmetUtilities.pressEnter ();
    }

  } // scanPostcard

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

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

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

        while (!EOF)
        {
          try
          {
            // read/write temporary object from the passenger file
            tempFltRec = (CFlightRecord)in.readObject ();
            out.writeObject (tempFltRec);
          }
          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 ("tempF.dat"));
      ObjectOutputStream out = new ObjectOutputStream (new FileOutputStream
          ("fltRec.dat"));

      while (!EOF)
      {
        try
        {
          tempFltRec = (CFlightRecord)in.readObject ();

          //
          // copy the temporary file to new flight record file
          // while inserting the flight record object in the proper location
          //
          if (reservationID.toLowerCase ().compareTo (tempFltRec.getReservationID
              ().toLowerCase ()) == 0)
          {
            out.writeObject (this);
            found = true;
          }
          else
            out.writeObject (tempFltRec);

        } // 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 methods
  //

  private boolean  checkReservationID ()
  //
  // checkReservationID determines if the reservationID is valid
  //
  {
    boolean        valid = true;  // indicates if the reservationID is valid
    short          i;      // used to iterate through chars in the
                      // reservationID
    char          ch;

    reservationID = reservationID.toUpperCase ();

    //
    // make sure that the reservationID is composed of exactly 6 chars
    //
    if (reservationID.length () == 6)
    {
      for (i = 0; i<6; i++)
        {
          ch = reservationID.charAt (i);
          if ((!Character.isLetter (ch)) || (ch == ' '))
            valid = false;
        }
    }
    else
      valid = false;

    return valid;

  } // checkReservationID

  private boolean  checkFlightNum ()
  //
  // checkFlightNum determines if the flight number is valid
  //
  {
    boolean        valid = true;  // indicates if the flight number is valid
    short          i;      // used to iterate thru the chars in flightNum
    StringBuffer      tempFltNum = new StringBuffer ();
                      // used in right justification

    //
    // flightNum must be composed of digits only
    //
    for (i = 0; i<flightNum.length (); i++)
    {
      if (!Character.isDigit (flightNum.charAt (i)))
        valid = false;
    }

    // right justify flightNum and zero-fill
    //
    if (valid)
    {
      for (i = 0; i<3; i++)
        if (i < 3 - flightNum.length ())
          tempFltNum.append ('0');
        else
          tempFltNum.append (flightNum.charAt (i + flightNum.length () - 3));

      flightNum = tempFltNum.toString ();

    }
    return valid;

  } // checkFlightNum

  private boolean checkSeatNum ()
  //
  // checkSeatNum determines if the seat number is valid
  //
  {
    boolean        valid = true;  // indicates if the flight number is valid
    short          i;      // used to iterate through chars in seatNum
    StringBuffer  tempSeatNum = new StringBuffer ();
                      // used in right justification

    //
    // seatNum must be composed of digits followed by a single char
    //
    if (Character.isLetter (seatNum.charAt (seatNum.length () - 1)))
    {
      for (i = 0; i< (seatNum.length () - 1); i++)
      {
        if (!Character.isDigit (seatNum.charAt (i)))
          valid = false;
      }
    }
    else
      valid = false;

    // right justify seatNum and zero-fill
    //
    if (valid)
    {
      for (i = 0; i<4; i++)
        if (i < 4 - seatNum.length ())
          tempSeatNum.append ('0');
        else
          tempSeatNum.append (seatNum.charAt (i + seatNum.length () - 4));

      seatNum = tempSeatNum.toString ();
    }

    return valid;

  } // checkSeatNum

  private boolean alreadyExists ()
  //
  // alreadyExists determines if the reservationID of the current object already exists in the file
  //
  {
    boolean        found = false;  // indicates if passenger already exists
    boolean        EOF = false;
    String          searchID;  // the passengerID for which to search
    File          fileExists = new File ("fltRec.dat");
                      // used to test if file exists
    CFlightRecord tempFltRec;    // used to read in object from flight record file

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

    searchID = reservationID;

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

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

          //
          // check if there is a match with the searchID
          //
          if (tempFltRec.getReservationID ().toLowerCase ().
              compareTo (searchID.toLowerCase ()) == 0)
          {
            found = true;
            this.Copy (tempFltRec);
            break;
          }
        } // try

        catch (EOFException e)
        {
          EOF = true;
        }

      } // while

      in.close ();
    } // try

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

    reservationID = searchID;
    return found;

  } // alreadyExists

  private boolean seatReserved ()
  //
  // seatReserved determines if the seat is already reserved
  //
  {
    boolean        found = false;  // indicates if seat reserved
    File          fileExists = new File ("fltRec.dat");
                      // used to test if file exists
    boolean        EOF = false;
    CFlightRecord tempFltRec;    // used to read in object from flight record file

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

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

      while (!EOF)
      {
        try
        {
          //
          // check if there is a match with the current seat
          //
          tempFltRec = (CFlightRecord)in.readObject ();

          if ((flightNum.toUpperCase ().compareTo (tempFltRec.getFlightNum ()) == 0)
              && (seatNum.toUpperCase ().compareTo (tempFltRec.
                  getSeatNum ()) == 0)
              && (tempFltRec.getFlightDate () == flightDate))
          {
            found = true;
            break;
          }

        } // try

        catch (EOFException e)
        {
          EOF = true;
        }

      } // while

      in.close ();
    } // try

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

    return found;

  } // seatReserved

} // class CFlightRecord


class CReport
{
  //
  // data members
  //

  protected Date      fromDate;  // fromDate and toDate represent the
  protected Date      toDate;  // range of dates used in the report
  protected short      recsPerScreen;  // # of records in a report per screen
  protected boolean    printHeader;  // indicates if theHeader is always to be
                      // printed
  protected String      theHeader;  // title of the report

  //
  // accessor methods
  //

  public Date    getFromDate ()    { return fromDate; }
  public Date    getToDate  ()    { return toDate; }

  //
  // mutator methods
  //
  public void    setFromDate (Date f)  { fromDate = f; }
  public void    setToDate (Date t)  { toDate = t; }

  //
  // overridden methods
  //
  // Description:
  //    -getQualifications:    This method retrieves from the user any values
  //                that are needed to determine if a record
  //                qualifies for inclusion in the report
  //    -qualifiesForReport:  This method applies the qualifications to a
  //                flight record to see if it should be in the report
  //    -printRecord:      This method prints a flight record to the screen
  //                based on the type of report
  //

  protected void getQualifications ()
  //
  // getQualifications is the default method for getting the qualifications.
  // It obtains the start and end dates that define the range of the report
  //
  {
    boolean        dateOK = false;  // indicates if a date was properly entered
    boolean        rangeOK = false;  // indicates if report range properly entered
    String          fromStrDate, toStrDate;
    SimpleDateFormat flightDateFormat = new SimpleDateFormat ("MMM/dd/yyyy");
    Calendar toCalendar = new GregorianCalendar ();
                      // used to convert to date
    Calendar fromCalendar = new GregorianCalendar ();
                      // used to convert from date

    AirGourmetUtilities.clearScreen ();

    while (!rangeOK)
    {
      //
      // retrieve and validate a value for the start date of the report
      //
      while (!dateOK)
      {
        System.out.print ("Enter the start date for the report: ");

        fromStrDate = AirGourmetUtilities.readString ();
        dateOK = true;

        try
        {
          fromDate = flightDateFormat.parse (fromStrDate);
          fromCalendar.setTime (fromDate);
        }
        catch (ParseException pe)
        {
          System.out.println ("\n\tYou entered the date incorrectly.\n");
          System.out.println ("\tPlease use the format mmm/dd/yyyy.\n\n");
          dateOK = false;
        }

⌨️ 快捷键说明

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