In: Computer Science
Carly’s Catering provides meals for parties and special events. In previous chapters, you have developed a class that holds catering event information and an application that tests the methods using four objects of the class. Now modify the Event and EventDemo classes as follows:
• Modify the method that sets the event number in the Event class so that if the argument passed to the method is not a four-character String that starts with a letter followed by three digits, then the event number is forced to A000. If the initial letter in the event number is not uppercase, force it to be so.
• Add a contact phone number field to the Event class.
• Add a set method for the contact phone number field in the Event class. Whether the user enters all digits or any combination of digits, spaces, dashes, dots, or parentheses for a phone number, store it as all digits. For example, if the user enters (920) 872-9182, store the phone number as 9208729182. If the user enters a number with fewer or more than 10 digits, store the number as 0000000000.
• Add a get method for the phone number field. The get method returns the phone number as a String constructed as follows: parentheses surround a three-digit area code, followed by a space, followed by the three-digit phone exchange, followed by a hyphen, followed by the last four digits of the phone number.
• Modify the EventDemo program so that besides the event number and guests, the program also prompts the user for and retrieves a contact phone number for each of the sample objects. Display the phone number along with the other Event details. Test the EventDemo application to make sure it works correctly with valid and invalid event and phone numbers. Save the files as Event.java and EventDemo.java
Since your previously written class Event and class EventDemo is not provided so i have only written the portions which were asked to be added / modified in the program such that this code yields all required output based on the data/instructions provided. The other details that were to be taken from your previous chapters or programs can be combined with this code.
CODE:
import java.util.Arrays;
import java.util.Scanner;
class Event {
String eventNumber;
String phoneNumber;
// setEventNumber() method sets the event number entered by user which is then passed as an argument to this method as per the requirments
void setEventNumber(String x) {
// if length of srting entered ny user is 4 then the string is further tested to satisfy other conditions
if (x.length() == 4) {
// check if the first character of string x is a digit and the other 3 characters are digits
// Character.isLetter(x.charAt(0)) returns true if character at index 0 of string x is a letter and Character.isDigit(x.charAt(i)) returns true if character at index 'i'=1,2,3 of string x is a digit
// when all 4 conditions return true then proceed further to check other conditions
if (Character.isLetter(x.charAt(0)) && Character.isDigit(x.charAt(1)) && Character.isDigit(x.charAt(2)) && Character.isDigit(x.charAt(3))) {
//check if first charater of string x is in uppercase
if (!Character.isUpperCase(x.charAt(0))) { //when this condition returns false convert it to uppercase using toUpperCase()
Character.toUpperCase(x.charAt(0));
}
this.eventNumber = x; //the eventNumber is set to x when x satisfies all conditions
}
else { //eventNumber set to "A000" when charactres of x do not satisfy the conditions
this.eventNumber = "A000";
}
}
// if length of srting entered ny user is not = 4 then the event number is set to A000
else {
this.eventNumber = "A000";
}
}
// this sets phone number to desired format by taking string n as a parameter which is a string entered by user
void setPhoneNumber(String n) {
String num ="" ; //an empty string used to store resulting phone number
for (int i = 0; i < n.length(); i++) {
if (Character.isDigit(n.charAt(i))) { //if character is a digit then add/concatenate it to string num
num = num + n.charAt(i);
}
}
if (num.length() == 10) //check if phone number has 10 digits else set phone number to 0000000000
this.phoneNumber = num;
else
this.phoneNumber = "0000000000";
}
String getPhoneNumber() {
String phone;
phone = "(" + this.phoneNumber.substring(0, 3) + ") " + this.phoneNumber.substring(3, 6) + "-"
+ phoneNumber.substring(6);
return phone; // returns phone number in desired format
}
}
class EventDemo {
public static void main(String[] args)throws Exception {
Scanner sc = new Scanner(System.in);
Event e1 = new Event();
System.out.println("Enter Event number");
String eventNum = sc.nextLine();
System.out.println("Enter Phone number");
String phoneNum = sc.nextLine();
e1.setEventNumber(eventNum);
e1.setPhoneNumber(phoneNum);
String phno = e1.getPhoneNumber();
System.out.println("EVENT DETAILS of event e1");
System.out.println("Event NUmber: "+e1.eventNumber);
System.out.println("Phone NUmber: "+phno);
sc.close();
}
}
SCREENSHOT OF CODE:-
OUTPUT: