In Java: Write a program that is a publisher/subscriber program. There should be
ID: 3776183 • Letter: I
Question
In Java: Write a program that is a publisher/subscriber program. There should be at least one student class with one student with info (generic email and phone number for text and email class), email class, text class which calls to the students info and sends out a text with the message (type of disaster), and a disaster class with different disasters that can happen(such as active shooter, earthquake, flood warning). Program can be completed using whatever methods make most sense but should be modifiable and not too complicated.
Explanation / Answer
1. Create class Student.java
package com.java.publisher;
import java.util.Scanner;
public class Student {
/**
* @param args
*/
public static void main(String[] args) {
String emailId;
String phoneNumber;
String disasterType;
Scanner scan = new Scanner(System.in);
System.out.println("Enter Student emailId");
emailId = scan.next();
if(new Email(emailId).validate()){
System.out.println("Enter Student phone number");
phoneNumber = scan.next();
if(new Text(phoneNumber).validatePhoneNumber()){
System.out.println("Enter type of disaster");
disasterType= scan.next();
if(new Disaster(disasterType).disasterType()){
Text text = new Text();
text.printText(disasterType);
} else {
}
} else {
System.out.println("invalid phone number");
}
} else {
System.out.println("invalid emailId");
}
}
}
2. Create class Email.java
package com.java.publisher;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Email {
// emailId from Student info
private String emailId;
public Email(String emailId) {
super();
this.emailId = emailId;
}
// @return the emailId
public String getEmailId() {
return emailId;
}
// @param emailId the emailId to set
public void setEmailId(String emailId) {
this.emailId = emailId;
}
// Pattern for Valid emailId
public static final Pattern VALID_EMAIL_ADDRESS_REGEX = Pattern.compile(
"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$",
Pattern.CASE_INSENSITIVE);
// return results of emailId validation
public boolean validate() {
Matcher matcher = VALID_EMAIL_ADDRESS_REGEX.matcher(emailId);
return matcher.find();
}
}
3. Create class Text.java
package com.java.publisher;
public class Text {
private String phoneNo;
public Text() {
super();
}
public Text(String phoneNo) {
super();
this.phoneNo = phoneNo;
}
public boolean validatePhoneNumber() {
//validate phone numbers of format "1234567890"
if (phoneNo.matches("\d{10}")) {
return true;
}
//validating phone number with -, . or spaces
else if(phoneNo.matches("\d{3}[-\.\s]\d{3}[-\.\s]\d{4}")) {
return true;
}
//validating phone number with extension length from 3 to 5
else if(phoneNo.matches("\d{3}-\d{3}-\d{4}\s(x|(ext))\d{3,5}")) {
return true;
}
//validating phone number where area code is in braces ()
else if(phoneNo.matches("\(\d{3}\)-\d{3}-\d{4}")) {
return true;
}
//return false if nothing matches the input
else {
return false;
}
}
public void printText(String disasterType){
System.out.println("Disaster : " + disasterType);;
}
}
4. Create class Disaster.java
package com.java.publisher;
public class Disaster {
private String disasterType = "";
public Disaster(String disasterType) {
super();
this.disasterType = disasterType;
}
public boolean disasterType() {
if(disasterType.equalsIgnoreCase("Earthquake") ||
disasterType.equalsIgnoreCase("Active shooter") ||
disasterType.equalsIgnoreCase("Flood warning")) {
return true;
}
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.