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: 3777131 • 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;

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];
    }

}


public class EventDemo{

    public static void main(String[] args) {

        final int SIZE = 8;
        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);
        }
      
        //snippet to prompt the user to sort events array
        //declare variables
        boolean flag=true;
        int choice;
        //delcare scanner variable
        Scanner input=new Scanner(System.in);
      
        //prompt the user until user enter 0
        do
        {
            System.out.println("Enter 1 to sort by event number, 2 by number of guests, 3 by event type,0 to exit");
            System.out.print("Enter your choice: ");
            choice=input.nextInt();
            if(choice==0)
            flag=false;
            else if(choice==1)
            {
                sortByEventNumber(event);
                for(Event e : event)
                display(e);
            }
            else if(choice==2)
            {
                sortByNumberOfGuests(event);
                for(Event e : event)
                display(e);
            }
            else if(choice==3)
            {
                sortByEventType(event);
                for(Event e : event)
                display(e);   
            }
            else
            System.out.println("Invalid choice!!Try again");
          
        }while(flag);
    }

    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("");
    }
  
   public static void sortByEventNumber(Event event[])
    {
        Event tempevent;
        for(int i=0;i<event.length;++i)
        {
            for(int j=i+1;j<event.length;++j)
            {
                if(event[i].getEventNumber().trim().compareTo(event[j].getEventNumber().trim())>0)
                {
                   tempevent=event[i];
                    event[i]=event[j];
                    event[j]=tempevent;
                }
            }
        }
    }
  
    //function to sort by number of guests
    public static void sortByNumberOfGuests(Event event[])
    {
        Event tempevent;
        for(int i=0;i<event.length;++i)
        {
            for(int j=i+1;j<event.length;++j)
            {
                if(event[i].getNumOfGuests()>event[j].getNumOfGuests())
                {
                    tempevent=event[i];
                    event[i]=event[j];
                    event[j]=tempevent;
                }
            }
        }
    }
  
    //function to sort by event type
    public static void sortByEventType(Event event[])
    {
        Event tempevent;
        for(int i=0;i<event.length;++i)
        {
            for(int j=i+1;j<event.length;++j)
            {
                if(event[i].getEventType().trim().compareTo(event[j].getEventType().trim())>0)
                {
                    tempevent=event[i];
                    event[i]=event[j];
                    event[j]=tempevent;
                }
            }
        }
    }
  
}


Sample Output:

Event #0:
What is the event number? K123
What is the event type? 3
How many guests are attending the event? 45
What is the contact phone number? 1111111111
Event #1:
What is the event number? H456
What is the event type? 2
How many guests are attending the event? 76
What is the contact phone number? 2222222222
Event #2:
What is the event number? T567
What is the event type? 4
How many guests are attending the event? 6
What is the contact phone number? 3333333333
Event #3:     
What is the event number? A345
What is the event type? 5
How many guests are attending the event? 98
What is the contact phone number? 4444444444
Event #4:
What is the event number? R213
What is the event type? 2
How many guests are attending the event? 99
What is the contact phone number? 5555555555
Event #5:   
What is the event number? F678
What is the event type? 4
How many guests are attending the event? 54
What is the contact phone number? 6666666666
Event #6:  
What is the event number? J879                                                                                                                                                                                                               
What is the event type? 2
How many guests are attending the event? 65                                                                                                                                                                                                  
What is the contact phone number? 7777777777
Event #7:
What is the event number? O987
What is the event type? 1  
How many guests are attending the event? 58
What is the contact phone number? 8888888888
Event number: K123
Event type: corporate  
Contact phone number: (111) 111-1111
Total price per guest: $35.00
Total price: $1575.00  
Large event: false  
Event number: H456
Event type: birthday
Total guests: 76   : (222) 222-2222
Total price per guest: $32.00  
Total price: $2432.00
Large event: true   
Event number: T567
Event type: other
Total guests: 6
Contact phone number: (333) 333-3333
Total price per guest: $35.00
Total price: $210.00
Large event: false
Event number: A345
Event type: other
Total guests: 98
Contact phone number: (444) 444-4444
Total price per guest: $32.00
Total price: $3136.00
Large event: true
Event number: R213
Event type: birthday
Total guests: 99
Contact phone number: (555) 555-5555
Total price per guest: $32.00
Total price: $3168.00
Large event: true
Event number: F678
Event type: other
Total guests: 54
Contact phone number: (666) 666-6666
Total price per guest: $32.00
Total price: $1728.00
Large event: true  
Event number: J879
Event type: birthday
Total guests: 65
Contact phone number: (777) 777-7777
Total price per guest: $32.00
Total price: $2080.00
Large event: true
Event number: O987
Event type: baptism
Total guests: 58
Contact phone number: (888) 888-8888
Total price per guest: $32.00
Total price: $1856.00
Large event: true

Enter 1 to sort by event number, 2 by number of guests, 3 by event type,0 to exit  
Enter your choice: 2  
Event number: T567  
Event type: other
Total guests: 6  
Contact phone number: (333) 333-3333
Total price per guest: $35.00   
Total price: $210.00   
Large event: false
Event number: K123
Event type: corporate
Total guests: 45
Contact phone number: (111) 111-1111
Total price per guest: $35.00
Total price: $1575.00
Large event: false
Event number: F678
Event type: other
Total guests: 54
Contact phone number: (666) 666-6666
Total price per guest: $32.00
Total price: $1728.00  
Large event: true
Event number: O987
Event type: baptism
Total guests: 58
Contact phone number: (888) 888-8888
Total price per guest: $32.00
Total price: $1856.00
Large event: true
Event number: J879
Event type: birthday
Total guests: 65
Contact phone number: (777) 777-7777
Total price per guest: $32.00
Total price: $2080.00
Large event: true
Event number: H456
Event type: birthday
Total guests: 76  
Contact phone number: (222) 222-2222
Total price per guest: $32.00
Total price: $2432.00
Large event: true
Event number: A345
Event type: other
Total guests: 98
Contact phone number: (444) 444-4444
Total price per guest: $32.00
Total price: $3136.00  
Large event: true
Event number: R213
Event type: birthday
Total guests: 99
Contact phone number: (555) 555-5555
Total price per guest: $32.00
Total price: $3168.00
Large event: true

Enter 1 to sort by event number, 2 by number of guests, 3 by event type,0 to exit                                                                                                                                                            

Enter your choice: 0