0

I have an assignment and I completed my code for it, but the issue remains that the Car type and the total fee are coming up blank at the end when I run it, It has to do with inheritance which is something new to me and I hope I did it right. The program is in 3 separate classes, any help would be greatly appreciated

import java.util.*;
public class UseCarRental {
public static void main(String[] args) 
{
       Scanner input = new Scanner(System.in);
        System.out.println("How many days do you need the rental?");
        int rentalLength = input.nextInt();
        System.out.println("Enter requested car size:" +
                "\nEconomy" + "\nMidsize" + "\nFullsize" + "\nLuxury");
        String rentalCarSize = input.nextLine();
        input.next();
        CarRental firstRental = new CarRental(rentalLength, rentalCarSize);
        firstRental.display();
        }

}

class CarRental {
    public  String  rentalCarSize = "";
    public  double rentalFeeDaily;
    public  int rentalLength = 0;
    public  double  rentalFeeTotal= rentalFeeDaily*rentalLength;
    public CarRental(int days, String carSize)
    {
        rentalCarSize   = carSize;
        rentalLength    = days;
    }
    public void display()
    {
        System.out.println(
                "#############" +
                "\nCar Size      = " + getRentalCarSize() +
                "\nRental Length = " + rentalLength +
                "\nTotal Fee     = " + rentalFeeTotal
                );
    }        
    public void setRentalLength(int length)
    {
        rentalLength = length;
    }
    public String getRentalCarSize()
    {
        return rentalCarSize;
    }
    public int getRentalLength()
    {
        return rentalLength;
    }
    public double getRentalFeeTotal()
    {
        return rentalLength * rentalFeeDaily;
    }
    public double getRentalFeeDaily(String carSize)
    {
        switch (carSize)
        {
            case "Economy":
                rentalFeeDaily = 29.99;
                break;
            case "Midsize":
                rentalFeeDaily = 38.99;
                break;
            case "Fullsize":
                rentalFeeDaily = 43.50;
                break;
            case "Luxury":
                rentalFeeDaily = 79.99;
                break;
        }
         return rentalFeeDaily;
        }

    }

import javax.swing.*;
public class LuxuryCarRental extends CarRental
{

 public LuxuryCarRental(int days, String carSize)
    {
        super(days, carSize);
    }

    @Override
    public void display()
    {
        JOptionPane.showMessageDialog(null,
            "\nCar Size      = " + getRentalCarSize() +
            "\nRental Fee    = " + rentalFeeDaily +
            "\nRental Length = " + rentalLength +
            "\nTotal Fee     = " + rentalFeeTotal);
    }
}

1 Answers1

1

You never calculate rentalFeeTotal after setting the values in the constructor. Change your code to:

class CarRental {
    public          String      rentalCarSize    = "";
    public          double      rentalFeeDaily;
    public          int         rentalLength     = 0;
    public          double      rentalFeeTotal   = 0d;

    public CarRental(int days, String carSize)
    {
        rentalCarSize   = carSize;
        rentalLength    = days;
        rentalFeeTotal   = getRentalFeeDaily(carsize)*rentalLength;

    }

You have an other prblem in your code:

   Scanner input = new Scanner(System.in);
    System.out.println("How many days do you need the rental?");
    int rentalLength = input.nextInt();
    input.nextLine(); // ADD this line
    System.out.println("Enter requested car size:" +
            "\nEconomy" + "\nMidsize" + "\nFullsize" + "\nLuxury");
    String rentalCarSize = input.nextLine();
    input.next();
Jens
  • 60,806
  • 15
  • 81
  • 95
  • Hi Jens, thanks for the speedy reply, I changed the code over to what you have and it still is failing to calculate the total fee, I think it has to do with the input for car size, but I still am having issues with it. – Dylan Kohel Mar 27 '17 at 06:52
  • @DylanKohel Have changed my answer. Please review – Jens Mar 27 '17 at 06:55
  • Thank you, I also found another issue myself, it was that I used input.next instead of Input.nextline – Dylan Kohel Mar 27 '17 at 07:02