1

Good evening all!

The assignments are getting confusing! Again, not asking for a straight up answer but rather a pointer in the right direction so any help is appreciated.

Edit 2: Here is the exercise in question if it helps clear anything up:

enter image description here

I'm wondering how I can set the dailyFee variable in the CarRental.java file using the UseCarRental.java file. The if statements from CarRental.java don't seem to be working properly (inputting size doesn't set the dailyFee variable). Additionally, if I enter "l" for the size, it's not prompting to LuxuryCarRental.java. Here is the code:

CarRental.java

import javax.swing.JOptionPane;

public class CarRental 
{
private String name;
private String zipCode;
private String size;
private double dailyFee;
private int rentalDays;
private double totalFee;

public CarRental(String name, String zipCode, String size, double dailyFee, int rentalDays, double totalFee)
{
    this.name = name;
    this.zipCode = zipCode;
    this.size = size;
    if (size == "e")
    {
        this.dailyFee = 29.99;
    }
    else if (size == "m")
    {
        this.dailyFee = 38.99;
    }
    else if (size == "f")
    {
        this.dailyFee = 43.50;
    }
    this.dailyFee = dailyFee;
    this.rentalDays = rentalDays;
    totalFee = dailyFee * rentalDays;
}

public CarRental(){}

public void display()
{
    JOptionPane.showMessageDialog(null, "Luxury car for " + name + " from zip code " + zipCode + "\n"
            + "Type = " + size + "\n"
            + "Daily Fee = " + dailyFee + "\n"
            + "Days = " + rentalDays + "\n"
            + "Your rental is $" + totalFee);
}

//includes getters and setters but I didn't include them in this post

LuxuryCarRental.java

import javax.swing.JOptionPane;

public class LuxuryCarRental extends CarRental
{
    public LuxuryCarRental(String name, String zipCode, String size, double dailyFee, int rentalDays, double totalFee, String includeChauffeur)
    {
        super(name, zipCode, size, dailyFee, rentalDays, totalFee);
        if (size == "l")
        {
            dailyFee = 79.99;
            includeChauffeur = JOptionPane.showInputDialog(null, "Include chauffeur? Y/N");
            if (includeChauffeur == "Y")
            {
                totalFee = totalFee + 200;
            }
        }
    }
}

UseCarRental.java (incomplete)

import javax.swing.JOptionPane;

public class UseCarRental 
{

    public static void main(String[] args) 
    {
        CarRental userInfo = new CarRental();
        userInfo.setName(JOptionPane.showInputDialog("Enter name"));
        userInfo.setZipCode(JOptionPane.showInputDialog("Enter zip code"));
        userInfo.setSize(JOptionPane.showInputDialog("Enter type of car" + "\n" + "e - economy" + "\n" + "m - midsize" + "\n" + "f - full" + "\n" + "l - luxury"));
        userInfo.setRentalDays(Integer.parseInt(JOptionPane.showInputDialog("Enter days to rent")));


        System.out.println(userInfo.getDailyFee());

        userInfo.display();

    }
}

As you can see, I'm completely stumped as to how I can use setDailyFee in UseCarRental.java to set the dailyFee variable, which is need to make the totalFee calculation.

Edit: Being asked to explain why this is not a duplicate question. I fixed the string comparison issue (not updated here yet) but the core issue is still setters and inheritance.

Edit 3: Well I guess the question of the century (for clarity's sake) is: how do I set dailyFee value? I thought that it would be set by the if statement in CarRental.java once size is entered?

BaloneyOs
  • 317
  • 3
  • 10

1 Answers1

3

You have a few problems here:

  • You're using == to compare strings.

  • In your CarRental constructor, you're setting this.dailyFee based on the size and then overwriting it with the parameter's value. You should either eliminate the parameter or not have that logic in the constructor.

  • The dailyFee field is private to CarRental, meaning it's not available for direct access in LuxuryCarRental. LuxuryCarRental should use this.setDailyFee() instead.

  • It's Bad Code to put interactions such as a dialog box in a constructor. It'll work for this assignment, but don't use it in Real Code. Separate the interface and the data classes.

  • You seem to be misunderstanding how inheritance works. You create a CarRental object in your main, but the code for handling l only exists in LuxuryCarRental. You would need to either unconditionally create a LuxuryCarRental (which you could still assign to the CarRental variable) or input the rental type and create a CarRental or LuxuryCarRental based on what it is. Note that while you have a default (no-argument) constructor for CarRental, you do not have one for LuxuryCarRental.

My overall recommendation, besides fixing the string comparison: Move all of the questions into your main method, and assign their values to local variables. Use those values in a call to new LuxuryCarRental(name, zipCode, size, rentalDays, includeChauffeur) (note that dailyFee is calculated based on the size, and the totalFee is calculated based on the daily fee, days, and chauffeur). Instead of having totalFee as a variable on your CarRental, make getTotalFee() a method that performs your calculations when called, since the fee will change whenever you change any of the other parameters.

(And learn about enums when you can; the size shouldn't be a string in the first place.)

Community
  • 1
  • 1
chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
  • I'm afraid I don't quite understand. Where would I place the dailyFee logic? I need to base it off size somehow. Why is the logic not working to begin with? – BaloneyOs Dec 04 '15 at 06:16