【发布时间】:2022-11-04 03:19:59
【问题描述】:
对于我的练习,我必须计算差异(持续时间长) 变量之间即时inHour和即时外出.
换句话说,我必须计算一个人在停车场停留的时间才能计算价格。
这是我第一次使用 Instant 类,所以我有点迷茫:)
有我的课:
public class FareCalculatorService {
public void calculateFare(Ticket ticket){
if( (ticket.getOutTime() == null) || (ticket.getOutTime().isBefore(ticket.getInTime())) ){
throw new IllegalArgumentException("Out time provided is incorrect:"+ticket.getOutTime().toString());
}
Instant inHour = ticket.getInTime();
Instant outHour = ticket.getOutTime();
//TODO: Some tests are failing here. Need to check if this logic is correct
long duration = outHour - inHour;
switch (ticket.getParkingSpot().getParkingType()){
case CAR: {
ticket.setPrice(duration * Fare.CAR_RATE_PER_HOUR);
break;
}
case BIKE: {
ticket.setPrice(duration * Fare.BIKE_RATE_PER_HOUR);
break;
}
default: throw new IllegalArgumentException("Unkown Parking Type");
}
}
感谢您的帮助。
【问题讨论】: