0

Okay, asked a question a while ago without providing all my code, dumb idea. Here it is. I'm trying to use an if statement with a string to get a cost. No matter what I do, my cost is getting returned as 0. See class CarRental, method getCost. I was told I could change it up and use a switch, which is fine, but I want to understand why what I'm doing isn't working anyway, for the sake of knowledge.

Update: Tried changing it to a switch, same results.

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.");
        }
}
MPelletier
  • 16,256
  • 15
  • 86
  • 137
  • I can also confirm that changing it to a switch didn't fix the issue, I am still getting 0 as a cost when displaying. There must be some other issue, but I can't see what it is. –  Sep 28 '13 at 23:33
  • "Please enter the size car you would like" What would be your input there? Please read all the comments and answers before asking another question with the same ROOT cause. – porfiriopartida Sep 28 '13 at 23:38
  • Hint: Insert calls to `System.out.println` to output values at various places in your code. Eg, insert one just ahead of the first `if` statement to output "size": `System.out.println("Size = " + size);`. – Hot Licks Sep 28 '13 at 23:51

2 Answers2

1

You are not invoking the getCost method in your code.

NoChance
  • 5,632
  • 4
  • 31
  • 45
1

Your display method is getting the "cost per day" from an instance variable called cost in the LuxuryCarRental. None of your code assigns anything to that field, so (naturally!) it is always zero.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • If I run the program, I get 0 even if I choose one of the other three car sizes. –  Sep 28 '13 at 23:58
  • @E.Watson - My answer explains that. You get zero *because* the field always contains zero. It always contains zero *because* you didn't assign anything to it. Your `getCost()` method is irrelevant *if you don't use it*. – Stephen C Sep 29 '13 at 00:45