In Chapter 10, you created a DinnerEvent class that holds event information, inc
ID: 3667489 • Letter: I
Question
In Chapter 10, you created a DinnerEvent class that holds event information, including menu choices. Modify the class to include an array of 15 Employee objects representing employees who might be assigned to work at a DinnerEvent. Include a method that accepts an Employee array parameter and assigns it to the Employee array field, and include a method that returns the Employee array. The filename is DinnerEvent.java. d. Write an application that declares a DinnerEvent object, prompts the user for an event number, number of guests, menu options, and contact phone number, and then assigns them to the object. Also prompt the user to enter data for as many Employees as needed based on the number of guests. A DinnerEvent needs one Waitstaff Employee for every event, two if an event has 10 guests or more, three if an event has 20 guests or more, and so on. A DinnerEvent also needs one Bartender for every 25 guests and one Coordinator no matter how many guests attend. All of these Employees should be stored in the Employee array in the DinnerEvent object. (For many events, you will have empty Employee array positions.) After all the data values are entered, pass the DinnerEvent object to a method that displays all of the details for the event, including all the details about the Employees assigned to work. Save the program as StaffDinnerEvent.java.In Chapter 11, you created an interactive StaffDinnerEvent class that obtains all the data for a dinner event for Carly's Catering, including details about the staff members required to work at the event. Now, modify the class so that it becomes immune to user data entry errors by handling exceptions for each numeric entry. Each time the program requires numeric data—for example, for the number of guests, selected menu options, and staff members' salaries—continuously prompt the user until the data entered is the correct type. Save the revised program as StaffDinnerEvent.java. This is all the detial i can give
Explanation / Answer
StaffDinnerEvent.java
import java.util.Scanner;
public class StaffDinnerEvent
{
public static void main(String[] args)
{
Employee[] emps = new Employee[15];
int count;
String eventNum;
int guests;
DinnerEvent event;
int option;
int x;
int y;
int waitStaff;
int bartenders;
int[] menuOption = new int[4];
eventNum = getEventNumber();
guests = getGuests();
for(y = 0; y < menuOption.length; ++y)
menuOption[y] = getMenu(y);
event = new DinnerEvent(eventNum, guests, menuOption[0], menuOption[1], menuOption[2], menuOption[3]);
event.setContactPhone(getPhone());
event.setEventType(getType());
waitStaff = guests / 10 + 1;
for(count = 0; count < waitStaff; ++count)
emps[count] = getWaitstaff();
bartenders = guests / 25 + 1;
for(; count < waitStaff + bartenders; ++count)
emps[count] = getBartender();
emps[count] = getCoordinator();
event.setEmps(emps);
++count;
System.out.println(" Now display the event");
displayDetails(event, count);
}
public static Waitstaff getWaitstaff()
{
Scanner in = new Scanner(System.in);
Waitstaff ws= new Waitstaff();
System.out.print("Enter Employee number for waitstaff >> ");
ws.setEmployeeNumber(in.nextLine());
System.out.print("Enter first name >> ");
ws.setFirstName(in.nextLine());
System.out.print("Enter last name >> ");
ws.setLastName(in.nextLine());
System.out.print("Enter pay rate >> ");
ws.setPayRate(in.nextDouble());
ws.setJobTitle();
return ws;
}
public static Bartender getBartender()
{
Scanner in = new Scanner(System.in);
Bartender bt= new Bartender();
System.out.print("Enter Employee number for bartender >> ");
bt.setEmployeeNumber(in.nextLine());
System.out.print("Enter first name >> ");
bt.setFirstName(in.nextLine());
System.out.print("Enter last name >> ");
bt.setLastName(in.nextLine());
System.out.print("Enter pay rate >> ");
bt.setPayRate(in.nextDouble());
bt.setJobTitle();
return bt;
}
public static Coordinator getCoordinator()
{
Scanner in = new Scanner(System.in);
Coordinator co= new Coordinator();
System.out.print("Enter Employee number for coordinator >> ");
co.setEmployeeNumber(in.nextLine());
System.out.print("Enter first name >> ");
co.setFirstName(in.nextLine());
System.out.print("Enter last name >> ");
co.setLastName(in.nextLine());
System.out.print("Enter pay rate >> ");
co.setPayRate(in.nextDouble());
co.setJobTitle();
return co;
}
public static String getEventNumber()
{
String num;
Scanner input = new Scanner(System.in);
System.out.print(" Enter event number >> ");
num = input.nextLine();
return num;
}
public static int getGuests()
{
int guests;
final int MIN_GUESTS = 5;
final int MAX_GUESTS = 100;
Scanner input = new Scanner(System.in);
System.out.print("Enter number of guests >> ");
guests = input.nextInt();
while(guests < MIN_GUESTS || guests > MAX_GUESTS)
{
System.out.println("The number of guests must be between " +
MIN_GUESTS + " and " + MAX_GUESTS);
System.out.print("Please renter >> ");
guests = input.nextInt();
}
input.nextLine();
return guests;
}
public static int getType()
{
int eType;
Scanner input = new Scanner(System.in);
System.out.println("Event types are");
for(int x = 0; x < Event.EVENT_TYPES.length; ++x)
System.out.println(" " + x + " " + Event.EVENT_TYPES[x]);
System.out.print("Enter event type >> ");
eType = input.nextInt();
return eType;
}
public static void displayDetails(DinnerEvent e, int count)
{
Employee[] emps = new Employee[15];
emps = e.getEmps();
System.out.println(" Event #" + e.getEventNumber());
System.out.println("The price for an event with " + e.getGuests() +
" guests at $" + e.getPricePerGuest() + " per guest is $" + e.getPriceForEvent());
System.out.println("Whether this is a large event is " +
(e.getGuests() >= e.LARGE_EVENT));
System.out.println("Contact phone number is: " + e.getContactPhone());
System.out.println("The event is type " + e.getEventType() + " which is the following type: " + e.getEventTypeString());
System.out.println("The menu includes " + e.getMenu());
System.out.println(" The staff includes:");
for(int x = 0; x < count; ++x)
System.out.println(emps[x].getEmployeeNumber() + " " + emps[x].getName() + " " + emps[x].getPayRate() + " " + emps[x].getJobTitle());
}
public static DinnerEvent getLarger(DinnerEvent e1, DinnerEvent e2)
{
DinnerEvent larger = e2;
if(e1.getGuests() >= e2.getGuests())
larger = e1;
return larger;
}
public static String getPhone()
{
String phone;
Scanner input = new Scanner(System.in);
System.out.print("Enter contact phone number >> ");
phone = input.nextLine();
return phone;
}
public static int getMenu(int y)
{
int choice;
if(y == 0)
choice = displayEntreeMenu();
else if (y == 1 || y == 2)
choice = displaySidesMenu();
else choice = displayDessertMenu();
return choice;
}
public static int displayEntreeMenu()
{
Scanner keyboard = new Scanner(System.in);
int x;
int choice;
System.out.println("Please select from the following entrees:");
for(x = 0; x < DinnerEvent.ENTREES.length; ++x)
System.out.println(x + " -- " + DinnerEvent.ENTREES[x]);
System.out.print(" >> ");
choice = keyboard.nextInt();
keyboard.nextLine();
return choice;
}
public static int displaySidesMenu()
{
Scanner keyboard = new Scanner(System.in);
int x;
int choice;
System.out.println("Please select from the following sides:");
for(x = 0; x < DinnerEvent.SIDES.length; ++x)
System.out.println(x + " -- " + DinnerEvent.SIDES[x]);
System.out.print(" >> ");
choice = keyboard.nextInt();
keyboard.nextLine();
return choice;
}
public static int displayDessertMenu()
{
Scanner keyboard = new Scanner(System.in);
int x;
int choice;
System.out.println("Please select from the following desserts:");
for(x = 0; x < DinnerEvent.DESSERTS.length; ++x)
System.out.println(x + " -- " + DinnerEvent.DESSERTS[x]);
System.out.print(" >> ");
choice = keyboard.nextInt();
keyboard.nextLine();
return choice;
}
}
DinnerEvent.java
class DinnerEvent extends Event
{
public final static String[] ENTREES = {"beef", "chicken", "fish", "pasta"};
public final static String[] SIDES = {"salad", "mixed vegetables", "baked potato", "garlic bread", "dinner roll"};
public final static String[] DESSERTS = {"chocolate cake", "apple pie", "butterscotch pudding"};
private int entreeChoice;
private int sideChoice1;
private int sideChoice2;
private int dessertChoice;
Employee[] emps = new Employee[15];
public DinnerEvent(String num, int guests, int choice1, int choice2, int choice3, int choice4)
{
super(num, guests);
if(choice1 < ENTREES.length)
entreeChoice = choice1;
else
entreeChoice = 0;
if(choice2 < SIDES.length)
sideChoice1 = choice2;
else
sideChoice1 = 0;
if(choice3 < ENTREES.length)
sideChoice2 = choice3;
else
sideChoice2 = 0;
if(choice4 < DESSERTS.length)
dessertChoice = choice4;
else
dessertChoice = 0;
}
public Employee[] getEmps()
{
return emps;
}
public void setEmps(Employee[] staff)
{
emps = staff;
}
public String getMenu()
{
String menu = ENTREES[entreeChoice] + ", " + SIDES[sideChoice1] + ", " +
SIDES[sideChoice2] + ", and " + DESSERTS[dessertChoice];
return menu;
}
}
Event.java
class Event
{
public final static double HIGH_GUEST_PRICE = 35.00;
public final static double LOW_GUEST_PRICE = 32.00;
public final static int LARGE_EVENT = 50;
public final static int EVENT_NUM_LENGTH = 4;
public final static String[] EVENT_TYPES =
{"wedding", "baptism", "birthday", "corporate", "other"};
private String eventNumber;
private int guests;
private double pricePerGuest;
private double priceForEvent;
private String contactPhone;
private int eventType;
public Event()
{
this("A000", 0);
}
public Event(String num, int guests)
{
setEventNumber(num);
setGuests(guests);
}
public void setEventNumber(String num)
{
boolean numOk = true;
if(num.length() != EVENT_NUM_LENGTH || !Character.isLetter(num.charAt(0)) || !Character.isDigit(num.charAt(1)) || !Character.isDigit(num.charAt(2)) || !Character.isDigit(num.charAt(3)))
eventNumber = "A000";
else
eventNumber = num.toUpperCase();
}
public void setGuests(int gsts)
{
guests = gsts;
if(isLargeEvent())
{
pricePerGuest = LOW_GUEST_PRICE;
priceForEvent = guests * LOW_GUEST_PRICE;
}
else
{
pricePerGuest = HIGH_GUEST_PRICE;
priceForEvent = guests * HIGH_GUEST_PRICE;
}
}
public String getEventNumber()
{
return eventNumber;
}
public int getGuests()
{
return guests;
}
public double getPriceForEvent()
{
return priceForEvent;
}
public double getPricePerGuest()
{
return pricePerGuest;
}
public String getContactPhone()
{
String phone;
phone = "(" + contactPhone.substring(0, 3) + ") " +
contactPhone.substring(3, 6) + "-" +
contactPhone.substring(6, 9);
return phone;
}
public void setContactPhone(String phone)
{
final int VALID_LEN = 10;
final String INVALID_PHONE = "0000000000";
contactPhone = "";
int len = phone.length();
for(int x = 0; x < len; ++x)
{
if(Character.isDigit(phone.charAt(x)))
contactPhone += phone.charAt(x);
}
if(contactPhone.length() != VALID_LEN)
contactPhone = INVALID_PHONE;
}
public void setEventType(int code)
{
if(code < EVENT_TYPES.length)
eventType = code;
else
eventType = EVENT_TYPES.length - 1;
}
public int getEventType()
{
return eventType;
}
public String getEventTypeString()
{
return EVENT_TYPES[eventType];
}
public boolean isLargeEvent()
{
boolean isLarge = false;
if(guests >= LARGE_EVENT)
isLarge = true;
return isLarge;
}
}
Bartender.java
public class Bartender extends Employee
{
public final static double PAY_RATE = 14.00;
public void setPayRate(double rate)
{
if(rate > PAY_RATE)
payRate = PAY_RATE;
else
payRate = rate;
}
public void setJobTitle()
{
jobTitle = "bartender";
}
}
Coordinator.java
public class Coordinator extends Employee
{
public final static double PAY_RATE = 20.00;
public void setPayRate(double rate)
{
if(rate > PAY_RATE)
payRate = PAY_RATE;
else
payRate = rate;
}
public void setJobTitle()
{
jobTitle = "coordinator";
}
}
Employee.java
public abstract class Employee
{
private String employeeNumber;
private String lastName;
private String firstName;
protected double payRate;
protected String jobTitle;
public void setEmployeeNumber(String num)
{
employeeNumber = num;
}
public void setLastName(String name)
{
lastName = name;
}
public void setFirstName(String name)
{
firstName = name;
}
public String getEmployeeNumber()
{
return employeeNumber;
}
public String getName()
{
return firstName + " " + lastName;
}
public double getPayRate()
{
return payRate;
}
public String getJobTitle()
{
return jobTitle;
}
public abstract void setPayRate(double rate);
public abstract void setJobTitle();
}
Waitstaff.java
public class Waitstaff extends Employee
{
public final static double PAY_RATE = 10.00;
public void setPayRate(double rate)
{
if(rate > PAY_RATE)
payRate = PAY_RATE;
else
payRate = rate;
}
public void setJobTitle()
{
jobTitle = "waitstaff";
}
}
Sample Output
Enter event number >> 1
Enter number of guests >> 5