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

pogibear77Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

matthew016Commented:
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);
0
pogibear77Author Commented:
Hey matthew016,
What do you mean // need something better than number1 and number2?
0
matthew016Commented:
Do not call your variable names "number1", "number2", "product",
it's not very oriented object programming.
Give them a name in the context
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

pogibear77Author Commented:
Could you help start me off with the code? I still get confused when adding multiple classes. Thanks
0
pogibear77Author Commented:
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

0
matthew016Commented:
Remove the Scanner import.
Your constructor should initialize all of the necessary fields.


public class Employee //class for Employee which will hold the employee data
{
        private String employeeName; // name of employee
        private int hourlyRate; // employee's hourly rate
        private int hoursWorked; 
        private int weeklyPay; 
        
        // constructor initializes employeeName
        public Employee( String name, int hourlyRate, int hoursWorked, int weeklyPay )
        {
                employeeName = name; // initializes employeeName
                this.hourlyRate = hourlyRate;
                this.hoursWorked = hoursWorked;
                this.weeklyPay = weeklyPay ;
        } // 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
        
        // method to set the hourly rate
        public void setHourlyRate(int hourlyRate)
        {
          this.hourlyRate = hourlyRate;
        }
        public void getHourlyRate()
        {
          return hourlyRate;
        }
        public void setHoursWorked (int hourlyRate)
        {
          this.hoursWorked = hoursWorked ;
        }
        public void getHoursWorked ()
        {
          return hoursWorked ;
        }
 
        public void getWeeklyPay ()
        {
          return hoursWorked * hourlyRate ;
        }
        
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
matthew016Commented:
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 ;
        }
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.