Java - adding to an inherited JOptionPane

Good evening all!

I think there's something I don't understand here about either inheritance or JOptionPane, but basically I want to figure out how to actually use a new JOptionPane in the LuxuryCarRental subclass.

Currently it displays 2 dialog boxes if the choice is "l", one box from the parent class and a new one I added in the subclass. Ideally I would want only one dialog box. I think this is something that I can do without JOptionPane but I would like to try to make it work with JOptionPane if possible.

My code: (parent class)

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, int rentalDays)
{ = name;
    this.zipCode = zipCode;
    this.size = size;
    if (size.equals("e"))
        dailyFee = 29.99;
    else if (size.equals("m"))
        dailyFee = 38.99;
    else if (size.equals("f"))
        dailyFee = 43.50;
    this.rentalDays = rentalDays;
    totalFee = dailyFee * rentalDays;

public void display()
    JOptionPane.showMessageDialog(null, "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 this in this post (subclass)

import javax.swing.JOptionPane;

public class LuxuryCarRental extends CarRental
    public LuxuryCarRental(String name, String zipCode, String size, int rentalDays)
        super(name, zipCode, size, rentalDays);
        if (size.equals("l"))
            String includeChauffeur;
            includeChauffeur = JOptionPane.showInputDialog(null, "Include chauffeur? Y/N");
            if (includeChauffeur.equals("Y") || includeChauffeur.equals("y"))
                JOptionPane.showMessageDialog(null, "Chauffeur @ $200/day = $" + 200 * this.getRentalDays());
} (driver class)

import javax.swing.JOptionPane;

public class UseCarRental 

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

        CarRental userInfo = new LuxuryCarRental(name, zip, size, days);


Any help would be greatly appreciated!


I think that the lesson here is to not mix UI code with model code. Understand that your CarRental class and all of its subclasses are logical or model classes, and can be thought of here as classes that model a physical or logical reality. They should be used in this capacity, and should be written so information can be passed into them and extracted out of them, but they should not interact directly with the user. Instead that is the responsibility of the UI (user interface) classes, of which here it is quite simple and only your main method. So I suggest that you get your JOptionPane calls out of both CarRental and LuxeryCarRental, and instead display the JOptionPane in your main method after extracting state from your CarRental object.

Otherwise, if you absolutely must have the model classes display their information, then do it in a method that can be fully overridden. In fact you would have your child class override the display() method, and then print out its data there.

Need Your Help

Reuse same single thread for same task again and again in wpf (c#) application without ThreadPool

c# wpf multithreading mvvm

Creating An MVVM application where application wants to connect to server on a button click. After clicking the button, a thread will be created to connect to the server so that UI doesn't freeze and

AngularJS app crashes IE dev tools

javascript angularjs internet-explorer internet-explorer-11

I have an angular application (works great in chrome and edge) that will crash IE 11 dev tools immediately upon opening.