java employeefileframe java import java awt flowlayout import java awt event actione 5152207

java

// EmployeeFileFrame.java
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Formatter;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class EmployeeFileFrame extends JFrame
{
   public HourlyEmployee hourlyEmployee;
   private final JTextField lastNameTextField;
   private final JTextField firstNameTextField;
   private final JTextField hourlyRateTextField;
   private final JLabel lastNameLabel;
   private final JLabel firstNameLabel;
   private final JLabel hourlyRateLabel;
   private final JButton printButton;
   private final JButton saveButton;
   private final int MAX_OLD_RECORDS = 50;
   private int numOldRecords = 0;
   private HourlyEmployee oldRecords[];
private Formatter output;
private Scanner input;

   // TextFieldFrame constructor adds JTextFields to JFrame
   public EmployeeFileFrame()
   {
   super(“Hourly Employee Data”);
   setLayout(new FlowLayout());
  
   // construct lastNameTextField
   lastNameTextField = new JTextField(24);
   lastNameLabel = new JLabel(“Last Name: “); // label
   lastNameLabel.setLabelFor(lastNameTextField);
   add(lastNameLabel);
   add(lastNameTextField); // add lastNameTextField to JFrame

      // construct firstNameTextField
   firstNameTextField = new JTextField(24);
   firstNameLabel = new JLabel(“First Name: “); // label
   firstNameLabel.setLabelFor(firstNameTextField);
   add(firstNameLabel);
   add(firstNameTextField);
  
   // construct hourlyRateTextField
   hourlyRateTextField = new JTextField(24);
   hourlyRateLabel = new JLabel(“Hourly Rate: “); // label
   hourlyRateLabel.setLabelFor(hourlyRateTextField);
   add(hourlyRateLabel);
   add(hourlyRateTextField);

   // create new Buttons and ButtonHandler for button event handling
   printButton = new JButton(“Print”);
   add(printButton);
   saveButton = new JButton(“Save”);
   add(saveButton);
   ButtonHandler buttonHandler = new ButtonHandler();
   printButton.addActionListener(buttonHandler);
   saveButton.addActionListener(buttonHandler);
   } // end TextFieldFrame constructor

// inner class for button event handling
private class ButtonHandler implements ActionListener
{
// handle button event
@Override
public void actionPerformed(ActionEvent event)
{
   if (event.getSource() == printButton)
   {
      hourlyEmployee = new HourlyEmployee(
              lastNameTextField.getText(),
              firstNameTextField.getText(),
              Double.parseDouble(
                   hourlyRateTextField.getText()));
  
       System.out.println(hourlyEmployee.toString());
   }
   if (event.getSource() == saveButton)
   {
       boolean oldData;
       if (oldData = openFileInput())
       {
           loadOldData(); // keep old data to append to
           closeFileInput();
       }
      
       hourlyEmployee = new HourlyEmployee(
              lastNameTextField.getText(),
              firstNameTextField.getText(),
              Double.parseDouble(
                   hourlyRateTextField.getText()));
      
       openFileOutput();
       if (oldData)
       {
           saveOldData(); //restore old data
       }
         
   // write new HourlyEmployee
    output.format(“%05d %s %s %.2f%n”,
           hourlyEmployee.getEmployeeID(),
           hourlyEmployee.getLastName(),
           hourlyEmployee.getFirstName(),
           hourlyEmployee.getHourlyRate() );
   closeFileOutput();
   }
}
}

private boolean openFileInput()
{
   try
   {
       input = new Scanner( new File( “hourly_employees.txt” ) );
       return true;
   }
catch ( FileNotFoundException fileNotFoundException )
{
System.err.println( “Error opening file.” );
return false;
} // end catch
}

private void loadOldData()
{
   // keep old data to restore and append to
   int i = 0;
   oldRecords = new HourlyEmployee[MAX_OLD_RECORDS];
   while (input.hasNext() && i
   {
       oldRecords[i++] = new HourlyEmployee(
               input.nextInt(),
               input.next(),
               input.next(),
               input.nextDouble());
   } // end while
   numOldRecords = i;
}

private void closeFileInput()
{
if ( input != null )
input.close(); // close file
}

// enable user to open file
private void openFileOutput()
{
try
{
output = new Formatter( “hourly_employees.txt” );
} // end try
catch ( FileNotFoundException filesNotFoundException )
{
System.err.println( “Error creating file.” );
System.exit( 1 );
} // end catch
} // end method openFile

private void saveOldData()
{
   //restore old data
   for (int i = 0; i
   {
// rewrite old HourlyEmployees
output.format( “%05d %s %s %.2f%n”,
       oldRecords[i].getEmployeeID(),
       oldRecords[i].getLastName(),
       oldRecords[i].getFirstName(),
       oldRecords[i].getHourlyRate());
   }
}

// close file
private void closeFileOutput()
{
if ( output != null )
output.close();
} // end method closeFile

} // end class EmployeeFileFrame

"Get 15% discount on your first 3 orders with us"
Use the following coupon
FIRST15

Order Now