-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCar.java
More file actions
83 lines (68 loc) · 2.89 KB
/
Car.java
File metadata and controls
83 lines (68 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.ynov.tp3;
public class Car {
private final String name;
private final Brand brand;
private final int maxNbDoors;
private final double maxFuel;
private Motor motor;
private Integer yearOfConstruction;
private int nbDoors;
private double nbKilometers;
private double currentFuel;
private Double consumption;
// A simple car constructor
public Car(final String name, final Brand brand, final int maxNbDoors, final double maxFuel) {
this.name = name;
this.brand = brand;
this.maxNbDoors = maxNbDoors;
this.maxFuel = maxFuel;
}
// A more complete car constructor
public Car(final String name, final Brand brand, final int maxNbDoors, final double maxFuel, final Motor motor, final double consumption) {
this(name, brand, maxNbDoors, maxFuel);
this.motor = motor;
this.consumption = consumption;
}
public void addDoors(final int nbDoorsToAdd) {
final int newNbDoors = nbDoors + nbDoorsToAdd;
nbDoors = Math.min(newNbDoors, maxNbDoors);
}
public void setMotor(final Motor motor, final double consumption) {
this.motor = motor;
this.consumption = consumption;
}
public void setYearOfConstruction(final int yearOfConstruction) {
this.yearOfConstruction = yearOfConstruction;
}
public boolean isConstruct() {
return yearOfConstruction != null && nbDoors == maxNbDoors && motor != null;
}
public void drive(final double nbKilometers) {
if (isConstruct() && currentFuel > 0) {
double litersConsumed = (nbKilometers * consumption) / 100;
double newCurrentFuel = currentFuel - litersConsumed;
if (newCurrentFuel < 0) newCurrentFuel = 0;
double delta = currentFuel - newCurrentFuel;
double realDistance = (delta * 100) / consumption;
currentFuel = newCurrentFuel;
this.nbKilometers += realDistance;
} else {
System.out.println("[ERROR] cannot drive because car is not constructed or don't have any fuel");
}
}
public void addFuel(double nbFuel) {
final double newFuel = currentFuel + nbFuel;
currentFuel = Math.min(newFuel, maxFuel);
}
public String toString() {
return "--------------------\n" +
"Car name = " + name + "\n" +
"Brand = " + brand + "\n" +
"Motor = " + motor + "\n" +
"Doors = " + nbDoors + "/" + maxNbDoors + "\n" +
"Fuel = " + currentFuel + "/" + maxFuel + "\n" +
"Consumption = " + consumption + " l/100km" + "\n" +
"Nb kilometers = " + nbKilometers + "\n" +
(isConstruct() ? "Year of construction = " + yearOfConstruction : "Not yet construct");
}
}