Java Program: Rocky Catering Menu using Array of Objects - Assignment

Verified

Added on  2022/08/20

|28
|2144
|15
Homework Assignment
AI Summary
This Java program, designed for a Rocky Catering Menu system, utilizes an array of objects to manage bookings. The program allows users to enter booking details (name and number of guests), display all bookings with calculated charges, view statistical summaries (average guests, total charges, and guest count extremes), search for specific bookings, and sort bookings. The code includes two Java files: `RockyCateringMenu.java` which contains the main program logic, and `Booking.java` which defines the Booking class with methods for setting booking details and calculating charges based on the number of guests. The program incorporates input validation, error handling (e.g., for blank input and numeric input), and a menu-driven interface for user interaction. The program also includes features like displaying statistics and searching for bookings. The complete solution, including the Java code and screenshots of the program's execution, is available on Desklib, a platform offering AI-based study tools for students.
Document Page
Running head: JAVA PROGRAM USING ARRAY OF OBJECTS
JAVA Program using array of objects
Name of the Student
Name of the University
Authors note
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
1JAVA PROGRAM USING ARRAY OF OBJECTS
Completed RockyCateringMenu.java
// TODO -- create header comments
import java.lang.reflect.Array;
import java.security.Key;
import java.time.Clock;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.Scanner;
import javax.swing.JOptionPane;
// TODO -- create the Booking class (separate file: Booking.java)
public class RockyCateringMenu
{
final int ENTER_BOOKING = 1;
final int DISPLAY_BOOKINGS = 2;
final int DISPLAY_STATISTICS = 3;
final int SEARCH_BOOKINGS = 4;
final int SORT_BOOKINGS = 5;
final int EXIT = 6;
double totvals;
//final int MAX_BOOKING = 10;
// TODO -- declare any further constants
Document Page
2JAVA PROGRAM USING ARRAY OF OBJECTS
// TODO -- declare array of booking objects
Booking [] booking=new Booking[10];
int currentBooking;
Booking np=new Booking();
String nameMax = null;
String nameMin = null;
// TODO -- declare variable for the current booking entered (integer)
Scanner inputMenuChoice = new Scanner(System.in);
//private int MaxBooking=10;
private int getMenuItem()
{
System.out.println("\nPlease select from the following");
System.out.println(ENTER_BOOKING + ". Enter booking name and number of
guests");
System.out.println(DISPLAY_BOOKINGS + ". Display all booking names,
number of guests and charges");
System.out.println(DISPLAY_STATISTICS + ". Display Statistics");
System.out.println(SEARCH_BOOKINGS + ". Search for booking");
System.out.println(SORT_BOOKINGS + ". Sort the bookings");
System.out.println(EXIT + ". Exit the application");
System.out.print("Enter choice==> ");
String choice = inputMenuChoice.nextLine();
while (choice.equals("") || !isStringNumeric(choice))
Document Page
3JAVA PROGRAM USING ARRAY OF OBJECTS
{
JOptionPane.showMessageDialog(null, "Error - Menu selection name
cannot be blank and must be numeric",
"Input Menu
Choice", JOptionPane.ERROR_MESSAGE);
System.out.print("Enter choice==> ");
choice = inputMenuChoice.nextLine();
}
return Integer.parseInt(choice);
}
private boolean isStringNumeric(String str)
{
for (int i = 0; i < str.length(); i++)
{
if (!Character.isDigit(str.charAt(i)))
return false;
}
return true;
}
private void processBookings()
{
int choice = getMenuItem();
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
4JAVA PROGRAM USING ARRAY OF OBJECTS
while (choice != EXIT)
{
switch (choice)
{
case ENTER_BOOKING:
enterBooking();
break;
case DISPLAY_BOOKINGS:
displayAllBookings();
break;
case DISPLAY_STATISTICS:
displayStatistics();
break;
case SEARCH_BOOKINGS:
searchBookings();
break;
case SORT_BOOKINGS:
sortBookings();
break;
default:
System.out.println("ERROR choice not recognised");
}
choice = getMenuItem();
}
}
private void enterBooking()
{
Document Page
5JAVA PROGRAM USING ARRAY OF OBJECTS
displayHeading();
//String guestNum=0;
// TODO -- check if maximum bookings has been reached (do this after getting
the other functionality working)
int numBook = 0;
//=0;
Integer gnum = 0 ;
//= 0;
//while(currentBooking<10)
{
//String mv =/1
//JOptionPane.showInputDialog(null, "Enter Booking Name");
String mv=JOptionPane.showInputDialog(null, "Please Enter the booking name",
"Input Booking name", JOptionPane.PLAIN_MESSAGE);
if(mv.isEmpty())
{
JOptionPane.showMessageDialog(null, "Error - Booking Name Can not be
Blank","Input Booking name!", JOptionPane.ERROR_MESSAGE);
}
else
{
booking[currentBooking]=new Booking();
booking[currentBooking].setBookingName(mv);
}
Document Page
6JAVA PROGRAM USING ARRAY OF OBJECTS
//h
try {
String guestNum = JOptionPane.showInputDialog(null,"Enter the number of
guests", "Input number of Guests", JOptionPane.PLAIN_MESSAGE);
gnum=Integer.parseInt(guestNum);
booking[currentBooking].setGuests(gnum);
} catch ( NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Number of Guests can not be
Blank","Input number of Guests!", JOptionPane.ERROR_MESSAGE);
}
//JOptionPane.showInputDialog(null,"Please Enter the booking name");
// JOptionPane.showInputDialog(null, this,"Enter value ", EXIT);
//JOptionPane.showInputDialog("Input Booking Name", "Please Enetr the
Booking Name");
System.out.println(booking[numBook].bookingName);
//System.out.println(gnum);
//System.out.println(numBook);
currentBooking++;
System.out.println(currentBooking);
//System.out.println(numBook);
}
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
7JAVA PROGRAM USING ARRAY OF OBJECTS
}
private void displayHeading()
{
System.out.printf("%-30s%-17s%-6s\n", "Booking Name", "Guests", "Charge");
}
private void displayAllBookings()
{
if(booking.length!=0)
{
for(int r=0;r<booking.length && booking[r] != null;r++)
{
double vals=np.calculateCharge(booking[r].getGuests());
//System.out.println(booking.length);
System.out.printf("%-30s%-17s%-6s\n", booking[r].getBookingName().toString(),
booking[r].getGuests().toString(),vals);
totvals+=vals;
//System.out.println("%-30s%-17s%-6s\\n\", \"Booking
Name\", \"Guests\", \"Charge\");
//System.out.println(booking[r].getBookingName());
//System.out.println(booking[r].getGuests());
}
Document Page
8JAVA PROGRAM USING ARRAY OF OBJECTS
//for(int y=0;y<booking.length;y++)
// TODO -- check if there has been a booking entered (do this after getting the other
functionality working)
// TODO -- display all of the entries entered so far (just display the current entries not
the whole array, use the current booking variable as the termination condition)
}
else
{
//System.out.println("no booking");
JOptionPane.showMessageDialog(null, "You Must Enter At least one Booking
","Display All Bookings", JOptionPane.ERROR_MESSAGE);
//System.out.println([y].bookingName);
//System.out.println(booking[y].guests);
}
}
private void displayStatistics()
{
//String nameMax=null;
//String nameMin=null;
System.out.println("Statistics for Rocky Catering Srevice");
int c=0;
int sumval=0;
for(int r=0;r<booking.length && booking[r] != null;r++)
{
Document Page
9JAVA PROGRAM USING ARRAY OF OBJECTS
sumval+= booking[r].guests;
c++;
}
//int mx=
float avgval=sumval/c;
System.out.println("Average number of Guests \t"+avgval);
System.out.println("Total charges are $\t"+totvals);
int min = booking[0].getGuests(); // assume first elements as smallest number
int max = max = booking[0].getGuests();
for (int i = 1; i < booking.length && booking[i] != null; i++) // iterate for loop from
arrays 1st index (second element)
{
if (booking[i].getGuests() > max)
{
max = booking[i].getGuests();
nameMax=booking[i].getBookingName();
}
if (booking[i].getGuests() < min)
{
min = booking[i].getGuests();
nameMin=booking[i].getBookingName();
}
}
//System.out.println(max);
System.out.println(nameMax+" has the maximum number of "+max+ " guests");
System.out.println(nameMin+" has the minimum number of "+min+ " guests");
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
10JAVA PROGRAM USING ARRAY OF OBJECTS
}
private void searchBookings()
{
String searchName=null;
if(booking==null)
{
JOptionPane.showMessageDialog(null, searchName+" You Must Enter At least one
Booking ","Search Bookings", JOptionPane.ERROR_MESSAGE);
// searchName = JOptionPane.showInputDialog(null,"Please Enter the Booking
Name", "Input Booking Name", JOptionPane.PLAIN_MESSAGE);
}
else
{
//System.out.println("Error Occured");
searchName = JOptionPane.showInputDialog(null,"Please Enter the Booking Name",
"Search Bookings", JOptionPane.PLAIN_MESSAGE);
for(int r=0;r<booking.length && booking[r] != null;r++)
{
if(booking[r].getBookingName().equals(searchName))
{
//double vals=np.calculateCharge(booking[r].getGuests());
//System.out.println(booking.length);
Document Page
11JAVA PROGRAM USING ARRAY OF OBJECTS
//System.out.printf("%-30s%-17s%-6s\n",
booking[r].getBookingName().toString(), booking[r].getGuests().toString(),vals);
// System.out.println("found");
System.out.println(" Record found");
double vals=np.calculateCharge(booking[r].getGuests());
System.out.println(booking.length);
System.out.printf("%-30s%-17s%-6s\n",
booking[r].getBookingName().toString(), booking[r].getGuests().toString(),vals);
}
else
{
JOptionPane.showMessageDialog(null, searchName+" Not found ","Search
Bookings", JOptionPane.ERROR_MESSAGE);
}
//sumval+= booking[r].guests;
// c++;
}
}//
}
private void sortBookings()
{
// booking[]
for(int r=0;r<booking.length && booking[r] != null;r++)
{
double vals=np.calculateCharge(booking[r].getGuests());
///////////Array.sort(booking[r].bookingName);
chevron_up_icon
1 out of 28
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]