Background:
Modify the Payroll Program so that it uses a class to store and retrieve the employee's
name, the hourly rate, and the number of hours worked. Use a constructor to initialize the
employee information, and a method within that class to calculate the weekly pay. Once
stop is entered as the employee name, the application should terminate. Make sure the
program maintains all the functionality required in previous assignments and your source
code is readable and well documented. Use feedback you have received from the
instructor to make any needed modifications.

here is what I have in the new class

package pay_1;




public class Employee {
	
	public static String name1;
	public static String name2;
	public static double hoursWorked;
	public static double payRate;
	
	public Employee(String name1, String name2, double hoursWorked,
			double payRate) {
		super();
		Employee.name1 = name1;
		Employee.name2 = name2;
		Employee.hoursWorked = hoursWorked;
		Employee.payRate = payRate;
	}
	
	public String getName1() {
		return name1;
	}
	public void setName1(String name1) {
		Employee.name1 = name1;
	}
	public String getName2() {
		return name2;
	}
	public void setName2(String name2) {
		Employee.name2 = name2;
	}
	public double getHoursWorked() {
		return hoursWorked;
	}
	public void setHoursWorked(double hoursWorked) {
		Employee.hoursWorked = hoursWorked;
	}
	public double getPayRate() {
		return payRate;
	}
	public void setPayRate(double payRate) {
		Employee.payRate = payRate;
	}
	
	

}

I have already completed the class to preform the calculations it reads:

package pay_1;

import java.util.Scanner;

public class Calcu {
	

	// Program execution starts here
	public static void main ( String [] args ) 
	{ 
		// Start Scanner
		Scanner input = new Scanner (System.in);
		String name;
		String name2;
		double payRate;
		double hoursWorked;
		
		
		 
		
		// Begin User Input
		System.out.println("Welcome:-)\nEnter employee's first name\nor type stop to quit");
	    name = input.next();
	    
	    // loop until stop begins
	    while ( !name.equals("stop") )
	    {
	    	  
		        	 System.out.println("Enter employee's last name");
		        	 name2 = input.next();// read last name from user
		        	 
		        	 System.out.println("Enter Pay Rate\n$");
		             payRate = input.nextFloat(); // read hourly rate from user
		             
		             // start pay rate while loop
		             while (payRate <= 0) 
		             { 
		            	 System.out.print( "Please use positive numbers: "); // prompt for input of positive number
		            	 payRate = input.nextFloat(); // read hourly rate from user
		             } 
		             
		             System.out.println("Enter number of hours worked");	    
		             hoursWorked = input.nextFloat(); // read hourly rate from user
		             
		             // start hours worked loop for positive number
		             while (hoursWorked <= 0) 
		             { 
		            	 System.out.print("please use positive numbers"); // prompt for positive hourly rate
		            	 hoursWorked = input.nextFloat(); //  user input
		             } 				
				
		             // Calculations Begin
		             Object product = payRate * hoursWorked;
		
		             //Display Results
		             System.out.println(name);//print first name
		             System.out.println(name2);//print last name
		             System.out.printf("total income this week is $%.2f\n", product);
		             
		             //1st prompt for user input in loop
		             System.out.println("Enter employee's first name\nor type stop to quit");//
		             name = input.next();
	   
		
	   }//end while
	                 // goodbye message
	                 System.out.println("Thank you for using my payroll program.");
	 }// End main method 

}

When I run the program; how do I store and retrieve the data as directed above?

Recommended Answers

All 9 Replies

Well, I guess that depends on what you have covered in class so far. My guess would be simple file I/O with a BufferedWriter.

Review your class notes.

Thanks for your reply, and this is my fourth week in this class. I am still lost my professor told me I should instantiate the Employee object and call the constructor with the name, rate, hour. Call the Employee object's WeeklyPay method. This is where I am lost should both of these classes be combined somehow. I have been working on this project tirelessly all week and I feel at this point I am just butchering my code. Any feed back is greatly appreciated. Thx in advance

Yes, you need to create a method on the Employee class that uses the info it contains to calculate and return the weekly pay.

Your main() method should create a new Employee with the info that you are getting from the keyboard and then call the calcWeeklyPay() method that you added.

Also note that the variables in your Employee class should not be static, they should be private.

Yes, you need to create a method on the Employee class that uses the info it contains to calculate and return the weekly pay.

Your main() method should create a new Employee with the info that you are getting from the keyboard and then call the calcWeeklyPay() method that you added.

Also note that the variables in your Employee class should not be static, they should be private.

I tried what you suggested to no avail. When I remove the static modifiers my IDE lights up like a christmass tree with compile errors, and when I actually run the program it runs but it doesnot store the data. I recieve a print format of count = 1, null, null, 0.00. Any advice?

I tried what you suggested to no avail. When I remove the static modifiers my IDE lights up like a christmass tree with compile errors, and when I actually run the program it runs but it doesnot store the data. I recieve a print format of count = 1, null, null, 0.00. Any advice?

Of course it shows many errors, because you are also accessing all of them statically in your methods

public void setName1(String name1) {
Employee.name1 = name1;
}

You need to use them as instance variables

public void setName1(String name1) {
this.name1 = name1;
}

Of course it shows many errors, because you are also accessing all of them statically in your methods

public void setName1(String name1) {
Employee.name1 = name1;
}

You need to use them as instance variables

public void setName1(String name1) {
this.name1 = name1;
}

I tried that still no luck here is how my code looks now

package pay_1;

import java.util.Scanner;


public class Employee {
	
	String name1;
	String name2;
	double payRate;
	double hoursWorked;
	double weeklyWages;
	public static int count;
	
	public String getName1() {
		return name1;
	}
	public void setName1(String name1) {
		this.name1 = name1;
	}
	public String getName2() {
		return name2;
	}
	public void setName2(String name2) {
		this.name2 = name2;
	}
	public double getPayRate() {
		return payRate;
	}
	public void setPayRate(double payRate) {
		this.payRate = payRate;
	}
	public double getHoursWorked() {
		return hoursWorked;
	}
	public void setHoursWorked(double hoursWorked) {
		this.hoursWorked = hoursWorked;
	}
	public double getWeeklyWages() {
		return weeklyWages;
	}
	public void setWeeklyWages(double weeklyWages) {
		this.weeklyWages = weeklyWages;
	}
	
	public static int getCount() {
		return count;
	}
	public static void setCount(int count) {
		Employee.count = count;
	}
	
		 
	public Employee(String name1, String name2, double payRate,
			double hoursWorked, double weeklyWages) {
		super();
		this.name1 = name1;
		this.name2 = name2;
		this.payRate = payRate;
		this.hoursWorked = hoursWorked;
		this.weeklyWages = hoursWorked * payRate;
		count = 0;
	}
	
	Employee employee = new Employee(name1, name2, payRate, hoursWorked, weeklyWages);
	
	 public static void main(String[] args) { 
		 
		 System.out.printf("System.out.printf( Employees before instantiation: %d\n",getCount(),getHoursWorked(),getName1(),getName2(),
					getWeeklyWages() );
		 
		
		 
		// Start Scanner
	 		Scanner input = new Scanner (System.in);
	 		
	 		
	 		String name1;
	 		String name2;
	 		double payRate ;
	 		double hoursWorked;
	 		
	 		
	 	
	 		 
	 		
	 		// Begin User Input
	 		System.out.println("Welcome:-)\nEnter employee's first name\nor type stop to quit");
	 	    name1 = input.next();
	 	    
	 	  
	 	        
	 	    
	 		// loop until stop begins
	 	    while ( !name1.equals("stop") )
	 	    {
	 	    	
	 		        	 System.out.println("Enter employee's last name");
	 		        	 name2 = input.next();// read last name from user
	 		        	count++;
	 		        	 System.out.println("Enter Pay Rate\n$");
	 		             payRate = input.nextFloat(); // read hourly rate from user
	 		             
	 		             // start pay rate while loop
	 		             while (payRate <= 0) 
	 		             { 
	 		            	 System.out.print( "Please use positive numbers: "); // prompt for input of positive number
	 		            	 payRate = input.nextFloat(); // read hourly rate from user
	 		             } 
	 		             
	 		             System.out.println("Enter number of hours worked");	    
	 		             hoursWorked = input.nextFloat(); // read hourly rate from user
	 		             
	 		             // start hours worked loop for positive number
	 		             while (hoursWorked <= 0) 
	 		             { 
	 		            	 System.out.print("please use positive numbers"); // prompt for positive hourly rate
	 		            	 hoursWorked = input.nextFloat(); //  user input
	 		             } 				
	 				
	 		             // Calculations Begin
	 		             Object product = payRate * hoursWorked;
	 		
	 		             //Display Results
	 		             System.out.println(name1);//print first name
	 		             System.out.println(name2);//print last name
	 		             System.out.printf("total income this week is $%.2f\n", product);
	 		            
	 		            
	 		           
	 		             //1st prompt for user input in loop
	 		             System.out.println("Enter employee's first name\nor type stop to quit");//
	 		             name1 = input.next(); 
	 		         
	 					
	 		             
	 		             
	 		            
	 		             
	 		     	    
	 					
	 		           
	 	   
	 		
	 	   }//end while
	 	  
	 	                 // goodbye message
	 	                 System.out.println("Thank you for using my payroll program.");
	 	                return String.format("System.out.printf( Employees after instantiation: %d\n, %s %s %d %d %d",
	 	           			getCount, getName1,getName2(), getPayRate() getHoursWorked, getWeeklyWages() );
	}
}

any suggestions on where I should go next?

Thanks for the assistance; I solved the problem by deadline.

If anyone reads this thread thinks they attend the same online school as me let me be the first one to say our textbook sucks. I had to read a different book to understand how to create a functional payroll program for pt3. This algorythim should help you sucessfully complete the assignment.

1st.java
employee class{
//set all variables
//create a constructor
// get and set()'s
//create a () for calculating weekly pay
}

2nd.java
payroll pt 2 class{

//payroll pt 2 code ^this will require modification^
//create an employee object(set parameters)
//display results

}

The input doesn't have to be saved just reprinted in the console upon completion of the program and the next prompt for stop or name input. Hope this helps and good luck :) FYI my IDE = eclipse.

:icon_exclaim:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.