Java - 如何使用setter和getter基于另一个参数设置参数?

时间:2015-12-04 21:08:39

标签: java

早上好!

我想知道如何解决这个问题。问题如下:

enter image description here

我遇到的问题是我需要从CarRental.java获取的其中一个参数是基于另一个参数设置的。在这种情况下,它是“dailyFee”,它基于“大小”。我完全不知道如何从驱动程序类中设置“dailyFee”,因为到目前为止,我不知道如何对其进行编码以便将其设置为if语句中的一个数字。

我的代码(当然不完整):

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(驱动程序类)

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

    }
}

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

将构造函数更改为仅接受用户输入的内容:

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;;
}

在main中的本地String变量中收集信息,将它们传递给带有参数的构造函数,即:public CarRental(String name, String zipCode, String size, int rentalDays)

像这样:

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

}

答案 1 :(得分:0)

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.