-2

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

}
}
user2854982
  • 1
  • 1
  • 1
  • 5
  • 1
    You have a problem with `String newCustomerName = getcustomerName;`. Is this compiling? – SlightlyCuban Oct 30 '13 at 18:44
  • 1
    You never declared and instantiated a `CarRental` object. That should be about the only field in your main method. – clwhisk Oct 30 '13 at 18:45
  • 1
    Can you give us the actual code that compiles, but runs incorrectly? I can't make your code here compile. – Dawood ibn Kareem Oct 30 '13 at 18:45
  • this wont even compile – Woot4Moo Oct 30 '13 at 18:46
  • No. I don't really understand how to use those methods in different classes, I just kind of put that in to try and call the get method. Didn't work. – user2854982 Oct 30 '13 at 18:47
  • Took that line out, it compiles now – user2854982 Oct 30 '13 at 18:48
  • Did you compile it, or are you just guessing? – Dawood ibn Kareem Oct 30 '13 at 18:57
  • I compiled it. It displays the prompts at least, and doesn't give any errors. – user2854982 Oct 30 '13 at 18:59
  • Then you need to have another go at pasting it in; because the code that's currently in your question definitely does NOT compile. – Dawood ibn Kareem Oct 30 '13 at 19:01
  • It did when I first stated that it compiles. The lines that have been added are the lines suggested by SlightlyCuban (below). Barring those, it compiles, but does not do what I want it to do. – user2854982 Oct 30 '13 at 19:03
  • I would have liked to be able to pick up your code, run it, maybe using a debugger, and actually find out what's wrong with it. But if the code that you post in your question is different from the code that you're actually running, it makes it MUCH harder to do this, and therefore to help you. I'm not trying to come across as angry; I'm trying to give you the best help that I can. But posting code that's different from the code that's actually giving you the problem just makes it more difficult for everyone. If you want people to help you, it helps you to help them. – Dawood ibn Kareem Oct 30 '13 at 19:59

3 Answers3

1

You may try something like this;

CarRental carRental = CarRental("customerName", "ratingKey", 0.0, 0.0, 0.0);
carRental.getcustomerName();
Devrim
  • 15,345
  • 4
  • 66
  • 74
0

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());
Community
  • 1
  • 1
SlightlyCuban
  • 3,185
  • 1
  • 20
  • 31
  • I've added that now, however it's displaying an error that newCustomerName is already defined – user2854982 Oct 30 '13 at 18:58
  • @user2854982 thought that was your instance of it based on your first edit. You've declared `newCustomerName` elsewhere, that is all. – SlightlyCuban Oct 30 '13 at 19:00
  • Nvm, fixed that portion. However, it still doesn't seem to receive those Strings in my CarRental class. – user2854982 Oct 30 '13 at 19:09
  • @user2854982, the `String`s are getting set in your constructor. You need to carefully step through your code and figure out what is going on. – SlightlyCuban Oct 30 '13 at 19:13
  • I really don't understand what's wrong. – user2854982 Oct 30 '13 at 19:28
  • @user2854982, to be blunt, this code has more problems than can be answered in SO's Q&A format--I could write a small book trying to fix this for you. Have a well-defined specific problem? We can help. Need someone to explain Java fundamentals to you? Find a tutor or join the chat. SO is simply not built to walk you through every problem while coding a solution. Take a break, read up on Java, then debug your code. – SlightlyCuban Oct 30 '13 at 19:47
  • 1
    Fixed it. This was all it took. rental.setcustomerName(newCustomername); rental.setratingKey(newRatingKey); rental.baseFactor(); rental.fudge(); rental.computeDailyFee(); – user2854982 Oct 30 '13 at 20:24
  • @user2854982 I knew you'd find it. Sorry if I came across as cold, but realizing how to call instance methods is a light-bulb-over-the-head moment that everyone has to get to on their own. – SlightlyCuban Oct 30 '13 at 20:42
0

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.

  • Thank you, I've included that now. It's still not working properly, and I don't really understand what exactly is wrong. – user2854982 Oct 30 '13 at 19:29