在Java中使用getter和setter方法

时间:2013-10-30 18:39:56

标签: java getter-setter

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

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

类RentalTest

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

}
}

3 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情;

CarRental carRental = CarRental("customerName", "ratingKey", 0.0, 0.0, 0.0);
carRental.getcustomerName();

答案 1 :(得分: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());

{{1}}

答案 2 :(得分:0)

这个程序中有很多错误,尝试重新查看代码,因为这两个类都被声明为public,因为它不能在单个java程序中运行而且

String newCustomerName = getcustomerName;

以上代码也会出现编译错误。

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

getcustomerName();

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