-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCarRegistrationCentre.java
162 lines (128 loc) · 4.39 KB
/
CarRegistrationCentre.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import java.util.ArrayList;
import java.util.HashMap;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author kdost
*/
public class VehicleRegister {
private HashMap<RegistrationPlate, String> owners;
public VehicleRegister() {
this.owners = new HashMap<RegistrationPlate, String>();
}
public boolean add(RegistrationPlate plate, String owner) {
if (this.owners.get(plate) == null) {
this.owners.put(plate, owner);
return true;
} else {
return false;
}
}
public String get(RegistrationPlate plate) {
if (this.owners.get(plate) == null) {
return null;
} else {
return this.owners.get(plate);
}
}
public boolean delete(RegistrationPlate plate) {
if (this.owners.get(plate) == null) {
return false;
} else {
this.owners.remove(plate);
return true;
}
}
public void printRegistrationPlates() {
for (RegistrationPlate plate : this.owners.keySet()) {
System.out.println(plate);
}
}
public void printOwners() {
ArrayList<String> ownersList = new ArrayList<String>();
for (RegistrationPlate plate : this.owners.keySet()) {
String owner = this.owners.get(plate);
if (ownersList.contains(owner) == false) {
ownersList.add(owner);
}
}
for (String name : ownersList) {
System.out.println(name);
}
}
}
public class RegistrationPlate {
// don't change the code which is already given to you
// ATTENTION: the object variable types are final, meaning that their value cannot be changed!
private final String regCode;
private final String country;
public RegistrationPlate(String country, String regCode) {
this.regCode = regCode;
this.country = country;
}
public String getReg() {
return this.regCode;
}
public String getCountry() {
return this.country;
}
@Override
public String toString() {
return country + " " + regCode;
}
@Override
public boolean equals(Object object) {
if (object == null) {
return false;
}
if (this.getClass() != object.getClass()) {
return false;
}
RegistrationPlate temp = (RegistrationPlate)object;
if (this.country == null || !this.getCountry().equals(temp.getCountry())) {
return false;
}
if (this.regCode == null || !this.regCode.equals(temp.getReg())) {
return false;
}
return true;
}
public int hashCode() {
if (this.country == null || this.regCode == null) {
return 7;
}
return this.country.hashCode() + this.regCode.hashCode();
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
// below is the same examle program as in the assignment
RegistrationPlate reg1 = new RegistrationPlate("FI", "ABC-123");
RegistrationPlate reg2 = new RegistrationPlate("FI", "UXE-465");
RegistrationPlate reg3 = new RegistrationPlate("D", "B WQ-431");
List<RegistrationPlate> finnish = new ArrayList<RegistrationPlate>();
finnish.add(reg1);
finnish.add(reg2);
RegistrationPlate newPlate = new RegistrationPlate("FI", "ABC-123");
if (!finnish.contains(newPlate)) {
finnish.add(newPlate);
}
System.out.println("Finnish: " + finnish);
// if the equals method hasn't been overwritten, the same registration plate is repeated in the list
Map<RegistrationPlate, String> owners = new HashMap<RegistrationPlate, String>();
owners.put(reg1, "Arto");
owners.put(reg3, "Jürgen");
System.out.println("owners:");
System.out.println(owners.get(new RegistrationPlate("FI", "ABC-123")));
System.out.println(owners.get(new RegistrationPlate("D", "B WQ-431")));
// if the hashCode hasn't been overwritten, the owners are not found
}
}