System.out.println("Employee Overtime: " + eStore.getempOvertime());
19 Replies - 2066 Views - Last Post: 13 June 2011 - 03:52 AM
#17
Re: Need help debugging my program
Posted 12 June 2011 - 06:34 PM
I think you have confused yourself a bit too much about what your class should contain and how it should handle things.
I have made an example of how I think the Employee class should look like
I'm going to bed now (almost 4am
), hope you find it useful
I have made an example of how I think the Employee class should look like
public class EmployeeStorage {
private String name;
private double rate;
private double hours;
private double overtimeRate;
public EmployeeStorage( String name, double rate, double overtimeRate, double hours ) {
this.name = name;
this.rate = rate;
this.overtimeRate = overtimeRate;
this.hours = hours;
}
public String getName() {
return name;
}
public double getRate() {
return rate;
}
public void setRate( double rate ) {
this.rate = rate;
}
public double getOvertimeRate() {
return overtimeRate;
}
public void setOvertimeRate( double overtimeRate ) {
this.overtimerate = overtimeRate;
}
public void addHours( double hours ) {
this.hours += hours;
}
public double getTotalPay() {
double totalPay = 0;
if ( hours > 40 ) {
double overtime = 40 - hours;
totalPay = overtime * overtimeRate + hours * rate;
}
else {
totalPay = hours * rate;
}
return totalPay;
}
}
I'm going to bed now (almost 4am
This post has been edited by CasiOo: 12 June 2011 - 06:36 PM
#18
Re: Need help debugging my program
Posted 12 June 2011 - 06:44 PM
LOL You are right 
I have confused myself,
Thank you SOOO much for all your help!
I feel like were married, sometimes you confuse me and want to shake your neck, but then when I get it right finally, i want to bake you some cookies.. LOL
Have a good night....
I have confused myself,
Thank you SOOO much for all your help!
I feel like were married, sometimes you confuse me and want to shake your neck, but then when I get it right finally, i want to bake you some cookies.. LOL
Have a good night....
#19
Re: Need help debugging my program
Posted 12 June 2011 - 09:41 PM
Well I fixed all my errors and my program runs..... But I have No idea how I messed it up, it keeps looping the name 
Can someone please help me make it run through the program.
Main
EmployeeStorage
Can someone please help me make it run through the program.
Main
package Payroll;
/**
*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.
*Be sure that the method considers payment of overtime.
*Once stop is entered as the employee name, the application should terminate.
*Make sure the code is readable and well documented.
* @author Diana
*/
import java.util.Scanner; // program uses Scanner
public class Payroll
{
// main method begins execution of Java application
public static void main( String args[] )
{
// employee's name
String name;
// hourly rate
double rate;
// number of hours worked for the week
double hours;
// product of rate and hours
double totalPay = 0;
//total overtime
double overtime;
//variable to control the loop
boolean newName=true;
// create Scanner to obtain input from command window
Scanner input = new Scanner( System.in );
while(newName)
{
// prompt user to input name
System.out.println( "Enter employee's name or Stop to exit: " );
//read employee's name from user's input
name = input.next();
if (name.equalsIgnoreCase("stop")){
// prompt user for employee's hourly rate
System.out.println( "Enter hourly rate :" );
// read hourly rate from user's input
rate = input.nextDouble();
while (rate<0){
System.out.println("Please enter a possitive number");
rate=input.nextDouble();
}
// prompt user to enter number of hours worked for the week
System.out.print( "Enter hours worked for the week: " );
// read number of weeks from user's input
hours = input.nextDouble();
while (hours<0){
System.out.println("Please enter a possitive number");
hours=input.nextDouble();
}
//passing to the constructor for storing
EmployeeStorage eStore = new EmployeeStorage (name, rate, hours, totalPay);
System.out.println("Employee Name: " + eStore.getEmpName()); //retrieving the value
System.out.println("Employee Overtime: " + eStore.getTotalPay());
// displays employee's name
System.out.printf( "The Employee" , name );
// displays weekly pay
System.out.printf( "Total pay is $%.2f\n" , totalPay );
}//end of else statement
}//end while loop
} // end method main
} // end class Payroll
EmployeeStorage
package Payroll;
/**
*
* @author Diana
*/
class EmployeeStorage {
//declare variables
private String empName;
private Double empRate;
private Double empHours;
private Double empTotalPay;
private Double empOvertime;
// constructor
public EmployeeStorage (String name, Double rate, Double hours, Double totalPay)
{
this.empName = name;
this.empRate = rate;
this.empHours = hours;
this.empTotalPay = totalPay;
}
/**
* @return the empName
*/
public String getEmpName() {
return empName;
}
/**
* @param empName the empName to set
*/
public void setEmpName(String empName) {
this.empName = empName;
}
/**
* @return the empRate
*/
public Double getEmpRate() {
return empRate;
}
/**
* @param empRate the empRate to set
*/
public void setEmpRate(Double empRate) {
this.empRate = empRate;
}
/**
* @return the empHours
*/
public Double getEmpHours() {
return empHours;
}
/**
* @param empHours the empHours to set
*/
public void setEmpHours(Double empHours) {
this.empHours = empHours;
}
/**
* @return the empOvertime
*/
/**
* @param totalPay the totalPay to set
*/
public void setempTotalPay(Double empTotalPay) {
this.empTotalPay = empTotalPay;
}
public Double getTotalPay()
{
if ( empHours > 40 )
{
empOvertime = empHours - 40;
empTotalPay = (40* empRate) + (empRate * empOvertime * 1.5);
// multiply numbers
}
else
{
empTotalPay = empHours * empRate;
}
return empTotalPay;
}
}//end class EmployeeStorage
#20
Re: Need help debugging my program
Posted 13 June 2011 - 03:52 AM
while(newName)
Somewhere in your while-loop you will have to set newName to false, else the loop will just continue
Somewhere in your while-loop you will have to set newName to false, else the loop will just continue

New Topic/Question
Reply



MultiQuote

|