Link to home
Start Free TrialLog in
Avatar of Kody-Burg
Kody-BurgFlag for United States of America

asked on

Java Method Question - Returning Multiple Println Statements

Hello Everyone,

I am learning to program in Java. Right now, I am learning the basics and creating programs that run off of the command line (accept input through scanner, return values through println). My latest project involves a program that computes employee payroll. It involves two files, EmployeePayroll, which contains the methods that make everything work, and EmployeePayrollUser, which accepts input from the scanner method and displays an employee payroll report. I have the entire program working, but the teacher of the class would like me to create a method in EmployeePayroll called displayEmployeeData() to display my payroll report by calling on that method in EmployeePayrollUser instead of just putting my println lines in EmployeePayrollUser itself. I think that I just need to know what type my method needs to be. Here is the code for the method:

 
public ??? displayEmployeeData()
{
		System.out.println("\nHere is the payroll report for " + account.getName() + ":" );
		System.out.println(" ");
		
		System.out.println(account.getName() + " works " + account.getHours() + " hour(s) per week.");
		System.out.println(" ");
		
		account.calculateWeeklyPay();
		System.out.println(account.getName() + " is paid $" + account.getWeeklyPay() + " per week before taxes are withheld.");
		System.out.println(" ");
		
		account.addYearlyHours();
		System.out.println(account.getName() + " works " + account.getYearlyHours() + " hour(s) per year (based on 52 weeks).");
		System.out.println(" ");
		
		account.calculateGrossPay();
		System.out.println(account.getName() + " grosses $" + account.getGrossPay() + " per year.");
		System.out.println(" ");
		
		account.calculateNetPay();
		System.out.println(account.getName() + " nets $" + account.getNetPay() + " per year.");
		System.out.println(" ");
}

Open in new window


1) how do I make this work?
2) how to I call upon this in my EmployeePayrollUser.java file?

I can attach both full java files if you need to see them to help.

Thank you for your help in advance!

Kody
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

you will have to pass the employee identification (id) to displayEmployeeData() method in EmployeePayrollUser class, and it will return you the EmployeePayrollUser object. Then you will print its values just like you would have done in EmployeePayrollUser class


And yes, seeing both files can  help more
Avatar of Kody-Burg

ASKER

Here is the .java file for EmployeePayroll:
 EmployeePayroll.java

Here is the .java file for EmployeePayrollUser:

 EmployeePayrollUser.java

As of right now, if you run EmployeePayrollUser, it will print the report to the screen. My objective is to move those println statements to EmployeePayroll under the displayEmployeeData() method and tell EmployeePayrollUser to get the report from there.
ASKER CERTIFIED SOLUTION
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
EmployeePayroll compiled just fine, but EmployeePayrollUser bombed with the following error:

EmployeePayrollUser.java:49: displayEmployeeData(EmployeePayroll) in EmployeePayroll cannot be applied to ()
      account.displayEmployeeData();
                   ^
1 error
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Typically, the proper way is to implement toString in the appropriate class. In this case, it's EmployeePayrollUser.

The calling class would execute

System.out.println(employee);

where 'employee' is an instance of EmployeePayrollUser

Here's a stub implementation you can fill out
public String toString() {
        final String LF = System.getProperty("line.separator");
        StringBuilder sb = new StringBuilder();
        sb.append(getName()).append(":");
        sb.append(LF);

        sb.append(getName()).append(" works ").append(getHours())
          .append(" hour(s) per week.");

        return sb.toString();
    }

Open in new window

Thank you. For some reason when I tried the same thing yesterday, I couldn't get it to work. You have saved me from my frustration.