-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathUserp2.java
153 lines (125 loc) · 2.82 KB
/
Userp2.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
I right click the method that has the Override and I select the option Generate Hash Code and Equals. I check firstName and lastName and this will the User class to this
//I will override the address output
import java.util.*;
public class User
{
private String fN;
public String lN;
static void log(Object o)
{
System.out.print(o);
}
static void logln(Object o)
{
System.out.println(o);
}
public String getFullName()
{
return getFirstName() + " " + lN.toUpperCase();
}
public String output()
{
return "Hi, my name is "+ getFirstName() + " " + getLastName() + ".";
}
//Now I will overload output
public String output(boolean nice)
{
if(nice)
{
return "You\'re an awesome person"+ " " + getFullName() + ".";
}
return "You\'re an idiot"+ " " + getFullName() + ".";
}
public String getFirstName()
{
return fN;
}
//Setter not returning anything ∴ void
public void setFirstName(String firstName)
{
//Assigning the fName value we pass in to the field firstName
fN=firstName.trim();
//the .strip remove any whitespaces before output.
}
public String getLastName()
{
return lN;
}
//Setter not returning anything ∴ void
public void setLastName(String lastName)
{
//Assigning the lN value we pass in to the field lastName
lN=lastName;
}
public static void printUsers(List<User> users)
{
//I want to iterate through the users
//For each User o in users)
for(User o:users)
{
logln(o.getFullName());
}
}
public static int searchList(List<User> users, String fN, String lN)
{
return searchList(users, fN + " " + lN);
}
//OVERLOAD
public static int searchList(List<User> users, String fullName)
{
//iterate through the list
for(int i=0;i<users.size();i++)
{
//Use .equals instead of ==
if(users.get(i).getFullName().equals(fullName))
{
return i;
}
}
//if no match
return -1;
}
//Now I will override the ToString
@Override
public String toString()
{
//To override remember to give the method to override the same name
return " " + getFullName() + "]";
}
//Intended to put obj in a hash table ∴ turn the hash code into a number
@Override
public int hashCode()
{
final int prime = 19;
int res = 1;
res= prime * res + ((firstName==null) ? 0 : firstName.hashCode());
res= prime * res + ((lastName==null) ? 0 : lastName.hashCode());
return res;
}
@Override
public boolean equals(Object obj)
{
if(this == obj)
return true;
if(obj == null)
return false;
if(getClass() != obj.getClass())
return false;
User other = (User) obj;
if(firstName == null)
{
if(other.firstName != null)
return false;
}
else if(!firstName.equals(other.firstName))
return false
if(lastName == null)
{
if(other.lastName != null)
return false;
}
else if(!lastName.equals(other.lastName))
return false;
return true;
}
}