I have an 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. Then writes out the data to a binary file.
The program I need assistance with reads in that file, adds a new field at the bottom, "Gross Pay", and calculates the gross pay from the values from the input file. If anyone can assist me it would be greatly appreciated.
Link to photobucket image of an example of desired output
Also, I'm not just reading in the next 4 bytes for the int. I want to read in a specific set of data from the file.
I will try to look for a method that reads in the entire next set of data and then selects out the individual parts needed.
Do I need to post my code that creates the file that I'm reading from so that you can see what I'm working with / reading from?
The instructions for an assignment was to create program to make the output file. We turned it in and then he wants us to create a program that reads in that exact file and re-sort the data, plus add the new field with its calculated valued in the text field of the GUI.
I've never been able to get the code tags on here to work for the inport codes and a few lines after that. I hope that it is readable.
Here is my code :
import java.io.*;
import java.awt.; import java.awt.event.; import javax.swing.*;
public class ReadPayrollFile 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(7, 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 JTextField grossPay = 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 JLabel gpLabel = new JLabel ("Gross Pay ");
private JButton nextRecordButton = new JButton(" Next Record ");
DataInputStream fstream;
public ReadPayrollFile() {
super("Read Payroll File - Assignment 11");
setSize(WIDTH, HEIGHT);
try
{
fstream = new DataInputStream(new FileInputStream("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);
dPanel7.add(gpLabel);
dPanel7.add(grossPay);
displayPanel.add(dPanel7);
buttonPanel.setBackground(Color.white);
buttonPanel.setLayout(new FlowLayout());
nextRecordButton.setMnemonic(KeyEvent.VK_E);
buttonPanel.add(nextRecordButton);
nextRecordButton.addActionListener(this);
contentPane.add(titlePanel, BorderLayout.NORTH);
contentPane.add(displayPanel, BorderLayout.CENTER);
contentPane.add(buttonPanel, BorderLayout.SOUTH);
addWindowListener(this);
}
private void readRecord(DataInputStream inputFile) {
String l_employeeName;
Double l_hourlyRate;
Integer l_employeeNumber, l_regularHours, l_overtimeHours;
boolean endOfFile = false;
try {
while (!endOfFile)
{
try
{
l_employeeName = inputFile.readUTF();
l_employeeNumber = inputFile.readInt();
l_hourlyRate = inputFile.readDouble();
l_regularHours = inputFile.readInt();
l_overtimeHours = inputFile.readInt();
fstream.readUTF(l_employeeName);
fstream.readInt(l_employeeNumber);
fstream.readDouble(l_hourlyRate);
fstream.readInt(l_regularHours);
fstream.readInt(l_overtimeHours);
calculateGrossPay(l_hourlyRate, l_regularHours, l_overtimeHours);
employeeName.setText("l_employeeName");
employeeNumber.setText("l_employeeNumber");
hourlyRate.setText("l_hourlyRate");
regularHours.setText("l_regularHours");
overtimeHours.setText("l_overtimeHours");
grossPay.setText("grossPayAmmount");
}
catch (NumberFormatException e2)
{
System.err.println("Invalid number ");
}
catch (IOException e3)
{
System.err.println("Error reading file");
System.exit(1);
}
}
public void actionPerformed(ActionEvent e) {
NextRecord();
}
public void windowClosing(WindowEvent e) {
try {
fstream.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) {
ReadPayrollFile cmof = new ReadPayrollFile();
cmof.setVisible(true);
}
public double calculateGrossPay(double l_hourlyRate, int l_regularHours, int l_overtimeHours)
{
double grossPayAmmount, overtimePayRate, overtimePay;
overtimePayRate = l_hourlyRate * 1.5;
overtimePay = l_overtimeHours * overtimePayRate;
grossPayAmmount = ((l_hourlyRate * l_regularHours) + overtimePay);
return grossPayAmmount;
}
}
The part I'm having problems with is
private void readRecord(DataInputStream inputFile) {
String l_employeeName;
Double l_hourlyRate;
Integer l_employeeNumber, l_regularHours, l_overtimeHours;
boolean endOfFile = false;
try {
while (!endOfFile)
{
try
{
l_employeeName = inputFile.readUTF();
l_employeeNumber = inputFile.readInt();
l_hourlyRate = inputFile.readDouble();
l_regularHours = inputFile.readInt();
l_overtimeHours = inputFile.readInt();
/On the next set "The method readUTF(DataInput) in the type DataInputStream is not applicable for the arguments (String)" with string replaced with Int or Double respectfully./
fstream.readUTF(l_employeeName);
fstream.readInt(l_employeeNumber);
fstream.readDouble(l_hourlyRate);
fstream.readInt(l_regularHours);
fstream.readInt(l_overtimeHours);
calculateGrossPay(l_hourlyRate, l_regularHours, l_overtimeHours);
employeeName.setText("l_employeeName");
employeeNumber.setText("l_employeeNumber");
hourlyRate.setText("l_hourlyRate");
regularHours.setText("l_regularHours");
overtimeHours.setText("l_overtimeHours");
grossPay.setText("grossPayAmmount");
}
catch (NumberFormatException e2)
{
System.err.println("Invalid number ");
}
catch (IOException e3)
{
System.err.println("Error reading file");
System.exit(1);
/Multiple markers at this line - Syntax error, insert "Finally" to complete BlockStatements - Syntax error, insert "}" to complete Block - Syntax error, insert "Finally" to complete TryStatement/ }
public void actionPerformed(ActionEvent e) {
// here I get "The method NextRecord() is undefined for the type ReadPayrollFile" //But I don't know exactly how to get the button to read in the next set of data NextRecord(); }
Any assistance would be greatly appreciated. Thanks in advance