Write a simple payroll program that would: Read employee’s information from a di
ID: 3879447 • Letter: W
Question
Write a simple payroll program that would:
Read employee’s information from a disk file (attached with email). The text file includes records of employees. Each record includes: employee’s name (30 bytes), employee’s id (5 bytes), hourly rate (int), hours worked (int).
Calculate the wage for each employee based on the hourly rate and the hours worked.
A.Smith 20001 25 40
T.Philip 20002 20 35
S.LOng 20003 15 50
G.Santos 20004 30 30
F.Farkas 20005 22 55
Display on the screen the employee’s name and wage in the order of wages.
Suggestions: array of structures should be used.
Explanation / Answer
If you have any problem with the answer just let me know in the comments and I'll try to solve it as soon as possible.
The code is in Java and reads from wages.txt. Please change the location in code Content of wage.txt are:
A.Smith 20001 25 40
T.Philip 20002 20 35
S.LOng 20003 15 50
G.Santos 20004 30 30
F.Farkas 20005 22 55
Code :
package test;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Rajesh
*/
public class Wage {
public static void main(String[] args) throws IOException {
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader("C:\Users\Rajesh\Desktop\Wage.txt"));
String line =null;
int max_wage = 0;
while ((line = bufferedReader.readLine())!= null) {
StringBuilder sb = new StringBuilder();
String arr[] = line.split(" ");
int hours_worked = 0;
int hourly_rate = 0;
for (int i = 0;i < arr.length;i++) {
if (i == arr.length-1 ) {
hours_worked = Integer.parseInt(arr[i]);
// System.out.println("Hours worked : "+hours_worked);
} else if (i == arr.length-2){
hourly_rate = Integer.parseInt(arr[i]);
// System.out.println("Hourly Rate : "+hourly_rate);
}else if(i == 0){
sb.append(arr[i]).append(" ");
}
}
int total_wage = hours_worked*hourly_rate;
System.out.println("Employee : "+sb.toString()+" Total Wage : "+total_wage);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Wage.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.