Produce a java class named Employee. In this single class an employee is describ
ID: 668923 • Letter: P
Question
Produce a java class named Employee. In this single class an employee is described with a:
name
and an ArrayList of Payroll objects
The java class needs to contain:
Two constructors, one setting up an Employee with a name only (this is when they currently are not being paid for some reason) and a second setting up an Employee with both name and inital Payroll.
Set and Get methods
an employee counter to keep record of how many employee objects exist
a toString method to generate a textal representation for the empolyee name and also payroll if appllicable
an addPay method that adds an invoice for the current employee
a getPayTotal method that retrurns total pay to the employee (sum of all Payroll)
Payroll class is provided here
public class Payroll
{
private String payrollTag;
private double currentPay;
public Payroll(String payrollTag, double amountDue)
{
this.payrollTag = payrollTag;
this.currentPay = currentPay;
}
public String getPayrollTag()
{
return payrollTag;
}
public double getCurrentPay()
{
return currentPay;
}
}
Explanation / Answer
// Payroll.java
public class Payroll{
private String payrollTag;
private double currentPay;
public Payroll(String payrollTag, double amountDue) {
this.payrollTag = payrollTag;
this.currentPay = currentPay;
}
public String getPayrollTag() {
return payrollTag;
}
public double getCurrentPay(){
return currentPay;
}
}
// Employee.java
import java.util.ArrayList;
public class Employee{
String name;
static int employeeCount = 0;
ArrayList<Payroll> list = new ArrayList<Payroll>();
Employee(String n){
name = n;
employeeCount++;
}
Employee(String n, Payroll p){
name = n;
list.add(p);
employeeCount++;
}
void setName(String n){
name = n;
}
void setPayrollList(ArrayList<Payroll> l){
list = l;
}
String getName(){
return name;
}
ArrayList<Payroll> getPayrollList(){
return list;
}
public String toString(){
return "Name: " + name + " Payroll List: " + list + " ";
}
void addPay(Payroll p){
list.add(p);
}
double getPayTotal(){
double sum = 0;
for(int i = 0; i < list.size(); i++){
sum += list.get(i).getCurrentPay();
}
return sum;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.