Now I have read and read on this but i am just stuck. Any help appreciated. I am looking for hints. The code compiles and runs fine but I don't think the variables are being stored in the employee class and I am not sure i am understanding my use of the constructor right. -Thanks
Requirements: I have completed:
Values are checked to ensure they are positive numbers.
Entering stop as the employee name end program.
Having trouble on
Uses a class to store
- name
- hourly rate
- hours worked
Use a constructor to initialize the employee information and a method within that class to calculate the weekly pay.
Thanks for all the help! FINAL ACCEPTED ASSIGNMENT CODE POSTED BELOW A+
/** Payroll3.java | Due 8/09/09
** IT 2015 Java Programming | lazfsh | Axia College - University of Phoenix */
import java.util.Scanner;// Import and use scanner
public class Payroll3 {// main method begins
public static void main( String args[] ) {
// Initialize
boolean stop = false;// Variable for exit procedure
String endProgram = "";// Variable to explain exit procedures blank 1st time
int version = 3;// Version number
// Welcome message
System.out.printf(
"\nWelcome to the Payroll Program v.%d\n", version );//Welcome message
while ( stop == false) {// Run program until stop equals true
Scanner input = new Scanner( System.in );// new Scanner for CL input
Employee Employee = new Employee(); // new Employee constructor
// Prompt for and input name
System.out.printf( "\nEnter name of the employee%s:", endProgram );
Employee.setempName(input.nextLine());// Accept input & Store
if ( Employee.getempName().equals( "stop" )) {// If = end program w/message
System.out.printf( "\n%s\n%s\n\n", "....", "Thanks for using Payroll Program");
stop = true;}
else{// Continue retrieve hourlyRate, hoursWorked & Calculate
do {// Prompt for and input hourlyRate
System.out.printf( "\n%s", "Enter hourly rate: $" );
Employee.sethourlyRate(input.nextFloat());// Accept input & Store
if ( Employee.gethourlyRate() < 1 ) {// Show error for negative #
System.out.printf( "%s", "Enter a positive number.");}
else;{}// Continue
} while ( Employee.gethourlyRate() < 1 );// End loop if positive number recieved
do {// Prompt for and input hoursWorked
System.out.printf( "\n%s", "Enter number of hours worked:" );
Employee.sethoursWorked(input.nextFloat());// Accept input & Store
if ( Employee.gethoursWorked() < 1 ) {// Show error for negative #
System.out.printf( "%s", "Enter a positive number.");}
else;{}// Continue
} while ( Employee.gethoursWorked() < 1 );// End loop if positive number recieved
// Display formatted results
System.out.printf( "\n%s's weekly pay is $%,.2f\nHourly rate ($%,.2f) multiplied by hours worked (%.0f)\n\n...Ready for next employee.\n\n",
Employee.getempName(), Employee.getweeklyPay(), Employee.gethourlyRate(), Employee.gethoursWorked() );
// Debug Line Employee.showVariables();
}// end retrieve hourlyRate, hoursWorked & Calculate
endProgram = ( ", \n(Or type \"stop\" to end the program)" );//explain exit procedure on second empName prompt
}// ends program when stop equals true
}// end method main
}//end class Payroll3
Employee class
/** Employee Class | Initializes and storeds data for employee
Also provides method to calculate weekly pay.
*/
// Import statements
import java.util.Scanner;// Import and use scanner
public class Employee {//Begin Employee class
Scanner input = new Scanner( System.in );// new Scanner for CL input
// Declare instance variables
String empName; // Declare name as string
float hourlyRate; // Declare hourlyRate as float
float hoursWorked; // Declare hoursWorked as float
// constructor initializes employee information
public Employee() { // Initialize (clear) instance variables here
empName = "";
hourlyRate = 0;
hoursWorked = 0;
} // end constructor
// Begin methods
public void setempName(String empName) {// public method to set the employee name
this.empName = empName;}// end method setempName
public String getempName() {// public method to get the employee name
return empName;}// end method getempName
public void sethourlyRate(float hourlyRate) {// public method to set the hourly Rate
this.hourlyRate = hourlyRate;}// end method set hourly Rate
public float gethourlyRate() {// public method to retrieve the hourly Rate
return hourlyRate;} // end method get hourly Rate
public void sethoursWorked(float hoursWorked) {// public method to set the hours Worked
this.hoursWorked = hoursWorked;} // end method set hours Worked
public float gethoursWorked() {// public method to retrieve the hours Worked
return hoursWorked;} // end method get hours Worked
public float getweeklyPay() {// public method to retrieve weekly Pay
return ( hoursWorked * hourlyRate );}// end method get weeklyPay
/* Test line
public void showVariables(){
System.out.printf( "empName=%s, hourlyRate=%s, hoursWorked=%s", this.empName, this.hourlyRate, this.hoursWorked );} */
} // end class Employee