-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCarBuyer.java
23 lines (18 loc) · 925 Bytes
/
CarBuyer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.Arrays;
public class CarBuyer {
public double lowestCost(String[] cars, int fuelPrice, int annualDistance, int years) {
String[] firstCar = cars[0].split(" ");
int[] car1 = Arrays.stream(firstCar).mapToInt(Integer::parseInt).toArray();
double lowest = this.calculate(car1, annualDistance, fuelPrice, years);
for(int i = 1; i < cars.length; ++i) {
String[] car = cars[i].split(" ");
int[] aCar = Arrays.stream(car).mapToInt(Integer::parseInt).toArray();
double p = this.calculate(aCar, annualDistance, fuelPrice, years);
lowest = Math.min(lowest, p);
}
return lowest;
}
private double calculate(int[] elements, int annualDistance, int fuelPrice, int years) {
return (double)(elements[0] + elements[1] * years) + (double)(years * fuelPrice * annualDistance) / (double)elements[2];
}
}