0

I'm having a bit of difficulty going about this. I need to return the regular (40 hours) and overtime pay (>40) of my worker in my worker class, along with the payTotal. My worker class is a subclass of my Person class. I haven't quite finished it yet but I want to know if I'm heading in the right direction with this class. Here is my code for my current method

String displayWeeklyPay(double hoursWorked){
    double overtime;
    double regular;
    String result;
    if (hoursWorked <= 40)
        {
        payTotal = hourlyPayRate * hoursWorked;
        regular = payTotal;
        overtime = 0;
        }
    else{ 
        payTotal = (hourlyPayRate * 40) + ((hoursWorked - 40) * (hourlyPayRate * 1.5));
        regular = hourlyPayRate * 40;
        overtime = ((hoursWorked - 40) * (hourlyPayRate * 1.5));
        }
    String overT = String.valueOf(overtime);
    String reg = String.valueOf(regular);
    String totalPay = String.valueOf(payTotal);

We were told to make this method return a string so I'm not quite sure how I'd return this. I've seen some posts that suggest an array but if I have to pass these values( regular, overtime, paytotal) to my worker constructor later on then will I be able to make the constructor differentiate between the three strings in the array? Thanks

Balla Baby
  • 35
  • 7
  • To return more than 1 thing, you need to stick all the things into 1 object. What object you stick it in depends on a lot of things though. If they're heavily related and you have a set amount, you could make a class with fields for each piece, then return an instance of the class. You could also just stick them in an Array/ArrayList, then pick the pieces out later. – Carcigenicate Sep 15 '17 at 14:56

3 Answers3

3

Returning an array with three String is clumsy : not meaningful and error prone.

You should rather create a custom class that owns three String fields and return an instance of it in your method.

Your method could so look like :

public MyResult displayWeeklyPay(double hoursWorked){

    ...    
    String overT = String.valueOf(overtime);
    String reg = String.valueOf(regular);
    String totalPay = String.valueOf(payTotal);
    return new MyResult(overT, reg, totalyPay);
}

If you use more than three fields, using a constructor is probably not the best way as the order of the passed arguments may become error prone.
A setter or fluent builder approach is probably preferable.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • My constructor for Worker uses super() to call the constructor for Person and then goes and sets the rest of the fields. Directions state, "displayWeeklyPay should indicate the number of hours of regular pay (40) and the total and the number of hours of overtime pay and the total as well as the total combined pay." I'm not supposed to necessarily hard code/ get and set for each regular, OT and total pay I don't think. Just call that method and have it return each – Balla Baby Sep 15 '17 at 15:07
2

Various options:

  • simple return a string a + "/" + b + "/" + c (bad idea)
  • return an array or a List<String>

The recommended option: create a specific class that "wraps around" those three values. And your method returns an instance of that class!

But for the record: don't just create a class with three fields that everybody else can read and write to. Rather set the field values via a constructor call; and have getter methods to fetch their content.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

If the return type of method has to be string then only possible solution would be to concatenate them using a separator probably a comma and then split it as you need. but of course this should be done only if the signature of the method cannot be changed. If possible to change the return type that should be preferred by returning an Object or an array of strings.