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
package string;
// Defines a class Event
class Event
{
// Instance variables to store event and phone number
String eventNumber;
String phoneNumber;
// Method to validate and set event number
void setEventNumber(String en)
{
// Checks if the length of the parameter event number length is 4
if(en.length() == 4)
{
// Checks if the first character of the parameter event number
// is an alphabet
if( (en.charAt(0) >= 'a' && en.charAt(0) <= 'z') ||
(en.charAt(0) >= 'A' && en.charAt(0) <= 'Z'))
{
// Checks rest 3 character of parameter event number is digit
if( (en.charAt(1) >= '0' && en.charAt(1) <= '9') &&
(en.charAt(2) >= '0' && en.charAt(2) <= '9') &&
(en.charAt(3) >= '0' && en.charAt(3) <= '9'))
{
// Converts to upper chase
en = en.toUpperCase();
// Assigns parameter event number to instance variable
eventNumber = en;
}// End of if condition
}// End of if condition
}// End of if condition
// Otherwise assigns default value
else
eventNumber = "A000";
}// End of method
// Method to validate and set phone number
void setPhoneNumber(String pn)
{
// To store number of digits. Initially zero
int countDigit = 0;
// Assigns left parenthesis for formated phone number
String phone = "(";
// To store only digits from the parameter phone number
String number = "";
// Loops till end of the parameter phone number
for(int c = 0; c < pn.length(); c++)
{
// Checks if current character is a digit
if(pn.charAt(c) >= '0' && pn.charAt(c) <= '9')
{
// Increase the digit counter
countDigit++;
// Concatenates the current digit to number
number += pn.charAt(c);
}// End of if condition
}// End of for loop
// Checks if number of digits is not equals to 10 assigns default value
if(countDigit != 10)
number = "0000000000";
// Loops 3 times to concatenate digits for area code
for(int c = 0; c < 3; c++)
phone += number.charAt(c);
// Concatenates right parenthesis
phone += ") ";
// Loops 3 times to concatenate digits for phone exchange
for(int c = 3; c < 6; c++)
phone += number.charAt(c);
// Concatenates hyphen character
phone += "-";
// Loops 4 times to concatenate digits for phone number
for(int c = 6; c < 10; c++)
phone += number.charAt(c);
// Assigns the formated phone number to instance variable
phoneNumber = phone;
}// End of method
// Method to return event number
String getEventNumber()
{
return eventNumber;
}// End of method
// Method to return phone number
String getPhoneNumber()
{
return phoneNumber;
}// End of method
}// End of class Event
// Driver class definition
public class EventDemo
{
// main method definition
public static void main(String []ss)
{
// Declares an object of class Event
Event e1 = new Event();
System.out.print("\n\n Test Event Number (P1111): ");
// Calls the method to validate and set event number
e1.setEventNumber("P1111");
// Calls the method to display event number
System.out.print("\n Event Number: " + e1.getEventNumber());
System.out.print("\n\n Test Event Number (PA111): ");
// Calls the method to validate and set event number
e1.setEventNumber("PA11");
// Calls the method to display event number
System.out.print("\n Event Number: " + e1.getEventNumber());
System.out.print("\n\n Test Event Number (1A11): ");
// Calls the method to validate and set event number
e1.setEventNumber("1A11");
// Calls the method to display event number
System.out.print("\n Event Number: " + e1.getEventNumber());
System.out.print("\n\n Test Event Number (p111): ");
// Calls the method to validate and set event number
e1.setEventNumber("P111");
// Calls the method to display event number
System.out.print("\n Event Number: " + e1.getEventNumber());
System.out.print("\n\n Test Phone Number (111-2222): ");
// Calls the method to validate and set phone number
e1.setPhoneNumber("111-2222");
// Calls the method to display event number
System.out.print("\n Phone Number: " + e1.getPhoneNumber());
System.out.print("\n\n Test Phone Number (111(2222)3359): ");
// Calls the method to validate and set phone number
e1.setPhoneNumber("111(2222)3359");
// Calls the method to display event number
System.out.print("\n Phone Number: " + e1.getPhoneNumber());
System.out.print("\n\n Test Phone Number (111(222)3359): ");
// Calls the method to validate and set phone number
e1.setPhoneNumber("111(222)3359");
// Calls the method to display event number
System.out.print("\n Phone Number: " + e1.getPhoneNumber());
}// End of method
}// End of driver class
Sample Output:
Test Event Number (P1111):
Event Number: A000
Test Event Number (PA111):
Event Number: A000
Test Event Number (1A11):
Event Number: A000
Test Event Number (p111):
Event Number: P111
Test Phone Number (111-2222):
Phone Number: (000) 000-0000
Test Phone Number (111(2222)3359):
Phone Number: (000) 000-0000
Test Phone Number (111(222)3359):
Phone Number: (111) 222-3359