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

📄 ag2.java

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

      } // while

      in.close ();
    } // try

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

  } // printRecord

  private boolean  alreadyEncountered (String searchID)
  //
  // alreadyEncountered determines if a passengerID has already been encountered by this report
  //
  {
    boolean        found = false;  // indicates if pass, already encountered
    String          tempID;  // temporary passengerID read from file
    File          fileExists = new File ("notLoaded.dat");
                      // used to test if file exists

    if (fileExists.exists ())
      try
      {
        RandomAccessFile randomReader = new RandomAccessFile
            ("notLoaded.dat", "r");

        //
        // loop through all IDs in the file
        //
        while ((tempID = randomReader.readLine ()) != null)
        {
          if (tempID.toUpperCase ().indexOf (searchID.toUpperCase ()) >= 0)
          {
            found = true;
            break;
          }
        }

        randomReader.close ();

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

    return found;

  } // alreadyEncountered

  private boolean  notLoadedMoreThanOnce (CFlightRecord aFlightRecord)
  //
  // notLoadedMoreThanOnce determines if a passenger has had more than one meal not loaded
  //
  {
    boolean        EOF = false;
    short          notLoadedCount = 0;  // count of meals not loaded
    CFlightRecord      tempFltRec;  // temporary record used for reading in
                      // and comparing all reservations
    File                 fileExists = new File ("fltRec.dat");
                      // used to test if file exists

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

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

            //
            // check if there is a match with the current flight record object
            // must have the same passengerID and be within report date range
            //
            if ((!tempFltRec.getMealLoaded () )
                && (tempFltRec.getCheckedIn ())
                && (tempFltRec.getPassengerID ().toUpperCase ().compareTo
                    (aFlightRecord.getPassengerID ().toUpperCase ()) == 0)
                && ((tempFltRec.getFlightDate ().after (fromDate))
              || (tempFltRec.getFlightDate ().equals (fromDate)))
                && ((toDate.after (tempFltRec.getFlightDate () ))
              || (toDate.equals (tempFltRec.getFlightDate () ))))
            {
              notLoadedCount++;
              if (notLoadedCount > 1)
                break;
             }
          }
          catch (EOFException e)
          {
            EOF = true;
          }

        } // while (!EOF)

        in.close ();
      } // if (fileExists.exists ())

    } // try

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

    return (notLoadedCount > 1);

  } // notLoadedMoreThanOnce


  private void markEncountered (String encounteredID)
  //
  // markEncountered adds the current passengerID to the notLoaded file
  //
  {
    try
    {
      RandomAccessFile randomWriter = new RandomAccessFile ("notLoaded.dat", "rw");

      //
      // write the new ID to the end of the file
      //
      randomWriter.seek (randomWriter.length ());
      randomWriter.writeBytes (encounteredID + "\n");
      randomWriter.close ();
    }
    catch (Exception e)
    {
      System.out.println ("Error: " + e.toString ());
    }

  } // markEncountered

  //
  // default constructor
  //

  public CNotLoadedReport ()
  {
    recsPerScreen = 2;
    printHeader = true;
    theHeader = "      Meals Not Loaded Report\n";
  }

  public void print ()
  //
  // print deletes a needed file before calling the base class print method
  //
  {
    File          fileNotLoaded = new File ("notLoaded.dat");
                      // used to test if file exists

    //
    // delete the notLoaded file before printing the report
    //
    if (fileNotLoaded.exists ())
      fileNotLoaded.delete ();

    super.print ();

  } // print ()

} // class CNotLoadedReport

class CPoorQualityReport extends CReport
{
  protected boolean qualifiesForReport (CFlightRecord aFlightRecord)
  //
  // qualifiesForReport qualifies a record for this report if it has a special meal loaded within the date
  // range of the report with a perceived quality less than 5
  //
  {
    return (( (aFlightRecord.getFlightDate ().after (fromDate))
          || (aFlightRecord.getFlightDate ().equals (fromDate)))
            && ((aFlightRecord.getFlightDate ().before (toDate))
          || (aFlightRecord.getFlightDate ().equals (toDate)))
            && (aFlightRecord.getMealLoaded () )
            && (aFlightRecord.getPerceivedQuality () < 5)
            && (aFlightRecord.getPerceivedQuality () > -1));
  } // qualifiesForReport

  protected void printRecord (CFlightRecord aFlightRecord)
  //
  // printRecord outputs the passenger, flight date, and meal type
  //
  {
    CPassenger      tempPassenger = new CPassenger ();
                      // represents the passenger assigned to
                      // this reservation
    SimpleDateFormat    flightDateFormat = new SimpleDateFormat ("MMM/dd/yyyy");
                      // used to format dates

    if (tempPassenger.getPassenger (aFlightRecord.getPassengerID () ))
    {
      System.out.println ("-----------------------------------------------------------------------------\n");
      System.out.println ("PASSENGER: " + tempPassenger.toString ());
      System.out.println ("FLIGHT DATE:   "
                  + flightDateFormat.format (aFlightRecord.getFlightDate ())
                   + "        MEAL TYPE: "
                  + CFlightRecord.mealTypeValues[aFlightRecord.getMealType ()
                  -  'A']);
    }
  } // printRecord

  //
  // default constructor
  //

  public CPoorQualityReport ()
  { recsPerScreen = 2;
    printHeader = true;
    theHeader = "         Poor Quality Report\n";
  }

} // class CPoorQualityReport

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

class CLowSodiumReport extends CReport
{
  protected boolean qualifiesForReport (CFlightRecord aFlightRecord)
  //
  // qualifiesForReport qualifies a record for this report if it has a loaded low-sodium meal within
  // the date range of the report
  //
  {
    return (( (aFlightRecord.getFlightDate ().after (fromDate))
          || (aFlightRecord.getFlightDate ().equals (fromDate)))
            && ((aFlightRecord.getFlightDate ().before (toDate))
          || (aFlightRecord.getFlightDate ().equals (toDate)))
            && (aFlightRecord.getMealType () == 'J')
            && (aFlightRecord.getMealLoaded () == true));
  } // qualifiesForReport

  protected void printRecord (CFlightRecord aFlightRecord)
  //
  // printRecord outputs the flight number, flight date, and perceived quality
  //
  {
    SimpleDateFormat flightDateFormat = new SimpleDateFormat ("MMM/dd/yyyy");
                      // used to format dates

    System.out.println ("-----------------------------------------------------------------------------");
    System.out.println ("FLIGHT NUMBER: " + aFlightRecord.getFlightNum ());
    System.out.println ("FLIGHT DATE:   "
        + flightDateFormat.format (aFlightRecord.getFlightDate ()));
    System.out.println ("PERCEIVED QUALITY: " + aFlightRecord.getPerceivedQuality ());
  } // printRecord

  //
  // default constructor
  //
  public CLowSodiumReport ()
  {
    recsPerScreen = 3;
    printHeader = true;
    theHeader = "          Low Sodium Report\n";
  }

} // class CLowSodiumReport


class CPercentageReport extends CReport
{
  // the following are used to keep track of the different kinds of totals
  // needed to display the various percentages
  //
  private int[]       loadedAsSpecifiedFound = new int[13];
  private int[]       onBoardFound = new int[13];
  private int[]       onBoardNotLoaded = new int[13];
  private int[]       totalEncountered = new int[13];

  protected boolean qualifiesForReport (CFlightRecord aFlightRecord)
  //
  // qualifiesForReport qualifies a record for this report if it has a special meal within the date
  // range of the report
  //
  {
    return (( (aFlightRecord.getFlightDate ().after (fromDate))
          || (aFlightRecord.getFlightDate ().equals (fromDate)))
            && ((aFlightRecord.getFlightDate ().before (toDate))
          || (aFlightRecord.getFlightDate ().equals (toDate))));
  } // qualifiesForReport

  protected void printRecord (CFlightRecord aFlightRecord)
  //
  // printRecord does not print any information.  Instead,
  // it updates several arrays that keep track of the different kinds of percentages
  //
  {
    ++totalEncountered[aFlightRecord.getMealType () -  'A'];
    ++totalEncountered[12];

    if (aFlightRecord.getMealLoaded () == true)
    {
      ++loadedAsSpecifiedFound[aFlightRecord.getMealType () -  'A'];
      ++loadedAsSpecifiedFound[12];
    }
    if (aFlightRecord.getCheckedIn () == true)
    {
      ++onBoardFound[aFlightRecord.getMealType () -  'A'];
      ++onBoardFound[12];
    }
    if ((aFlightRecord.getCheckedIn () == true)
        && (aFlightRecord.getMealLoaded () == false))
    {
      ++onBoardNotLoaded[aFlightRecord.getMealType () - 'A'];
      ++onBoardNotLoaded[12];
    }
  } // printRecord

  //
  // default constructor
  //

  public CPercentageReport ()
  {
    recsPerScreen = 2;
    printHeader = false;
    theHeader = "         Percentages Report\n";
  }

  public void print ()
  //
  // print overrides the print method in the base class.
  // It initializes an array that keeps track of meals, calls the base
  // class print method, and then prints out the array
  //
  {
    int          i;
    NumberFormat     df = NumberFormat.getPercentInstance ();
                      // used to format percentages

    //
    // initialize the totals
    //
    for (i = 0; i < 13; i++)
    {
      loadedAsSpecifiedFound[i] = 0;
      onBoardFound[i] = 0;
      onBoardNotLoaded[i] = 0;
      totalEncountered[i] = 0;
    }

    super.print ();

    //
    // print header
    //
    System.out.println ("\t\t                     Air Gourmet");
    System.out.println ("\t\t\t" + theHeader + "\n");

    System.out.println ("MEAL TYPE\t     % LOADED\t      % ON BOARD" +
                      "     % ON BOARD, NOT LOADED");
    System.out.println ("-----------------------------------------------------------------------------");

    //
    // print the percentages for each meal type
    //
    for (i = 0; i < 12; i++)
    {
      System.out.print (CFlightRecord.mealTypeValues[i] + "\t     ");

      if (totalEncountered[i] == 0)
        System.out.print ("   NA\t\t");
      else
        System.out.print ("   "+ df.format ((float) loadedAsSpecifiedFound[i] /
              (float) totalEncountered[i]) + " \t\t");

      if (totalEncountered[i] == 0)
        System.out.print ("   NA\t\t   ");
      else
        System.out.print ("   "+ df.format ((float) onBoardFound[i] /
                (float) totalEncountered[i]) + "\t\t   ");

      if (totalEncountered[i] == 0)
        System.out.print ("   NA");
      else
        System.out.print ("   " + df.format ((float) onBoardNotLoaded[i] /
                (float) totalEncountered[i]));

      System.out.println ("");
    }

    //
    // print the totals
    //
    System.out.print ("\nTOTALS:        \t     ");

    if (totalEncountered[12] == 0)
      System.out.print ("  NA\t\t");
    else
      System.out.print ("   "+ df.format ((float) loadedAsSpecifiedFound[12] /
                    (float) totalEncountered[12]) + " \t\t");

    if (totalEncountered[12] == 0)
      System.out.print ("   NA\t\t   ");
    else
      System.out.print ("   "+ df.format ((float) onBoardFound[12] /
                  (float) totalEncountered[12]) + "\t\t   ");

    if (totalEncountered[12] == 0)
      System.out.print ("   NA");
    else
      System.out.print ("   " + df.format ((float) onBoardNotLoaded[12] /
                (float) totalEncountered[12]));

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

  } // print

} // class CPercentageReport

class AirGourmetUtilities
{
  //
  // public static methods
  //

  public static char getChar ()
  //
  // getChar re

⌨️ 快捷键说明

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