· 7 years ago · May 09, 2019, 06:32 AM
1
2public class Foothill {
3
4 public static void main(String[] args) {
5 Message message1, message2, message3;
6
7 message1 = new Message();
8 message2 = new Message("hello ok hi bye");
9 message3 = new Message("123");
10
11 System.out.println(message1.toString());
12 System.out.println(message2.toString());
13 System.out.println(message3.toString());
14
15 message1.setMessage("i just set a new message, that's cool!");
16 message2.setMessage("hi");
17 message3.setMessage("alright, is this message long enough for you??");
18
19 System.out.println(message1.toString());
20 System.out.println(message2.toString());
21 System.out.println(message3.toString());
22
23 Email email1, email2, email3;
24
25 email1 = new Email();
26 email2 = new Email("this is your message to me", "bob@gmail.com",
27 "thatonefriend@gmail.com");
28 email3 = new Email("whoa", "@.", "amazing@gmail.com");
29
30 System.out.println(email1.toString());
31 System.out.println(email2.toString());
32 System.out.println(email3.toString());
33
34 System.out.println("\ntesting Email accessors:");
35 System.out.println(email2.getFromAddress());
36 System.out.println(email2.getToAddress());
37
38 System.out.println("\ntesting Email mutators:");
39 if (email3.setFromAddress("john.wick@continental.com")) {
40 System.out.println(" successfully set email as "
41 + email3.getFromAddress());
42 }
43 else {
44 System.out.println(" failed so set email");
45 }
46
47 if (email3.setToAddress("aa@gmail.com")) {
48 System.out.println(" successfully set email as "
49 + email3.getToAddress());
50 }
51 else {
52 System.out.println(" failed so set email");
53 }
54
55 Shweet shweet1, shweet2, shweet3;
56
57 shweet1 = new Shweet();
58 shweet2 = new Shweet("I am an intelligent doggo", "mydog");
59
60
61
62 System.out.println(shweet1.toString());
63 System.out.println(shweet2.toString());
64
65 System.out.println("testing Shweet accessors:");
66 System.out.println(shweet2.getFromID());
67
68 System.out.println("\ntesting Shweet mutators:");
69
70 if (shweet2.setFromID("1nva1iD name")) {
71 System.out.println("acceptable shwitter ID");
72 }
73 else {
74 System.out.println("bad swhitter ID");
75 }
76 System.out.println(shweet2.toString());
77
78 if (shweet2.setFromID("mycat")) {
79 System.out.println("acceptable shwitter ID");
80 }
81 else {
82 System.out.println("bad swhitter ID");
83 }
84 System.out.println(shweet2.toString());
85
86
87 }
88}
89
90//Base class that contains basic elements for a message, class constants that
91//define length, and a private data String member called message
92class Message {
93
94 //define class constants, protected so subclasses can access
95 protected static final int MIN_MSG_LENGTH = 1;
96 protected static final int MAX_MSG_LENGTH = 100000;
97 protected static final String DFLT_MSG = "default_message";
98 protected static final String MSG_BORDER = "\n Message -------------------";
99 protected static final String NEW_LINE = "\n";
100
101 //define private member data
102 private String message;
103
104 //nonparamterized constructor
105 public Message() {
106 message = DFLT_MSG;
107 }
108
109
110 //paramterized constructor, checks bounds with a mutator
111 public Message(String message) {
112 setMessage(message);
113
114 }
115
116
117 //mutator that uses a helper class to determine if message is valid
118 public boolean setMessage(String message) {
119 if (!isValidMessage(message)) {
120 this.message = DFLT_MSG;
121 return false;
122
123 }
124
125 this.message = message;
126 return true;
127 }
128
129 //accessor
130 public String getMessage() {
131 return message;
132 }
133
134 //custom toString method
135 public String toString() {
136 return MSG_BORDER + NEW_LINE + message + NEW_LINE;
137 }
138
139 //helper class to check to see if message is valid based on given bounds
140 private static boolean isValidMessage(String message) {
141 if (message.length() < MIN_MSG_LENGTH ||
142 message.length() > MAX_MSG_LENGTH) {
143 return false;
144 }
145
146 return true;
147 }
148}
149
150
151//this is a derived class from Message, email contains everything from the
152//Message class as well as additional member data
153class Email extends Message {
154
155 //define class constants
156 public static final int MIN_EMAIL_ADDRESS_LENGTH = 3;
157 public static final int MAX_EMAIL_ADDRESS_LENGTH = 254;
158 public static final String DFLT_EMAIL_ADDRESS = "default@gmail.com";
159 public static final String FROM_HEADER = "From: ";
160 public static final String TO_HEADER = "To: ";
161
162 //private member data,
163 private String fromAddress;
164 private String toAddress;
165
166 //default non-paramaterized constructor
167 public Email() {
168 super();
169 fromAddress = DFLT_EMAIL_ADDRESS;
170 toAddress = DFLT_EMAIL_ADDRESS;
171 }
172
173
174 //paramaterized constructor , uses mutators to check for valid paramters
175 public Email(String message, String fromAddress, String toAddress) {
176 super(message);
177 setFromAddress(fromAddress);
178 setToAddress(toAddress);
179
180 }
181
182
183
184 //mutator that checks for valid email address
185 public boolean setFromAddress(String emailAddress) {
186 if(!isValidEmailAddress(emailAddress)) {
187 fromAddress = DFLT_EMAIL_ADDRESS;
188 return false;
189 }
190
191 fromAddress = emailAddress;
192 return true;
193 }
194
195 //mutator that checks for valid email address, similar to the one above
196 public boolean setToAddress(String emailAddress) {
197
198 if(!isValidEmailAddress(emailAddress)) {
199 toAddress = DFLT_EMAIL_ADDRESS;
200 return false;
201 }
202
203 toAddress = emailAddress;
204 return true;
205
206 }
207
208 //accessor for the fromAddress
209 public String getFromAddress() {
210
211 return fromAddress;
212
213 }
214
215 //accessor for the toAddress
216 public String getToAddress() {
217
218 return toAddress;
219
220 }
221
222 //overridden toString method based on the derived class
223 public String toString() {
224
225 String output;
226 output = FROM_HEADER + fromAddress + "\n" + TO_HEADER + toAddress;
227
228 return output + super.toString();
229
230 }
231
232 //private static validation helper method for the main constructor,
233 //utilizes two helper methods containsEssentialChars and
234 //validateEmailAddressBody
235 private static boolean isValidEmailAddress(String emailAddress) {
236
237 if (!containsEssentialChars(emailAddress) ||
238 !validateEmailAddressBody(emailAddress) ) {
239 return false;
240 }
241
242 return true;
243
244 }
245
246 //private static helper method that checks to see if email address
247 //contains the essential chars '@' and '.'
248 private static boolean containsEssentialChars(String emailAddress) {
249
250 return emailAddress.contains("@") && emailAddress.contains(".");
251
252 }
253
254 //private static helper method that checks length of actual address rather than
255 //include the @gmail.com or @whatever.com part
256 private static boolean validateEmailAddressBody(String emailAddress) {
257
258 //separates the actual address of the email, ex. "myname@gmail.com"
259 //would produce the string "myname"
260 String emailAddressBody =
261 emailAddress.substring(0, emailAddress.indexOf('@'));
262
263 if (emailAddressBody.length() < MIN_EMAIL_ADDRESS_LENGTH ||
264 emailAddressBody.length() > MAX_EMAIL_ADDRESS_LENGTH) {
265 return false;
266 }
267 else {
268 return true;
269 }
270
271 }
272
273}
274
275
276//derived class from Message, it contains the message data and some overriden
277//base methods such as toString and setMessage
278class Shweet extends Message {
279
280 //define class constants
281 public static final int MAX_SWHITTER_ID_LENGTH = 15;
282 public static final int MAX_SHWEET_LENGTH = 140;
283 public static final String DEFAULT_USER_ID = "defaultID";
284
285 //private member data
286 private String fromID;
287
288 //default non-paramaterized constructor
289 public Shweet() {
290 super();
291 fromID = DEFAULT_USER_ID;
292 }
293
294
295 //paramtartized constructor that uses class mutators to validate input
296 public Shweet(String message, String fromID) {
297 setMessage(message);
298
299 setFromID(fromID);
300 }
301
302
303 //mutator that checks for valid input based on a private helper method
304 public boolean setFromID(String userID) {
305 if (!isValidShwitterID(userID)) {
306 fromID = DEFAULT_USER_ID;
307 return false;
308 }
309
310 fromID = userID;
311 return true;
312 }
313
314 //accessor for private member data fromID
315 public String getFromID() {
316
317 return fromID;
318
319 }
320
321 //overridden toString method based on the base class
322 public String toString() {
323
324 return "\nShweet: @" + fromID + "\n" + super.getMessage() + "\n";
325
326 }
327
328 //overridden setMessage method based from the base class, uses the helper
329 //method isValidShweet to ensure that a message is within 140 characters
330 public boolean setMessage(String message) {
331 if(!isValidShweet(message)) {
332 super.setMessage(DFLT_MSG);
333 return false;
334 }
335
336 super.setMessage(message);
337 return true;
338 }
339
340 //private helper method that validates the length of a message based on
341 //class constants from the base class and from the Shweet class, essentially
342 //provides stricter limits for message length
343 private static boolean isValidShweet(String message) {
344
345 if (message.length() < MIN_MSG_LENGTH ||
346 message.length() > MAX_SHWEET_LENGTH) {
347 return false;
348 }
349
350 return true;
351 }
352
353 //private helper method that checks for valid Shwitter ID
354 private static boolean isValidShwitterID(String userID) {
355 boolean isValidLength = (userID != null) &&
356 (userID.length() < MAX_SWHITTER_ID_LENGTH);
357
358 return isValidLength && stringHasOnlyAlphaOrNumOrUnderScore(userID);
359 }
360
361 //private helper method that's used to help check for valid ShwitterID
362 //essentially ensures the ID is alphanumeric only
363 private static boolean stringHasOnlyAlphaOrNumOrUnderScore(String userID) {
364
365 for(int i = 0; i < userID.length(); i++) {
366 if ( !(('A' <= userID.charAt(i) && 'Z' >= userID.charAt(i))
367 ||
368 ('a' <= userID.charAt(i) && 'z' >= userID.charAt(i))
369 ||
370 ('0' <= userID.charAt(i) && '9' >= userID.charAt(i))
371 ||
372 userID.charAt(i) == '_'
373
374 )) {
375 return false;
376 }
377 }
378 return true;
379 }
380}