need a bit of direction with this CS lab thing(java)

Status
Not open for further replies.

wafflehammer

Fully Optimized
Messages
2,502
Location
Logan, WV
Code:
A company pays its employees in one of three ways:
a) Salaried workers earn a fixed amount each week regardless of how many hours they work
b) Hourly workers earn $10.00 per hour and they earn time-and-a-half overtime (i.e., $15.00 per hour) for any hours worked beyond 40
c) Part time workers earn $8.00 per hour and are not paid overtime regardless of hours worked
You're going to write an interface and three classes that conform to this interface. You'll also write a class to test your code. Be sure to document the public interface of your code (per section 3.3 in the text).
1. Write an interface called Payday that specifies two abstract methods:
a. findEarnings that has no parameters and returns a double
b. payStub that has no parameters and returns a String
2. Write a class called SalariedEmployee that implements Payday:
a. Member variables are employee ID (String) and salary (double)
b. Methods:
i. a constructor
ii. payStub that returns “Salaried” followed by employeeID and salary
iii. findEarnings that calculates earnings for salaried employees as described in the introductory paragraph
3. Write a class called HourlyEmployee that implements Payday:
a. Member variables are employee ID (String) and hoursWorked (double)
b. Methods:
i. constructor
ii. payStub that returns “Hourly” followed by hours worked followed by ID and pay
iii. findEarnings (as described in the introductory paragraph)
4. Write a class called PartTimeEmployee that implements Payday:
a. Member variables are employee ID (String) and hoursWorked (double)
b. Methods:
i. constructor
ii. payStub that returns “Part Time” followed by hours worked followed by ID and pay
iii. findEarnings (as described in the introductory paragraph)
5. Write a test program that implements the following menu:
1. Add Salaried Employee
2. Add Hourly Employee
3. Add Part Time Employee
4. Display All
5. Quit
6. Display this menu using a while loop to allow the user to loop until the Quit option is selected.
7. Declare an ArrayList (for example myList) of type Payday.
8. If the user enters option 1, ask the user for the employees Id and salary. Create an instance of a SalariedEmployee and add it to the ArrayList. Options 2 and 3 should be handled similarly.
9. For menu option 4, use a for loop to cycle through the ArrayList and print the payStub for each element in the ArrayList:
for(int i=0; i < myList._______; i++)
System.out.println(myList.______.payStub());
10. Obtain user input by means of the Scanner class. To avoid the null buffer problem, obtain numeric values using the Double wrapper class, as in:
System.out.print("Enter name: ");
String name = myScanner.nextLine();
System.out.print("Enter hours worked: ");
double hours = Double.parseDouble(myScanner.nextLine());;


what does 1 actually mean? so far this semester they keep "going over" stuff that we haven't even learned yet and it's getting insanely hard :(

After that I have no clue about the "findearnings" part of any of those..i get the rest..but not that part...and part 5 I can do because they've actually taught us that :|

any help is appreciated...
 
Haha
Code:
 public interface Payday {
public double findEarnings();
public String payStub();
}

Code:
public class SalariedEmployee implements Payday {
public SalariedEmployee() {
...
}
public double findEarnings() {
double earnings;
...
return earnings;
}

....

They aren't just giving you a task they are pretty much telling you how to do it ;)
 
here's the finished thing :D

 
Code:
public interface Payday
{
    public double findEarnings();
    public String payStub();
}

Code:
public class SalariedEmployee implements Payday
{
    private String employeeID;
    private double salary;
    
    public SalariedEmployee(String employeeID, double salary)
    {
        this.employeeID = employeeID;
        this.salary = salary;
    }
    
    /** Returns that the employee is salaried, the employee ID,
     *  and salary of the employee
     */
    public String payStub()
    {
        return "Salaried"+ " " + employeeID + " " + salary;
    }
    
    /** Calculators the earnings of the employee.
     * 
     */
    public double findEarnings()
    
    {
        return salary;
    }
}

Code:
public class PartTimeEmployee implements Payday
{
    private String employeeID;
    private double hoursWorked;
    
    public PartTimeEmployee(String employeeID, double hoursWorked)
    {
        this.employeeID = employeeID;
        this.hoursWorked = hoursWorked;
    }
    
    /** Returns that the employee is a part time, the employee ID, and
     *  what the employee earned.
     */
    public String payStub()
    {
        return "Part Time" + " " + hoursWorked + " " + employeeID + " " + findEarnings();
    }
    
    /** Calculates the pay for the part time worker.
     * 
     */
    public double findEarnings()
    {
        return hoursWorked*8.00;
    }
}

Code:
public class HourlyEmployee implements Payday
{
   private String employeeID;
   private double hoursWorked;
   
   public HourlyEmployee(String employeeID, double hoursWorked)
   {
       this.employeeID = employeeID;
       this.hoursWorked = hoursWorked;
   }
   
   /** Returns that the employee is hourly paid, the hours worked, employee ID,
    *  and what the employee earned.
    */
   public String payStub()
   {
       return "Hourly" + " " + hoursWorked + " " + employeeID + " " + findEarnings();
   }
   
   /** Calculates the earnings for the employee
    * 
    */
   public double findEarnings()
   {
       if(hoursWorked<=40)
       return hoursWorked*10;
       else
       return 400*15*(hoursWorked-40);
   }
}

Code:
import java.util.ArrayList;
import java.util.Scanner;
public class PaydayTest
{
    public static void main(String[] args)
    {
        ArrayList<Payday> myList;
        myList = new ArrayList<Payday>();
        Scanner myScanner = new Scanner(System.in);
        String answer, response;
        boolean done = false;
        
        do
        {
            /** Menu to display the options */
            System.out.println("1: Add Salaried Employee");
            System.out.println("2: Add Hourly Employee");
            System.out.println("3: Add Part Time Employee");
            System.out.println("4: Display All Employees");
            System.out.println("5: QUIT");
            
            answer = myScanner.nextLine();
            
            /** Allows the user to input information for a salaried employee */
            if(answer.equals("1"))
            {
                System.out.println();
                Scanner aScanner = new Scanner(System.in);
                
                System.out.print("Enter a new Salaried Employee ID: ");
                String employeeID = myScanner.nextLine();
                System.out.print("Enter salary: ");
                double salary = Double.parseDouble(myScanner.nextLine());;
                SalariedEmployee a;
                a = new SalariedEmployee(employeeID, salary);
                myList.add(a);
            }
            
            /** Allows the user to input information for a hourly employee */
            else if (answer.equals("2"))
            {
                System.out.println();
                Scanner aScanner = new Scanner(System.in);
                
                System.out.println("Enter a new Hourly Employee ID: ");
                String employeeID = myScanner.nextLine();
                System.out.print("Enter hours worked: ");
                double hours = Double.parseDouble(myScanner.nextLine());;
                HourlyEmployee a;
                a = new HourlyEmployee(employeeID, hours);
                myList.add(a);
            }
            
            /** Allows the user to input information for a part time employee */
            else if (answer.equals("3"))
            {
                System.out.println();
                Scanner aScanner = new Scanner(System.in);
                
                System.out.println("Enter a new Part Time Employee: ");
                String employeeID = myScanner.nextLine();
                System.out.print("Enter hours worked: ");
                double hours = Double.parseDouble(myScanner.nextLine());;
                PartTimeEmployee a;
                a = new PartTimeEmployee(employeeID, hours);
                myList.add(a);
            }
            
            /** Prints the list of employee ID's and their earnings. */
            else if (answer.equals("4"))
            {
                System.out.println();
                
                System.out.println("Printing the list...");
                
                for(int i = 0; i < myList.size(); i++)
                System.out.println(myList.get(i).payStub());
                System.out.println();
            }
            
            /** Finishes the loop and prints an exit message */
            else if (answer.equals("5"))
            {
                done = true;
                System.out.println("Good Bye!");
            }
            
            /** If user inputs a wrong choice it printst a message informing them
             *  that they done so.
             */
            else
            {
                System.out.println();
                System.out.println("Wrong Choice!");
                System.out.println();
            }
        }while (done==false);
            System.exit(0);

           
    }
}
 
Status
Not open for further replies.
Back
Top Bottom