Java CarRental project need a little input

I have a project in my College java class and I need a little help.
The program's out put should be a customers name, the make of a vehicle, and the price of a vehicle according to a rating key. the key S C U M T B all have separate values added on to a base fee of 92.76. Also a second key (N, or V,)can be added to determine if the car has anything special added to it (New of Vintage) example N B = New Bigfoot. I have supplied the CarRental class and the tester for it. My main issue is that my rating key is out of bounds. I need someone to help me troubleshoot this and give me a few pointers if they could because I am at my wits end with this project. Thanks a lot guys and I hope I included enough relevant information.




Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(String.java:686)
at CarRental.computeDailyFee(CarRental.java:69)
at CarRentalTester.main(CarRentalTester.java:9

public class CarRental {

private String customerName;
private String make;
private String ratingKey;
private double dailyFee;

//@param instance String customerName is the New customers name
//@param instance String make is the vehicle make
//@param instance String ratingKey is the rating key for the rented vehicle
//@param constructor contains 3 instance fields;custName,make,ratingKey
public CarRental(String customerName, String make, String ratingKey)
{

this.customerName = customerName;
this.make = make;
this.ratingKey = ratingKey;


}


public String getCustomerName()
{
return customerName;
}




public String getMake()
{
return make;
}


public String getRatingKey()
{
return ratingKey;
}

//Mutator
public void setCustomerName(String customerName)
{
this.customerName = customerName;
}

public void setRatingKey(String ratingKey)
{
this.ratingKey = ratingKey;
}


//@param computeDailyFee computes the daily fee added to all rentals


public double computeDailyFee()
{



final double BASE_FEE = 92.76;

double dailyFee = 0;

if ((ratingKey.charAt(0)) == 'S' || (ratingKey.charAt(0)) == 's')
{dailyFee = 1.0*(BASE_FEE);
}
if ((ratingKey.charAt(1)) == 'N' || (ratingKey.charAt(0)) == 'n')
{dailyFee = 1.0*(BASE_FEE + 12.2);
}
else if ((ratingKey.charAt(1)) == 'V' || (ratingKey.charAt(1)) == 'v')
{dailyFee = 1.0*(BASE_FEE - 11.4);
}

else
{System.out.println(99999.99);

}

return dailyFee;

}
}

public class CarRentalTester{

public static void main(String[] args) {

CarRental car1 = new CarRental("Andy" , "Chevy", "S");
System.out.println("Customer Name:" +car1.getCustomerName());
System.out.println("Rental Make: " +car1.getMake());
System.out.println("Rental Code: " +car1.getRatingKey());
System.out.println("Final Price: " +car1.computeDailyFee());
}
}























Comments

  • You made an error in you if-else statement using the chatAt method.You used two cond. i.e charAt(0) n charAt(1) which means that your ratingKey must have atleast two index positions.In your Test class you specified just one index position("S") and this is at index 0.If u said ("sS") then no alarm.But i kn u do not which to run ur program dat way. So this I what i came wit.
    public class CarRent{
    private String Rating;
    public CarRent(String Rating){
    this.Rating = Rating;
    }
    public double compute(){
    double value = 0;
    if(Rating.equalsIgnoreCase("s")){
    value = 22.9;

    }else{
    value = 20.2
    }
    return value;
    }
    public static void main(String args []){
    CarRent test = new CarRent("S")
    System.out.println(test.compute());
    }
    }

    the output should be 22.9.

    NOTE: I am only pointing out the need to use equalsIgnoreCase() in place of charAt().
    One love(donmorris)
  • Thank you very much i was hoping I could code it a little more efficiently but it doesn't look like I'll be able to. That's great now all i need to do is make it round to two digits instead of these insane numbers i get and make an illegal key produce 99999.99
  • Alright I didnt want to have to come back here unless I needed to. Well....I need to :-). Okay so then I got my code pumpin out numbers that are waaay to big or once the computations is multiplied by more than 1.6 it gives me a flat 0.0. I have been trying to figure out how to round the double down to two decimals. I have tried dividing by ten and 100 but no luck on that. I have tried casting to a float because i found a piece of code that would do it but it required printf, float gives a result of 0.0. I have seen a lot of code on different sites that remedies this issue but I can seem to figure out how to implement it.
  • Never mind I figured it out thanks guys s much
  • Pls drop the finish code i would love to see ur use of printf. Method.Pls atleast let's learn....
    One love(donmorris)
  • oh I did not use printf I couldnt get it to go. What I did was made the outcome (dailyFee) = a new variable and basically multiplied it by 100 implement math.rint and then divided that by 100 and it seemed to do the trick!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories