Like many of the posts I see out here I have decided to learn Java. It is a bit more than I bargained for. From what I see every class in the world starts with a payroll program, mine is no different.
I have done ok with it up to this point, but I have been asked to take my current working program, and alter it.
import java.util.Scanner; //uses class Scanner
public class Payroll
{
public static void main(String[] args)
{
// create another Scanner to obtain name from command window
Scanner input2 = new Scanner(System.in);
char eName; // name of employee
// create Scanner to obtain input from command window
Scanner input = new Scanner(System.in);
double rate; // hourly rate of employee
double hours; // hours worked by employee
double result; // product of rate and hours
System.out.print("Enter Employee's Name or Enter STOP to Exit: "); // prompt for name
String name = input2.nextLine(); //name input from user
while (!(name).equalsIgnoreCase("STOP"))
{
System.out.print("Enter Employee's Hourly Pay Rate: "); // prompt for rate
rate = input.nextDouble(); // rate input from user.
while (rate <= 0)
{
System.out.print("Rate Must Be Greater Than Zero. Enter Rate: ");
rate = input.nextDouble(); // rate loop from user.
} // End while
System.out.print("Enter Hours Employee Worked This Week: "); // prompt for hours worked
hours = input.nextDouble(); // hours student worked this week.
while (hours <= 0)
{
System.out.print("Hours Worked Must Be Greater Than Zero. Enter Hours: ");
hours = input.nextDouble(); // Hours loop from user
} // End while
result = rate * hours; // figures students salary
System.out.print(name); // display name
System.out.printf("'s salary for the week is %c%.2f\n", '$', result); //display salary
System.out.print("Enter Employee's Name or Enter STOP to Exit: "); // internal loop prompt
name = input2.nextLine(); //name input from user
} // End While
System.out.print("Ending Program.");
}
}
I have been told I need two classes, a constructor to initialize the Employee object, and methods to compute weekly pay and such. I am confused and a bit lost at the moment.
This is what I have tried: (first class)
public class Payroll
{
public static void main(String[] args)
{
// create Employee object nextEmployee
// pass employee name to constructor
Employee nextEmployee = new Employee("stupid");
nextEmployee.displayMessage(); // display message
}
} // end class Payroll
and (second class)
import java.util.Scanner; //uses class Scanner
public class Employee
{
private String employeeName; // name of employee
// constructor inititalizes employeeName
public Employee(String eName)
{
employeeName = eName; // initializes employeeName
} // end constructor
// method to set the employee's name
public void setEmployeeName(String eName)
{
employeeName = eName; // store the employee name
} // end method setEmployeeName
// method to retrieve the course name
public String getEmployeeName()
{
return employeeName;
} // end method getEmployeeName
// display message
public void displayMessage()
{
// getEmployeeName gets the name of the employee
System.out.printf("Please Enter Employee's Name: \n%s!\n\n",
getEmployeeName() );
} //end method display message
}
The resulting program compiles, but will only work if I actually put something in the parenthesis of the first class. I still need the program to prompt and store whatever name is entered. I want to get the name part working and understood before I move on to the math, but I am completely stumped.
Can anyone offer any direction?