接着上次的Part继续代写,实现Crew, Plane,这两个类的逻辑部分。
Program Requirements
You have been asked to write an interactive program, in Java, to aid in
monitoring and maintaining all aspects of Flight Operations.
This program expands on the earlier work done in Assignment Part C and now
needs to handle 90 Planes. Each Plane can have more than one Crew associated
with it.
To aid in the rapid development of this program, 3 Java files and 1 sample
input file are provided for you: Crew.java, Plane.java, Carrier.java and a
sample input file a.dat.
In order to further speed up the development of this program, some of the
files listed above have been partially implemented for you, read the comments
in the files and in this document.
Crew.java
All Crew objects have the following object attributes:
- name: This a String (text) and is the name of the Crew, may be more than one word
- role: This a String (text) and is the role (job) of the Crew, may be more than one word
- id: This is an integer and is the unique id of the Crew
- misions: This is an integer and is the number of missions that the Crew has undertaken.
- experience level: This is a String and is one of the four levels as described above. The experience level is always based on the number of missions that the Crew has flown and must be set by the program. The user must NEVER be able to enter the experience level, nor is it ever stored in a file.
The Crew class requires the following functionality:
A constructor that takes name, id, role and missions as parameters. This is
the constructor that is called when a new Crew is added from the text file.
From the file, all four of these parameters have values.
You might want to consider writing a copy constructor for the Crew.
The format of the text file is shown on page 10
Recall that it is the number of missions that determine the experience level,
so there has to be a way for the constructor to set the experience level. If
this constructor is also used for keyboard input, then the number of missions
must be set to 0 as the Crew has just been created.
Alternatively, you could write an overloaded constructor that just takes the
first three values (name, role and id) as parameters and assigns 0 to the
number of missions.
Either way, the experience level must be set by the constructor, not the user.
The Crew class also requires accessor methods as you deem appropriate.
The Crew class also requires a method to increment the number of missions.
Calling this method adds 1 to the number of missions, which can change the
experience level.
The Crew class requires a toString method which returns a String with
information about the Crew object, see page 16 for the format of the String to
be returned.
Please note that this time you are free to format the screen output in any
way, provided only that the user of the program can easily understand the
information being displayed.
This is Carrier Air Group operations after all, the users need to be able to
take in the information at a glance, not spend time trying to decipher poorly
formatted output. Planes don’t stop in the air while the user tries to read
information.
The one addition to the functionality of this Crew class that you might
(optional, not have to) consider is a method that can be called to write all
of the Crew information to a text file.
There are other ways of doing this, this is not the only way and is NOT the
“right” answer.
Plane.java
The Plane class has the following attributes:
- name: This is a String (text) and is name of the Plane, may consist of more than one word
- model: This is a String and is the model of the Plane, may be more than one word
- flying: This is a boolean variable, the value false indicates that the Plane is not flying (airborne) which means that the Plane is NOT on a mission. The value true indicates that the Plane is airborne and so is on a mission
- tail number: This is a String (text) and may consist of more than one word. The tail number is the unique identifier for a Plane
- crew: This is now an array of Crew class object references for the Crew objects associated with this Plane (when the Crew objects are added and instantiated)
- max crew: This is the maximum number of Crew that can be in this Plane. This is set by the user when the Plane object is created
- current crew: This is the current number of Crew actually in the Plane, starts at 0 when the Plane is instantiated
- mission time: This is the number of “turns” (through the main menu) the Plane has been on a mission. The mission time is reset to 0 when the Plane ends a mission.
The Plane class still requires at least 2 overloaded constructors.
One constructor takes all the parameters for a Plane object. This constructor
would be used when reading from a text file and we have a Plane record,
regardless of any Crew attached to that Plane.
A second, overloaded, constructor for keyboard input for a Plane. This
constructor would take just the name, model, tail number and max crew
attributes as parameters. Since we have just created the Plane object the
value of flying must be false (it cannot be on a mission) and there are no
Crew associated with this Plane yet, so the current crew is 0 and the mission
time must be 0.
The Plane class will require accessor methods as you deem appropriate.
One of those accessor methods may be a method to return a copy of the crew
array. If you decide to write this accessor, remember to take privacy leaks
into consideration.
The Plane class also requires a toString method that returns a String with
information about the state of that Plane object. The format of the String is
shown in the example output on page 16. As with the Crew screen output, this
time you are free to format the output anyway you want.
The Plane class requires at least one method to add a Crew to the Plane. This
method takes all of the relevant parameters for instantiating a Crew object
from the keyboard, that is, name role and id. Using the information in these
parameters, instantiate a Crew object which is stored in the crew array,
provided that there is a free space. As before, the program first needs to
check that the crew id entered by the user is indeed unique.
A second, overloaded method to add a Crew should be considered. This method
will take all the information for adding a Crew from the text file. The
difference is that, from the file, the number of missions is included, whereas
when reading the information from the keyboard, the number of missions is, of
course, 0.
There will be a number of mutator methods that you will find you need to
write, amongst them, a method to increment the amount of time that a Plane has
spent on a mission and a method to increment the number of missions for all
the Crew, in that Plane, every time the Plane starts a new mission.
As with the Crew class, you may want to consider a method writing a method
that writes the details of the Plane object to a text file. Remember to write
the actual number of Crew figure into the text file. This is required so that
when we use that output file as an input file, the program knows how many Crew
records it needs to read before the next Plane records starts.
As discussed above, the overall experience level of all the Crew, in a Plane,
must be greater than or equal to the experience level of the mission as
entered by the user. You may want to consider writing a method that takes the
user mission experience level as a parameter and returns true or false
whether, or not, the overall experience level of the Crew is sufficient for
the Plane to be assigned the mission (provided. first, that all the other
conditions are met)
loading the array of Planes from a text file
The program now starts by asking the user for the name of a text file, see
page 10 for the format. This file will contain a number of Plane/Crew records.
The filename, will as always, exist and this time will not be empty.
The contents of this file is used to populate the array of Planes. Once the
contents of this file have been read into the array of Planes, the file is
closed. The only time that a file is opened again is if the user selects the
save option.
Where the Plane has a Crew (meaning any number of Crew greater than 0) then
you want to call your overloaded addCrew method from the Plane class, the one
that takes all the Crew attributes. Use this method to add Crew to the correct
Plane.
This results in a change to the menu that is presented to the user. The menu
is first presented to the user after the contents of the text file has been
read into the array of Planes.
The program must work with any file name entered by the user (of the correct
format), that is, the file name must not be hard coded.
An example of the information, in the text file, on each Plane consists of 7
lines. There may be any number of 4 line Crew records after the Plane
information. The Plane and Crew information is collectively known as a record.
In the example above, the first 7 lines are required to instantiate the Plane
object reference in the Plane object, the seventh line indicates the number of
Crew objects to read, followed by this number of groups of 4 lines, which are
required to instantiate the Crew object references in that Plane object.
The file may contain any number of records.
Given that this method is called before the user gets to see the main menu,
there is now no need to worry about unique Crew id’s and Plane tail number’s
conflicting with anything that the user may enter.