📄 ag2.java
字号:
}
//
// retrieve and validate a value for the end date of the report
//
dateOK = false;
while (!dateOK)
{
System.out.print ("Enter the end date for the report: ");
toStrDate = AirGourmetUtilities.readString ();
dateOK = true;
try
{
toDate = flightDateFormat.parse (toStrDate);
toCalendar.setTime (toDate);
}
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;
}
}
//
// validate the report date range
//
if ((toCalendar.after (fromCalendar)) || (toCalendar.equals (fromCalendar)))
rangeOK = true;
else
{
System.out.println ("\n\tThe 'end' date is less than the 'start' date.\n");
System.out.println ("\tPlease enter a valid date range.\n\n");
}
} // while (!rangeOK)
} // getQualifications
protected boolean qualifiesForReport (CFlightRecord aFlightRecord) {return true;}
protected void printRecord (CFlightRecord aFlightRecord) {}
//
// public method
//
public void print ()
//
// print is the default method for printing a report. It iterates through all of the records in the
// flight record file. For each record that qualifies for the report, it invokes function printRecord
//
{
int numRecs = 0; // count of total flight records
boolean EOF = false;
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 ())
{
getQualifications ();
AirGourmetUtilities.clearScreen ();
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 the flight date is within the given range
// and that this the record qualifies for the report
//
if (qualifiesForReport (tempFltRec))
{
if (printHeader)
{
//
// pause the screen after every recsPerScreen flight records
//
if (( (numRecs % recsPerScreen) == 0) && (numRecs != 0))
{
System.out.println ("\n\n Press <ENTER> to view"
+ " the next screen...");
AirGourmetUtilities.pressEnter ();
}
//
// display a header message when needed
//
if ((numRecs % recsPerScreen) == 0)
{
AirGourmetUtilities.clearScreen ();
System.out.println ("\n\n Air Gourmet\n");
System.out.println (" " + theHeader);
}
} // if (printHeader)
//
// call the method to handle this record
//
printRecord (tempFltRec);
numRecs++;
} // if (qualifiesForReport (tempFltRec))
} // try
catch (EOFException e)
{
EOF = true;
}
} // while
in.close ();
} // try
catch (Exception e)
{
e.printStackTrace (System.out);
}
//
// print the report trailer
//
if (printHeader)
{
if (numRecs == 0)
System.out.println ("\n\n\tThere were no records to print...\n\n");
System.out.println ("\n\nPress <ENTER> to return to main menu...");
AirGourmetUtilities.pressEnter ();
}
} // if (fileExists.exists ())
} // print
} // class CReport
class CCatererReport extends CReport
{
//
// data members
//
private String flightNum; // the report is for a specific flight number
private int[] specialMealTally = new int[13];
// represents a tally of the different types of
// meals needed from the caterer
//
// protected methods
//
protected void getQualifications ()
//
// getQualifications retrieves and validates a flight date and a flight number for the caterer
//
{
boolean dateOK = false; // indicates if flight date properly entered
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
{
fromDate = 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;
}
}
//
// retrieves a value for flightNum
//
System.out.print ("Enter the FLIGHT NUMBER: ");
flightNum = AirGourmetUtilities.readString ();
} // getQualifications
protected boolean qualifiesForReport (CFlightRecord aFlightRecord)
//
// qualifiesForReport qualifies a record for this report if it has the same flight date
// and flight number as that specified by the user
//
{
return ((aFlightRecord.getFlightDate ().equals (fromDate))
&& (aFlightRecord.getFlightNum ().compareTo (flightNum) == 0));
} // qualifiesForReport
protected void printRecord (CFlightRecord aFlightRecord)
//
// printRecord does not print any information. It updates an array that keeps track of the different
// kinds of meals that need to be delivered by the caterer
//
{
++specialMealTally[aFlightRecord.getMealType () - 'A'];
} // printRecord
//
// default constructor
//
public CCatererReport ()
{
recsPerScreen = 1;
printHeader = false;
theHeader = " Caterer Special Meals List";
}
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
//
{
char ch;
for (ch = 'A'; ch < 'N'; ch++)
specialMealTally[ch - 'A'] = 0;
super.print ();
System.out.println ("\t\t Air Gourmet");
System.out.println ("\t\t\t" + theHeader + "\n");
System.out.println ("MEAL TYPE\t\t NUMBER OF MEALS NEEDED\n");
System.out.println ("-----------------------------------------------------------------------------");
for (ch = 'A'; ch < 'N'; ch++)
System.out.println (CFlightRecord.mealTypeValues[ch - 'A'] + "\t\t\t "
+ specialMealTally[ch - 'A']);
System.out.println ("\n\nPress <ENTER> to return to main menu...");
AirGourmetUtilities.pressEnter ();
} // print
} // class CCatererReport
class COnBoardReport extends CReport
{
//
// data member
//
private String flightNum; // the report is for a specific flight number
protected void getQualifications ()
//
// getQualifications retrieves and validates a flight date and a flight number for the caterer
//
{
boolean dateOK = false; // indicates if flight date properly entered
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
{
fromDate = 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;
}
}
//
// retrieves a value for flightNum
//
System.out.print ("Enter the FLIGHT NUMBER: ");
flightNum = AirGourmetUtilities.readString ();
} // getQualifications
protected boolean qualifiesForReport (CFlightRecord aFlightRecord)
//
// qualifiesForReport qualifies a record for this report if it has the same flight date
// and flight number as that specified by the user
//
{
return ((aFlightRecord.getFlightDate ().equals (fromDate))
&& (aFlightRecord.getFlightNum ().compareTo (flightNum) == 0));
} // qualifiesForReport
protected void printRecord (CFlightRecord aFlightRecord)
//
// printRecord outputs the passenger, seat number, meal type, and checkbox for this record
//
{
System.out.println ("-----------------------------------------------------------------------------");
System.out.println ("PASSENGER: " + aFlightRecord.getPassengerID ()
+ " SEAT: " + aFlightRecord.getSeatNum ()
+ " MEAL TYPE:"
+ CFlightRecord.mealTypeValues[aFlightRecord.getMealType () - 'A']
+ " |__|");
} // printRecord
//
// default constructor
//
public COnBoardReport ()
{
recsPerScreen = 10;
printHeader = true;
theHeader = " On Board Meals List\n";
}
} // class COnBoardReport
class CNotLoadedReport extends CReport
{
protected boolean qualifiesForReport (CFlightRecord aFlightRecord)
//
// getQualifications qualifies a record for this report if it is within the date range
// of the report and having more than one meal not loaded within the date range
//
{
return (( (aFlightRecord.getFlightDate ().after (fromDate))
|| (aFlightRecord.getFlightDate ().equals (fromDate)))
&& ((aFlightRecord.getFlightDate ().before (toDate))
|| (aFlightRecord.getFlightDate ().equals (toDate)))
&& (aFlightRecord.getCheckedIn ())
&& (!alreadyEncountered (aFlightRecord.getPassengerID () ))
&& (notLoadedMoreThanOnce (aFlightRecord)));
} // qualifiesForReport
protected void printRecord (CFlightRecord aFlightRecord)
//
// printRecord outputs the passenger name/address and the dates when a meal was not loaded
//
{
CFlightRecord tempFltRec; // temporary record used for reading in
// all reservations
CPassenger tempPassenger = new CPassenger ();
// represents the passenger assigned to
// this reservation
boolean EOF = false;
File fileExists = new File ("fltRec.dat");
SimpleDateFormat flightDateFormat = new SimpleDateFormat ("MMM/dd/yyyy");
if (tempPassenger.getPassenger (aFlightRecord.getPassengerID ()))
{
markEncountered (tempPassenger.getPassengerID () );
System.out.println ("");
System.out.println ("-----------------------------------------------------------------------------");
System.out.println ("PASSENGER: " + tempPassenger);
System.out.print ("DATES: ");
}
//
// loop through all flight records
//
try
{
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.getPassengerID ().
compareTo (aFlightRecord.getPassengerID () ) == 0)
&& ((tempFltRec.getFlightDate ().after (fromDate)
|| (tempFltRec.getFlightDate ().equals (fromDate))
&& ((tempFltRec.getFlightDate ().before (toDate))
|| (tempFltRec.getFlightDate ().equals (toDate))))))
System.out.print (flightDateFormat.format (tempFltRec.getFlightDate ())
+ " ");
} // try
catch (EOFException e)
{
EOF = true;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -