mercredi 25 octobre 2017

Two options using strings

I'm attempting to have the variable "cost" changed depending on if one of two conditions are met "caravan" or "tent", at the moment the switch statement is not working and it using the default "not set" option. Any advice and help would be amazing.

    //Getter public String getType() {

    if((type.equals("tent")) || (type.equals("caravan"))){
         return type; 
    }else{
        type = "not set";
        System.out.println("type must be set as caravan or tent");
    }

    return type;
}

//Setter
public void setType(String type) {
    if((type.equals("tent")) || (type.equals("caravan"))){
         this.type = type; 
    }else{
        type = "not set";
        System.out.println("type must be set as caravan or tent");
    }

    this.type = type;
}



 @Override
 public double getCost() {
    if (days == 1){
        cost = 20;
    }else if (days >1){
        cost = 15*days;
    }if (concession == true){
        cost = cost*.75;

    }


    switch (type) {
        case "tent":
            cost += 12.50;
            break;
        case "caravan":
            cost +=22.50;
            break;
        default:
            cost += 0;
            break;

    }
    System.out.println(cost);
    return cost;

Aucun commentaire:

Enregistrer un commentaire