// This program reads in a roster of course staff members and computes and outputs // each person's pay for the week. Each type of staff member is paid using a // different model. // // DEVELOPMENT NOTES: // (These notes would not be in your program's comments. They are here // to help you understand important topics or elements of this code.) // // Notice the use of line-based file input in main, and the use of a Scanner // to tokenize a String in the pay methods. Notice also the use of PrintStream // for file output. // // Although we have methods calling each other in a row, this program is NOT // an example of chaining, because the structure of the code accurately // reflects the task being performed, and main remains a concise overall // summary of the behavior. import java.util.*; import java.io.*; public class Payroll { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("employees.txt")); Scanner console = new Scanner(System.in); System.out.print("Output file name? "); String outFileName = console.nextLine(); PrintStream output = new PrintStream(new File(outFileName)); while (input.hasNextLine()) { String employee = input.nextLine(); process(employee, output); } } // Processes an employee text string // // String employee - the employee to determine pay for // PrintStream output - the PrintStream to use for output public static void process(String employee, PrintStream output) { if (employee.startsWith("Instructor")) { payInstructor(employee, output); } else if (employee.startsWith("Grader")) { payGrader(employee, output); } else if (employee.startsWith("TA")) { payTA(employee, output); } } // Computes and outputes the pay for an instructor; instructors get paid a flat // amount per week // Instructor input will consist of the employee type, a name, and the weekly pay // rate for that instructor // // String instructor - the input for the instructor to pay // PrintStream output - the PrintStream to use for output public static void payInstructor(String instructor, PrintStream output) { Scanner tokens = new Scanner(instructor); String type = tokens.next(); String name = tokens.next(); double pay = tokens.nextDouble(); output.println(name + " (" + type + ") earned $" + pay + " this week."); } // Computes and outputes the pay for a grader; graders get paid a given amount // for each student they grade // Grader input will consist of the employee type, a name, and the number of // students graded, and the pay per student for that grader // // String grader - the input for the grader to pay // PrintStream output - the PrintStream to use for output public static void payGrader(String grader, PrintStream output) { Scanner tokens = new Scanner(grader); String type = tokens.next(); String name = tokens.next(); int students = tokens.nextInt(); double rate = tokens.nextDouble(); double pay = students * rate; output.println(name + " (" + type + ") earned $" + pay + " this week."); } // Computes and outputes the pay for a TA; TAs get paid $15/hour and can work any // number of days // TA input will consist of the employee type, a name, and some number of values // representing the hours worked on each of several days // // String ta - the input for the TA to pay // PrintStream output - the PrintStream to use for output public static void payTA(String ta, PrintStream output) { Scanner tokens = new Scanner(ta); String type = tokens.next(); String name = tokens.next(); int hours = 0; while (tokens.hasNextInt()) { hours += tokens.nextInt(); } double pay = hours * 15.00; output.println(name + " (" + type + ") earned $" + pay + " this week."); } }