|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.List; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class User |
| 6 | +{ |
| 7 | + private String lN; |
| 8 | + private String fN; |
| 9 | + |
| 10 | + static void log(Object o) |
| 11 | + { |
| 12 | + System.out.print(o); |
| 13 | + } |
| 14 | + |
| 15 | + static void logln(Object o) |
| 16 | + { |
| 17 | + System.out.println(o); |
| 18 | + } |
| 19 | + |
| 20 | + //Setter not returning anything ∴ void |
| 21 | + public void setFirstName(String firstName) |
| 22 | + { |
| 23 | + //Assigning the fName value we pass in to the field firstName |
| 24 | + fN=firstName.toLowerCase(); |
| 25 | + //the .strip remove any whitespaces before output. |
| 26 | + |
| 27 | + } |
| 28 | + |
| 29 | + public String getFullName() |
| 30 | + { |
| 31 | + return getFirstName() + " " + lN.toUpperCase(); |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | + public String getLastName() |
| 36 | + { |
| 37 | + return lN; |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + public String output() |
| 42 | + { |
| 43 | + return "Hi, my name is "+ getFirstName() + " " + getLastName() + "."; |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + public String output(boolean nice) |
| 48 | + { |
| 49 | + if(nice) |
| 50 | + { |
| 51 | + return "You\re an awesome person"+ " " + getFullName() + "."; |
| 52 | + |
| 53 | + } |
| 54 | + return "You\'re an idiot"+ " " + getFullName() + "."; |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + public void setLastName(String lastName) |
| 60 | + { |
| 61 | + //Assigning the lN value we pass in to the field lastName |
| 62 | + lN=lastName; |
| 63 | + } |
| 64 | + |
| 65 | + public String getFirstName() |
| 66 | + { |
| 67 | + return fN.toUpperCase(); |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + public static void printUsers(List<User> users) |
| 72 | + { |
| 73 | + //I want to iterate through the users |
| 74 | + //For each User o in users) |
| 75 | + for(User o:users) |
| 76 | + { |
| 77 | + logln(o.getFullName()); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + public String toString() |
| 83 | + { |
| 84 | + return fN+lN; |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + public static void main(String [] args) |
| 89 | + { |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + User myself = new User(); |
| 94 | + myself.setFirstName("Omar"); |
| 95 | + myself.setLastName("Belkady"); |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + /* User him = new User(); |
| 100 | + /*him.setFirstName("Alan "); |
| 101 | + him.setLastName(null); |
| 102 | + logln(him.toString()); |
| 103 | + */ |
| 104 | + User she = new User(); |
| 105 | + she.setFirstName("Angela "); |
| 106 | + she.setLastName(""); |
| 107 | + //a blank string is not the same as NULL. This WILL NOT PRINT ANGELA NULL |
| 108 | + logln(she); |
| 109 | + //Should output Alan null; |
| 110 | + //This will trigger a null pointer exception because we are trying to referene a null valeu |
| 111 | + |
| 112 | + List<User> users = new ArrayList<User>(); |
| 113 | + users.add(myself); |
| 114 | + //users.add(him); |
| 115 | + |
| 116 | + //Pass in an entire list |
| 117 | + User.printUsers(users); |
| 118 | + |
| 119 | + logln(myself.output()); |
| 120 | + |
| 121 | + logln(myself.output());//nice message |
| 122 | + logln(myself.output());//mean message |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments