总方向=1540m
第一公里=10美元
第2公里=8美元
其他(正常)公里=5美元
我如何计算这个逻辑?用户应支付10美元的第一公里,然后2ns公里8美元,其他5美元。
model.startUpCharge
是阵列,存储10美元和8美元
/// [totalDistance] is in meters
double calculateTripPrice(RideModel model, double totalDistance) {
double totalPrice = 0;
double standardPrice = 0;
totalPrice = model.normalCharge *
((totalDistance / 1000) - model.startUpCharge.length);
for (var i = 0; i < model.startUpCharge.length; i++) {
standardPrice = standardPrice + model.startUpCharge[i];
}
totalPrice = totalPrice + standardPrice;
return totalPrice;
}
@CreativeCreatorMayben不回答
问题是:如果我加1米,价格应该是50。第一公里开始充电。不会改变
import 'dart:math' as math;
class RideModel{
final List startUpCharge;
final double normalCharge;
RideModel({this.startUpCharge,this.normalCharge});
}
main() {
calculateTripPrice(RideModel(startUpCharge: [50,51],normalCharge: 10),1);
}
double calculateTripPrice(RideModel model, double totalDistance) {
var remainingDistance = totalDistance, price = .0;
// Kilometers with special charge
for (var i = 0; i < model.startUpCharge.length; i++) {
price += model.startUpCharge[i] / 1000 * math.min(1000, remainingDistance);
remainingDistance -= math.min(1000, remainingDistance);
}
// Remaining kilometers
price += model.normalCharge / 1000 * remainingDistance;
remainingDistance = 0;
print(price);
return price;
}