0

This little method I made here isn't returning a correct cost value. I'm wondering if I am using the if statement incorrectly. Since I am new to Java, this is my first time having to use an if statement with a string involved. I have no book or teacher, just learning on my own. Any help would be much appreciated.

EDIT: Posted entire code

import java.util.*;

public class UseCarRental {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Thank you for choosing ICT car Rentals\n"
            + "Pleae enter your full name:");
    String renterName = input.nextLine();
    System.out.println("Please enter your zip code:");
    int renterZipcode = input.nextInt();
    input.nextLine();
    System.out.println("Please enter the size car you would like:\n"
            + "economy\n"
            + "midsize\n"
            + "fullsize\n"
            + "luxury?");
    String carSize = input.next();
    System.out.println("How many days do you wish to rent this?");
    int rentalDays = input.nextInt();

    if (carSize.equals("luxury")) {
        System.out.println("Will you be wanting a chauffer (y or n");
        String chauffer = input.next();
        LuxuryCarRental rentIt = new LuxuryCarRental(renterName, 
        renterZipcode, carSize, rentalDays,chauffer);
        rentIt.display();
    } else {
        CarRental rentIt = new CarRental(renterName, renterZipcode, 
        carSize, rentalDays);
        rentIt.display();
    }




} //  end main method

} //end class UseCarRental

class CarRental {

private int days;
private int zip;
private double cost;
private String size;
private double total;
private String name;

    CarRental(String renterName, int renterZipcode, String carSize, int rentalDays){
        this.days = rentalDays;
        this.zip = renterZipcode;
        this.name = renterName;
        this.size = carSize;
    }

    double getCost(){
        if(size.equals("economy")){
            cost = 29.99;
        }
        if(size.equals("midsize")){
            cost = 38.99;
        }
        if(size.equals("fullsize")){
            cost = 43.50;
        }
        return cost;
    } 

    void display(){
        System.out.println("Thank you for using our service.");
        System.out.println("Your order is as follows:");
        System.out.println("Name: " + name);
        System.out.println("Zip code: " + zip);
        System.out.println("Car size: " + size);
        System.out.println("Cost per day: " + cost);
        System.out.println("Days requested: " + days);
        total = days * cost;
        System.out.println("Total cost: " + total);
        System.out.println("If any of the above information is incorrect, too bad bud, because it isn't.");
    }

}

class LuxuryCarRental extends CarRental {

private int chauffer = 200;
private int days;
private int zip;
private double cost;
private String size;
private double total;
private String name;

LuxuryCarRental(String renterName, int renterZipcode, String carSize, int rentalDays, String chauffer){
    super(renterName, renterZipcode, carSize, rentalDays);
    this.days = rentalDays;
    this.zip = renterZipcode;
    this.name = renterName;
    this.size = carSize;
}

@Override
void display(){
        System.out.println("Thank you for using our service.");
        System.out.println("Your order is as follows:");
        System.out.println("Name: " + name);
        System.out.println("Zip code: " + zip);
        System.out.println("Car size: Luxury");
        System.out.println("Cost per day: " + cost);
        System.out.println("Days requested: " + days);
        System.out.println("Chauffer cost: " + chauffer);
        total = days * cost + chauffer;
        System.out.println("Total cost: " + total);
        System.out.println("If any of the above information is incorrect, too bad bud, because it isn't.");
    }

}

5 Answers5

0

What is likely happening here is that size is not "midsize" "fullsize" or "economy". one quick way to fix this is to add this line to the start of the function: cost = 9001;//or whatever number you want

David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122
0

Assuming size is a String, you're on the right way. You can also use String.equalsIgnoreCase(String) to compare the string without considering uppercase/lowercase.

Bear in mind that size is undefined inside your function and you need to pass it in the function attributes to be considered

double getCost(String size)

Apart from that, you're in the right trail.

SonicARG
  • 499
  • 9
  • 14
  • Since `getConst` is not `static`, we can safely assume that `size` is a member field, and therefore you don't need to pass it as argument(it's already part of the implicit `this` argument) – Idan Arye Sep 28 '13 at 23:13
  • You're right, at the moment of replying there was no full code; I had to answer with the tools I had at the moment. – SonicARG Sep 29 '13 at 18:22
0

You usually want to use an if-else-if chain when you have mutual exclusive if statements:

double getCost(){
    if(size.equals("economy")){
        cost = 29.99;
    }
    else if(size.equals("midsize")){
        cost = 38.99;
    }
    else if(size.equals("fullsize")){
        cost = 43.50;
    }
    return cost;
}

In your case it's not a problem, but it's usually a good practice, as it's more efficient(with very little work), and more importantly - you can guarentee that only one branch will be executed, which is important if code inside a branch can change the value of a later condition expression.

Anyways, your code has a much more serious problem: getCost not only returns the cost - it also sets it! This is a big problem because in order for display to work properly, getCost needs to be called before display. This is a very weird behavior, which is confusing and bug-prune even if you document it.

What you should do is get rid of the cost variable altogether. display, instead of using cost directly, should use getCost to calculate the cost:

System.out.println("Cost per day: " + getCost());

As for getCost, instead of setting cost(which no longer exists) it should directly return the cost:

double getCost(){
    if(size.equals("economy")){
        return 29.99;
    }
    else if(size.equals("midsize")){
        return 38.99;
    }
    else if(size.equals("fullsize")){
        return 43.50;
    }
    throw new RuntimeException(String.format("'%s' is not a valid size.", size));
}

Note that in this case you don't need the elses - but it doesn't hurt to keep them, and it's good for consistency.

Idan Arye
  • 12,402
  • 5
  • 49
  • 68
0

It depends whether size and cost are instance variables of the class containing this method, I assume they are or else this code wouldn't even compile, in which case the problem is probably in the fact that the size String does not equal "economy", "midsize" or "fullsize".

One thing to consider would be making size an enum and thus restricting it to only valid values

public enum Size {
ECONOMY(29.99), MIDSIZE(38.99), FULLSIZE(43.50);

private double cost;

Size(double cost) {
    this.cost = cost;
}

public double getCost() {
    return cost;
}       

}

Alternatively, if size and cost are not instance variables of your class you could write a functional method like this

public static double getCost(String size){
    double cost; 
    if(size.equals("economy")){
        cost = 29.99;
    } else if(size.equals("midsize")){
        cost = 38.99;
    } else if(size.equals("fullsize")){
        cost = 43.50;
    }
    return cost;
}

Note the use of else if to prevent from checking for fullsize / midsize if we already determined it was economy size

Josh
  • 838
  • 3
  • 11
  • 22
0

If your String carSize = input.next(); has a "luxury" value then you will not have what to get in getCost, you will create a luxury car

LuxuryCarRental(String renterName, int renterZipcode, String "luxury", int rentalDays, String chauffer){
    super(renterName, renterZipcode, "luxury", rentalDays);

....

double getCost(){
    if(size.equals("economy")){
        cost = 29.99;
    }
    else if(size.equals("midsize")){
        cost = 38.99;
    }
    else if(size.equals("fullsize")){
        cost = 43.50;
    }
    return cost; //if luxury???
}

MAYBE this would help:

LuxuryCarRental(String renterName, int renterZipcode, String carSize, int rentalDays, String chauffer){
    super(renterName, renterZipcode, "fullsize", rentalDays);

Or the main method creating a luxury car pass another argument, or adding another value for luxury. Or better, using ENUM so you cannot pass invalid values.

In general, there is no guarantee that you pass a valid value. You have to handle that validation with ENUM, or force the user to enter one of the 4 valid values and ask again if he provides something else... adding ignore case if the case is not important "economic" does not equal "Economic"

And finally,

This

System.out.println("Cost per day: " + cost);

must be this:

System.out.println("Cost per day: " + getCost());

Since you never set the cost on your constructor or anywhere else.

porfiriopartida
  • 1,546
  • 9
  • 17