Hello Js_Sol,
Before we get to the unveiling of my answer I wanted to point out a few things that may help you in the future with regards to programming something like this. I do understand you are probably new to the programming world, so don't take any of these comments to heart.
1) First, instead of using a bunch of println() calls, you can get the new lines by simply adding a newline character in your printed comments. Notice I took them out and instead added some "\n" to the output, this will give you new lines and won't cause extra function calls which will slow your program down.
2) Notice that even though I setup an EmployeeInfo variable, I do not set it to an instance of the object until I have all my information available. No need to set this up at the beginning of the program loop. I will set it only after I can give it valid info. This is known as throwing the object into a valid state.
3) In your EmployeeInfo object, you did a very good job setting this up. Again, to make sure the object is in a valid state I setup a small call to your other constructor giving it valid information. The line that reads this("Unknown",0.00,0.00); is calling your other constructor.
4) In the weekly pay line at the end, notice now that we are using the object's methods to fetch the name and the pay of the specific employee. This will be great after you learn arrays because in this program you could easily store the employee in an array and move through the employees, asking each object to calculate its own pay.
5) Lastly I setup a helper function to prompt for a name. Now this isn't necessary, but the code can be called from different parts of your program and instead of repeating code, we can put it in its own function and call the function instead. This is known as refactoring since we took it out of main flow and made it its own function to then call whenever we please. Notice I make two calls to the function. If I left it in, I would have had to repeat the same lines again. Doing it this way, with its own function, I cut down on repeat code, I reduce the complexity of the project, and I can make it more maintainable for programmers later.
Now for the solution. This is still in a rough state and by no means bulletproof. If you enter letters in for pay or hours, you are going to get errors. Since I don't think you have gotten to error handling yet, I will hold off on showing any of that. This code has been tested and works great. Hopefully it is what you are looking for to get you that push in the right direction.
EmployeeInfo.java
CODE
class EmployeeInfo
{
public String empName;
public double hourlyPay, hoursWorked;
public EmployeeInfo()
{
this("Unknown",0.00, 0.00);
}
public EmployeeInfo(String name, double hourlyPay, double hoursWorked)
{
this.empName = name;
this.hourlyPay = hourlyPay;
this.hoursWorked = hoursWorked;
}
public void setName(String name)
{
this.empName = empName;
}
public String getName()
{
return empName;
}
public void setHourlyPay(double hourlyPay)
{
this.hourlyPay = hourlyPay;
}
public double getHourlyPay()
{
return hourlyPay;
}
public void setHoursWorked(double hoursWorked)
{
this.hoursWorked = hoursWorked;
}
public double getHoursWorked()
{
return hoursWorked;
}
public double getWeeklyPay()
{
return hourlyPay * hoursWorked;
}
}
Payroll.java
CODE
import java.util.Scanner;
public class Payroll
{
public static void main( String args[] )
{
Scanner input = new Scanner ( System.in );
double hourlyPay;
double hoursWorked;
double sum;
System.out.print("\nPayroll Calculation Program\nPlease type STOP to end program\n\n\n");
// Create an EmployeeInfo variable
EmployeeInfo aEmployee;
String empName = askForName(input);
while ( !empName.equalsIgnoreCase("stop") )
{
// Ask for hourly pay
System.out.printf( "Enter Hourly Rate for %s: $",empName );
hourlyPay = input.nextDouble();
while (hourlyPay < 0.00) {
System.out.print("Please re-enter using positive numbers only: $");
hourlyPay = input.nextDouble();
}
// Ask for hours worked
System.out.print ( "Enter Hours Worked: ");
hoursWorked = input.nextDouble();
while (hoursWorked < 0.00) {
System.out.print ("Please re-enter using positive numbers only: ");
hoursWorked = input.nextDouble();
}
// Now setup our employee using the name, pay, and hours worked to initialize it.
aEmployee = new EmployeeInfo(empName, hourlyPay, hoursWorked);
// Display the name and weekly pay using our object.
System.out.printf ("%s's weekly pay is: $%.2f\n\n", aEmployee.getName(), aEmployee.getWeeklyPay());
input.nextLine();
empName = askForName(input);
}
}
// This helper method prompts the user for an employee name.
// It takes in a Scanner object as a parameter.
private static String askForName(Scanner input) {
System.out.print("Enter Employee Name: ");
String empName = input.nextLine();
// If they didn't give us a name, ask again.
while (empName.equals("")) {
System.out.print("Enter Employee Name: ");
empName = input.nextLine();
}
return empName;
}
}
Enjoy!