Saturday, 28 September 2013

How do I modify this program to make use of an ArrayList?

How do I modify this program to make use of an ArrayList?

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.

No comments:

Post a Comment