import java.awt.*; import java.awt.event.*; import java.io.*; public class Payroll extends Frame { private static Frame outputDisplay; //Declare a Frame for total static double CalcPay(/* in */ double payRate, //Employee's pay rate /* in */ double hours) //Hours worked // CalcPay computes wages from the employee's pay rate // and the hours worked, taking overtime into account { final double MAX_HOURS = 40.0; //Maximum normal work hours final double OVERTIME = 1.5; //Overtime pay rate factor if (hours > MAX_HOURS) //Is there overtime? return (MAX_HOURS * payRate) + //Yes (hours - MAX_HOURS) * payRate * OVERTIME; else return hours * payRate; //No } public static void main(String args[]) throws IOException, NumberFormatException // Main is where excution starts. It opens the DataFile and PayFile, and processes // the data file. After that, it shows the total in a Frame on the screen and exits // when the user closes the window { String empNum; //Employee ID Number double payRate; //Employee's pay rate double hours; //Hours worked double wages; //Wages earned double total = 0.0; //Total company payroll BufferedReader dataFile; //Declare dataFile for input PrintWriter payFile; //Declare payFile for printing //Open company payroll files dataFile = new BufferedReader(new FileReader("datafile.dat")); payFile = new PrintWriter(new FileWriter("payfile.dat")); //Process the input on dataFile, outputting results on payFile empNum = dataFile.readLine(); //Read employee ID number while (empNum != null) { payRate = Double.valueOf(dataFile.readLine()).doubleValue(); //Read payRate hours = Double.valueOf(dataFile.readLine()).doubleValue(); //Read hours wages = CalcPay(payRate, hours); //Compute wages total = total + wages; //Add wages to total //Put results into payFile payFile.println(empNum + " " + payRate + " " + hours + " " + wages + " "); empNum = dataFile.readLine(); //Get next ID number } dataFile.close(); //Close input file payFile.close(); //Close output file //Create a Frame and display it on the screen outputDisplay = new Frame(); //Instantiate a Frame object outputDisplay.setLayout(new FlowLayout()); //Specify layout manager for frame outputDisplay.add(new Label("Total payroll for the week is $" + total)); outputDisplay.add(new Label("Close window to exit program.")); outputDisplay.pack(); //Pack the frame outputDisplay.show(); //Show the frame on the screen //Event handler for window closing outputDisplay.addWindowListener(new WindowAdapter() //Create a WindowAdapter { public void windowClosing(WindowEvent event) //Method to handle event { outputDisplay.dispose(); //Remove frame from the screen System.exit(0); //Quit the program } }); } }