tomaszsvd - 1 month ago 13

Java Question

I have a problem

Parking charge is 3$ per hour for the first 3 hours and for each extra hour you'll be charged 1$ and 24 hrs is 30$ but you can stay more than 24 hours.

I don't know how to solve the hours>24 like if car stays there for 37 hours, or more. I think that modulus has to be used for this, unfortunately I cant get this working. any help ??

`if (hours >= 24) {`

price = 30;

price += (hours - 3) % 1; //should the mod even go here?

} else if (hours < 24 && hours > 3) {

price = 9;

price += (hours - 3) * 1;

} else {

price = hours * 3;

}

System.out.println("Hours: " + hours + " Price: " + price);

Answer

This works:

```
if (hours >= 24) {
price = (hours / 24)*30;
int h = hours%24;
int h4 = Math.min(h, 3)*2;
price += h4 +((hours)%24);
} else if (hours < 24 && hours > 3) {
price = 9;
price += (hours - 3) * 1;
} else {
price = hours * 3;
}
System.out.println("Hours: " + hours + " Price: " + price);
```