我对编程很陌生。这是carHire类。两个人中有一个。我已经要求开发一个基于GUI的汽车租赁应用程序。所以当我输入一个条目时,一切看起来都很好,但是我的租金仍然是0。我搞不懂。
公共类CarHire {
private String customerName;
private String licenseNumber;
private int daysHired;
CarHire(){
customerName=null;
licenseNumber=null;
daysHired=0;
}
CarHire(String customerName, String licenseNumber, int daysHired){
this.customerName = customerName;
this.licenseNumber = licenseNumber;
this.daysHired = daysHired;
}
public void setCustomerName(String customerName){
this.customerName = customerName;
}
public void setLicenseNumber(String licenseNumber){
this.licenseNumber = licenseNumber;
}
public void setDaysHired(int daysHired){
this.daysHired = daysHired;
}
public String getCustomerName()
{
return customerName;
}
public String getLicenseNumber()
{
return licenseNumber;
}
public int getDaysHired()
{
return daysHired;
}
public double calculateHireRental(){
final double BASE_RATE = 34.5;
final double NEXT_TIER_RATE = 30.5;
final double LAST_TIER_RATE = 22.5;
final int NEXT_TIER_START_DAY=4;
final int LAST_TIER_START_DAY=7;
double rental= 0.0;
int days = 0;
if(days<NEXT_TIER_START_DAY){
rental=days*BASE_RATE;
}
else if(days<=LAST_TIER_START_DAY){
rental=3*BASE_RATE+(days-3)*NEXT_TIER_RATE;
}
else{
rental=3*BASE_RATE+4*NEXT_TIER_RATE+(days-7)*LAST_TIER_RATE;
}
return rental;
}
}`下面是GUI类。
//进程输入数据
`public void enterData()
{
if (nameField.getText().compareTo("") == 0)
{
JOptionPane.showMessageDialog(null,"You must enter a customer name","XYZ Car Hire App",JOptionPane.ERROR_MESSAGE);
return;
}
if (licenseField.getText().compareTo("") == 0)
{
JOptionPane.showMessageDialog(null,"You must enter a license number","XYZ Car Hire App",JOptionPane.ERROR_MESSAGE);
return;
}
if (daysField.getText().compareTo("") == 0)
{
JOptionPane.showMessageDialog(null,"You must enter days hired","XYZ Car Hire App",JOptionPane.ERROR_MESSAGE);
return;
}
//if (currentCustomer == MAX_NUM)
String customerName = nameField.getText();
String licenseNumber = licenseField.getText();
int daysHired = Integer.parseInt(daysField.getText());
displayHeading();
CarHire g = new CarHire(customerName,licenseNumber,daysHired);
carHireArray[currentCustomer] = g;
textArea.append(String.format("%-25s%-28s%-32s$%3.2f\n",customerName, licenseNumber, daysHired, g.calculateHireRental()));
if (enterButton.isEnabled())//todo-- clear textfields and return focus
{
nameField.setText("");
nameField.requestFocus();
licenseField.setText("");
licenseField.requestFocus();
daysField.setText("");
daysField.requestFocus();
}
currentCustomer++;//todo-- incremental current cusomer number
}
// Display all bookings
public void displayAll()
{
textArea.setText("");
displayHeading();
//todo-- call displayHeading() method
for(int i=0;i<MAX_NUM;i++)
{
CarHire listCustomer = carHireArray[i];
textArea.append(String.format("%-25s%-22s%-28s$%3.2f\n",listCustomer.getCustomerName(), listCustomer.getLicenseNumber(), listCustomer.getDaysHired(), listCustomer.calculateHireRental()));
}//todo-- display all entries entered so far (need using a loop)
//todo-- display number of entries, average days hired, total rental
if (nameField.getText().compareTo("") == 0)
{
JOptionPane.showMessageDialog(null,"No customer entered","XYZ Car Hire App",JOptionPane.ERROR_MESSAGE);
}//todo-- complete error message
}`发布于 2018-09-26 12:14:43
在calculateHireRental()中有int days = 0;,所以第一个if是真的,并且返回days * BASE_RATE,即0。
删除days变量并使用类成员daysHired。
发布于 2018-09-26 12:15:06
在您提供的代码中:在GUI中,将结果赋值给'daysHired‘变量
int daysHired = Integer.parseInt(daysField.getText());但是,在类CarHire中,变量天数已经预定义为0:
int days = 0; 因此,无论你把多少乘以“天”(乘以0),你都会得到0:
rental=days*BASE_RATE;发布于 2018-09-26 12:15:17
看起来您总是设置days =0 ...look up
https://stackoverflow.com/questions/52517272
复制相似问题