-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyHash.java
100 lines (70 loc) · 3.18 KB
/
MyHash.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
package DSL_MiniProject;
class MyHash {
LinkedList hashTable[]; //NOTE : Change this size later
MyHash() {
hashTable = new LinkedList[10]; //array(of 10 indexes) of LinkedList
// for (int i = 0; i < 10; i++) {
// hashTable[i] = new LinkedList();
// }
}
public int hash(Passenger pg) {
int hashCode = 0;
int prime = 31;
String pg_Eml = pg.getEmail();
for (char c : pg_Eml.toCharArray()) {
hashCode = (hashCode * prime + c) % 10; // Later , REMEMBER to use a larger modulus
}
return hashCode;
}
public void create(Passenger pg) {
int hashCode = hash(pg);
if (hashTable[hashCode] == null) {
hashTable[hashCode] = new LinkedList();
}
hashTable[hashCode].insert(pg);
}
public void display(Passenger pg) { //Displaying passenger email and its hashcode
String passengerEmail = pg.getEmail();
System.out.println("passengerEmail = " + passengerEmail + " : " + hash(pg));
}
public Passenger search(String email) {
Passenger pgToBeSearched = new Passenger(email, "", "", 0, "");
int hashCode = hash(pgToBeSearched);
LinkedList bucketToSearchIn = hashTable[hashCode];
if (bucketToSearchIn != null) {
Passenger foundPassenger = bucketToSearchIn.search(email);
if (foundPassenger != null) {
System.out.println("Passenger with email Id " + foundPassenger.getEmail() + " is " + foundPassenger.getName());
return foundPassenger;
} else
System.out.println("Passenger with email Id " + email + " NOT FOUND");
} else
System.out.println("Passenger with email Id " + email + " NOT FOUND");
return null; // Passenger NOT found
}
public void update_Email(String currentEmail, String newEmail) {
Passenger pgToUpdateEml = search(currentEmail);
if (pgToUpdateEml != null) {
pgToUpdateEml.setEmail(newEmail); //Updating the email in Passenger object
// System.out.println("Email updated for " + pgToUpdate.getName() + " to " + newEmail);
System.out.println("Email updated for " + pgToUpdateEml.getName() + " to " + pgToUpdateEml.getEmail());
} else {
System.out.println("Passenger with email " + currentEmail + " not found.");
}
}
public void deletePassenger(String email) {
Passenger pgToDelete = search(email);
if (pgToDelete != null) {
int hashCode = hash(pgToDelete);
LinkedList bucketToDeleteFrom = hashTable[hashCode];
bucketToDeleteFrom.deleteEmail(pgToDelete.getEmail());
if (bucketToDeleteFrom.head == null) {
hashTable[hashCode] = null; // Setting the bucket to null if its found empty
}
System.out.println("Passenger with email " + email + " has been DELETED.");
}
// else {
// System.out.println("Passenger with email " + email + " NOT found .");
// }
}
}