Chapter 7 - Case Problems - Page 391 1. Carly\'s Catering provides meals for par
ID: 3698257 • Letter: C
Question
Chapter 7 - Case Problems - Page 391
1. 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.
Save the file as Event.java and EventDemo.java
Explanation / Answer
import java.util.Scanner;
/***
* The Event class that represents the object of Event class
* that contains methods to set event number, phone number
* and returns event number,phone number
* */
//Event.java
public class Event
{
public static double pricePerGuestHigh = 35.00;
public static double pricePerGuestLow = 32.00;
public static final int LARGE_EVENT_MAX = 50;
private int guests;
private String eventNumber;
private String phone;
//default costructor
public Event()
{
eventNumber="";
phone="";
guests=0;
}
//prompt to enter phone number
public void setPhone()
{
String tempPhone="";
Scanner scanner=new Scanner(System.in);
System.out.println("Enter phone number ");
phone=scanner.nextLine();
for (int i = 0; i < phone.length(); i++)
{
if(Character.isDigit(phone.charAt(i)))
tempPhone+=phone.charAt(i);
}
if(tempPhone.length()< 10 || tempPhone.length()>10)
this.phone="0000000000";
else
this.phone=tempPhone;
}
//Sets event number
public void setEvent(String eventNumber)
{
if(eventNumber.length()<4)
this.eventNumber="A000";
else if(eventNumber.length()==4)
{
if(!Character.isAlphabetic(eventNumber.charAt(0)) ||
!Character.isDigit(eventNumber.charAt(1))||
!Character.isDigit(eventNumber.charAt(2))||
!Character.isDigit(eventNumber.charAt(3))
)
this.eventNumber="A000";
else
this.eventNumber=eventNumber;
}
else
this.eventNumber=eventNumber;
}
//Returns event number
public String getEvent()
{
return eventNumber;
}
//Returns the phone number in a format of (XXX)XXX-XXXX
public String getPhone()
{
String formattedPhoneNumber="";
/* append the "(" +xxx++")" three characters to
formattedPhoneNumber */
formattedPhoneNumber+="("+phone.substring(0, 3)+")";
//append next three characters
formattedPhoneNumber+=phone.substring(3, 6);
//Then append the "-" sign to the formattedPhoneNumber
formattedPhoneNumber+="-";
/* Then append rest of the phoneNumber to
the formattedPhoneNumber */
formattedPhoneNumber+=
phone.substring(6, phone.length());
phone=formattedPhoneNumber;
return phone;
}
//Prompts user to enter number of guests
public void setGuests()
{
Scanner enter = new Scanner(System.in);
System.out.println("Enter the number of guests for event ");
guests = enter.nextInt();
}
//return number of guests
public int getGuests()
{
return guests;
}
//Returns true if the event is large
//otherwise return false
public boolean isLargeEvent()
{
if (guests >= LARGE_EVENT_MAX)
return true;
else
return false;
}
}
------------------------------------------------------------------------------------------------------------------------------------
/***
* The class EventDemo that tests the Event class
* that calls setEvent ,setPhone and setGuests
* and prints the event object details to console.
* */
//EventDemo.java
public class EventDemo
{
public static void main(String[] args)
{
//Create an instance of Event class
Event event = new Event();
//call set methods
event.setEvent("1213");
event.setPhone();
event.setGuests();
System.out.println("Event Name : "+event.getEvent());
System.out.println("Phone number : "+event.getPhone());
System.out.println("Number of guests : "+event.getGuests());
System.out.println("Is Larget event : "+event.isLargeEvent());
}
}
---------------------------------------------------------------------------------------------------------
sample output:
Enter phone number
(123)121-2324
Enter the number of guests for event
30
Event Name : A000
Phone number : (123)121-2324
Number of guests : 30
Is Larget event : false
sample output:
Enter phone number
(123)-343-343434
Enter the number of guests for event
50
Event Name : A000
Phone number : (000)000-0000
Number of guests : 50
Is Larget event : true
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.