0

I'm doing an assignment on creating an STP - A Payroll Management System for a company. I was given 11 classes (Employee, Employees, Employer, Employers, STP, STPLog, Report, In, Session, Utils) I just finished initializing data members in Employee and Employers but in STP, there are members which require calculation from the members in Employee. For example: totalWages = sum of 'income' in Employee. Im just wondering if it's possible to do that calculation in STP constructor? And the way to do so? Because I can do that in Employee with its data members. Here's my code:

//STP.java//
import java.util.ArrayList;
import java.util.Date;

public class STP {
    private double totalWages;
        
public STP (double totalWages){
   this.totalWages = totalWages; 
>                    ---------^ part need to calculate 
  }


}
//Employee.java//
public class Employee {
    
    private int hours;
    private double payPerHour;
    private double income;
    

    public Employee (int hours, double payPerHour) {
        
        this.hours = hours;
        this.payPerHour = payPerHour;
        this.income = hours*payPerHour*52;
        
    }
    
    
}

Mimi
  • 1
  • 4
  • It seems like you want many employees but you have not defined an array. Once you have an array you will need a `for` loop inside the STP constructor to what you want. – JoelFan Oct 13 '21 at 04:44
  • Yes, you can do a calculation in a constructor. I assume that `Employees` contains a list of `Employee`. I also guess that you need to iterate through the list, get the `income` for each `Employee` and add them together. If you [edit] your question and post a [mcve] then I may be able to show you the relevant code. – Abra Oct 13 '21 at 04:49
  • @Abra Hi I have edited to the one i need, I'm new to all this so i don't know if that's correct, my apologise if so. – Mimi Oct 13 '21 at 05:20
  • I see that you are having trouble posting a [mcve]. What is not clear? Post the minimal amount of code that I can copy and compile and run. Just indicate which part (of the code in your minimal, reproducible example) you need help with. – Abra Oct 13 '21 at 06:55

0 Answers0