0

I am trying to use if-else statements to get the date entered by the user and display a constant for that particular season.

So for example if I were to enter a date of 04/25/2015 I would want it to show the price for Spring. When I print the price it displays zero instead.

Any help will be great. Method will be shown below

public double determinePrice(){

    double price = 0;
    if(rentalDate.substring(1, 3).equals(3) || rentalDate.substring(1, 3).equals(4)|| rentalDate.substring(1, 3).equals(5))
        price = SPRING; 
    else if(rentalDate.substring(1, 3).equals(6) || rentalDate.substring(1, 3).equals(7)|| rentalDate.substring(1, 3).equals(8))
        price =  SUMMER;
    else if(rentalDate.substring(1, 3).equals(9) || rentalDate.substring(0, 3).equals(10)|| rentalDate.substring(0, 3).equals(11))
        price = FALL;   
    else if(rentalDate.substring(0, 3).equals(12) || rentalDate.substring(0, 3).equals(01)|| rentalDate.substring(1, 3).equals(02))
        price = WINTER;

    return price;

}
  • Possible duplicate of [How to parse/format dates with LocalDateTime? (Java 8)](http://stackoverflow.com/questions/22463062/how-to-parse-format-dates-with-localdatetime-java-8) –  Oct 08 '15 at 11:07
  • Did you check the values of `SPRING`, `SUMMER`, etc, if these are not zeros? –  Oct 08 '15 at 11:25
  • The error in the code above is the usage of the equals method (testing Strings against integers). He should try to parse the substring into an int first. But this would bloat this really unfine if another time. – Aron_dc Oct 08 '15 at 11:37

1 Answers1

1

You maybe should try to parse your string into a LocalDate and then work with these (1). With the usage of LocalDate you can define the 3 dates determining the start of summer,autumn and winter and simply check your dates like:

givenDate.isBefore(summerStartDate)

for dates in spring or

givenDate.isAfter(summerStartDate) && givenDate.isBefore(autumnStartDate)

for dates in summer...

Another possibility with the use of LocalDate would be to write a class describing timeframes with a method isIn(LocalDate) that checks whether the given date lies between start and end.

edit: Just saw that you're only testing for the month belonging to a specific month. After parsing your original date into a LocalDate you could use the methods getMonth (gives you the month in the form of an enum value) or getMonthValue (gives you a numerical representation 1-12).

(1) Howto convert: How to parse/format dates with LocalDateTime? (Java 8)

Community
  • 1
  • 1
Aron_dc
  • 883
  • 4
  • 14