Question: Need done by 5 p.m thumbs up! Assignment8.java(More code need to be added) Project.java(given by the instructor, it needs to be modified for this assignment)
Need done by 5 p.m thumbs up!
Assignment8.java(More code need to be added) Project.java(given by the instructor, it needs to be modified for this assignment) Manager.java(given by the instructor, it needs to be modified for this assignment) ProjectNumberComparator.java ManagerComparator.java Sorts.java ProjectManagement.java
Assignment8.java code:
import java.io.*; public class Assignment8 { public static void main (String[] args) { char input1; String projTitle, projNumStr, projLocation, firstName, lastName, deptNumStr; int projNumber, deptNumber; boolean operation = false; int operation2 = 0; String line; String filename; // create a ProjectManagement object. This is used throughout this class. ProjectManagement manage1 = null; try { // print out the menu printMenu(); // create a BufferedReader object to read input from a keyboard InputStreamReader isr = new InputStreamReader (System.in); BufferedReader stdin = new BufferedReader (isr); System.out.print("Please enter a maximum number of projects "); String maxStr = stdin.readLine().trim(); //read in the max number as a string int max = Integer.parseInt(maxStr); manage1 = new ProjectManagement(max); do { System.out.print("What action would you like to perform? "); line = stdin.readLine().trim(); //read a line input1 = line.charAt(0); input1 = Character.toUpperCase(input1); if (line.length() == 1) //check if a user entered only one character { switch (input1) { case 'A': //Add Project System.out.print("Please enter a project to add: "); System.out.print("Please enter its title to add: "); projTitle = stdin.readLine().trim(); System.out.print("Please enter its project number to add: "); projNumStr = stdin.readLine().trim(); projNumber = Integer.parseInt(projNumStr); System.out.print("Please enter its project location to add: "); projLocation = stdin.readLine().trim(); System.out.print("Please enter its manager's first name to add: "); firstName = stdin.readLine().trim(); System.out.print("Please enter its manager's last name to add: "); lastName = stdin.readLine().trim(); System.out.print("Please enter its manager's department number to add: "); deptNumStr = stdin.readLine().trim(); deptNumber = Integer.parseInt(deptNumStr); operation = manage1.addProject(projTitle, projNumber, projLocation, firstName, lastName, deptNumber); if (operation == true) System.out.print("project added "); else System.out.print("project not added "); break; case 'C': //Create a new project management System.out.print("Please enter a new maximum number of projects: "); maxStr = stdin.readLine().trim(); //read in the max number as a string max = Integer.parseInt(maxStr); manage1 = new ProjectManagement(max); break; case 'D': //Search by project number System.out.print("Please enter project number to search: "); projNumStr = stdin.readLine().trim(); projNumber = Integer.parseInt(projNumStr); operation2=manage1.projectNumberExists(projNumber); if (operation2 > -1) System.out.print("project number " + projNumber + " found "); else System.out.print("project number " + projNumber + " not found "); break; case 'E': //Search by manager System.out.print("Please enter the first name of a manager to search: "); firstName = stdin.readLine().trim(); System.out.print("Please enter the last name of a manager to search: "); lastName = stdin.readLine().trim(); System.out.print("Please enter the department number of a manager to search: "); deptNumStr = stdin.readLine().trim(); deptNumber = Integer.parseInt(deptNumStr); operation2=manage1.managerExists(firstName, lastName, deptNumber); if (operation2 > -1) { System.out.print("project manager " + lastName + "," + firstName + " of the department " + deptNumber + " found "); } else { System.out.print("project manager " + lastName + "," + firstName + " of the department " + deptNumber + " not found "); } break; case 'L': //List projects System.out.print(manage1.listProjects()); break; case 'O': // Sort by project numbers manage1.sortByProjectNumber(); System.out.print("sorted by project numbers "); break; case 'P': // Sort by manager information manage1.sortByManager(); System.out.print("sorted by managers "); break; case 'Q': //Quit break; case 'R': //Remove by project number System.out.print("Please enter project number to remove: "); projNumStr = stdin.readLine().trim(); projNumber = Integer.parseInt(projNumStr); operation=manage1.removeProjectNumber(projNumber); if (operation == true) System.out.print("project number " + projNumber + " removed "); else System.out.print("project number " + projNumber + " not found "); break; case 'T': //Close ProjectManagement manage1.closeProjectManagement(); System.out.print("project management system closed "); break; case 'U': //Write Text to a File System.out.print("Please enter a file name to write: "); filename = stdin.readLine().trim(); /************************************************************************************ *** ADD your code to write a text (string) to the specified file. Catch exceptions. ************************************************************************************/ break; case 'V': //Read Text from a File System.out.print("Please enter a file name to read: "); filename = stdin.readLine().trim(); /************************************************************************************ *** ADD your code to read a text (string) from the specified file. Catch exceptions. ************************************************************************************/ break; case 'W': //Serialize ProjectManagement to a File System.out.print("Please enter a file name to write: "); filename = stdin.readLine().trim(); /************************************************************************************ *** ADD your code to write the project management oject to the specified file. Catch exceptions. ************************************************************************************/ break; case 'X': //Deserialize ProjectManagement from a File System.out.print("Please enter a file name to read: "); filename = stdin.readLine().trim(); /************************************************************************************ *** ADD your code to read a project management object from the specified file. Catch exception. ***********************************************************************************/ break; case '?': //Display Menu printMenu(); break; default: System.out.print("Unknown action "); break; } } else { System.out.print("Unknown action "); } } while (input1 != 'Q' || line.length() != 1); } catch (IOException exception) { System.out.print("IO Exception "); } } /** The method printMenu displays the menu to a user **/ public static void printMenu() { System.out.print("Choice\t\tAction " + "------\t\t------ " + "A\t\tAdd Project " + "C\t\tCreate ProjectManagement " + "D\t\tSearch by Project Number " + "E\t\tSearch by Manager " + "L\t\tList Projects " + "O\t\tSort by Project Number " + "P\t\tSort by Manager " + "Q\t\tQuit " + "R\t\tRemove by Project Number " + "T\t\tClose ProjectManagement " + "U\t\tWrite Text to File " + "V\t\tRead Text from File " + "W\t\tSerialize ProjectManagement to File " + "X\t\tDeserialize ProjectManagement from File " + "?\t\tDisplay Help "); } }
Project.java code:
public class Project { private String projTitle; private int projNumber; private String projLocation; private Manager projManager; //Constructor to initialize all member variables public Project() { projTitle = "?"; projNumber = 0; projLocation = "?"; projManager = new Manager(); } //Accessor methods public String getProjTitle() { return projTitle; } public int getProjNumber() { return projNumber; } public String getProjLocation() { return projLocation; } public Manager getProjManager() { return projManager; } //Mutator methods public void setProjTitle(String aTitle) { projTitle = aTitle; } public void setProjNumber(int aNumber) { projNumber = aNumber; } public void setProjLocation(String aLocation) { projLocation = aLocation; } public void setProjManager(String first, String last, int deptNum) { projManager.setFirstName(first); projManager.setLastName(last); projManager.setDeptNum(deptNum); } //toString() method returns a string containg its title, number, location and manager public String toString() { String result = " Project Title:\t\t" + projTitle + " Project Number:\t\t" + projNumber + " Project Location:\t" + projLocation + " Project Manager:\t" + projManager.toString() + " "; return result; } }
Manager.java code:
public class Manager { private String firstName; private String lastName; private int deptNum; /************************************************************************ Constructor method to initialize intance variables. ************************************************************************/ public Manager() { firstName = new String("?"); lastName= new String("?"); deptNum = 0; } /************************************************************************ Accessor methods ************************************************************************/ public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public int getDeptNum() { return deptNum; } /************************************************************************ Modifier methods ************************************************************************/ public void setFirstName(String someFirstName) { firstName = someFirstName; } public void setLastName(String someLastName) { lastName = someLastName; } public void setDeptNum(int someDeptNum) { deptNum = someDeptNum; } /***************************************************************************** This method return a string containing the attribute information of a manager *****************************************************************************/ public String toString() { String result; result = firstName + " " + lastName + ", Dept Num:" + deptNum; return result; } }
Requirements to get full credits in Documentation
The assignment number, your name, StudentID, Lecture number/time, and a class description need to be included at the top of each file/class.
A description of each method is also needed.
Some additional comments inside of methods(especially for the "main" method) to explain code that are hard to follow should be written.
You can look at the Java programs in the text book to see how comments are added to programs.
Skills to be Applied
In addition to what has been covered in previous assignments, the use of the following items, discussed in class, will probably be needed:
Sorting Searching Aggregate Objects Interfaces Serialization File read/write
Program Description
Class Diagram:
Manager
The Manager class implements the "Serializable" interface so that its object can be stored. (The Serializable interface is defined in the "java.io" package.) It needs to define the following method:
public void copy(Manager other)
It needs to copy every member variable value from "other" parameter to their corresponding variable of the object itself using "this" reference. For instance, if we have two objects of Manager, manager1 and manager2, then, executing the following statement: manager1.copy(manager2) should resulting to have the object manager1 to have the same firstName, lastName, and deptNum value as the ones for manager2, but manager1 and manager2 should not become aliases.
Project
The Project class implements the "Serializable" interface so that its object can be stored. (The Serializable interface is defined in the "java.io" package.) It needs to define the following method:
public void copy(Project other)
It needs to copy every member variable value from "other" parameter to their corresponding variable of the object itself using "this" reference.
ProjectNumberComparator
The ProjectNumberComparator class implements the "Comparator" interface (The Comparator interface is in "java.util" package.). It needs to define the following method that was an inherited abstract method from Comparator interface:
public int compare(Object first, Object second)
(Note that you can also define: public int compare(Project first, Project second) instead by making the class implements Comparator
If the first argument object has its projNumber less than that of the second argument, an int less than zero is returned. If the first argument object has its projNumber larger than that of the second argument, an int greater than zero is returned. If their projNumbers are same, then 0 should be returned.
ManagerComparator
The ManagerComparator class implements the "Comparator" interface (The Comparator interface is in "java.util" package.). It needs to define the following method that was an inherited abstract method from Comparator interface:
public int compare(Object first, Object second)
(Note that you can also define: public int compare(Project first, Project second) instead by making the class implements Comparator
If the first argument object has its deptNum less than that of the second argument, an int less than zero is returned. If the first argument object has its deptNum larger than that of the second argument, an int greater than zero is returned. If their deptNums are same, then their firstNames should be compared. (if the first argument object has its firstName smaller (in alphabetical order -- compareTo method of the String class), then it should return a negative integer. If the first argument object has its firstName larger, then it should return a positive integer. If their deptNums and firstNames are same, then their lastNames in a similar way. If their deptNums, firstNames, and lastNames are same, then 0 should be returned.
Sorts
The Sorts class is a utility class that will be used to sort a list of Project objects. Sorting algorithms are described in the algorithm note posted under Notes section of the course web site. These algorithms sort numbers stored in an array. It is your job to modify it to sort an array of objects. The Sorts class object will never be instantiated. It must have the following methods:
public static void sort(Project[] projectList, int size, Comparator
Your sort method utilizes the compare method of the parameter Comparator object to sort. You can use one of Selection sort, Insertion Sort, MergeSort, or QuickSort. The parameter size specifies how many first elements should be sorted. Note that not all elements in the array should be sorted, since some of them will be null pointers.
ProjectManagement
The ProjectManagement class has a list of Project objects that can be organized at the project management system. The project management system will be a fully encapsulated object. The ProjectManagement class implements the Serializable interface. It has the following attributes:
| Attribute name | Attribute type | Description |
|---|---|---|
| projectList | an array of Project objects | A list of Project objects in the project management system |
| currentProjectsCount | int | the number of Project objects created and stored in the projectList |
| maxSize | int | the maximum number of Project objects that can be stored in the projectList array. It is also the size of the projectList array. |
The following public methods should be provided to interact with the project management system:
| Method | Description |
|---|---|
| ProjectManagement(int maximumsize) | A Constructor of the ProjectManagement class. Using the parameter value, it should initialize the member variable maxSize. Then it should instantiate an array of Project objects using the maxSize, and initialize each slot of the array to null for every index. It should also initialize the member variable currentProjectsCount to 0. |
| int projectNumberExists(int projectNumber) | Search for a Project object by projectNumber, and return the index of the object if found. Return -1 if not found. The parameter is projNumber of a Project object. |
| int managerExists(String firstName, String lastName, int deptNum) | Search and for Project objects in the project list that have the same first name, last name, and department number as the parameter values and return the index of such object if found. Return -1 if not found. |
| boolean addProject(String projTitle, int projNumber, String projLocation, String firstName, String lastName, int deptNum) | Add a Project object to the project list and return true if such object was added successfully. Return false if an object with the same project number already exists or currentProjectCount is already same as maxSize, i.e., the array is full (the new object is not added). |
| boolean removeProjectNumber(int projectNumber) | Remove a Project object from the project list. Return true if the object was removed successfully. Return false if the object with the given project number does not exist. |
| void sortByProjectNumber() | Sort the list of Project objects by project numbers. This method calls the sort method defined in the Sorts class, using an object of ProjectNumberComparator class as its second parameter. You should also make a use of copy() method of Project class. |
| void sortByManager() | Sort the list of Project objects by their manager information including its first name, last name, and department number. This method calls the sort method defined in the Sorts class, using an object of ManagerComparator class as its second parameter. You should also make a use of copy() method of Project class. |
| String listProjects() | List all Project objects in the project list. The returned string is the concatenation of each Project object information in the list for the number of currentProjectCount (do not use the size of the array here) Hint: you can utilize Project's toString method to help you complete this method. If there is no object in the list, This method should return the string containing " no project ". |
| void closeProjectManagement() | Closes the project management system by making the list empty. This can be done by making every slot of the projectList array to null, and also by setting currentProjectCount to be 0. |
No input/output should occur in the project management system. User interaction should be handled only by the driver class.
You may add other methods to the class in order to make your life easier.
Assignment8
All input and output should be handled in Assignment8 class. The main method should start by displaying this updated menu in this exact format:
Choice\t\tAction ------\t\t------ A\t\tAdd Project C\t\tCreate ProjectManagement D\t\tSearch by Project Number E\t\tSearch by Manager L\t\tList Projects O\t\tSort by Project Number P\t\tSort by Manager Q\t\tQuit R\t\tRemove by Project Number T\t\tClose ProjectManagement U\t\tWrite Text to File V\t\tRead Text from File W\t\tSerialize ProjectManagement to File X\t\tDeserialize ProjectManagement from File ?\t\tDisplay Help
Next, the following prompt should be displayed:
What action would you like to perform?
Read in the user input and execute the appropriate command. After the execution of each command, redisplay the prompt. Commands should be accepted in both lowercase and uppercase. The following commands are modified or new.
Add Project
This part is already implemented in Assignment8.java file. Please see the case A in the main of Assignment8.java.
Create ProjectManagement
This part is already implemented in Assignment8.java file. Please see the case C in the main of Assignment8.java.
Search by Project Number
This part is already implemented in Assignment8.java file. Please see the case D in the main of Assignment8.java.
Search by Manager
This part is already implemented in Assignment8.java file. Please see the case E in the main of Assignment8.java.
Sort by Project Number
This part is already implemented in Assignment8.java file. Please see the case O in the main of Assignment8.java.
Remove by Project Number
This part is already implemented in Assignment8.java file. Please see the case R in the main of Assignment8.java.
List Projects
Each Project object information in the project list should be displayed using the toString method provided in the Project class. (and use listProjects( ) method in the ProjectManagement class.) This part is already implemented in Assignment8.java file. Please see the case L in the main of Assignment8.java.
Close ProjectManagement
Delete all Project objects. Then, display the following:
project management system closed This part is already implemented in Assignment8.java file. Please see the case R in the main of Assignment8.javaT
Write Text to File
Your program should display the following prompt:
Please enter a file name to write:
Read in the filename and create an appropriate object to get ready to read from the file. Then it should display the following prompts:
Please enter a string to write in the file:
Read in the string that a user types, say "input", then attach " " at the end of the string, and write it to the file. (i.e. input+" " string will be written in the file.)
If the operation is successful, display the following:
FILENAME was written
Replace FILENAME with the actual name of the file.
Use try and catch statements to catch IOException. The file should be closed in a finally statement.
Read Text from File
Your program should display the following prompt:
Please enter a file name to read:
Read in the file name create appropriate objects to get ready to read from the file. If the operation is successful, display the following (replace FILENAME with the actual name of the file):
FILENAME was read
Then read only the first line in the file, and display:
The first line of the file is:
CONTENT
where CONTENT should be replaced by the actual first line in the file.
Your program should catch the exceptions if there are. (Use try and catch statement to catch, FileNotFoundException, and the rest of IOException.)
If the file name cannot be found, display
FILENAME was not found
where FILENAME is replaced by the actual file name.
Serialize ProjectManagement to File
Your program should display the following prompt:
Please enter a file name to write:
Read in the filename and write the serialized ProjectManagement object (the variable manage1) out to it. Note that any objects to be stored must implement Serializable interface. The Serializable interface is defined in java.io.* package. If the operation is successful, display the following:
FILENAME was written
Replace FILENAME with the actual name of the file.
Use try and catch statements to catch NotSerializableExeption and IOException.
Deserialize ProjectManagement from File
Your program should display the following prompt:
Please enter a file name to read:
Read in the file name and attempt to load the ProjectManagement object from that file. Note that there is only one ProjectManagement object (the variable manage1) in the Assignment8 class, and the first object read from the file should be assigned to the ProjectManagement object. If the operation is successful, display the following (replace FILENAME with the actual name of the file):
FILENAME was read
Your program should catch the exceptions if there are.
(Use try and catch statement to catch ClassNotFoundException, FileNotFoundException, and the rest of IOException.)
See the output files for exception handling.
Test cases:
Input
The following files are the test cases that will be used as input for your program (Right-click and use "Save As"):
Test Case 1:
8 A Grand Road Improvement Project 3 Tucson Karl Thompson 9 A Helping Children Project 11 Phoenix Kate Middleton 15 A Farming Project 7 Phoenix Bob Farmer 8 L D 9 D 16 D 7 E Kate Middleton 15 E Tom Farmer 8 L R 15 L R 11 L W project1.dat T L X task1.dat X project1.dat L Q
Output
The following files are the expected outputs of the corresponding input files from the previous section (Right-click and use "Save As"):
Test Case 1:
Choice Action ------ ------ A Add Project C Create ProjectManagement D Search by Project Number E Search by Manager L List Projects O Sort by Project Number P Sort by Manager Q Quit R Remove by Project Number T Close ProjectManagement U Write Text to File V Read Text from File W Serialize ProjectManagement to File X Deserialize ProjectManagement from File ? Display Help Please enter a maximum number of projects What action would you like to perform? Please enter a project to add: Please enter its title to add: Please enter its project number to add: Please enter its project location to add: Please enter its manager's first name to add: Please enter its manager's last name to add: Please enter its manager's department number to add: project added What action would you like to perform? Please enter a project to add: Please enter its title to add: Please enter its project number to add: Please enter its project location to add: Please enter its manager's first name to add: Please enter its manager's last name to add: Please enter its manager's department number to add: project added What action would you like to perform? Please enter a project to add: Please enter its title to add: Please enter its project number to add: Please enter its project location to add: Please enter its manager's first name to add: Please enter its manager's last name to add: Please enter its manager's department number to add: project added What action would you like to perform? Project Title: Grand Road Improvement Project Project Number: 3 Project Location: Tucson Project Manager: Karl Thompson, Dept Num:9 Project Title: Helping Children Project Project Number: 11 Project Location: Phoenix Project Manager: Kate Middleton, Dept Num:15 Project Title: Farming Project Project Number: 7 Project Location: Phoenix Project Manager: Bob Farmer, Dept Num:8 What action would you like to perform? Please enter project number to search: project number 9 not found What action would you like to perform? Please enter project number to search: project number 16 not found What action would you like to perform? Please enter project number to search: project number 7 found What action would you like to perform? Please enter the first name of a manager to search: Please enter the last name of a manager to search: Please enter the department number of a manager to search: project manager Middleton,Kate of the department 15 found What action would you like to perform? Please enter the first name of a manager to search: Please enter the last name of a manager to search: Please enter the department number of a manager to search: project manager Farmer,Tom of the department 8 not found What action would you like to perform? Project Title: Grand Road Improvement Project Project Number: 3 Project Location: Tucson Project Manager: Karl Thompson, Dept Num:9 Project Title: Helping Children Project Project Number: 11 Project Location: Phoenix Project Manager: Kate Middleton, Dept Num:15 Project Title: Farming Project Project Number: 7 Project Location: Phoenix Project Manager: Bob Farmer, Dept Num:8 What action would you like to perform? Please enter project number to remove: project number 15 not found What action would you like to perform? Project Title: Grand Road Improvement Project Project Number: 3 Project Location: Tucson Project Manager: Karl Thompson, Dept Num:9 Project Title: Helping Children Project Project Number: 11 Project Location: Phoenix Project Manager: Kate Middleton, Dept Num:15 Project Title: Farming Project Project Number: 7 Project Location: Phoenix Project Manager: Bob Farmer, Dept Num:8 What action would you like to perform? Please enter project number to remove: project number 11 removed What action would you like to perform? Project Title: Grand Road Improvement Project Project Number: 3 Project Location: Tucson Project Manager: Karl Thompson, Dept Num:9 Project Title: Farming Project Project Number: 7 Project Location: Phoenix Project Manager: Bob Farmer, Dept Num:8 What action would you like to perform? Please enter a file name to write: project1.dat was written What action would you like to perform? project management system closed What action would you like to perform? no project What action would you like to perform? Please enter a file name to read: task1.dat was not found What action would you like to perform? Please enter a file name to read: project1.dat was read What action would you like to perform? Project Title: Grand Road Improvement Project Project Number: 3 Project Location: Tucson Project Manager: Karl Thompson, Dept Num:9 Project Title: Farming Project Project Number: 7 Project Location: Phoenix Project Manager: Bob Farmer, Dept Num:8 What action would you like to perform?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
