-2

尝试创建一个项目,该项目使用用户输入和已知信息来确定每天的汽车租赁费用。我遇到的问题是 CarRental 类似乎没有从测试类获得输入。不知道如何解决它。

租车类

public class CarRental {

private String customerName;
private String ratingKey;
private double baseFactor;
private double basefactor;
private double fudge;
private double computeDailyFee;

public CarRental(String customerName, String ratingKey, double baseFactor, double fudge, double computeDailyFee) {
    this.customerName = customerName;
    this.ratingKey = ratingKey;
    this.baseFactor = baseFactor;
    this.basefactor = basefactor;
    this.fudge = fudge;
    this.computeDailyFee = computeDailyFee;
    //intitialize
}

public String getcustomerName() {
    return this.customerName;
}

public String getratingKey() {
    return ratingKey;
}



public void setcustomerName(String newCustomerName) {

    customerName = newCustomerName;
    System.out.println(customerName);
}

public void setratingKey(String newRatingKey) {
    ratingKey = newRatingKey;
    System.out.println(ratingKey);
}



public double baseFactor() {
    switch (ratingKey.charAt(0)) {
        case 'S':
            baseFactor = 1.0;
            break;
        case 'C':
            baseFactor = 1.2;
            break;
        case 'U':
            baseFactor = 1.4;
            break;
        case 'T':
            baseFactor = 1.6;
            break;
        case 'B':
            baseFactor = 2.0;
            break;
        default:
            System.out.println("Invalid type");
            break;
    }
    System.out.println(baseFactor);
    return baseFactor;
}

public double fudge() {
    switch (ratingKey.charAt(1)) {
        case 'N':
            fudge = 11.40;
            break;
        case 'V':
            fudge = 0;
            break;
        default:
            System.out.println("Invalid type");
            break;
    }
    System.out.println(fudge);
    return fudge;
}

public double computeDailyFee() {
    computeDailyFee = (baseFactor() * (89.22 - fudge()));


    System.out.println(computeDailyFee);
    return computeDailyFee;
}
}

类出租测试

public class RentalTest {

public static void main(String[] args) {

     CarRental rental = new CarRental("customerName", "ratingKey", 0.0, 0.0, 0.0); 
String newCustomerName = rental.getcustomerName();   
    String newCustomerName = JOptionPane.showInputDialog("Enter your name");
   //System.out.println(newCustomerName);
    RentalTest rentaltest = new RentalTest();
            JOptionPane.showMessageDialog(null, "Input code for model, then code for condition (No Spaces)");

    String newRatingKey = JOptionPane.showInputDialog(
            "Models: 'S' = Sub-compact   'C'=Car   'U'=SUV   'T'=Truck   'B'=Bigfoot\n"
            + "Condition: 'N'= New   'V' = Vintage");
   newRatingKey = newRatingKey.toUpperCase();

    //System.out.println(newRatingKey);

}
}
4

3 回答 3

1

你可以试试这样的;

CarRental carRental = CarRental("customerName", "ratingKey", 0.0, 0.0, 0.0);
carRental.getcustomerName();
于 2013-10-30T18:45:37.687 回答
0

这个程序有很多错误,尝试查看代码,因为你的两个类都被声明为公共的,因为它不能在单个 java 程序中工作,并且

String newCustomerName = getcustomerName;

上面的代码也会给出编译错误。

这里我认为 getCustomerName 是一个方法,所以它应该被声明为这样的方法

getcustomerName();

并访问它尝试创建 carRental 类的对象,即使您还没有通过在构造函数中传递值来初始化 carRental 类的实例成员。

于 2013-10-30T19:08:48.570 回答
0

您需要实际构造 的实例CarRental然后获取名称。在main

// Build the object
CarRental rental = new CarRental("first", "last", 1.0, 1.0, 100.0);
// Now we can call instance methods.
System.out.println(rental.getCustomerName());
于 2013-10-30T18:52:10.890 回答