![](/img/trans.png)
[英]How do I make setters and getters for JList and DefaultListModel? (Java)
[英]Java - How do I set a parameter based on another parameter, using setters and getters?
大家早上好!
我想知道如何解决这个问题。 问题如下:
我在这里遇到的问题是,我需要从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();
}
}
任何帮助将不胜感激!
更改构造函数以仅接受用户输入:
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();
}
如果我正确理解了您的要求,则未设置它的原因是因为用于确定dailyFee
所有逻辑都在您未使用的构造函数中。 您可以将该逻辑分解为dailyFee
的getter(如@kolsyrad建议,首选),也可以将其放入自己的方法中,并从setter中调用以获取size
。
无论哪种方式,如果这些值都是静态的,最好将这种逻辑保留在CarRental内,并放弃对dailyFee
IMO的设置器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.