University Personnel Database
Note: Projects are to be completed by each student individually (not by groups of students).
This project involves implementing a simple university personnel database. The database contains two different kinds of objects: students and employees. For each object, the database stores relevant information such as university ID, name, etc. Different information is stored depending on the type of the object. For example, a student has a major and a GPA, while an employee has an office.
You are required to do the following:
- Implement Java classes for storing student and employee objects.
- Write code to save an array of student and employee objects to a database file.
- Write code to load the objects from a database file into memory, returning the objects in an array.
Project Files
The following files are required to complete the project. You should download these files with a web browser. You are not allowed to modify these files in any way except for implementing methods that were intentionally left empty. Specifically, interface definitions may not be modified, and all code must remain in the cs235.java
package.
1. Write a Class that Implements the Person
Interface
The file Person.java defines an interface for Person
objects. A Person
object contains a university ID and a name. The Person
interface defines methods that apply to all university personnel, regardless of whether they are a student or employee. You could write a class named PersonImpl
that implements the methods defined on the Person
interface. The outline of the PersonImpl
class would look like this:
public class PersonImpl implements Person {
String getID() { ... }
void setID(String id) { ... }
String getName() { ... }
void setName(String name) { ... }
...
}
Put your code in the cs235.java
package. All the provided interfaces are defined in a package named cs235.java
. The classes you write must also be placed in the cs235.java
package.
2. Write a Class that Implements the Student
Interface
The file Student.java defines an interface for Student
objects. A Student
object has a university ID, a name, a major, and a GPA. The Student
interface defines methods that are specific to students. The easiest way to implement the Student
interface is to write a class (perhaps with the name StudentImpl
) that inherits from PersonImpl
.
3. Write a Class that Implements the Employee
Interface
The file Employee.java defines an interface for Employee
objects. An Employee
object has a university ID, a name, and an office address. The Employee
interface defines methods that are specific to employees. The easiest way to implement the Employee
interface is to write a class (perhaps with the name EmployeeImpl
) that inherits from PersonImpl
.
4. Implement the Methods in the Factory Class
The file Factory.java contains a class named Factory. The methods in this class are empty (actually, they contain just enough code to allow them to compile). These methods are used to create Student
and Employee
Java objects. Your implementations of these methods should be very simple. All they need to do is instantiate an object that implements the appropriate interface (Student
or Employee), initialize it with the passed-in values, and return it from the method.
5. Test Your Code
You are responsible for testing your code and making sure that it works. Hopefully by this point you have already done some testing of your individual classes. A test driver program is also provided to help you test your code. The test driver should not be viewed as a replacement for your own testing. If your code passes the test driver, it is still possible that it will fail at pass off because a different test program is used for pass off. It is your responsibility to make sure your code works.
6. Implement the toString
Method on Your Classes
For the class that implements the Student
interface, the toString method should return a String of the form:
"student\nID\nName\nMajor\nGPA\n"
where ID
should be replaced by the student's university ID, Name
should be replaced by the student's name, etc., and \n
is the new-line character.
For the class the implements the Employee
interface, the toString
method should return a String
of the form:
"employee\nID\nName\nOffice\n"
7. Implement the equals
Method on Your Classes
Your classes that implement the Student
and Employee
interfaces are required to implement the equals
method. For this project, two person objects are equal if and only if their university IDs are equal.
HINT: Use the equals method on the String
class.
HINT: If you use a class hierarchy that mirrors the interface hierarchy, you should only need to implement the equals method in the root class (PersonImpl
).
8. Implement the java.lang.Comparable
Interface on Your Classes
Your classes that implement the Student
and Employee
interfaces are required to implement the Comparable
interface. When implementing the compareTo
method, you should determine the relationship between the two objects by comparing their university IDs.
HINT: The String
class implements the Comparable
interface. Use the compareTo
method on the String
class.
HINT: If you use a class hierarchy that mirrors the interface hierarchy, you should only need to implement the compareTo
method on the root class (PersonImpl
).
9. Implement the Methods in the PersonIO
Class
The file PersonIO.java contains a class named PersonIO
. The methods in this class are empty (actually, they contain just enough code to allow them to compile).
The save
method is used to save an array of Person
objects to a database file. The two parameters to the save
method are the array of objects to be saved and the name of the database file. Your implementation of the method should open the specified file and write all of the objects in the array to the file using the format described below. If the objects are successfully saved to the file, the save method must return true
. If the objects cannot be saved, either because the file cannot be opened or there is an error in writing, the save method must return false
.
The load
method is used to load the objects from a database file into an array of Java objects. Your implementation of the method should open the specified database file, read all of the objects that it contains, and create an equivalent array of Java objects. For example, for each student object in the database, you should create an instance of your class that implements the Student
interface, initialize it with the values from the database file (ID, name, etc.), and append it to the array. Employee
objects should be processed similarly. If the file is successfully loaded, the load
method should return the array of objects. If the database file cannot be read, either because the file cannot be opened or the contents of the file are invalid, the load
method should return null
.
The load
and save
methods are not allowed to throw any exceptions other than IllegalArgumentException
. This means that you must handle any exceptions that occur during the file I/O process internally within the methods, and return null
or false
if something goes wrong.
Database File Format
A database file is a text file that contains zero or more person objects. The format of a database file looks like this:
3
student
123456789
Bill White
Computer Science
3.56
employee
000006
Robert Millett
106 JSB
employee
000001
Cecil O. Samuelson
1 ASB
The first line in the file contains the number of objects in the database. The number of objects can be any non-negative integer, including zero. Following the object count line, the file contains the specified number of person objects. Each person is a student or an employee. The first line in an object's textual representation indicates which kind of person it is: "student
" or "employee
". Notice that these strings are required to be all lower-case. The second line contains the person's unique university ID. Although the IDs used in the example look like numbers, a person's ID may actually be any character string. The third line contains the person's full name.
The content of the lines following the name depends on the type of the person. For students, the fourth line contains their major, and the fifth line contains their GPA. For employees, the fourth line contains their office address.
Finally, each person in the file is followed by a single blank line.
10. Throw the Appropriate Exceptions
The documentation accompanying each of the methods in the Person
, Student
, and Employee
interfaces describes what exceptions are thrown by each method and under what circumstances they are thrown. In general, each method throws an IllegalArgumentException
when it is passed an invalid argument value. Your implementations of these methods must throw the appropriate exceptions as described in the comments.