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

📄 airgourmet.java.java

📁 AirGourmet:空中美食管理系统。国外一个学习软件工程的经典例子。
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
  //
  // mutator methods
  //
  public void  setPassengerID (String s)    { passengerID = s.toUpperCase (); }
  public void  setReservationID (String r)    { reservationID = r.toUpperCase (); }
  public void  setFlightNum (String f)    { flightNum = f.toUpperCase (); }
  public void  setFlightDate (Date d)    { flightDate = d; }
  public void  setSeatNum (String s)    { seatNum = s.toUpperCase (); }
  public void  setMealType (char m)    { mealType = m; }
  public void  setPerceivedQuality (short p)    { perceivedQuality = p; }
  public void  setCheckedIn (boolean c)    { checkedIn = c; }
  public void  setMealLoaded (boolean m)    { mealLoaded = m; }

  //
  // public methods
  //
  public synchronized String toString ()
  //
  // toString will compose a string representation of the flight record object
  //
  {
    return passengerID + "  " + reservationID + "\n" +  flightNum + "  " + flightDate + "\n"
        + seatNum + "  " + mealType + "\n" + perceivedQuality + "  " + checkedIn + "  "
        + mealLoaded + "\n";
  }

  public void Copy (CFlightRecord tempFltRec)
  //
  // this will make a copy of tempFltRec into the current object
  //
  {
    this.passengerID = tempFltRec.getPassengerID ();
    this.reservationID = tempFltRec.getReservationID ();
    this.flightNum = tempFltRec.getFlightNum ();
    this.flightDate = tempFltRec.getFlightDate ();
    this.seatNum = tempFltRec.getSeatNum ();
    this.mealType = tempFltRec.getMealType ();
    this.perceivedQuality = tempFltRec.getPerceivedQuality ();
    this.checkedIn = tempFltRec.getCheckedIn ();
    this.mealLoaded = tempFltRec.getMealLoaded ();
  }

  public void  getReservation ()
  //
  // getReservation retrieves flight reservation information
  //
  {
    CPassenger      aPassenger = new CPassenger ();
                      // passenger assigned to this reservation
    boolean        mealOK = false;  // indicates if meal type properly entered
    boolean        dateOK = false;  // indicates if flight date properly entered
    boolean        reservationOK = false;  // indicates if reservationID is valid
    boolean        seatOK = false;  // indicates if seat number is valid
    boolean        flightNumOK = false;  // indicates if flight number ID is valid
    String          flightStrDate;  // used to get a string representing a date
    SimpleDateFormat    flightDateFormat = new SimpleDateFormat ("MMM/dd/yyyy");
                      // used to parse a date

    AirGourmetUtilities.clearScreen ();

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

    //
    // retrieve and validate a value for reservationID
    //
    while (!reservationOK)
    {
      System.out.print ("Enter the RESERVATION ID: ");
      reservationID = AirGourmetUtilities.readString ();

      if (checkReservationID () )
        if (!alreadyExists () )
          reservationOK = true;
        else
        {
          System.out.println ("\n\tThis RESERVATION ID already exists.\n");
          System.out.println ("\tPlease try again...\n\n");
        }
      else
      {
        System.out.println ("\n\tA RESERVATION ID must be 6 letters.\n");
        System.out.println ("\tPlease try again...\n\n");
      }
    }

    //
    // retrieve and validate a value for flightNum
    //
    while (!flightNumOK)
    {
      System.out.print ("Enter the FLIGHT NUMBER: ");
      flightNum = AirGourmetUtilities.readString ();

      if (checkFlightNum () )
        flightNumOK = true;
      else
      {
        System.out.println ("\n\tA FLIGHT NUMBER must be 3 digits.\n");
        System.out.println ("\tPlease try again...\n\n");
      }
    }

    //
    // retrieve and validate a value for flightDate
    //
    while (!dateOK)
    {
      System.out.print ("Enter the DATE of the flight: ");

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

      try
      {
        flightDate = flightDateFormat.parse (flightStrDate);
      }
      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;
      }

    }

    //
    // retrieve and validate a value for seatNum
    //
    while (!seatOK)
    {
      System.out.print ("Enter the SEAT NUMBER assigned to this passenger: ");
      seatNum = AirGourmetUtilities.readString ();

      if (checkSeatNum () )
        if (!seatReserved () )
          seatOK = true;
        else
        {
          System.out.println ("\n\tThis seat is already reserved.\n");
          System.out.println ("\tPlease choose another seat.\n\n");
        }
      else
      {
        System.out.println ("\n\tA SEAT NUMBER must be 3 digits followed by" +
            " an uppercase letter.\n");
        System.out.println ("\tPlease try again...\n\n");
      }
    }

    //
    // retrieve and validate a value for mealType
    //
    while (!mealOK)
    {
      System.out.println ("\n\nList of available special meals:\n\n");
      System.out.println ("  A - Child             B - Diabetic          C - Halaal\n");
      System.out.println ("  D - Kosher            E - Lactose Free      F - Low Cal\n");
      System.out.println ("  G - Low Cholesterol   H - Low Fat           I - Low Protein\n");
      System.out.println ("  J - Low Sodium        K - Sea Food          L - Vegan\n");
      System.out.println ("  M - Vegetarian\n\n");

      System.out.print ("Enter the SPECIAL MEAL for this reservation: ");

      mealType = Character.toUpperCase (AirGourmetUtilities.getChar ());

      if ((mealType >= 'A') && (mealType <= 'M'))
        mealOK = true;
      else
      {
        System.out.println ("Invalid response!\n");
        System.out.println ("Please enter a value from the following list:\n");
      }
    }

    //
    // get passenger information and insert the passenger in the passenger file
    //
    aPassenger.getDescription ();
    aPassenger.insert ();

    //
    // copy the passengerID from the passenger object to the flight record object
    // initialize the values for perceivedQuality, checkedIn, mealLoaded
    //
    passengerID = aPassenger.getPassengerID ();
    perceivedQuality = -1;
    checkedIn = false;
    mealLoaded = false;

    insert ();
    // insert the reservation into the reservation file

  } // getReservation


  public void checkInPassenger ()
  //
  // checkInPassenger sets checkedIn to true for a specific reservation
  //
  {
    boolean        reservationOK = false;  // indicates if reservationID is valid

    AirGourmetUtilities.clearScreen ();
    //
    // retrieve and validate a value for reservationID
    //
    while (!reservationOK)
    {
      System.out.print ("Enter the RESERVATION ID: ");
      reservationID = AirGourmetUtilities.readString ();

      if (checkReservationID () )
        reservationOK = true;
      else
      {
        System.out.println ("\n\tA RESERVATION ID must be 6 letters.\n");
        System.out.println ("\tPlease try again...\n\n");
      }
    }

    //
    // search for the reservation, set checkedIn to true, and then insert the change
    //
    if (alreadyExists () )
    {
      checkedIn = true;
      insert ();

      System.out.println ("\n\n\tThe passenger has been checked in.\n");
      System.out.println ("\tPlease check their identification.\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 ();
    }

  } // checkInPassenger

  public boolean scanSpecialMeals ()
  //
  // scanSpecialMeals queries the user whether the meal was loaded and then updates the file.
  // It does this for all of the reservations on a specific flight (flight number + flight date)
  //
  {
    boolean        dateOK = false;  // indicates if flight date is properly entered
    boolean        flightNumOK = false;  // indicates if flight number is valid
    boolean        EOF = false;
    char          ch;    // holds user response to Y/N question
    File          fileExists = new File ("fltRec.dat");
                      // used to test if file exists
    CFlightRecord tempFltRec;    // temporary object used for file copying
    String flightStrDate;        // used to get a string representing a date
    SimpleDateFormat flightDateFormat = new SimpleDateFormat ("MMM/dd/yyyy");
                      // used to parse a date

    AirGourmetUtilities.clearScreen ();
    //
    // retrieve and validate a value for flightDate
    //
    while (!dateOK)
    {
      System.out.print ("Enter the DATE of the flight: ");

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

      try
      {
        flightDate = flightDateFormat.parse (flightStrDate);
      }
      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;
      }

    }

    //
    // retrieve and validate a value for flightNum
    //
    while (!flightNumOK)
    {
      System.out.print ("Enter the FLIGHT NUMBER: ");
      flightNum = AirGourmetUtilities.readString ();

      if (checkFlightNum () )
        flightNumOK = true;
      else
      {
        System.out.println ("\n\tA FLIGHT NUMBER must be 3 digits.\n");
        System.out.println ("\tPlease try again...\n\n");
      }
    }

    AirGourmetUtilities.clearScreen ();

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

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

      while (!EOF)
      {
        try
        {
          //
          // copy the current flight record file to a temporary file
          //
          tempFltRec = (CFlightRecord)in.readObject ();
          out.writeObject (tempFltRec);
        } // try

        catch (EOFException e)
        {
          EOF = true;
        }

      } // while

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

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

    EOF = false;

    //
    // copy the temporary file to new flight record file
    // while inserting the passenger object in the proper location after
    // updating mealLoaded
    //

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

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

          if ((flightDate.equals (tempFltRec.getFlightDate ()))
              && (flightNum.toUpperCase ().compareTo (tempFltRec.
                getFlightNum ().toUpperCase () ) == 0))
          {
            System.out.println ("\n\nPASSENGER: " + tempFltRec.getPassengerID ());
            System.out.println ("  SEAT: " + tempFltRec.getSeatNum ());
            System.out.println ("  MEAL TYPE: "
                + mealTypeValues[tempFltRec.getMealType () - 'A']);
            System.out.print ("\n\nWas the meal for this passenger loaded (Y/N) ? ");

            ch = AirGourmetUtilities.getChar ();
            if (Character.toUpperCase (ch) == 'Y')
              tempFltRec.setMealLoaded (true);
            else
              tempFltRec.setMealLoaded (false);
          }
          out.writeObject (tempFltRec);

        } // try

        catch (EOFException e)
        {
          EOF = true;
        }

      } // while

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

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

    return true;
  } // scanSpecialMeals

  public void scanPostcard ()
  //
  // scanPostcard sets perceivedQuality for a specific reservation to a value entered
  // by the user and inserts the change
  //
  {
    boolean        reservationOK = false;   // indicates if reservationID is valid

    AirGourmetUtilities.clearScreen ();

    //
    // retrieve and validate a value for reservationID
    //
    while (!reservationOK)
    {
      System.out.print ("Enter the RESERVATION ID: ");
      reservationID = AirGourmetUtilities.readString ();

      if (checkReservationID () )
        reservationOK = true;
      else
      {
        System.out.println ("\n\tA RESERVATION ID must be 6 letters.\n");
        System.out.println ("\tPlease try again...\n\n");
      }
    }

    //
    // if the reservation exists, get the perceivedQuality and then
    // write the change back to the file
    //
    if (alreadyExists () )
    {
      String tempString;
      System.out.print ("Enter perceived meal quality (1 thru 5):");

⌨️ 快捷键说明

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