What's wrong with my code? (FullTimeEmployee)

edited April 2016 in Questions about Code

Here are the directions:

Employee : name, hourly pay rate, # of hours per week

FullTimeEmployee : Employee that works 40 hours per week

NonExemptEmployee : Employee with overtime benefits.

All overtime hours are paid at 1.5 times regular hourly

rate (also known as 1.5X level)

SalesEmployee : FullTimeEmployee with commission

commission % on revenues generated

Read the data from a file, load them up in array,

print out weekly salary for each employee and

the total payout.

Sample input data:

E Jey 100.45 15.5

F Jack 75.65

N John 65.15 55.4

S Jill 50.15 0.05 2000000

...

any # of lines like these...

E: name, pay rate, hours

F: name, pay rate

N: name, payrate, hours

S: name, pay rate, commission%, sales

Output:

name1 pay

name2 pay

name3 pay

...

Total pay: ....

Then, get the employee name as user input and lookup

and print pay information for that employee.

Here is my code I have so far, Help!

class Employee {
  String name;
  float hourlyRate;
  float hours;

  Employee(String nm, float rate, float hrs) {
    name = nm;
    hourlyRate = rate;
    hours = hrs;
  }

  float computePay() {
    return hourlyRate * hours;
  }

}

class FullTimeEmployee extends Employee {
  FullTimeEmployee(String nm, float rate) {
    super(nm, rate, 40);
  }
}

class NonExemptEmployee extends Employee {
  NonExemptEmployee(String nm, float rate, float hrs) {
    super(nm, rate, hrs);
  }

  float computePay() {


    float pay = super.computePay();
    if (hours > 40) 
      pay += hourlyRate * 0.5 * (hours - 40);
  }
}

class SalesEmployee extends FullTimeEmployee {
  float commissionRate, revenue;
  SalesEmployee(String nm, float rate, float comRate, float r) {
  }
  float computePay() {
    super.computePay(); (hourlyRate + commissionRate);
}
}
Employee e = new Employee("Tom", 1000.0, 20);
println(e.name + " " + e.computePay());

Answers

  • Homework?

    Maybe start with setup and draw

  • edited April 2016
    // forum.Processing.org/two/discussion/16181/what-s-wrong-with-my-code
    // GoToLoop (2016-Apr-22)
    
    static final String EMPLOYEE_FILE = "employees.txt";
    Employee[] employees;
    
    void setup() {
      loadEmployees();
      exit();
    }
    
    void loadEmployees() {
      final String[] lines = loadStrings(EMPLOYEE_FILE);
      final int len = lines.length;
    
      employees = new Employee[len];
    
      for (int i = 0; i < len; ++i) {
        final String[] elems = splitTokens(lines[i]);
        println(elems);
    
        employees[i] = createEmployee(elems);
        println(employees[i].hourlyRate);
      }
    }
    
    Employee createEmployee(final String... data) {
      final char type = data[0].toUpperCase().charAt(0);
      final String name = data[1];
      final float[] vals = float(data);
    
      switch (type) {
      default:
        return new Employee(name, vals[2], vals[3]);
      case 'F':
        return new FullTimeEmployee(name, vals[2]);
      case 'N':
        return new NonExemptEmployee(name, vals[2], vals[3]);
      case 'S':
        return new SalesEmployee(name, vals[2], vals[3], vals[4]);
      }
    }
    
Sign In or Register to comment.