Question

Trying to create a project that uses user input and known information to determine how much a car rental costs per day. The problem I'm having is the CarRental class doesn't seem to be getting the input from the test class. Not sure how to solve it.

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

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

}
}
Was it helpful?

Solution

You may try something like this;

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

OTHER TIPS

You need to actually construct an instance of CarRental, then get the name. In 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());

There are many errors in this program , try to re view the code as your both the class are declared as public as it won't work in a single java program and

String newCustomerName = getcustomerName;

this above code will alos give compilation error .

Here i thnk getCustomerName is a method so it should be declared as a method like this

getcustomerName();

and to access it try to create the object of the carRental class and even you have not initialize the instance member of the carRental class as well by passing the value in the constructor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top