Question
Goals and Overview. 1. To use a text file for output and later for input. 2. To use exceptions and write an exception handler that
Goals and Overview.
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..
The first time the program is used, the user will enter his or her own data into the program and will become the Boss.
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 log in and create other new employees by entering the persons name (first, middle initial and last), login name, and base salary. This data will be used to initialize a new Employee object. The object will also store the current date and a unique ID number for this employee. Then the new Employee object 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. 5. Any employee in the collection can log in, see his or her own data, and change the name. Logging in will automatically log out the prior Employee. 6. 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.
6. 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.
2 The Employee Class: Part of the Model for this application. Implement these data members for P4; more may be added in later versions of the program:
- a) An object of type Employee will have the following data members:
A login name, containing no spaces.
The base salary (a double or a float).
The Employees name. When entered, it can include spaces and punctuation and will be terminated by the end of the line. Store it as a single String.
A Date variable, set to the date that the employee was entered into the system.
The Employee ID: a final int variable. It should be printed as a 5-digit number with leading zeros. (Use printf with a format %05d).
A static int class variable called nextId. See the detailed instructions below.
2. Provide a constructor with three parameters (login, salary, name) that initializes all Employee data members.
3. Do not implement get functions (accessors) for variables unless they are needed. You may not need any for this assignment.
4. Implement a set function for the salary. This is known as a mutator.
5. 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, salary, date, and name separated by tab characters.
2.1 The Employee ID The employees ID number will be generated by the system, using the static class member Employee.nextId. The boss will become employee 0. Each time an employee is created, the nextId must be copied into that employees ID number, then the next ID must be incremented. In this way, no two employees will ever have the same ID. Make this a system generated variable that is 5 digits long and starts with 00001 and goes up from there. An Employee ID cannot be re-used.
3 The Payroll Class for P4: the controller for this application.
1. The Payroll 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.
2. You may use this to define the menu:
private static String menu = Payroll Menu \t1. Log In "+
" \t2. Enter employees
\t3. List Employees"+
" \t4. List employees" +
" \t5. Terminate employees"+
" \t6. Pay employees
\t0. Exit system"
3. Use a switch to process menu choices. Write a separate function for each option! They will be necessary when we convert the application to a GUI.
4. In this assignment, we will implement menu options 1, 2, and 0
3.1 The Payroll constructor. The first time this program is run, the file of Employee data (employee file) will not exist. On second and further runs, it will exist. The Exception system makes it easy to implement the right functionality. Enclose the file handling in a try block and write a catch block for FileNotFoundException. Note: an IOException should never happen because of the way things are being done. If it does happen, let the exception pass up to main. You dont need to handle it here. The try block should do three things:
Open the Employee file and a Scanner to read it.
Read all the data one line at a time using a normal loop and hasNext(). Create a series of Employee objects using the 5-parameter constructor and store them in the Employee collection. The employees name (the only field with embedded spaces) should be at the end of the line.
Close the Scanner (which closes the input file).
If control goes to the FileNotFound handler, print a clear comment about the missing file. Then prompt for the logon name, salary, and name of the boss, create an Employee object with ID number 0, and add it to the empty Employee collection. Then continue with normal execution. DO NOT abort execution. This is normal the first time you run this application.
On the second and later executions, the file will exist, no exception will happen, and the Payroll constructor must process the file.
3.2 The doMenu() function Write a main loop that displays the menu forever (an infinite loop), until the user selects 0. Exit System.
1. In the loop, prompt the user for a menu choice and process that choice with a switch. Do not use an if. . . else structure.
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.
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.
Create four stub functions to process the menu options that are not handled in this assignment: 3. List employees, 4. Change employee data, 5. Terminate employees, and 6. Pay employees. A stub function is a normal function with an empty body. Creating stubs lets you compile and debug partly constructed code.
3.3 Other Payroll functions.
1. The Payroll class needs a private utility function, dologin() to implement option 1, login. dologin() prompts the user to enter a login name and checks the Employee ArrayList to see if that name is in the collection. If the user is not in the Employee collection, print a message saying so and return to the menu. If the user IS in the collection, this function sets the currentUser variable (a class member) to the Employee record that was found. It also sets the currentID to that users ID.
2. newEmployee() creates and initializes a new Employee object (about 20 lines of code, including whitespace and comments). This is run when the Boss runs the program the first time and when option 2, Enter Employees is chosen by the Boss (whose Employee ID is 0).
Prompt for and read the Employee persons full name as a single string. (Assume that there is a first and last name, separated by a space. A middle initial might also be there, and the last name might have a hyphen this should make no difference in your code. Then prompt for and read the login name (no embedded spaces), and salary. Then read the login and salary for this Employee. Create a new Employee with this data and put it into the ArrayList of Employees.
3. No function is needed to implement option 0: Exit the System. Simply break out of both the switch and the infinite menu-loop if the user selects 0. Leaving the loop will end the try block, which will send control directly to the finally block.
4. The Main Function for P4
The main() function can be in a separate class called Main or it can be inside the Payroll class. The first line of this method should print a title line to the console (the program name and your name). Then instantiate a Payroll object and call the Payroll objects doMenu() function. Surround the code with a try block and catch IOExceptions. When caught, print a comment, a stack trace, and abort.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started