Reputation: 85
First of all, here are the instructions:
http://ideone.com/eRHwUo
Yes, this is homework! With that being said (and because I simply love the language), when you do post answers, please try to use pseudo code so that I just don't copy and paste into my program. Thank you!
As far as the code goes, I've done everything needed in for the section where the user enters all of the input. However, I need help with 'transferring' the data from the classes to the 'ArrayList' that we're supposed to use. I figure that once I get that down, I'll be able to just sort through the array to find the ID number for selection "B", and if the user enters "C" I'll just cycle through the array displaying everything in it.
Anyway, onto the code (this is main):
/*
* Name:
* Date:
* Assignment 2
*/
import java.util.Scanner;
public class homework
{
public static void main(String[] args)
{
char userSelection;
String convertString;
String userStrings;
Scanner kbd = new Scanner(System.in);
do
{
System.out.println("Here are your choices:");
System.out.println("A. Enter employee data" +
"\nB. Search for employee data" +
"\nC. List all data" +
"\nD. Exit");
convertString = kbd.next();
userSelection = convertString.charAt(0);
switch(userSelection)
{
case 'A':
GetUserInfo();
break;
case 'B':
// Stuff;
break;
case 'C':
// Display all data;
break;
case 'D':
System.out.println("Goodbye!");
break;
default:
System.out.println("Error, that is not a valid entry. Please try again.");
}
} while (userSelection > 'D');
}
// Write functions here
public static void GetUserInfo()
{
String firstName;
String lastName;
String empID;
double hourlyRate;
int hoursWorked;
double withPercent;
Scanner kbd = new Scanner(System.in);
System.out.println("What is your first name?");
firstName = kbd.next();
System.out.println("What is your last name?");
lastName = kbd.next();
System.out.println("What is your employee ID?");
empID = kbd.next();
Employee user = new Employee(empID);
user.setFirstName(firstName);
user.setLastName(lastName);
System.out.println("What is your hourly rate?");
hourlyRate = kbd.nextDouble();
System.out.println("How many hours did you work?");
hoursWorked = kbd.nextInt();
System.out.println("What is your withholding percentage?");
withPercent = kbd.nextDouble();
Pay user1 = new Pay();
user1.setHourlyRate(hourlyRate);
user1.setHoursWorked(hoursWorked);
user1.setWithPercent(withPercent);
}
}
This is the Employee class:
public class Employee
{
// Members of the class
String firstName;
String lastName;
String employeeID;
// remember about the pay object
// EmployeeID constructor
public Employee(String empID)
{
this.employeeID = empID;
}
// Below are the various getters and setters of the Employee class
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public String getEmployeeID()
{
return employeeID;
}
}
And this is the Pay class:
public class Pay
{
// Members of the class
double hourlyRate;
int hoursWorked;
double withPercent;
// Various getters and setters of the Pay class
public double getHourlyRate()
{
return hourlyRate;
}
public void setHourlyRate(double hourlyRate)
{
this.hourlyRate = hourlyRate;
}
public int getHoursWorked()
{
return hoursWorked;
}
public void setHoursWorked(int hoursWorked)
{
this.hoursWorked = hoursWorked;
}
public double getWithPercent()
{
return withPercent;
}
public void setWithPercent(double withPercent)
{
this.withPercent = withPercent;
}
// Calculates the raw payment
public double CalcPayRate(double hourlyRate, int hoursWorked)
{
return hourlyRate * hoursWorked;
}
// If the employee has worked overtime, calculates the new payment
public double CalcOvertimePay(double hourlyRate, int hoursWorked)
{
double rawPay = 0;
rawPay = hourlyRate * hoursWorked;
if (hoursWorked > 40)
{
rawPay *= 1.5;
}
return rawPay;
}
// Calculates final amount that the employee will be paid
public double CalcTotalPay(double hourlyRate, int hoursWorked, double withPercent)
{
double rawPay = 0;
double subTotalPay = 0;
double finalPay = 0;
rawPay = hourlyRate * hoursWorked;
subTotalPay = rawPay * withPercent;
finalPay = rawPay - subTotalPay;
return finalPay;
}
}
So, thoughts please?
Also, final comments:
(1) I don't understand what 'Pay Object' is supposed to do under the Employee class?
(2) How do I use the input data to create a 'Pay Object' and then create an employee object?
Those are the two parts from the instructions I'm a little unclear about, and as such if you could shade some light on that, it'd be helpful!
(3) If there's anything that seems off about my syntax, please let me know about it so I can change it accordingly. I'm still new to this language, so any help would be great.
Edit: I have updated my code with comments:
public class homework
{
public static void main(String[] args)
{
// ArrayList<Employee> employeeList = new ArrayList<Employee>();
do
{
// Stuff commented out for readability
case 'A':
PromptForInput();
// Employee emp = PromptForInput();
// employeeList.add(emp);
break;
} while (userSelection > 'D');
}
// public static Employee PromptInput()
public static void PromptForInput()
{
Employee user = new Employee(empID);
// Isn't this supposed to be returned as well?
Pay user1 = new Pay();
user1.setHourlyRate(hourlyRate);
user1.setHoursWorked(hoursWorked);
user1.setWithPercent(withPercent);
//return user;
}
}
Is that how it's supposed to look?
I still do not understand the 'pay object' part of the assignment..
Upvotes: 3
Views: 2040
Reputation: 347214
Start by taking a look at Collections.
If I was doing this...
I would return the Employee
from the getUserInfo
method and add it to the ArrayList
within the calling method. This keeps the getUserInfo
method focused on a single responsibility.
I would also allow the getUserInfo
to return null
if the values entered by the user did not pass validation, but this might be beyond the scope of the requirements.
For example...
List<Employee> employees = // create ArrayList...
//...
Employee emp = getUserInfo();
if (emp != null) {
employees.add(emp);
}
You should also setting a Pay
object to the Employee
when you create it, this will associate the Pay
object to the Employee
so you can find it later
As to your Pay
object. The CalcPayRate
, CalcOvertimePay
and CalcTotalPay
don't required parameters, as all the information required for these methods should be available from the properties of the object...
For example...
public double getRawPay() {
return getHoursWorked() & getHourlyRate();
}
I would also encourage you to take a look at Code Conventions for the Java Programming Language
Updated #1
Associating the Pay
object with the Employee
is no different from how you assiciated things like the employee's names
Basically, you need to supply a instance field/property to hold the reference and some means to set and get the reference, for example...
public class Employee
{
//...
Pay pay;
// EmployeeID constructor
public Employee(String empID)
{
this.employeeID = empID;
}
// EmployeeID constructor
public Employee(String empID, Pay pay)
{
this(empID);
this.pay = pay;
}
public void setPay(Pay pay) {
this.pay = pay;
}
public Pay getPay() {
return pay
}
//...
}
I've supplied a second constructor, you don't really need it, but it's a demonstration of how it might be achieved.
Then you would simply pass the reference of Pay
to Employee
...
Employee emp = new Employee(...);
//...
Pay pay = new Pay();
//...
emp.setPay(pay);
Update #2
Another approach would be to create an instance of Pay when Employee is created...
public class Employee
{
//...
Pay pay = new Pay();
// EmployeeID constructor
public Employee(String empID)
{
this.employeeID = empID;
}
This would suggest we no longer need setPay
, but that's up to you.
When you need to get or set values for Pay, you simple ask the instance of Employee for it
Employee emp = ...;
emp.getPay().setHourlyRate(hourlyRate);
//...
double hourlyRate = emp.getPay().getHourlyRate();
Updated #3
Basically, you need to associate an instance of Pay
with an instance of Employee
. If it makes it easier, name it something else...
Based on the example from Update #1
public static Employee PromptForInput()
{
String firstName;
String lastName;
String empID;
double hourlyRate;
int hoursWorked;
double withPercent;
//...
Employee user = new Employee(empID);
user.setFirstName(firstName);
user.setLastName(lastName);
//...
Pay payObj = new Pay();
payObj .setHourlyRate(hourlyRate);
payObj .setHoursWorked(hoursWorked);
payObj .setWithPercent(withPercent);
// Don't forget to give an instance of Pay to the instance
// of Employee
user.setPay(payObj);
return user;
}
Think of Employee
as a container, it contains the name and ID of the employee as well as their pay details. When you start with a new instance of Employee
, it is just an empty container, you need to fill it...
When you need to, you simply request and instance of Pay
from the Employee
via the getPay
Update #4
Based on you new code, I would only make one small suggestion...
In your display all data choice, you could use the following loop to display the Employee AND the Employee's pay rate, for example...
for (int i = 0; i < employeeList.size(); i++)
{
Employee emp = employeeList.get(i);
System.out.println(emp);
System.out.println(emp.getPay().CalcPayRate());
}
Upvotes: 4
Reputation: 3118
A. Enter Employee Data
You are utilizing a class (noun) to perform actions. Actions generally translate to method calls. Even your usage of 'get' (by convention classes should not start with get) suggests introducing a method. So, change GetUserInfo to
private static Employee promptForEmployeeInfo()
//or more simply
private static Employee getEmployee()
Then you can return an employee object after all the necessary information is gathered, including the pay information. Then you will need to provide the pay information to the employee object. In the most basic form, this will be accomplished by instantiating a Pay object and passing it into the constructor of Employee (for immutable objects which is more advanced techniques than you require) or via a setter method.
Inside your main method, the List will be instantiated before your do/while loop. Every time the user enters 'A' you call employeeList.add('insert Employee method call here').
(1) I don't understand what 'Pay Object' is supposed to do under the Employee class
In object oriented programming (OOP), encapsulating data and actions on that data into a descriptive, appropriately named class is part of OOP convention.
Upvotes: 1
Reputation: 4060
Looking at the instructions, I think your just supposed to create an ArrayList with a generic type of Employee. The GetUserInfo() should return the Employee object it just created, and this Employee object should be stored into the ArrayList - right after the function call - with its .add() function.
Edit:
My attempt at your bottom three questions.
1) The Pay Object should be referenced with a variable in the Employee Object. You can use create another setter method in the Employee Object to do this.
2) I believe you already did this by the following code:
user1.setHourlyRate(hourlyRate);
user1.setHoursWorked(hoursWorked);
user1.setWithPercent(withPercent);
3) The syntax looks fine to me.
Upvotes: 2