gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:47:14 25 4
gpt4 key购买 nike

大家早上好!

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

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

}
}

任何帮助将不胜感激!

最佳答案

更改构造函数以仅接受用户输入:

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, intrentrentDays)

像这样:

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

}

关于Java - 如何使用 setter 和 getter 根据另一个参数设置一个参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34097476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com