简体   繁体   中英

Java - How do I set a parameter based on another parameter, using setters and getters?

Good morning all!

I was wondering how I can approach this problem. The question is as follows:

在此处输入图片说明

The problem I ran into here is that one of the parameters I need to get from CarRental.java is based on what another parameter is set to. In this case, it's "dailyFee", which is based on "size". I'm at a total loss at how I can set the "dailyFee" from the driver class, because so far I have no clue how I can code it so that it sets it to one of the numbers in the if statement.

My code (incomplete of course):

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

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 this in this post

UserCarRental.java (driver class)

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();

    }
}

Any help would be greatly appreciated!

Change your constructor to accept only what is user input:

public CarRental(String name, String zipCode, String size,  int rentalDays)
{
    this.name = 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;
    this.totalFee = dailyFee * rentalDays;;
}

Collect information in local String varibles inside main, pass them to the Constructor with parameters, ie: public CarRental(String name, String zipCode, String size, int rentalDays)

Like this:

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 CarRental(name, zip, size, days);

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

    userInfo.display();

}

If I understand what you're asking correctly, the reason why it is not being set is because all of the logic that is used to determine the dailyFee is in the constructor that you're not using. You can break that logic out into the getter for dailyFee (as @kolsyrad suggested, preferred), or put it into it's own method and call it from the setter for size .

Either way, if those values are static, you'd be better off keeping that logic inside of CarRental and dropping the setter for dailyFee IMO.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM