Carly\'s Catering provides meals for parties and special events. In previous cha
ID: 3669453 • Letter: C
Question
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) 8729182, 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 EventDemoapplication to make sure it works correctly with valid and invalid event and phone numbers.
Explanation / Answer
/**The Event class has methods to set contact number, set event nam
* eand number of guests and get methods to return contact number,
* event name and number of guests*/
//Event.java
public class Event
{
public static final int PRICE_PER_GUEST=35;
public static final int CUTOFF=50;
private String eventNumber;
private int numGuests;
private double price;
private String phoneNumber;
//default constructor
public Event()
{
eventNumber="";
numGuests=0;
price=0;
phoneNumber="";
}
//Set phone number
public void setContactNumber(String phoneNumber)
{
if(!valid(phoneNumber) &&phoneNumber.length()>10 ||phoneNumber.length()<10 )
{
this.phoneNumber="0000000000";
}
else
this.phoneNumber=phoneNumber;
}
//Returns true if the phone number is of valid type
private boolean valid(String phoneNumber)
{
boolean correctFormat=true;
int count=0;
for (int i = 0; i < phoneNumber.length(); i++)
{
if(Character.isDigit(phoneNumber.charAt(i)))
count++;
}
if(count==10)
correctFormat=true;
else
correctFormat=false;
return correctFormat;
}
//Returns the phone number as a format of (XXX)XXX-XXXX
public String getPhoneNumber()
{
return "("+phoneNumber.substring(1, 4)+")"
+phoneNumber.substring(6, 9)+"-"+phoneNumber.substring(10, phoneNumber.length());
}
//Set event number
public void setEventNumber(String eventNumber)
{
this.eventNumber=eventNumber;
if(eventNumber.length()!=4)
eventNumber="A000";
else if(eventNumber.length()==4 && !Character.isUpperCase(eventNumber.charAt(0)))
{
char ch=Character.toUpperCase(eventNumber.charAt(0));
this.eventNumber=ch+eventNumber.substring(1,eventNumber.length());
}
}
//Set number of guests
public void setGuests(int numGuests)
{
this.numGuests=numGuests;
price=PRICE_PER_GUEST*numGuests;
}
//Returns event number
public String getEventNumber()
{
return eventNumber;
}
//Set number of guests
public int getNumGuests()
{
return numGuests;
}
//Set price
public double getPrice()
{
return price;
}
}
------------------------------------------------------------------------------------------------------------------------------------------------
/**The java program EventDemo that prompts user to enter the even number,
* number of guests and phone number from console and prints the event
* name, price and phone number to the console*/
//EventDemo.java
import java.util.Scanner;
public class EventDemo
{
public static void main(String[] args)
{
Scanner scanner=new Scanner(System.in);
String phoneNumber="";
String eventNumber;
int numGuests;
System.out.println("Enter event number");
eventNumber=scanner.nextLine();
System.out.println("Enter number of guest");
numGuests=Integer.parseInt(scanner.nextLine());
System.out.println("Enter phone number");
phoneNumber =scanner.nextLine();
//Create an event object
Event event=new Event();
//Call setEventNumber
event.setEventNumber(eventNumber);
//Call setGuests
event.setGuests(numGuests);
//Call setContacatNumber
event.setContactNumber(phoneNumber);
//print name, guests and phone number
System.out.println("Event Number : ");
System.out.println(event.getEventNumber());
System.out.println("Number of Guest : ");
System.out.println(event.getNumGuests());
System.out.println("Phone Number : ");
System.out.println(event.getPhoneNumber());
System.out.println("Price : ");
System.out.println(event.getPrice());
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------
Sample output:
Enter event number
m100
Enter number of guest
10
Enter phone number
(920) 8729182
Event Number :
M100
Number of Guest :
10
Phone Number :
(920)872-9182
Price :
350.0