Write an interactive program in java to aid in monitoring


Assessment Objectives:

- to design programs that conform to given specifications
- to practise combining multiple classes and methods into a whole program
- to practice using arrays of objects
- to implement programs in java

Problem Background:

Humankind has expanded into space. Humans live and work in cities in space. To maintain and construct these cities, is the responsibility of a dedicated workforce.

The people who carry out this work are known as Crew.

A pilot program was started in which each Crew had their own spaceship, known as a Ship. This prototype was explored in AssignC and proved to be very successful. Now the program is to be expanded.

Now the Ship's carry a maximum of 10 Crew. Tasks are now assigned to Ship's, not to individual Crew.

The Crew is still trained to undertake any sort of Task and their Ship's can be configured in any way, regardless of the stated purpose of the Ship. As before, some Tasks are more difficult and dangerous than others. As such, some Tasks can only be performed by highly experienced Crew. When considering assigning a Task, the Task is still rated as requiring a certain minimum level of experience points. Now, however, as the Task is assigned to a Ship, it is the total number of experience points of all the Crew in the Ship that determine whether or not the Ship can be assigned the Task. Also, as before, the Ship has to have the status of "available" before even considering whether the Ship can be assigned the proposed Task, this has not changed.

When a Crew joins the space city, they start with 0, or more, experience points. Each time a Ship successfully completes a Task, all of the Crew in the Ship gain 10 experience points. Some Tasks are especially challenging and bonus points may still be awarded, now there is no maximum, although the bonus points must be greater than 0. These bonus points are shared equally amongst all the Crew in the Ship, regardless of the classification of the individual Crew. The number of experience points still determines the classification of the Crew, but no longer determines the Tasks that they may be assigned as it is the Ship that is assigned a Task.

The four levels of experience remain the same - this is the classification of the Crew

• Trainee 0 to 20 experience points (inclusive)
• Trained 21 - 40 experience points (inclusive)
• Experienced 41 - 100 experience points (inclusive)
• Specialist greater than 100 experience points

The Ships that the Crew use are still sometimes damaged during a Task. (You may assume that a Ship is never destroyed). A Ship has a status, which can be one of:

• available
• damaged
• being repaired
• on task

This is the same as in AssignC, with all the same conditions.

Now that Ship's are big enough to have a number of Crew, there is no need for a WorkUnit. The WorkUnit proved to be successful in the prototype but has now been dropped completely from this program.

As stated above, to be assigned a Task, the Ship must be available and total number of experience points of all the Crew must be greater than or equal to the experience points required of the Task. If the Ship is available, this automatically means that all the Crew are available.

Tasks are assigned to the Ship and when the Ship finishes the Task the Ship's crew notify the central control. Tasks do not have a fixed time length.

Program Requirements

You have been asked to write an interactive program, in Java, to aid in monitoring and maintaining all aspects of Tasks, Crew and Ship's, that is, maintaining the Space City.

This prototype program (AssignC) proved very successful and is now expanded. The space city now has a maximum of 100 Ship's. Each Ship has a maximum of 10 Crew.

To aid in the rapid development of this program, 3 Java files and 1 sample input file are provided for you:

Crew.java, Ship.java, SpaceCity.java and a sample input file city01.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.

Crew.java (This remains basically the same as in AssignC) All Crew objects have the following object attributes:

The Crew class requires the following functionality:

A constructor that takes name, id and experience points as parameters. This is the constructor that is called when a new Crew is added from the keyboard. Recall that the classification is set by the number of experience points and the status must be true as the Crew has just be instantiated and so cannot have been assigned a Task.

The classification must never be set by the user.

An overloaded constructor that takes all five attributes listed above as parameters. This constructor is called when constructing a Crew object from data read from a text file. The text file (see the format on page 10) has a record of previously entered Ship objects (this includes information about the Crew). There are no calculations, just assign the data from the file to the appropriate attribute.

You may consider writing a further overloaded constructor for the Crew class. This is the "copy constructor" as discussed in lectures relating to preventing privacy leaks. The "copy constructor" takes as its single parameter, an existing Crew object reference and copies all the values from this existing Crew object to the new Crew object being instantiated.

The Crew class requires a private method to set the classification, based on the experience points. (Hint: this will need to called from the keyboard constructor, but not from the file read constructor)

The Crew class also requires a method to change the status attribute. This simply toggles the status attribute when called. If the status attribute is true, calling this method sets it to false, if the value is false when this method is called, then the value is set to true.
As such, this method does not take a parameter.

The Crew class also requires a method to add experience points. Calling this method adds 10 experience points. Hint: this may change the classification of the Crew, you need to check.

The Crew class also requires an overloaded method to add experience points, this method takes one parameter, the number of extra points to add. There is now no maximum to the number of bonus experience points that may be added, although the number must be greater than 0. Otherwise the extra experience points are added to the total experience points. Hint: this may change the classification of the Crew, this method needs to check.

There is no longer any need to have a method to check if an individual Crew is available to undertake a Task. This is now determined in the Ship class, so the available method in the Crew class may be removed. If the Ship that holds this Crew is working on a task, then the Crew is to be shown as unavailable, any other status for the Ship means that the Crew should be shown as available.

The Crew class still requires a toString method which returns a String with information about the Crew object, see pages 16 -17 for the format of the String to be returned.

Finally, you might consider adding a method to the Crew class that takes a PrintWriter object reference (or any other suitable output text file class) as a parameter. Both the Crew and Ship will need to be written back to a text file in this assignment. How to write to text files is explained at the end of this document.

The Ship class still requires a constructor that takes as parameters just the id and the purpose. This constructor is called when instantiating an object with information from the keyboard. The status must be available as the Ship object has just been instantiated so it cannot have been assigned a Task. When instantiating a Ship object from the keyboard, no Crew information is entered, although you need to consider where you are going to instantiate the crew array.

The Ship class also still requires an overloaded constructor that takes as parameters, all three attributes. This constructor is called when instantiating an object with information read from a text file. The text file will hold the values of a previously constructed Ship object and we are now restoring that object. Even though all the information for all the Crew in this Ship will be available in the text file, immediately after the Ship information, the restriction about not creating Crew or Ship objects in the main driver program (SpaceCity.java) remains. This means that this constructor should stick to just the three parameters as in AssignC. Again, if you start by reading from a text file, then you need to consider where you are going to instantiate the crew array.

(Hint: you can do this instantiation outside of any constructors, but you must instantiate the crew array somewhere in the Ship class)

The Ship class will require accessor methods as you deem appropriate.

The Ship class also requires a toString method that returns a String with information about the state of that Ship object. The format of the String is shown in the example output on page 21. Note that the toString method will now also have to return all the Crew associated with this Ship.

The Ship class still requires a mutator method to change the value of the status attribute. This method takes one parameter, a String with the new status.

• If the value of the new status parameter is on task, then the status attribute of the Ship is set to this new value, if and only if, the current value of the status attribute is available.

• If the value of the new status parameter is being repaired, then the status attribute of the Ship is set to this new value, if and only if, the current value is damaged.

• If the value of the new status parameter is damaged, then the status attribute of the Ship is set to this value, if and only if, the current value of the status attribute is on task

• If the value of the new status parameter is available, then the status attribute of the Ship is set to this value, if and only if, the current value of the status attribute is on task or being repaired. (available is when the status was on task and End Task is called with the Ship not being damaged or when the Ship was not on a Task and finishes being repaired.)

This method now requires some additions. When the Ship status is on task, all of the Crew in this Ship must have their status set to unavailable (false). When the Ship has any other status all the Crew must have their status set to available (true).

Any other combination of new status parameter value and current Ship status attribute value is an error, has no effect on the Ship status attribute and an appropriate message should be displayed to the screen.

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

1. Display Menu

This menu choice displays a sub menu like this:

SpaceCity Display Menu
1. Display All
2. Display Ships only
3. Display All Crew only
4. Display Single Ship
5. Display Single Crew
6. Return to main menu Enter display choice >>

If the user selects choice 1, then the contents of the ships array (including Crew information) is displayed to the screen.

If the user selects choice 2, then only the information about all the Ships (no Crew information) in the ships array is displayed to the screen.

If the user selects choice 3, then only the information about all the Crew (no Ship information) in the ships array is displayed to the screen.

If the user selects choice 4, then the user is prompted (asked) for the id of a Ship. If there is a matching Ship in the array, then all the information about this Ship, including any Crew in this Ship, is displayed to the screen.

If the user selects choice 5, then the user is prompted (asked) for the id of a Crew. If there is a matching Crew in one of the Ships in the array, then all the information about this Crew, no Ship information, is displayed to the screen.

As a final note, consider that when the user enters Crew id and Ship id from the keyboard, the program must check that these id's are not already in use. If they are then the user is informed with an appropriate message to the screen and the user can be asked to enter another id or the method can return to the main menu.

Id's in the input file are guaranteed to be unique and you may assume that the user will not enter an id (of either Crew or Ship) from the keyboard that is in the input text file. Your program does NOT need to check for this, but it needs to check id's already in the array.

2. Add Task

This menu choice first checks that there is at least one Ship in the ships 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 Ship object reference then the user is prompted (asked) for the id of a Ship. The program must then find the Ship that contains that id in the ships array. If the Ship with that id is found and the Ship has the status of available, then the user is prompted (asked) for the number of experience points required to carry out the potential Task. If the total number of experience points of all the Crew in this Ship is greater than or equal to the points needed for the Task, then the Ship (and its Crew) is assigned the Task, with the appropriate change in status of both the Ship and all its Crew.

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

3. End Task

This menu choice prompts (asks) the user for the id of a Ship. As with Add Task appropriate messages should be displayed to the screen if the Ship with that id is not found or if the Ship with that id is found but is not already on a Task. (You can't end a Task unless the Ship is currently on task)

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

If the id exists and the Ship is on task, then the user is prompted (asked) if the Ship has been damaged, if the answer is yes, then the Ship status is changed to damaged, if not, the Ship status is changed to available.

Regardless, the Crew status of all Crew in this Ship is changed from false to true.

Also, 10 experience points are added to the total experience points of each one of the Crew
in this Ship, this may result is a change of classification.

The user is also prompted (asked) if bonus experience points should be awarded. If the answer is yes then the user is asked to enter the points and these are shared equally between all the Crew in that Ship and this share is added to the total experience points of each Crew in this Ship, again, this may result in a change of classification. The program needs to check for this as described above in the Crew information. Reminder, there is now no upper limit to the

bonus awarded and any "left over" bonus points are discarded (Although you are free to write code to solve this, as a bonus marks task)

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

4. Change Ship Status

This menu choice prompts (asks) the user for a Ship id (after doing the same checks and appropriate actions as in Add Task and End Task menu choices, if the Ship id does not exist).

If the Ship id is found, the user is prompted (asked) for the new status of the Ship. This new status must follow the rules as described above in the Ship class. For instance, a Ship that has current status of available cannot be given a new status of being repaired.

If the Ship status is currently on task, then the user must be advised that changing the status will end the task. The user is given the choice of proceeding or cancelling the action. If the user chooses to proceed then all the actions for End Task must be called.

If the current status of the Ship is damaged, for example, then the Ship cannot be on task
and the only change that can be made is to make the status being repaired.

Reminder, changing the status of the Ship may change the status of the Crew associated with this Ship, this is detailed above in the Ship class. Some changes to the Ship status will not change the Crew status.

5. Add Ship

This menu choice first checks that there is space in the ships array. If there is no space then the user is informed with an appropriate message and no further information is asked from the user.

If there is space in the array then the user is asked for the id of the Ship to add. If this id is already in use, then, once again, the user is informed of this fact. (Hint: good use for your search method. ? ) If this occurs, your program can return to the main menu, or ask the user if they would like to enter another id.

If the program gets to this point where the user enters a valid Ship id, then the user is prompted for the purpose of the Ship and that Ship is added to the array (remembering to increment the counter). No Crew information is requested in this menu choice, just Ship information.

6. Add Crew

This menu choice first checks that there is at least one Ship in the array. If there are no Ships, then the user is informed with an appropriate message and the program returns to the main menu.

If there is at least one Ship in the array then the user is prompted (asked) for the id of a Ship. If there is no Ship with this id in the array, again, the user is informed with an appropriate message and the program can either return to the main menu or ask the user if they want to enter another Ship id.

If the Ship with the requested id is found in the array (search method again), then the user is prompted (asked) for the id of the Crew to be added. This requested id must be checked that it is not already in use.

This is a somewhat complex process as the program needs to look at all the Crew in all the Ships in the array. One way of doing this is to use your getCrew method from Ship class to return a copy of the Crew array for a Ship into this main program, then go through the copy of that Crew array. You do this one Ship at a time until you have searched all the Ships in the array, or you find that the requested Crew id already exists (it doesn't matter in which Ship the Crew id is found)

If your program gets to this point, then we know that the requested Crew id is unique and the user is prompted (asked) for the rest of the Crew information (keyboard entry, see Crew class, reminder that the user never enters the classification of the Crew). That information is passed to the addCrew method of the appropriate Ship object and the Crew is added to the crew array of that Ship.

Something to consider, at what point do you check that there is actually space in the crew array of the Ship object?

7. Save

This menu choice prompts (asks) the user for the name of an output text file. This can be the same name as the input file or a different file name. Regardless, the output file name must not be hard coded. The contents of the ships array is then written back to this output file. This does not close the program.

8. Exit

This menu choice closes the program. It is up to you whether or not you ask the user if they want to save before exiting. This menu option must not, however, automatically call save

Request for Solution File

Ask an Expert for Answer!!
JAVA Programming: Write an interactive program in java to aid in monitoring
Reference No:- TGS01266398

Expected delivery within 24 Hours