-
Notifications
You must be signed in to change notification settings - Fork 0
/
ElectricCar.java
95 lines (75 loc) · 2.66 KB
/
ElectricCar.java
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
84
85
86
87
88
89
90
91
92
93
94
95
package P8;
public class ElectricCar extends Car {
// Atributos de instancia
private int electricPower;
private float batteryCharge;
// Atributos estáticos
private static int minPower = 50;
private static int maxPower = 800;
private static final float minCharge = 0;
private static final float maxCharge = 100;
public static final int BATTERY_CAPACITY = 100;
// Constructor vacío
public ElectricCar() {
}
// Constructor con argumentos
public ElectricCar(String plate, String manufacturer, int electricPower, float batteryCharge) {
super(plate, manufacturer);
this.electricPower = electricPower;
this.batteryCharge = batteryCharge;
}
// Métodos estáticos o de clase
public static boolean isValidPower(int electricPower) {
if (electricPower >= minPower && electricPower <= maxPower) {
return true;
} else {
System.out.println(
"Incorrect power value: " + electricPower + ". Valid range is " + minPower + "-" + maxPower);
return false;
}
}
public static boolean isValidBattery(float batteryCharge) {
if (batteryCharge >= minCharge && batteryCharge <= maxCharge) {
return true;
} else {
System.out.println("Incorrect battery charge value: " + batteryCharge + ". Valid range is " + minCharge
+ "-" + maxCharge);
return false;
}
}
// Métodos de instancia
public int getElectricPower() {
return this.electricPower;
}
public void setElectricPower(int electricPower) {
this.electricPower = electricPower;
}
public float getBatteryCharge() {
return this.batteryCharge;
}
public void setBatteryCharge(float batteryCharge) {
this.batteryCharge = batteryCharge;
}
public void setMinPower(int minPower) {
ElectricCar.minPower = minPower;
}
public void setMaxPower(int maxPower) {
ElectricCar.maxPower = maxPower;
}
public int getTotalPower() {
return this.electricPower;
}
public void increaseBatteryChargeLevel(float chargeTime) {
// Se calcula el nuevo valor de la carga
float newLevel = this.batteryCharge + (chargeTime * ElectricCharger.POWER / BATTERY_CAPACITY) * 100;
// Se comprueba que no sea mayor a cien
if (newLevel > 100) {
this.batteryCharge = 100;
} else {
this.batteryCharge = newLevel;
}
}
public String toString() {
return "E" + super.toString() + electricPower + ";" + Float.toString(batteryCharge).replace('.', ',');
}
}