Link to home
Start Free TrialLog in
Avatar of pogibear77
pogibear77

asked on

Need help modifying a code

I was wondering if anyone can help me modify my code. I need to use a class to store and retrieve employee name, hourly rate, and number of hours worked. I am guessing I will need to use a constructor to initialize the information but I also need a method within the class to calculate the weekly pay. I also want to be able to have the user type STOP which will completely terminate the program. Thanks!
import java.util.*;
public class Payroll
{
	// main method begins execution of Java application
	public static void main( String args[] )
	{
		
		// create Scanner to obtain input from command window
		Scanner input = new Scanner( System.in );
		
		String name = ""; // employee's name
		int number1; // hourly rate
		int number2; // number of hours worked for the week
		int product; // product of number1 and number2
 
		while(name!=null && !name.equalsIgnoreCase("stop"))
 {
	 	number1 =-1;
		number2 = -1;
	 
		System.out.print( "Enter employee's name: "); //prompt user to input name
		name = input.nextLine(); //read employee's name from user's input
		while(number1<0)
		{
		System.out.print( "Enter hourly rate: " ); // prompt user for employee's hourly rate
		number1 = input.nextInt(); // read hourly rate from user's input
		}
		while(number2<0)
		{	
		System.out.print( "Enter hours worked for the week: " ); // prompt user to enter number of hours worked for the week
		number2 = input.nextInt(); // read number of weeks from user's input
		}
		
		product = number1 * number2; // multiply numbers
		
		System.out.print( "The Employee " + name ); // displays employee's name
		System.out.println( " weekly pay is $%d\n"  + product ); // displays weekly pay
 }
 
	} // end method main
 
} // end class Payroll

Open in new window

Avatar of matthew016
matthew016
Flag of Belgium image

Create a class Employee and as field :

private String name;
private int hourlyRate;
private int hoursWorked;
private int weeklyPay;
// need something better than "number1" and "number2"

then you create a constructor, getters, and setters for all of the fields.

The input from the user can stay in the Payroll class and once you have all of the fields information, you call the constructor.
Or can you create an empty Employee object, and call the setter methods as you get the input.

When user types STOP, just do a System.exit(0);
Avatar of pogibear77
pogibear77

ASKER

Hey matthew016,
What do you mean // need something better than number1 and number2?
Do not call your variable names "number1", "number2", "product",
it's not very oriented object programming.
Give them a name in the context
Could you help start me off with the code? I still get confused when adding multiple classes. Thanks
This is what i did so far. what u think?
import java.util.Scanner;
 
public class Employee //class for Employee which will hold the employee data
{
	private String employeeName; // name of employee
	
	// constructor initializes employeeName
	public Employee( String name )
	{
		employeeName = name; // initializes employeeName
	} // end constructor
	
	// method to set the employee name
	public void setEmployeeName( String name )
	{
		employeeName = name; // store the employee's name
	} // end method setEmployeeName
	
	// method to retrieve the employee name
	public String getEmployeeName
	{
		return employeeName;
	} // end method getEmployeeName
	
	private int hourlyRate; // employee's hourly rate
	
	// constructor initializes hourlyRate
	public Employee( String name )
	{
		Hourly Rate = int hourlyRate; // initializes hourlyRate
	} // end constructor
	
	// method to set the hourly rate
	public void setHourlyRate()
	{
		

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of matthew016
matthew016
Flag of Belgium 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
Forget the weeklyPay in the constructor, I forgot this was calculated  :-)

should be:
        public Employee( String name, int hourlyRate, int hoursWorked)
        {
                employeeName = name; // initializes employeeName
                this.hourlyRate = hourlyRate;
                this.hoursWorked = hoursWorked;
        } // end constructor

and I correct setHoursWorked from my snippet:

        public void setHoursWorked (int hoursWorked )
        {
          this.hoursWorked = hoursWorked ;
        }