1

this is a subclass and am trying to shifts variable to accept only 1 of 3 values, that's morning evening night am a newbie so please forgive me for such a simple question

public class PartTimeStaffHire extends StaffHire
{
    // instance variables - replace the example below with your own
    private int workingHour
private double wagesPerHour
private String shifts;
    private boolean terminated

This is subclass of StaffHire and accept all this variables and the last one am trying to accept only 1 of this 3 values

/**
     * Constructor for objects of class PartTimeStaffHire
     */
    public PartTimeStaffHire(int vacancy, String designation1, String typeofjob,
    String staffName1, String joinDate, String qualification1, String appointed, 
    boolean joined, int hoursPerDay, double wagePerHour, String shift)
    
    {
        super(vacancy, designation1, typeofjob, staffName1, joinDate,
        qualification1, appointed, joined);
        workingHour = hoursPerDay;
        wagesPerHour = wagePerHour;
        shifts = shift;
        if( shifts == "morning") {
        
        shifts =  "morning"; 
        }
        else if(shifts == "evening") {
        shifts =  "evening"; 
        }
        else if(shifts == "night") {
        shifts =  "night"; 
        
        }
        else { 
            System.out.println("Choose (morning, evening, night)" );
        }
        //(morning, evening, night)
        
    }
public String shift()
    {
        if( shifts == "morning") {
        
        shifts =  "morning"; 
        }
        else if(shifts == "evening") {
        shifts =  "evening"; 
        }
        else if(shifts == "night") {
        shifts =  "night"; 
        
        }
        else { 
            System.out.println("Choose (morning, evening, night)" );
        }
        return shifts;
    
    }
    
    /**
     * An example of a method - replace this comment with your own
     *
     * @param  y  a sample parameter for a method
     * @return    the sum of x and y
     */
    public void print()
    {
       super.print();
       System.out.println("The yearly salary is " + wagesPerHour);
       System.out.println("Weekly working hours are " + workingHour;
System.out.println("##################" + shifts);
    }
}

thanks in advance

kr3m213
  • 13
  • 3
  • I'm sorry for creating this in not such a great manner I will try to improve as I want to use stack on the daily basis – kr3m213 Jan 12 '23 at 20:37
  • 5
    Don't use strings, but define an `enum` which only has those three values; then the compiler will already guarantee that only correct values are passed in (well, and the dreaded `null` – you have to check for that at runtime) – knittl Jan 12 '23 at 20:40
  • 2
    Also, `double wagePerHour` -- NEVER use floating point for currency. Please read [Is floating point math broken](https://stackoverflow.com/q/588004/18157) and [What Every Computer Scientist Should Know About Floating-Point Arithmetic](https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) – Jim Garrison Jan 12 '23 at 20:41
  • 1
    Also `shifts == "evening"` you typically want to compare `String` with `.equals()` instead of `==` like 99.9% of the time (though as mentioned in another comment, you may not use `String` at all in this scenario.) – Nexevis Jan 12 '23 at 20:46
  • You would normally validate the argument to be passed to the constructor before calling the constructor, not in the constructor itself. If you need the constructor to validate a parameter, and the constructor finds it is not valid, the constructor should throw an exception. – Old Dog Programmer Jan 12 '23 at 20:49
  • `if(shifts == "evening")` tests if `shifts` and `"evening"` are the same `Object`. If you want to test to see if they have the same contents, use `.equals` or `.equalsIgnoreCase` method. And what did you want to do with `if(shifts == "evening") { shifts = "evening"; }` ? It doesn't do anything. It's like `if (foo == 2) { foo = 2; }` – Old Dog Programmer Jan 12 '23 at 20:58
  • ``` wagesPerHour = wagePerHour; shifts = shift; if( shifts.equals(morning)) { shifts = "morning"; } else if(shifts.equals(evening)) { shifts = "evening"; } else if(shifts.equals(night)) { shifts = "night"; } else { System.out.println("Choose (morning, evening, night)" ); } //(morning, evening, night) } ```` – kr3m213 Jan 12 '23 at 20:59
  • how can I add a comment with the code I just change the == to .equals but is still not working I mean I can put in this field any values constructor accept anything and I want to accept it only 1 of the 3 – kr3m213 Jan 12 '23 at 21:01
  • Did you want to edit the question? – Old Dog Programmer Jan 12 '23 at 21:03
  • Did you mean `if (shifts.equals("morning"))` ? – Old Dog Programmer Jan 12 '23 at 21:04
  • 1
    Also: The constructor of an object is not the correct place to validate input. – f1sh Jan 12 '23 at 21:04

1 Answers1

1

If you know the possible values of a type, an enum is what you want to use. So in this case you can create an enum Shift with the three possible constants:

public enum Shift {
    MORNING,
    EVENING,
    NIGHT;
    
    //this lets you convert a String such as `morning` to an enum constant
    //if the enum not not one of the 3 constants (ignoring the case), this returns null. So you can use it to also validate the input.
    public static Shift getShiftByName(String name) {
        for(Shift s:values()) {
            if(s.name().equalsIgnoreCase(name)) {
                return s;
            }
        }
        return null;
    }

    //if you want to convert it back to a String (for output), overwrite toString:
    @Override
    public String toString() {
        //name() returns the constant's name as a String, such as "MORNING".
        // since you use it as lowercase, overriding toString here makes sense
        return name().toLowerCase();
    }

}

You can then use it as

String strShiftInput = "morning";
Shift chosenChift = getShiftByName(strShiftInput);
if(chosenShift == null) {
   //invalid input, handle accordingly
}
new PartTimeStaffHire(theOtherParameters, chosenShift);
f1sh
  • 11,489
  • 3
  • 25
  • 51