-
Notifications
You must be signed in to change notification settings - Fork 0
/
HybridCar.java
127 lines (101 loc) · 3.88 KB
/
HybridCar.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package P8;
public class HybridCar extends Car {
// Atributos de instancia
private int mechanicalPower;
private int electricPower;
private float batteryCharge;
// Atributos estáticos
private static int minMechanicalPower = 60;
private static int maxMechanicalPower = 500;
private static int minElectricPower = 20;
private static int maxElectricPower = 100;
private static final float minCharge = 0;
private static final float maxCharge = 100;
public static final int BATTERY_CAPACITY = 15;
// Constructor vacío
public HybridCar() {
}
// Constructor con argumentos
public HybridCar(String plate, String manufacturer, int mechanicalPower, int electricPower, float batteryCharge) {
super(plate, manufacturer);
this.mechanicalPower = mechanicalPower;
this.electricPower = electricPower;
this.batteryCharge = batteryCharge;
}
// Métodos estáticos o de clase
public static boolean isValidMechanicalPower(int mechanicalPower) {
if (mechanicalPower >= minMechanicalPower && mechanicalPower <= maxMechanicalPower) {
return true;
} else {
System.out.println("Incorrect power value: " + mechanicalPower + ". Valid range is " + minMechanicalPower
+ "-" + maxMechanicalPower);
return false;
}
}
public static boolean isValidElectricPower(int electricPower) {
if (electricPower >= minElectricPower && electricPower <= maxElectricPower) {
return true;
} else {
System.out.println("Incorrect power value: " + electricPower + ". Valid range is " + minElectricPower + "-"
+ maxElectricPower);
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 getMechanicalPower() {
return this.mechanicalPower;
}
public void setMechanicalPower(int mechanicalPower) {
this.mechanicalPower = mechanicalPower;
}
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 setMinMechanicalPower(int minMechanicalPower) {
HybridCar.minMechanicalPower = minMechanicalPower;
}
public void setMaxMechanicalPower(int maxMechanicalPower) {
HybridCar.maxMechanicalPower = maxMechanicalPower;
}
public void setMinElectricPower(int minElectricPower) {
HybridCar.minElectricPower = minElectricPower;
}
public void setMaxElectricPower(int maxElectricPower) {
HybridCar.maxElectricPower = maxElectricPower;
}
public int getTotalPower() {
return this.mechanicalPower + 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 "H" + super.toString() + mechanicalPower + ";" + electricPower + ";"
+ Float.toString(batteryCharge).replace('.', ',');
}
}