Need help passing data between classes
I am working on an assignment for a java programming class and have a logic error that I can't figure out. I will post the code to both of the classes at the bottom. The first class asks the user to input an employee's name, rate of pay, and hours worked. These variables are passed to another class where the weekly pay is calculated. Ideally, the weekly pay will be returned in a printf statement. What is happening is that I get $0.00 no matter what I put in for hours and pay rate. Can anyone help?
import java.util.Scanner; // scanner will be used to capture input from command screen
public class Payroll3
{
public static void main( String args [] )
{
String employeeName; // Employee's name
double hours; // number of hours worked
double hourlyRate; // employee's hourly rate of pay
double weeklyPay; //calculation of employee's weekly pay
//create scanner to obtain input from command window
Scanner input = new Scanner( System.in );
System.out.print( "Enter the employee's name. Enter 'stop' to quit:" ); //prompt
employeeName = input.next (); // read employee name
while ( !employeeName.equalsIgnoreCase ("stop") )
{
System.out.print( "Enter the number of hours the employee worked: " ); // prompt
hours = input.nextDouble (); // read number of hours
while ( hours < 0 ) // verify a positive value for hours worked
{
System.out.print ( "Please enter a positive value for hours worked:" );// error message
hours = input.nextDouble (); // new value for hours
}
System.out.print( "Enter the employee's hourly rate of pay: $" ); // prompt
hourlyRate = input.nextDouble (); // read employee rate of pay
while (hourlyRate < 0 ) // verify a positive value for hourly rate
{
System.out.print( "Please enter a positive value for hourly rate of pay:" );// error message
hourlyRate = input.nextDouble ();// new value for hourly rate
}
Employee myEmployee = new Employee( employeeName, hourlyRate, hours );
System.out.printf( "Employee %s", employeeName ); // display employee name
System.out.printf( " earned $%.2f\n", myEmployee.getWeeklyPay() ); // display weekly pay
System.out.println(); // print a blank line to separate employees
System.out.print( "Enter the employee's name. Enter 'stop' to quit:" ); //prompt
employeeName = input.next (); // read employee name
}
System.out.println(); // print a blank line between employee's and exit message
System.out.println( "Thank you for using the Payroll Program!" ); // exit message
} // end method main
} // end class Payroll2
public class Employee // Class Employee to hold variables and calculate pay
{
String name; // instance variable for employee name
double rate; // instance variable for pay rate
double hoursWorked; // instance variable for the number of hours worked
public Employee( String name, double rate, double hoursWorked ) // create constructor
{
name = null; // pass employee's name
rate = 2; // pass rate of pay
hoursWorked = 2; // pass number of hours worked
} // end constructor
public void setName ( String employeeName ) // method to set employee name
{
name = employeeName; // store employee name
} // end method setName
public String getName() // method to retrieve employee name
{
return name;
} // end method getName
public void setRate ( double hourlyRate ) // method to set rate of pay
{
rate = hourlyRate; // store rate of pay
} // end method setRate
public double getRate() // method to retrieve rat of pay
{
return rate;
} // end method getRate
public void setHoursWorked ( double hours ) // method to set number of hour worked
{
hoursWorked = hours; // store number of hours worked
} // end method setHoursWorked
public double getHoursWorked() // method to retrieve number of hours worked
{
return hoursWorked;
} // end method getHoursWorked
public double getWeeklyPay() // method to retrieve weekly pay
{
return hoursWorked * rate;
} // end method getWeeklyPay
} // end class Employee