Modify payroll program so that it uses a class to store and retrive the employee's name, 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 code is readable and well documented.
This is what I have done is this correct, as per the parameters above: - import java.util.ArrayList;
-
import java.util.Scanner; // program uses class Scanner
-
-
class EmployeeData {
-
EmployeeData(String newName, float newHourlyRate, float newHoursWorked) {
-
name = newName; hourlyRate = newHourlyRate; hoursWorked = newHoursWorked;
-
}
-
-
public String getName() { return name; }
-
public float getWeeklyPay() { return hourlyRate * hoursWorked; }
-
-
private String name;
-
private float hourlyRate, hoursWorked;
-
} // end of EmployeeData class
-
-
public class payroll4a
-
{
-
private String EmployeeData; // employee information for this Payroll
-
-
private static int ArrayList;
-
-
private static int i;
-
-
// main method begins execution of java application
-
public static void main( String args[] )
-
{
-
ArrayList employees = new ArrayList (); // employee information to be stored for all employees
-
-
boolean stop = false; // This flag will control whether you exit the loop below
-
// Loop until user types "stop" as the employee name:
-
while (!stop)
-
-
{
-
// create scanner to obtain input from command window
-
Scanner input = new Scanner ( System.in );
-
-
System.out.println(); // outputs a blank line
-
System.out.println( "Please enter the employee name or 'stop' to end program: " ); // prompt
-
String empName = input.nextLine(); // read employee name
-
-
if ( empName.compareTo("stop") == 0) // Check whether user indicated to stop program
-
{
-
System.out.println( "Program ended." );
-
stop = true;
-
}
-
else
-
{
-
// User did not indicate to stop, so continue reading info for this iteration:
-
EmployeeData employee;
-
float hourlyRate; // first number to multiply
-
float hoursWorked; // second number to multiply
-
float weeklyPay; // product of hourlyRate and hoursWorked
-
// If we are at the end of input then NoSuchElement;
-
// If there is still input left then InputMismatch
-
{
-
-
System.out.println( "Please enter hourly rate: $" ); // prompt
-
hourlyRate = input.nextFloat(); // read hourly rate from user
-
if (hourlyRate <= 0) // prompt until a positive value is entered
-
-
{
-
System.out.println( "Hourly rate must be a positive value. " +
-
"Please enter the hourly rate again: $" ); // prompt for positive value for hourly rate
-
hourlyRate = input.nextFloat(); // read hourly rate again
-
-
}
-
-
System.out.println( "Please enter hours worked: " ); // prompt
-
hoursWorked = input.nextFloat(); // read number of hours worked from user
-
while (hoursWorked <= 0) // prompt until a positive value is entered
-
{
-
System.out.println( "Hours worked must be a positive value. " +
-
"Please enter the hours worked again: " ); // prompt for positive value for hours worked
-
hoursWorked = input.nextFloat(); // read hours worked again
-
}
-
-
employee = new EmployeeData(empName, hourlyRate, hoursWorked) {
-
///weeklyPay = hourlyRate * hoursWorked; // multiply
-
-
};
-
System.out.print( employee.getName() ); // display employee name
-
System.out.printf( "'s weekly pay is: $%,.2f\n", employee.getWeeklyPay() ); // display weekly pay
-
-
-
-
-
-
}
-
}
-
-
} // end method main
-
-
}} // end class Payroll4a
5 3017
Is there any place in particular you're having problems? Is your code not doing what you want it to? Skimming it, I don't see anything that screams "WRONG!", though you never use employees anywhere. Also, if you're working in Java 1.5 or later (you should be, 1.6 is recommended), use an ArrayList<EmployeeData> so that you don't have to cast values accessed to EmployeeData.
Yes,
As per the instructions store and retrieve name, hourly rate and the number of hours worked. Use a constructor to intialize the employee information.
Does what I have written fit? It does compile and work........ Just want to ensure I have this done correctly
Thank You
Hammer45
It looks fine to me, though if you're going to declare an ArrayList to store employee data, you should put employee data into the list.
Your constructor is fine as it is.
Thank you both for your guidance.
V/R
Hammer
Sign in to post your reply or Sign up for a free account.
Similar topics
by: RAD |
last post by:
I am working with an evaluation copy of SQL Server 2000 for the first
time; my DB experience lies with MS Access.
I have a simple table in SQL Server (tblCompany) that has a field
called...
|
by: Ryan Stewart |
last post by:
In some situations, I'd like to put some content within a box with a header.
No real problem there. But what if I want the header to have something on
the left hand side as well as on the right,...
|
by: Vortex Soft |
last post by:
http://www.junglecreatures.com/
Try it and tell me what's happenning in the Microsoft Corporation.
Notes:
VB, C# are CLS compliant
|
by: Joel Hedlund |
last post by:
Hi!
The question of type checking/enforcing has bothered me for a while, and
since this newsgroup has a wealth of competence subscribed to it, I
figured this would be a great way of learning...
|
by: Frinavale |
last post by:
Filtering user input is extremely important for web programming.
If input is left unfiltered users can input malicious code that can cripple your website.
This article will explain how to make...
|
by: Thamizh |
last post by:
Hi All,
I am writing an Axis web service in Java. It has been used by .Net client.
1. Is there any way to debug the web service when it requested by .net client?
2. Is there any way to...
|
by: Marty James |
last post by:
Howdy,
I was reflecting recently on malloc.
Obviously, for tiny allocations like 20 bytes to strcpy a filename or
something, there's no point putting in a check on the return value of
malloc....
|
by: David |
last post by:
Hi list.
What strategies do you use to ensure correctness of new code?
Specifically, if you've just written 100 new lines of Python code, then:
1) How do you test the new code?
2) How do...
|
by: David |
last post by:
Hi list.
Do test-driven development or behaviour-driven development advocate
how to do higher-level testing than unit testing?
types of testing:
unit
integration
system
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |