Newbie needs help again.
Hello, i'm really new to Java and have what is probably a really simple question. How do I get the Payroll.java class to use the EMPData.java class to store and retrieve the employee's name, hourlyrate and hours worked.? I think I have created the EMPData class correctly using a constructor. I'm just kind of lost on the rest. Here's the code....
// Payroll.java
// Payroll program that calculates an employee's weekly pay.
import java.util.Scanner; // program uses class Scanner
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 );
double hourlyRate = 0.0; //employee's pay to mulitple
double hoursWorked = 0.0; //employee's hours to multiple
double pay; // total of hourlyRate and hoursWorked
String name = ""; //sets variable name
while ( !name.equals( "stop" ) ) //ends program after entering stop for the employee name
{ //starts while loop
System.out.print( "Enter employee's name - " ); //prompt
System.out.flush(); //empties buffer before you input text.
name = input.nextLine(); //read employee's name
System.out.print( "What is their hourly rate? $" ); //prompt
hourlyRate = input.nextDouble(); //read first number from user
System.out.flush(); //empties buffer before you input text.
while ( hourlyRate < 0.0 ) //starts while loop to make sure a positive number is entered.
{
System.out.print( "Input must be a positive number. What is their hourly rate? $" ); //prompt
hourlyRate = input.nextDouble(); //read first number from user
input.nextLine(); //prompt user for input
}
System.out.print( "How many hours did they work this week? " ); //prompt
hoursWorked = input.nextDouble(); //read second number from user
input.nextLine(); //prompt user for input
while ( hoursWorked < 0.0 ) //starts while loop to make sure a positive number is entered.
{
System.out.print( "Input must be a positive number. How many hours did they work this week? " ); //prompt
hoursWorked = input.nextDouble(); //read second number from user
input.nextLine(); //prompt user for input
}
pay = hourlyRate * hoursWorked; //calculates weekly pay
System.out.printf( "Employee - %s\n", name ); //display employee's name
System.out.printf( "Their pay for the week is $%.2f\n", pay ); //display total pay for the week
} //ends while loop
} //end method main
}//end class Payroll
public class EMPData // Start of public class EMPData that stores employee's name, rate and hours worked.
{
private String name; //get string name
private double hourlyRate;// get double value for hourlyrate
private double hoursWorked;//get double value for hoursworked
public EMPData(String nameStr,double rate,double hours)
{ // constructor to initialize the fields
this.name = nameStr;
this.hourlyRate = rate;
this.hoursWorked = hours;
}
public double getWeeklyPay()
{
return this.hourlyRate * this.hoursWorked;
}
} //end public class EMPData
Thanks much....
> Hello, i'm really new to Java and have what is
> probably a really simple question. How do I get the
> Payroll.java class to use the EMPData.java class to
> store and retrieve the employee's name, hourlyrate
> and hours worked.?
Well, you'll have to pass an instance of EMPData to write to it somewhere.
You'll have to pass some kind of identity to Payroll and have that method return the EMPData.
You'll have to decide what "store" and "retrieve" mean. To/from a file? An XML stream? A relational database? Can't answer the first question without answering the second.
Use [code][/ code] tags.
I don't think these classes look very useful. What's all that I/O doing in the Payroll class?
Where are the methods besides main? This Payroll class is useless.Where are the private data members like List of employees?
Your EMPData is almost as bad.
%
This is what my assignment states,
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.
We had to previous create a Payroll class so it would prompt to enter and employee's name hours and rate and calculate their weekly pay. The next part was to do while loops to ensure that positive numbers were entered. Now we have to what I stated above and i'm lost. :(
> This is what my assignment states,
>
> 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.
>
> We had to previous create a Payroll class so it would
> prompt to enter and employee's name hours and rate
> and calculate their weekly pay.
No, you have to write a program that prompts for these things. But your Payroll class is instantly less usable the moment you stuff if full of I/O code.
> The next part was to
> do while loops to ensure that positive numbers were
> entered. Now we have to what I stated above and i'm
> lost. :(
That's just Java language stuff. Sounds like you haven't studied the language very hard yet.
What don't you understand? while loops? positive numbers? numbers? what?
Why don't you try writing more than you have and see if it works? Nobody here will do it for you. You ask more direct questions, less open ended than "I don't get it" or "I'm lost" and we answer.
%
Let's look at that Employee class:
// I'd recommend that you lose those awful comments like "start of public class..." Write a javadoc header instead.
// Name that class Employee instead of EMPData. Much more readable than your comment.
public class EMPData // Start of public class EMPData that stores employee's name, rate and hours worked.
{
private String name; //get string name
private double hourlyRate; // get double value for hourlyrate
private double hoursWorked; //get double value for hoursworked
public EMPData(String nameStr,double rate,double hours)
// Lose that worthless comment.
// What if the name passed in is blank or null? What if the rate or hours are negative?
// What do you propose to do to notify the user? (Hint: throw IllegalArgumentException
{ // constructor to initialize the fields
this.name = nameStr;
this.hourlyRate = rate;
this.hoursWorked = hours;
}
public double getWeeklyPay()
{
return this.hourlyRate * this.hoursWorked;
}
// no getter for name?
// no read or write access to anything else?
// lose that awful comment.
} //end public class EMPData
%
Shouldn't a Payroll class have a List of Employees? Wouldn't you expect to see a method to calculate the sum of all the salaries? Methods to find/add/remove an Employee from Payroll?%
Here's a hint on that Payroll class:
public class Payroll implements Serializable
{
private List<Employee> employees;
public Payroll() { this(new ArrayList()); }
public Payroll(List employees)
{
this.employees = new ArrayList(employees);
}
public void addEmployee(Employee e)
{
this.employees.add(e);
}
public void removeEmployee(Employee e)
{
this.employees.remove(e);
}
public Employee findByName(String name)
{
Employee employeeByName = null;
for (Employee e : this.employees)
{
if (name.equals(e.getName())
{
employeeByName = e;
break;
}
}
return employeeByName;
}
public double calculatePayroll()
{
double payroll = 0.0;
// How would you code this?
return payroll;
}
}
%
Might have caused you a problem with that Java 5 notation for List<Employee>. Just make it a List.%