Create a text file using Notepad or any other application that saves as a text f
ID: 3696598 • Letter: C
Question
Create a text file using Notepad or any other application that saves as a text file format.
Save this file to the root folder of your NetBeans project as employees.txt. In this file, enter
five employee records. Each record should consist of a first name in positions 1 through 10,
followed by the employee's years of service in positions 11 and 12. This is a fixed width file,
so unused spaces for the name would hold a space. The years of service must use numerics for both
positions. For example, one record would be for "Mike", who has been employed at the company for
four years. His record would consist of positions 1 through 10 as "Mike " (no quotes) and
positions 11 and 12 as "04" (no quotes).
Create a method named readEmployees that reads the employees.txt file to the command line.
Each first name should be preceded with "First name: ". Each year of service should be preceded
with "Year(s) of service: ". Be sure to format each year of service so that any leading zeroes
are not displayed. Use a try…catch block read and output the text file's records. Handle two
potential errors via the catch block. The first should take care of a file that can't be found.
The second should be of an Exception type. Each raised exception should include a suitable custom
message for that exception type along with the result of its .getMessage() string.
Call each of the methods as necessary above from the main method. The pseudocode for the main
method will be as given below.
Create the employees.txt file (this is done using your text editor).
Open and read the employees.txt file and output its contents on a record-by-record basis.
Explanation / Answer
package payslips; import java.util.*; import payslips.Employee; import payslips.Payslip; public class MainProgramme { public static String name; public static String street; public static String town; public static String postcode; public static int payrollNo; public static char taxcode; public static String type; static Scanner sc = new Scanner(System.in); static Scanner sd = new Scanner(System.in); static int tempvar; static int temppayrollNo; static ArrayList list = new ArrayList(); static String names[] = { "John Hepburn", "David Jones", "Louise White", "Harry Martin", "Christine Robertson" }; static String streets[] = { "50 Granton Road", "121 Lochend Park", "100 Govan Avenue", "51 Gorgie Road", "1 Leith Street" }; static String towns[] = { "Edinburgh", "Edinburgh", "Glasgow", "Edinburgh", "Edinburgh" }; static String postcodes[] = { "EH6 7UT", "EH1 1BA", "G15 5GG", "EH5 2PR", "EH4 4ST" }; static int payrollNos[] = { 10001, 10002, 10003, 10004, 10005 }; static char taxcodes[] = { 'C', 'B', 'C', 'C', 'B' }; static String types[] = { "Monthly", "Weekly", "Monthly", "Monthly","Weekly" }; public static void main(String[] args) { for (int i = 0; i < 5; i++) { name = names[i]; street = streets[i]; town = towns[i]; postcode = postcodes[i]; payrollNo = payrollNos[i]; taxcode = taxcodes[i]; type = types[i]; Employee e = new Employee(name, street, town, postcode, payrollNo,taxcode, type); list.add(e); } // statements and prompts within the console for the user to follow System.out.println("Welcome to your Payroll System"); System.out.println(); System.out.println("Please enter your choice below from the following options"); System.out.println(); System.out.println("View all current weekly employees = 1 "); System.out.println("View all current monthly employees = 2 "); System.out.println("Delete an employee = 3 "); System.out.println("Add an employee = 4 "); System.out.println("Print an employee payslip = 5"); System.out.println("To exit the system = 0 "); // allows user to enter number of choice and this reflects which statement is ran in userChoice method tempvar = sc.nextInt(); // runs the userChoice method userChoice(); } // method to determine what statement runs according to which choice user makes public static void userChoice() { Employee tempEmployee = new Employee(); boolean foundEmployee = false; // if user enters 1 it prints out the employee list. if (tempvar == 1) { Weekly.printWeekly(); } else if (tempvar == 2) { Monthly.printMonthly(); } else if (tempvar == 3) { printEmployeelist(); System.out.println(""); System.out.println("Above are a list of all employees."); System.out.println("Please enter the payroll number of the employee you wish to delete from the system"); temppayrollNo = sc.nextInt(); // while loop to search on payroll number, deletes the employee if correct, error message if not if (list.isEmpty() == false) { int a = 0; while (a 5 && tempstring7.length() < 9) // sets the length of string { tempEmployee.setPostcode(tempstring7); } else { tempEmployee.setPostcode("You have not entered a valid UK postcode"); } tempEmployee.setPayrollNo(payrollNo + 1); // sets payroll number to next in sequence System.out.println("Please enter your Tax code (A, B or C): "); tempEmployee.setTaxcode(sd.next().charAt(0));// takes in tax code using scanner System.out.println("Please enter Employee Type (Weekly or Monthly): "); tempEmployee.setType(sd.next()); //takes in type of employee list.add(tempEmployee);// creates temp employee and adds to list printEmployeelist();// prints employee list to view } else if (tempvar == 5) { Payslip.Payslips(); //runs payslip method from payslip class } else if (tempvar == 0) // if user hits 0 it allows them to exit the programme { System.out.println("You have exited the system"); System.exit(0); } else // if any other choice entered they will be met with this message { System.out.println("You have entered the wrong choice"); } } // method to create the book list using a for loop public static void printEmployeelist() { for (int i = 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.