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:
Save the files as Event.java and EventDemo.java.
/***************************************************
Event Class
*************************************************/
import java.util.Scanner;
public class Event {
//Initialize the variables
public static double
pricePerGuestHigh = 35.00;
public static double
pricePerGuestLow = 32.00;
public static final int
LARGE_EVENT_MAX = 50;
public String phnum="";
public String eventNumber="";
private int guests;
private double pricePerEvent;
//Method definition of
setPhoneNumber
public void setPhoneNumber()
{
//Create a
Scanner class's object
Scanner s = new
Scanner(System.in);
int
count=0;
//Prompt and
read the input from the user
System.out.println("Enter Phone Number:");
String
pnumb=s.nextLine();
int
len=pnumb.length();
for(int
i=0;i<len;i++)
{
char c=pnumb.charAt(i);
if(Character.isDigit(c))
{
count++;
String
ss=Character.toString(c);
phnum=phnum.concat(ss);
}
}
if(count!=10)
{
phnum="";
phnum=phnum.concat("0000000000");
}
}
public String
getPhoneNumber()
{
String
ret="("+this.phnum.charAt(0)
+""+this.phnum.charAt(1)+""+this.phnum.charAt(2)
+")"+this.phnum.charAt(3)+""+this.phnum.charAt(4)
+""+this.phnum.charAt(5)+""+this.phnum.charAt(6)
+""+this.phnum.charAt(7)+""+this.phnum.charAt(8)
+""+this.phnum.charAt(9);
return
ret;
}
//prompts user to enter event
number
public void setEventNumber()
{
Scanner scanner
= new Scanner(System.in);
int len;
System.out.println("Enter 4 digit event number: ");
String enumb =
scanner.nextLine();
len=enumb.length();
char
c1,c2,c3,c4;
String
defv="A000";
c1=enumb.charAt(0);
c2=enumb.charAt(1);
c3=enumb.charAt(2);
c4=enumb.charAt(3);
if(Character.isLetter(c1) &&
Character.isDigit(c2) &&
Character.isDigit(c3) &&
Character.isDigit(c4))
{
enumb=enumb.toUpperCase();
eventNumber=eventNumber.concat(enumb);
}
{
eventNumber=eventNumber.concat(defv);
}
}
//Method to set number of
guest
public void setGuests(int
guests)
{
this.guests =
guests;
if(isLargeEvent())
pricePerEvent=pricePerGuestHigh;
else
pricePerEvent=pricePerGuestLow;
}
//Returns the number of guests of
event
public int getGuestsCount()
{
return
guests;
}
//Returns true if event is large
otherwise false
public boolean isLargeEvent()
{
if (guests >=
LARGE_EVENT_MAX)
{
return true;
}
else if (guests
< LARGE_EVENT_MAX)
{
return false;
}
return
isLargeEvent();
}
//Returns the event number
public String
getEventNumber()
{
String
ret1="Event Number :"
+this.eventNumber;
return
ret1;
}
public int getGuests(boolean
largeEvent)
{
return
guests;
}
}//end of the class Event
/***************************************************
EventDemo Class
***************************************************/
import java.util.Scanner;
public class EventDemo {
public static void main(String[] args)
{
//Set size
final int SIZE=4;
//Create an array of Event
type
Event[] eventObjects = new
Event[SIZE];
//declare an integer variable
int numGuests;
//Create a Scanner class
Scanner scanner = new
Scanner(System.in);
//run for loop for 4 times
for (int i = 0; i <
eventObjects.length;i++)
{
eventObjects[i]=new Event();
eventObjects[i].setEventNumber();
eventObjects[i].setPhoneNumber();
/*prompt user to
enter number of guests
until the user
enters in a range of 5
to 100*/
do
{
System.out.println("Enter number of guests :
");
numGuests=scanner.nextInt();
if(numGuests<5 ||numGuests>100)
System.out.println("Number of guests must be in
5 to 100");
}
while(numGuests<5 ||numGuests>100);
//set number of
guests
eventObjects[i].setGuests(numGuests);
}
//Select first object of Event
from eventObjects array
Event
oneEvent=eventObjects[0];
Event
twoEvent=eventObjects[1];
Event
threeEvent=eventObjects[2];
Event
fourEvent=eventObjects[3];
for(int i=0;i<4;i++)
{
System.out.println(" "
+eventObjects[i].getEventNumber()
+""
+ " contact number:"
+eventObjects[i].getPhoneNumber()
+""
+ " printing message
"+eventObjects[i].getGuestsCount()+" times");
/*printing
message "Please come to my event!"
to console
*/
for (int j = 0;
j < eventObjects[i].getGuestsCount(); j++)
{
System.out.println("Please come to
event!");
}
}
}//end of the main method
}//end of the class EventDemo