Your company has asked you to develop a report detailing payroll information wit
ID: 3674001 • Letter: Y
Question
Your company has asked you to develop a report detailing payroll information with the following format:
Acme Corporation
Number of Employees: 3
Average Salary: 53,502.32
Annual Total: 160,506.95
Name Rank Salary
Jones, William B2 42,500.00
Baker, Susan A3 65,000.00
Baer, Teddy B4 53,006.95
Develop an Employee class that contains first name, last name, rank, and salary. The class should have the following methods:
parameterized constructor
getters necessary for printing the report
formatName() method that returns a String with the name in the format last, first
Employee information is stored in a sequential text file named employees.txt. The file has the format:
employees.txt file: text file with the following fields separated by spaces:
Fields
Description
First Name
string
Last Name
string
Rank
string
Salary
double
Note: Names will not contain spaces.
Write a Java program named Prog5 that reads employee information from the file and places a corresponding Employee object into an array. After the file has been read, print the report shown above using proper formatting.
Acme Corporation
Number of Employees: 3
Average Salary: 53,502.32
Annual Total: 160,506.95
Name Rank Salary
Jones, William B2 42,500.00
Baker, Susan A3 65,000.00
Baer, Teddy B4 53,006.95
Explanation / Answer
Empolyee.java
public class Employee {
private String firstName;
private String lastName;
private String rank;
private double salary;
public Employee(String firstName, String lastName, String rank, double salary) {
this.firstName = firstName;
this.lastName = lastName;
this.rank = rank;
this.salary = salary;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getRank() {
return rank;
}
public double getSalary() {
return salary;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setRank(String rank) {
this.rank = rank;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String formatName() {
return lastName + ", " + firstName;
}
}
Main.java
import java.util.*;
import java.io.*;
import java.lang.Double;
public class Main {
public static void main(String[] args) throws IOException {
Scanner reader = new Scanner(new File("employees.txt"));
ArrayList<Employee> employees = new ArrayList<>();
// read input
String line;
while ((line = reader.nextLine()) != null) {
String[] info = line.split("\s+");
Employee newEmployee = new Employee(info[0], info[1], info[2], Double.parseDouble(info[3]));
employees.add(newEmployee);
}
double sum = 0.0;
for (int i = 0; i < employees.size(); ++i) {
sum += employees.get(i).getSalary();
}
System.out.println("Acme Corporation");
System.out.println("Number of Employees: " + employees.size());
System.out.println("Average Salary: " + Double.toString(sum / employees.size()));
System.out.println("Annual Total: " + Double.toString(sum));
System.out.println("Name Rank Salary");
for (int i = 0; i < employees.size(); ++i) {
Employee current = employees.get(i);
System.out.println(current.formatName() + " " + current.getRank() + " " + Double.toString(current.getSalary()));
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.