Java代写:CSE100F Flight Operations Part3

完成UI部分,也就是菜单的交互逻辑,需要接着上次的继续代写。

Carrier

The driver program, Carrier.java then presents the user with a menu, as follows:

Flight Ops Menu
    1. Add Plane
    2. Add Crew
    3. Display
    4. Assign mission
    5. End mission
    6. Save to file
    7. Close Flight Ops
Enter choice >>

Note: Carrier class must NOT have Crew object attribute(s). All interactions involving Crew will still be through the relevant Plane object.

Implement the functionality of each menu choice as follows (assume user input is always an integer).

Add Plane

This menu option is chosen when the user attempts to add a Plane object from the keyboard.

This time the difference is the program must first check if there is a free space in the planes array.

As before, the first thing that the user will enter is the tail number of the Plane to add. The program must check that this tail number is not already assigned to any Plane in the array of Planes.

If there is space and the tail number is unique, the user is asked for all the relevant Plane information and the resultant Plane object is added to the next free space in the array of Planes. If there is no free space in the array, the program returns to the main menu without asking the user for any information.

If the tail number is already assigned to a Plane, the user is informed via a message to the screen and the program returns to the main menu.

Still NO Crew information is entered in this menu choice.

Add Crew

This menu choice adds a Crew to a Plane, using information from the keyboard. First, of course, the program must check that there is actually at least one Plane in the array. If there are no Planes (that is, the array of Planes is empty), then the user is informed with a message to the screen and the program returns to the main menu. The user is not asked for any further information.

If there is at least one Plane object in the array, then the user is asked for the tail number of a Plane. The program tries to find the Plane with this tail number.

If the Plane with the user entered tail number is found, then there is one further check. This is to check that that Plane does not already have its maximum number of Crew. If the Plane already has its maximum number of Crew, then the user is informed via a message to the screen, no further information is asked from the user, and the program returns to the main menu.

If the Plane with the user entered tail number is found and is not at its maximum crew, then the user is asked to enter the id of a Crew. There is one final check. The program must check that this user entered Crew id is not already in use. If the user entered Crew id is already in use, then the user is informed, via a message to the screen, no further information is requested from the user, and the program returns to the main menu.

This is a more complex problem than in Assignment C, although it is still the same condition. As each Plane now has an array of Crew, the program has to go through all the Plane objects in the array of Planes and check through the array of Crew in each Plane. Only when this has been done and the id for that Crew has not been found do we know that the Crew id entered by the user is indeed unique.

There are 2 basic strategies for doing this, the first is to return a (privacy leak free) copy of the Crew array for an individual Plane to the main driver program (Carrier). The program can then go through this array checking the Crew id’s in that array with the id entered by the user. If the id is not found and there is another Plane in the array of Planes, the program moves to the next Plane, gets a copy of the Crew array for that Plane and searches through that array.

The alternative is to write a method in the Plane class that takes the user entered id as a parameter. The method in the Plane class searches through the Crew array and returns true or false as to whether the Crew id passed in as a parameter was found.

Either method is acceptable, write the one that you feel most comfortable with. If the program gets to this point, then there is a Plane object with the user entered tail number and the user entered Crew id is unique. The user is then asked to enter the name and role of the Crew and the Crew object is added to the Crew array for that Plane object. (The number of missions must be 0, as we have just instantiated the Crew and the experience level is worked out by the Crew constructor, based on the number of missions).

If there is not a Plane with the user entered tail number, then a message is displayed to the screen and the program returns to the main menu, without asking for any more information.

Display

This menu choice displays the contents of the non-null Plane object references to the screen. The format is shown in the sample run of the program on page 16. This time you are free to format the output any way you see fit, with a reminder that the output must be easy for the user of the program to quickly understand. Calling any of these menu choices must not result in the program crashing (for example, NullPointerException)

This is now the top level menu for display. When the user selects this top level option, another sub menu will be presented.

Flight Ops Display Menu
    1. Display all
    2. Display Planes (no Crew)
    3. Display Single Plane
    4. Display Single Crew
    5. Display Planes on a mission
    6. Return to main menu
Enter choice >>
  1. Display all
    Choosing this menu option, all of the Planes, with all their Crew, are displayed to the screen.

  2. Display Planes (no Crew)
    Choosing this menu option, all of the Planes, this time without any Crew information, are displayed to the screen.

  3. Display Single Plane
    Choosing this menu option, the user is prompted (asked) for the tail number of a Plane. If the Plane with that tail number is found in the array, then all the information about that Plane, including its Crew, are displayed to the screen. If the Plane with that tail number is not found in the array, then an appropriate message is displayed to the screen.

  4. Display Single Crew
    Choosing this menu option, the user is prompted (asked) for the id of a Crew. If the Crew with that id is found, then the information for that one Crew is displayed to the screen. If the Crew with the requested id is not found, then an appropriate message is displayed to the screen.

  5. Display Planes on a mission
    Choosing this menu option, only those Planes that are on a mission, with their Crew information, are displayed to the screen

  6. Return to main menu

Assign Mission

This menu choice first checks that there is at least one Plane object reference in the array. If not, then an appropriate message is displayed to the screen and the program returns to the main menu. If there is at least one non-null Plane object reference in the array, then the user is prompted (asked) for the tail number of a Plane.

The program must then find the Plane that contains the Plane object with this tail number. This could be anywhere in the array. If the Plane with that tail number is not found, then an appropriate message is displayed to the screen and the program returns to the main menu.

If the Plane with that tail number is found, then the program must check that this Plane is available for a mission.

To be available for a mission, the user must enter the required experience level of the mission. The Plane must not be flying and the Plane must have a Crew with the overall experience level greater than or equal to the experience level entered by the user. This is explained in detail above.

If the Plane is not available for a mission, then an appropriate message is displayed to the screen and the program returns to the main menu.

If the Plane can be assigned a mission, then the appropriate changes are made to the Plane object. These are that the number of missions flown by all the Crew, of that Plane, are incremented by 1 (do not forget to check whether this crosses one of the boundaries of the experience level, resulting in a change in experience level) and the flying attribute is set to true, indicating that the Plane is on a mission.

The program always returns to the main menu at the end of the menu choice, regardless of the action taken or not taken.

End Mission

This menu choice prompts (asks) the user for the tail number of a Plane, after making the same checks as in Assign Mission, that is, there is actually at least one non-null Plane object in the array. As with Assign Mission appropriate messages should be displayed to the screen if the Plane tail number is not found, array of Planes is empty, or if the tail number is found but that Plane is not already on a Mission. (You can’t end a mission unless the Plane is currently on a mission.)

If any of the above are true, then the program returns to the main menu.

If the Plane tail number exists and the Plane is on a mission, then the flying attribute is set to false, indicating that the Plane is not on a mission.

The mission time for the Plane is also reset to 0.

After the conclusion of any and all actions in this menu choice, the program returns to the main menu.

Save to file

Choosing this menu option, the user is prompted (asked) for the name of an output text file and all the information in the array is written to this text file.

File names must NOT be hard coded.

Also recall that this output file must be able to be used as input file the next time that the program is run, so the output file needs to be written to the file in the format as shown on page 10.

Close Flight Ops

This menu choice closes the program, without asking the user if they would like to save their changes.