This program will read the data file that was created from another program. The
ID: 3555757 • Letter: T
Question
This program will read the data file that was created from another program. The file is a text file: payroll.txt. Copy that data file into the root directory of the Project Folder.
The data file being used contains records with an employee's name, the number of hours they worked, and their hourly pay rate. Write a program to read the records of information and output (to the Output window or a dialog box) the employee's name and their Gross Pay.
Compute their gross pay by using the following formulas:
If the employee worked 40 hours or less, their pay should be: hours * hourlyPayRate
But if the employee worked more than 40 hours, they get paid their regular amount for the first 40 hours plus "time and a half" for the hours worked over 40. Time and a half means 1.5 times their hourly pay rate. Use the following formula: (40 * hourlyPayRate) + (hours-40) * 1.5 * hourlyPayRate
Explanation / Answer
Class that calculates the information-dependent on input file items separated by space and each entry on its own line
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class ImportFile extends JFrame {
private File file;
private ArrayList<String> name;
private ArrayList<Double> grossPay;
private FileReader fileReader;
private BufferedReader bufferedFileReader;
private JScrollPane output;
private JTextArea textContainer;
public ImportFile(File file){
this.file = file;
name = new ArrayList<>();
grossPay = new ArrayList<>();
textContainer = new JTextArea();
output = new JScrollPane(textContainer);
output.setOpaque(true);
}
private void read(File file){
try {
fileReader = new FileReader(file);
bufferedFileReader = new BufferedReader(fileReader);
String line ="";
String[] information;
double hoursWorked;
double payRate;
double overTime;
while((line = bufferedFileReader.readLine())!= null){
information = line.split(" ");
name.add(information[0]+" "+information[1]);
hoursWorked = Double.parseDouble(information[2]);
payRate = Double.parseDouble(information[3]);
if(hoursWorked <= 40){
grossPay.add(new Double(hoursWorked * payRate));
}else{
overTime = (40 * payRate)+((hoursWorked - 40) * 1.5 * payRate);
grossPay.add(new Double(overTime));
}
}
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null,e.toString(),"Error",JOptionPane.ERROR_MESSAGE);
} catch (IOException e) {
JOptionPane.showMessageDialog(null,e.toString(),"Error",JOptionPane.ERROR_MESSAGE);
}finally{
if(bufferedFileReader!=null && fileReader != null){
try {
bufferedFileReader.close();
fileReader.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(null,e.toString(),"Error",JOptionPane.ERROR_MESSAGE);
}
}
}
}
public void inIt(){
add(output,BorderLayout.CENTER);
read(file);
for(int i=0; i<name.size();++i){
textContainer.append(name.get(i)+"-Gross Pay: "+Double.toString(grossPay.get(i))+" ");
}
setPreferredSize(new Dimension(400,400));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------
import java.io.File;
import javax.swing.SwingUtilities;
public class Tester {
public static void main (String[] args){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
File file = new File(args[0]);
ImportFile imf = new ImportFile(file);
imf.inIt();
}
});
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.