1 Goals.
1. To use a text file for output and later for input.
2. To use exceptions and write an exception handler that does more than abort execution.
3. To Use a static class variable.
4. To use a boolean variable and a Date variable.
5. To use an ArrayList.
6. To implement Phase 1 of a four phase well-structured project with multiple classes using the Model-View-Controller design pattern (MVC).
2 Overview
This section discusses a finished application that you will construct in several phases. When all phases are finished, it will simulate a human resources system to keep track of savings through the company. There is a Human Resources Manager (Boss), and employees (eventually more than one type of employee will be used). Eventually, this system will be created as an application with a windows interface. The Employee’s login name will eventually be encrypted and the data will be stored in an object file. Program 4 implements only part of the menu, uses a text file and does not encrypt the employee data. At first this will be a menu-driven application allowing for these actions:
1. The first time the program is used, the user will enter his or her own data in to the program and will become the Boss (Human Resources Manager) with access to everyone’s data.
2. After that, when the program starts up, it will read in a database of employee data and store it in the employee collection.
3. After that, the Boss will be able to log in and create new employees by entering the person’s name (first, middle initial-optional, and last), login name, base salary, and savings percent. This data will be used to initialize a new employee object. The object will also record the current date and a unique ID number for this employee. The new employee will be added to the Employee collection.
4. The Boss can log in and display a list of all Employees in the collection and can change the base salary or percent savings for any employee. In the list, display the full name (first, middle initial and last), login name, Employee ID, base salary, percent savings and date of hire for each Employee.
5. Any Employee in the collection can log in and see his or her own data and change their name and percent savings (due to marriage, etc).
6. An Employee whose login name is in the collection may log in and out. Logging in will automatically log out the prior Employee.
7. The menu has an option to quit and quitting will cause the final contents of the collection to be written back to the database file (replacing it).
8. Since you have
more than one class
in this project, you will
need to create a package (call it “savings” – note the first letter is lower case)
and put all of the classes in the package. To tell your program to look for the other classes, you need to put the following in each class file near your import statements.
package
savings;
3 The Employee Class: Superclass of the Model (from MVC) for this application.
a) An object of type Employ ...
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
1 Goals. 1. To use a text file for output and later for in.docx
1. 1 Goals.
1. To use a text file for output and later for input.
2. To use exceptions and write an exception handler that does
more than abort execution.
3. To Use a static class variable.
4. To use a boolean variable and a Date variable.
5. To use an ArrayList.
6. To implement Phase 1 of a four phase well-structured project
with multiple classes using the Model-View-Controller design
pattern (MVC).
2 Overview
This section discusses a finished application that you will
construct in several phases. When all phases are finished, it will
simulate a human resources system to keep track of savings
through the company. There is a Human Resources Manager
(Boss), and employees (eventually more than one type of
employee will be used). Eventually, this system will be created
as an application with a windows interface. The Employee’s
login name will eventually be encrypted and the data will be
stored in an object file. Program 4 implements only part of the
menu, uses a text file and does not encrypt the employee data.
At first this will be a menu-driven application allowing for
these actions:
1. The first time the program is used, the user will enter his or
her own data in to the program and will become the Boss
(Human Resources Manager) with access to everyone’s data.
2. After that, when the program starts up, it will read in a
database of employee data and store it in the employee
collection.
3. After that, the Boss will be able to log in and create new
employees by entering the person’s name (first, middle initial-
2. optional, and last), login name, base salary, and savings
percent. This data will be used to initialize a new employee
object. The object will also record the current date and a unique
ID number for this employee. The new employee will be added
to the Employee collection.
4. The Boss can log in and display a list of all Employees in the
collection and can change the base salary or percent savings for
any employee. In the list, display the full name (first, middle
initial and last), login name, Employee ID, base salary, percent
savings and date of hire for each Employee.
5. Any Employee in the collection can log in and see his or her
own data and change their name and percent savings (due to
marriage, etc).
6. An Employee whose login name is in the collection may log
in and out. Logging in will automatically log out the prior
Employee.
7. The menu has an option to quit and quitting will cause the
final contents of the collection to be written back to the
database file (replacing it).
8. Since you have
more than one class
in this project, you will
need to create a package (call it “savings” – note the first letter
is lower case)
and put all of the classes in the package. To tell your program
to look for the other classes, you need to put the following in
each class file near your import statements.
package
savings;
3 The Employee Class: Superclass of the Model (from MVC) for
this application.
a) An object of type Employee will have the following data
members:
i)
A
3. static
int variable called nextId. See the detailed instructions below.
This is to make sure that the Employee ID is not re-used.
There is one of these for the class and we use it to set the
employee ID.
ii) The Employee ID is a
final
int variable. It cannot be updated. In your output, it should be
printed as a five digit number with leading zeros (use printf
with the format “%05d” so it looks like this 00001).
iii) The Employee’s name. When entered, it can include spaces
and punctuation, and will be terminated by the end of line
character. Store it as a single String. The name should be
entered as Last name, first name and middle initial (optional).
iv) Login name
has no spaces
and it can contain letters or numbers. It
must be at least 6 characters long.
For Program 5 you will have to make this unique in the database
(no duplicates).
When an employee logs-in they use this data member (we have
not implemented password yet).
v) The base salary (a double).
vi) The percent savings (a double).
vii) Date variable, set to the date that the employee was entered
in to the system.
viii) A boolean variable that is set to true, isEmployee. False
means this employee is no longer an employee and should not
be paid or be allowed to add to their company savings.
b) Provide a constructor with four parameters (login, name,
salary and percent savings) that initializes all Employee data
members.
c) Provide a constructor with all data members as parameters
that reads the data from the file and adds an instance of the
Employee class to the ArrayList or other collection and
initializes all Employee data members.
4. d) Do not implement get functions (accessors) for variables
unless they are needed. You will need them at some point, but
you may not need any accessors for P4.
e) Implement a set function for the salary, percent savings and
name. These are known as mutators.
f) Implement a toString() function that will format the data
members of Employee. Put each Employee on a single line of
the output. Include the ID, login name, name, salary, percent
savings, and date of hire separated by tab characters.
2)
The Employee ID
The employee ID number will be generated by the system,
using the static class member Employee.nextId
. The Boss will become employee ID 0. Each time an employee
is created, the nextID must be copied in to that employee’s ID
number, then nextId must be incremented. In this way no two
employees can have the same ID. In your output and file, make
this a system generated variable that is five digits long and
starts with 00001 (00000 for the Boss) and goes up from there.
An Employee ID number cannot be reused.
4 The main() function for P4:
The main() function When the file is saved, the nextID number
must be saved on the file first before the ArrayList data is
saved. When the file is read, the value of the nextID number
must be initialized (via the mutator function setId) to the saved
value from the file.
Main can be in a separate class called Main or it can be inside
the Savings class.
method should print a title line to the
console showing the program name and your name.
Then it should instantiate a Savings object and call the Savings
object’s doMenu() function.
When caught, print a comment, a stack trace, and abort.
5. execution terminates for any reason, the data in the collection
must be written back out to the file. This means you need a
finally block following the main’s try block. Do the following:
1. First, reopen the input file for output. This will destroy the
original contents of the file.
2. Then execute a “serialize” operation: write each object in the
collection to the file using a loop.
3. Lastly, close the new database file.
5 The Savings Class for P4: the controller class (from MVC) of
this application.
a. The Savings class should contain an ArrayList of Employee,
and variables to store the current Employee (a reference to an
Employee in the ArrayList), a menu, a Scanner for the
keyboard, a Scanner for the Employee-file, and a PrintWriter
for the Employee file.
b.
You may use this to define the menu:
private static String menu =
“Savings System Menunt1. Log In n”
+ “
t2. Enter employees nt3. List employees”
+ “
nt4. Change employee data nt5. Terminate an employee”
+
nt6. Report employee savings nt0. Exit system”
c. Use a switch statement to process the menu choices. Write a
separate function for each option! They will be necessary when
we convert the application to a GUI.
d. In program 4, we will implement menu options 1, 2, and 0.
5.1 The Savings constructor.
e. The first time this program is run, the file of Employee data
6. (employee file) will not exist. On second and further runs, it
will exist. The Exception system allows us to implement the
right functionality using a try..catch. Enclose the file handling
in a try block and write a catch block for
FileNotFoundException
. Note: an IOException should not happen because of the way
you have set up your program to prevent it by checking the
input data. If it does, let the exception pass up to main. You do
not need to handle it here.
The try block should do the following:
i. Open the Employee file and a Scanner to read it.
ii. Read all of the data one line at a time using a normal loop
and hasNext(). Create a series of Employee objects and store
them in the Employee collection (ArrayList).
iii. Close the Scanner (which closes the input file).
The catch block should do the following:
iv. This is the handler for a FileNotFoundException, which will
occur the first time this program is run and anytime you delete
the data file. If the file exists (second and later execution), this
error will not occur.
v. Print a clear comment about the missing file.
vi. Prompt the user for the logon name, salary, percent savings
and name of the boss, create an Employee object with ID
Number 0 and add it to the empty
Program 4: Employee Savings Database
CSCI 6617 Spring 2016
4
Employee collection (ArrayList). Then continue with normal
execution of this application, DO NOT ABORT.
f. At this point, the data from the file has been read, if it
existed. The next step is to open an output file with the same
name as the now-closed input file (replace it).
7. i. Put this code in second a try block that immediately follows
the catch block from the earlier try.
ii. Control will come here after the first try-block or its catcher
finishes its work. Notice that we are reentering execution after
an exception.
iii. At the end of the constructor, the program, its Employee
ArrayList, and its output file are ready for use.
4.2 The doMenu() function
Write a main loop that displays the menu forever (an infinite
loop), until the user selects ”0. Exit System”.
iv. Put the entire menu loop in a try block with a finally clause.
1. In the finally clause, write all of the Employees in the
Employee ArrayList to the output file using a loop, close the
file, and print a message on the console screen saying this was
completed.
2. After the finally block, control should return to main, where
it hits the end of another try block and ends the program.
v. Prompt the user for a menu choice and process that choice
with a switch. Do not use an if. . . else structure.
1. In the switch, do NOTHING except call one of the functions
below. When the program is converted to Java FX, this switch
will be replaced by a bunch of Buttons and other objects.
2. Break out of both the switch and the infinite loop if the user
selects
”0. Exit System”
. Leaving the loop will end the try block, which will send
control directly to the finally block.
3. Create an empty method (stub) to process each of the
following
“3. List employees”,
“
4. Change employee data”, “5. Terminate employees” and “6.
Pay employees”
because these menu choices are not implemented in P4.
8. Savings needs public functions for the menu options.
i.
The Savings class needs a private utility function, login() to
implement ”1. Log In”
.
4. login() prompts the user to enter their
login name
and checks the Employee ArrayList to see if they are an
Employee (if that Login Name is in the collection).
5. If the user is not in the Employee collection, they cannot
proceed. Print a message saying so and allow them to return to
the menu.
ii. newEmployee(): to create a new Employee member (about
20-30 lines of code, including whitespace and comments). This
is run when there are no employees in the ArrayList or when “2
Enter Employees” is chosen by the Boss (when logged in
Employee ID is 0).
6. Prompt for and read the Employee person's full name (assume
that there is a first and last name, but make the middle initial
optional. Allow for a hyphen in the last name), and the other
data members (fields).
7. Create a new Employee with this data and put it into the
ArrayList of Employees.
Program 4: Employee Savings Database
CSCI 6617 Spring 2016
5
iii. No function is needed for the ”0 Exit the System” menu
item. Just break out of the loop.
8. Break out of both the switch and the infinite loop if the user
selects
”0 Exit the System”
.
9. Leaving the loop will end the try block, which will send
control directly to the finally block.
9. 6 Testing and Submission.
Due by 11:59 PM on the due date.
When you are sure your program works, delete the input and
output files. Then follow the testing procedure below.
1. Start the program and enter your own name. Make up the data
(name, logon name, salary and percent savings). Enter this user
as the Boss.
2. Add a second user you made up.
3. Exit the system.
4. Make a copy of the database file and make its name run1.txt
and copy and paste your console output to a file called
console1.txt (you can use notepad to save this).
5. Start the program again. Enter two more users – make them
up. This makes four users in total.
6. Exit the system.
Use the two runs to test errors in entering the data. Finally,
hand in a zipped folder containing:
Employee classes and Main if you created one).
and paste them in to the same file, but clearly mark them as run
1 and run 2 console output.