|
| 1 | +//I will override the address output |
| 2 | +import java.util.*; |
| 3 | +public class User |
| 4 | +{ |
| 5 | + private String firstName; |
| 6 | + public String lastName; |
| 7 | + |
| 8 | + static void log(Object o) |
| 9 | + { |
| 10 | + System.out.print(o); |
| 11 | + } |
| 12 | + |
| 13 | + static void logln(Object o) |
| 14 | + { |
| 15 | + System.out.println(o); |
| 16 | + } |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + public String getFullName() |
| 21 | + { |
| 22 | + return getFirstName() + " " + lastName.toUpperCase(); |
| 23 | + } |
| 24 | + |
| 25 | + public String output() |
| 26 | + { |
| 27 | + return "Hi, my name is "+ getFirstName() + " " + getLastName() + "."; |
| 28 | + } |
| 29 | + |
| 30 | + //Now I will overload output |
| 31 | + public String output(boolean nice) |
| 32 | + { |
| 33 | + if(nice) |
| 34 | + { |
| 35 | + return "You\'re an awesome person"+ " " + getFullName() + "."; |
| 36 | + |
| 37 | + } |
| 38 | + return "You\'re an idiot"+ " " + getFullName() + "."; |
| 39 | + } |
| 40 | + |
| 41 | + public String getFirstName() |
| 42 | + { |
| 43 | + return firstName; |
| 44 | + } |
| 45 | + |
| 46 | + //Setter not returning anything ∴ void |
| 47 | + public void setFirstName(String fN) |
| 48 | + { |
| 49 | + //Assigning the fName value we pass in to the field firstName |
| 50 | + fN=firstName; |
| 51 | + //the .trim at the end will raise a null pointer exception because |
| 52 | + //it doesn't know what are we referencing |
| 53 | + //the .strip remove any whitespaces before output. |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + public String getLastName() |
| 58 | + { |
| 59 | + return lastName; |
| 60 | + } |
| 61 | + |
| 62 | + //Setter not returning anything ∴ void |
| 63 | + public void setLastName(String lN) |
| 64 | + { |
| 65 | + //Assigning the lN value we pass in to the field lastName |
| 66 | + lN=lastName; |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + public static void printUsers(List<User> users) |
| 71 | + { |
| 72 | + //I want to iterate through the users |
| 73 | + //For each User o in users) |
| 74 | + for(User o:users) |
| 75 | + { |
| 76 | + logln(o.getFullName()); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public static int searchList(List<User> users, String fN, String lN) |
| 81 | + { |
| 82 | + return searchList(users, fN + " " + lN); |
| 83 | + } |
| 84 | + |
| 85 | + //OVERLOAD |
| 86 | + public static int searchList(List<User> users, String fullName) |
| 87 | + { |
| 88 | + //iterate through the list |
| 89 | + for(int i=0;i<users.size();i++) |
| 90 | + { |
| 91 | + //Use .equals instead of == |
| 92 | + if(users.get(i).getFullName().equals(fullName)) |
| 93 | + { |
| 94 | + return i; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + //if no match |
| 99 | + return -1; |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + //Now I will override the ToString |
| 104 | + @Override |
| 105 | + public String toString() |
| 106 | + { |
| 107 | + //To override remember to give the method to override the same name |
| 108 | + return " " + getFullName() + "]"; |
| 109 | + } |
| 110 | + |
| 111 | + //Intended to put obj in a hash table ∴ turn the hash code into a number |
| 112 | + @Override |
| 113 | + public int hashCode() |
| 114 | + { |
| 115 | + final int prime = 19; |
| 116 | + int res = 1; |
| 117 | + res= prime * res + ((firstName==null) ? 0 : firstName.hashCode()); |
| 118 | + res= prime * res + ((lastName==null) ? 0 : lastName.hashCode()); |
| 119 | + return res; |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public boolean equals(Object obj) |
| 124 | + { |
| 125 | + if(this == obj) |
| 126 | + { |
| 127 | + return true; |
| 128 | + } |
| 129 | + if(obj == null) |
| 130 | + { |
| 131 | + return false; |
| 132 | + } |
| 133 | + if(getClass() != obj.getClass()) |
| 134 | + { |
| 135 | + return false; |
| 136 | + } |
| 137 | + |
| 138 | + User other = (User) obj; |
| 139 | + |
| 140 | + if(firstName == null){ |
| 141 | + if(other.firstName != null) |
| 142 | + { |
| 143 | + return false; |
| 144 | + } |
| 145 | + } |
| 146 | + else if(!firstName.equals(other.firstName)) |
| 147 | + { |
| 148 | + logln(false); |
| 149 | + |
| 150 | + if(lastName == null) |
| 151 | + { |
| 152 | + if(other.lastName != null) |
| 153 | + { |
| 154 | + return false; |
| 155 | + } |
| 156 | + } |
| 157 | + else if(!lastName.equals(other.lastName)) |
| 158 | + { |
| 159 | + return false; |
| 160 | + } |
| 161 | + } |
| 162 | + return true; |
| 163 | + } |
| 164 | +} |
0 commit comments