Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

**Please respond only if you can answer the whole question** ~~~~~~~~~~~~~~~~~~~

ID: 3776036 • Letter: #

Question

**Please respond only if you can answer the whole question**

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Java Programming: Ch 9 Case Problem 1

In the last chapter, you modified the EventDemo program for Carly’s Catering to accept and display data for an array of three Event objects. Now, modify the program to use an array of eight Event objects. Prompt the user to choose an option to sort Events in ascending order by event number, number of guests, or event type. Display the sorted list, and continue to prompt the user for sorting options until the user enters a sentinel value. Save the file as EventDemo.java.

Please use code and display output

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

import java.util.Scanner;

public class Event {

    public final static double LOWER_PRICE_PER_GUEST = 32.00;
    public final static double HIGHER_PRICE_PER_GUEST = 35.00;
    public final static int CUTOFF_VALUE = 50;

    public final String EVENT_TYPES[] = {
            "wedding", "baptism", "birthday", "corporate" , "other"
    };

    public int eventType;
    public boolean largeEvent;
    private String eventNum;
    private int numOfGuests;
    private double price;
    private double pricePerGuest;
    private String contact;
    private Scanner input = new Scanner(System.in);

    public Event(String event, int guests) {
        eventNum = event;
        numOfGuests = guests;
    }

    public Event() {
        this("A000", 0);
    }


    public void setEventNumber() {
        System.out.print("What is the event number? ");
        eventNum = input.nextLine();

        if (eventNum.length() != 4)
            eventNum="A000";

        if (eventNum.length() == 4
                && Character.isLetter(eventNum.charAt(0))
                && Character.isDigit(eventNum.charAt(1))
                && Character.isDigit(eventNum.charAt(2))
                && Character.isDigit(eventNum.charAt(3))){
        }else{
            eventNum = "A000";
        }
        eventNum = eventNum.toUpperCase();
    }

    public void setEventType(int i){
        System.out.print("What is the event type? ");
        i = input.nextInt();
        if(i < EVENT_TYPES.length)
            eventType = i;
        else
            eventType = EVENT_TYPES.length - 1;
    }

    public void setNumOfGuests() {

        do {
            System.out.print("How many guests are attending the event? ");
            numOfGuests = input.nextInt();
            input.nextLine();
            if (numOfGuests < 5 || numOfGuests > 100) {
                System.out.println("The number of guests must be between 5 and 100 ");
            }else{
                break;
            }
        }while (true);

        if (isLargeEvent()) {
            pricePerGuest = LOWER_PRICE_PER_GUEST;
        }else{
            pricePerGuest = HIGHER_PRICE_PER_GUEST;
        }
        largeEvent = (numOfGuests >= CUTOFF_VALUE);
        price = numOfGuests * pricePerGuest;
    }

    public void setContactPhone() {
        System.out.print("What is the contact phone number? ");
        contact = input.nextLine();
        contact = contact.replaceAll("[^0-9]", "");
        if (contact.length() != 10)
            contact = "0000000000";
    }

    public boolean isLargeEvent(){
        if(this.getNumOfGuests() >= 50)
            return true;
        else
            return false;
    }

    public String getEventNumber() {
        return eventNum;
    }

    public String getContactPhone(){
        return "(" + contact.substring(0, 3) + ")" + " "
                + contact.substring(3, 6) + "-"
                + contact.substring(6);
   }

    public int getNumOfGuests() {
        return numOfGuests;
    }

    public double getPrice() {
        return price;
    }

    public double getPricePerGuest(){
        return pricePerGuest;
    }

    public String getEventType(){
        return EVENT_TYPES[eventType];
    }

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

class EventDemo {

    public static void main(String[] args) {

        final int SIZE = 3;
        Event[] event = new Event[SIZE];

        for(int i = 0; i < event.length; i++){
            Event e = new Event();
            System.out.println("Event #"+ i +": ");
            e.setEventNumber();
            e.setEventType(i);
            e.setNumOfGuests();
            e.setContactPhone();
            System.out.println("");
            event[i] = e;
        }

        for(Event e : event){
            display(e);
        }
    }

    public static void display(Event e) {
        System.out.println("Event number: " + e.getEventNumber());
        System.out.println("Event type: " + e.getEventType());
        System.out.println("Total guests: " + e.getNumOfGuests());
        System.out.println("Contact phone number: " + e.getContactPhone());
        System.out.println("Total price per guest: $" + String.format("%.2f", e.getPricePerGuest()));
        System.out.println("Total price: $" + String.format("%.2f", e.getPrice()));
        System.out.println("Large event: " + e.isLargeEvent());
        System.out.println("");
    }
}

Explanation / Answer

import java.util.Scanner;

public class Event {

    public final static double LOWER_PRICE_PER_GUEST = 32.00;
    public final static double HIGHER_PRICE_PER_GUEST = 35.00;
    public final static int CUTOFF_VALUE = 50;

    public final String EVENT_TYPES[] = {
            "wedding", "baptism", "birthday", "corporate" , "other"
    };

    public int eventType;
    public boolean largeEvent;
    private String eventNum;
    private int numOfGuests;
    private double price;
    private double pricePerGuest;
    private String contact;
    private Scanner input = new Scanner(System.in);

    public Event(String event, int guests) {
        eventNum = event;
        numOfGuests = guests;
    }

    public Event() {
        this("A000", 0);
    }


    public void setEventNumber() {
        System.out.print("What is the event number? ");
        eventNum = input.nextLine();

        if (eventNum.length() != 4)
            eventNum="A000";

        if (eventNum.length() == 4
                && Character.isLetter(eventNum.charAt(0))
                && Character.isDigit(eventNum.charAt(1))
                && Character.isDigit(eventNum.charAt(2))
                && Character.isDigit(eventNum.charAt(3))){
        }else{
            eventNum = "A000";
        }
        eventNum = eventNum.toUpperCase();
    }

    public void setEventType(int i){
        System.out.print("What is the event type? ");
        i = input.nextInt();
        if(i < EVENT_TYPES.length)
            eventType = i;
        else
            eventType = EVENT_TYPES.length - 1;
    }

    public void setNumOfGuests() {

        do {
            System.out.print("How many guests are attending the event? ");
            numOfGuests = input.nextInt();
            input.nextLine();
            if (numOfGuests < 5 || numOfGuests > 100) {
                System.out.println("The number of guests must be between 5 and 100 ");
            }else{
                break;
            }
        }while (true);

        if (isLargeEvent()) {
            pricePerGuest = LOWER_PRICE_PER_GUEST;
        }else{
            pricePerGuest = HIGHER_PRICE_PER_GUEST;
        }
        largeEvent = (numOfGuests >= CUTOFF_VALUE);
        price = numOfGuests * pricePerGuest;
    }

    public void setContactPhone() {
        System.out.print("What is the contact phone number? ");
        contact = input.nextLine();
        contact = contact.replaceAll("[^0-9]", "");
        if (contact.length() != 10)
            contact = "0000000000";
    }

    public boolean isLargeEvent(){
        if(this.getNumOfGuests() >= 50)
            return true;
        else
            return false;
    }

    public String getEventNumber() {
        return eventNum;
    }

    public String getContactPhone(){
        return "(" + contact.substring(0, 3) + ")" + " "
                + contact.substring(3, 6) + "-"
                + contact.substring(6);
   }

    public int getNumOfGuests() {
        return numOfGuests;
    }

    public double getPrice() {
        return price;
    }

    public double getPricePerGuest(){
        return pricePerGuest;
    }

    public String getEventType(){
        return EVENT_TYPES[eventType];
    }

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

class EventDemo {

    public static void main(String[] args) {

        final int SIZE = 3;
        Event[] event = new Event[SIZE];

        for(int i = 0; i < event.length; i++){
            Event e = new Event();
            System.out.println("Event #"+ i +": ");
            e.setEventNumber();
            e.setEventType(i);
            e.setNumOfGuests();
            e.setContactPhone();
            System.out.println("");
            event[i] = e;
        }

        for(Event e : event){
            display(e);
        }
    }

    public static void display(Event e) {
        System.out.println("Event number: " + e.getEventNumber());
        System.out.println("Event type: " + e.getEventType());
        System.out.println("Total guests: " + e.getNumOfGuests());
        System.out.println("Contact phone number: " + e.getContactPhone());
        System.out.println("Total price per guest: $" + String.format("%.2f", e.getPricePerGuest()));
        System.out.println("Total price: $" + String.format("%.2f", e.getPrice()));
        System.out.println("Large event: " + e.isLargeEvent());
        System.out.println("");
    }
}