I have created the output for a program that allows a user to input their employee name and number and then their hourly wage and their total number of regular hours and overtime hours. This is my first time working with this type of program in java and I'm having an issue with the Try-Catch block where it gets the text input from the user and then sets it. If anyone can assist me it would be greatly appreciated.
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CreatePayrollFile extends JFrame implements ActionListener, WindowListener {
public static final int WIDTH = 400;
public static final int HEIGHT = 300;
JPanel titlePanel = new JPanel();
JPanel displayPanel = new JPanel(new GridLayout(6, 1));
JPanel dPanel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel dPanel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel dPanel3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel dPanel4 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel dPanel5 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel dPanel6 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel dPanel7 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel buttonPanel = new JPanel();
private JLabel companyName = new JLabel("Payroll INC.");
Font bigFont = new Font("Helvetica", Font.ITALIC, 24);
private JLabel prompt = new JLabel("Enter Payroll Information");
private JTextField employeeName = new JTextField(10);
private JTextField employeeNumber = new JTextField(10);
private JTextField hourlyRate = new JTextField(10);
private JTextField regularHours = new JTextField(10);
private JTextField overtimeHours = new JTextField(10);
private JLabel enameLabel = new JLabel("Employee Name ");
private JLabel enumLabel = new JLabel("Employee Number ");
private JLabel hrLabel = new JLabel("Hourly Rate ");
private JLabel rhLabel = new JLabel("Regular Hours ");
private JLabel orLabel = new JLabel("Overtime Hours ");
private JButton enterDataButton = new JButton(" Enter data ");
DataOutputStream ostream;
public CreatePayrollFile() {
super("Create Payroll File - Assignment 10");
setSize(WIDTH, HEIGHT);
try {
ostream = new DataOutputStream(new FileOutputStream("payroll.dat"));
} catch (IOException e) {
System.err.println("File not opened");
System.exit(1);
}
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
companyName.setFont(bigFont);
titlePanel.add(companyName);
titlePanel.setBackground(Color.white);
dPanel1.add(prompt);
displayPanel.add(dPanel1);
dPanel2.add(enameLabel);
dPanel2.add(employeeName);
displayPanel.add(dPanel2);
dPanel3.add(enumLabel);
dPanel3.add(employeeNumber);
displayPanel.add(dPanel3);
dPanel4.add(hrLabel);
dPanel4.add(hourlyRate);
displayPanel.add(dPanel4);
dPanel5.add(rhLabel);
dPanel5.add(regularHours);
displayPanel.add(dPanel5);
dPanel6.add(orLabel);
dPanel6.add(overtimeHours);
displayPanel.add(dPanel6);
buttonPanel.setBackground(Color.white);
buttonPanel.setLayout(new FlowLayout());
enterDataButton.setMnemonic(KeyEvent.VK_E);
buttonPanel.add(enterDataButton);
enterDataButton.addActionListener(this);
contentPane.add(titlePanel, BorderLayout.NORTH);
contentPane.add(displayPanel, BorderLayout.CENTER);
contentPane.add(buttonPanel, BorderLayout.SOUTH);
addWindowListener(this);
}
private void writeRecord() {
String employeeName;
Double hourlyRate;
Integer employeeNumber, regularHours, overtimeHours;
// this is where I am having errors. It is saying that the getText method is undefined
// for each respective use (int, string, double). I am also getting error "The method
// writeString(String) is undefined for the type DataOutputStream" and the same
// undefined errors for the setText for each respective use.
try {
employeeName = String.parseString(employeeName.getText());
employeeNumber = Integer.parseInt(employeeNumber.getText());
hourlyRate = Double.parseDouble(hourlyRate.getText());
regularHours = Integer.parseInt(regularHours.getText());
overtimeHours = Integer.parseInt(overtimeHours.getText());
ostream.writeString(employeeName);
ostream.writeInt(employeeNumber);
ostream.writeDouble(hourlyRate);
ostream.writeInt(regularHours);
ostream.writeInt(overtimeHours);
employeeName.setText("");
employeeNumber.setText("");
hourlyRate.setText("");
regularHours.setText("");
overtimeHours.setText("");
} catch (NumberFormatException e2) {
System.err.println("Invalid number ");
} catch (IOException e3) {
System.err.println("Error writing file");
System.exit(1);
}
}
public void actionPerformed(ActionEvent e) {
writeRecord();
}
public void windowClosing(WindowEvent e) {
try {
ostream.close();
} catch (IOException e4) {
System.err.println("File not closed");
System.exit(1);
}
System.exit(0);
}
public void windowClosed(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowOpened(WindowEvent e) {
}
public void windowActivated(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
public static void main(String[] args) {
CreatePayrollFile cmof = new CreatePayrollFile();
cmof.setVisible(true);
}
}
Here is a example of output and the instructions. I used software to make larger and it appears as such on my computer but photobucket is reducing it's size. I hope that it gives the general idea however.
http://i108.photobucket.com/albums/n24/zypher89/Payroll-1.jpg
Thanks in advance to all who provide assistance.