-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCar.java
83 lines (63 loc) · 2.3 KB
/
Car.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
package P8;
public abstract class Car implements Comparable<Car> {
// Atributos de instancia
private String plate;
private String manufacturer;
// Atributos estáticos
public static final String PLATE_FORMAT = "DDDDLLL";
// Constructor vacío
public Car() {
}
// Constructor con argumentos
public Car(String plate, String manufacturer) {
this.plate = plate;
this.manufacturer = manufacturer;
}
// Métodos estáticos o de clase
public static boolean isValidPlate(String plate) {
// Si la longitud es diferente a siete caracteres, la matrícula errónea
if (plate.length() != 7) {
System.out.println("Incorrect plate value: " + plate + ". Not comply with format " + PLATE_FORMAT);
return false;
}
// Se recorren todos los caracteres de la matrícula
for (int i = 0; i < plate.length(); i++) {
// Si uno de los tres primeros no se corresponde con un número, se considera
// erróneo
if (i < 3) {
if (!Character.isDigit(plate.charAt(i))) {
System.out.println("Incorrect plate value: " + plate + ". Not comply with format " + PLATE_FORMAT);
return false;
}
}
// Si uno de los cuatro últimos no es una letra mayúscula, se considera erróneo
if (i >= 4) {
if (!Character.isLetter(plate.charAt(i)) || !Character.isUpperCase(plate.charAt(i))) {
System.out.println("Incorrect plate value: " + plate + ". Not comply with format " + PLATE_FORMAT);
return false;
}
}
}
return true;
}
// Métodos de instancia
public String getPlate() {
return this.plate;
}
public void setPlate(String plate) {
this.plate = plate;
}
public String getManufacturer() {
return this.manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public abstract int getTotalPower();
public String toString() {
return ";" + getPlate() + ";" + getManufacturer() + ";";
}
public int compareTo(Car car) {
return this.plate.compareTo(car.plate);
}
}