-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathUser.java
187 lines (154 loc) · 3.35 KB
/
User.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import java.util.*;
public class User
{
private String firstName;
public String lastName;
static void log(Object o)
{
System.out.print(o);
}
static void logln(Object o)
{
System.out.println(o);
}
public String getFullName()
{
return getFirstName() + " " + lastName.toUpperCase();
}
public String output()
{
return "Hi, my name is "+ getFirstName() + " " + getLastName() + ".";
}
public String getFirstName()
{
return firstName;
}
//Setter not returning anything ∴ void
public void setFirstName(String fN)
{
/*
if(fN == null)
{
logln("It's null");
}
*/ //Not Null I tried running it
//Assigning the fName value we pass in to the field firstName
firstName=fN.trim();
//the .strip remove any whitespaces before output.
}
public String getLastName()
{
return lastName;
}
//Setter not returning anything ∴ void
public void setLastName(String lN)
{
//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());
}
}
//return type will be User. However we cannot have the same parameters. This
//will not be a valid overload. So we either have to name it differently or change
//parameter list
public static User searchForUser(List<User> users, User o)
{
//Creating a for Each loop. (For each user in the users list of Type User)
for(User user:users)
{
if(user.equals(o))
{
return user;
}
}
//If we loop through and find nothing then we return null;
return null;
}
public static int searchList(List<User> users, User o)
{
return searchList(users, 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;//because return type in method is an integer
}
/*
public void changeSth(int x)
{
x++;
}
*/
public void changeSth(User x)
{
x.setFirstName(“Roberto”);
}
//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;
}
}