JAVA Program Using Array of Objects

Verified

Added on  2022/08/20

|28
|2144
|15
AI Summary

Contribute Materials

Your contribution can guide someone’s learning journey. Share your documents today.
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

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
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();

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
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);
}

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");

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
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);
Document Page
12JAVA PROGRAM USING ARRAY OF OBJECTS
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());
}
JOptionPane.showMessageDialog(null, "You Must Enter At least Two Bookings to
sort","Sort Bookings", JOptionPane.ERROR_MESSAGE);
}
public static void main(String [] args)
{
RockyCateringMenu app = new RockyCateringMenu();
System.out.println("Welcome to the Rocky Catering Service Management System");
app.processBookings();
System.out.println(" Thank you for Using Rocky Catering Service Management
System");
System.out.println("Program is written by [Student_ID]");

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
13JAVA PROGRAM USING ARRAY OF OBJECTS
}
}
Completed Booking.java
public class Booking {
public Booking(String bookingName, Integer guests) {
this.bookingName = bookingName;
this.guests = guests;
}
Booking() {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of
generated methods, choose Tools | Templates.
}
public String getBookingName() {
return bookingName;
}
public void setBookingName(String bookingName) {
this.bookingName = bookingName;
}
public Integer getGuests() {
return guests;
}
public void setGuests(Integer guests) {
Document Page
14JAVA PROGRAM USING ARRAY OF OBJECTS
this.guests = guests;
}
public Double calculateCharge(int vale)
{
double charges=0;
if(vale<=20 )
{
charges=vale*29.50;
//charges=(1080+((vale-40)*19.50));
}
else if (vale<=40) {
charges=(590+((vale-20)*24.50));
}
else
{
charges=(1080+((vale-40)*19.50));
}
return charges;
}
String bookingName;
Integer guests;
}
Screenshots
Welcome and Exit message
Document Page
15JAVA PROGRAM USING ARRAY OF OBJECTS
Prompting user to input booking name

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
16JAVA PROGRAM USING ARRAY OF OBJECTS
Number of Guests
Document Page
17JAVA PROGRAM USING ARRAY OF OBJECTS
Error prompts while asking input from user
Booking name validation
Document Page
18JAVA PROGRAM USING ARRAY OF OBJECTS
Number of Guests field left blank

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
19JAVA PROGRAM USING ARRAY OF OBJECTS
Display error due to lack of Data
Document Page
20JAVA PROGRAM USING ARRAY OF OBJECTS
Sorting error due to lack of entries
Document Page
21JAVA PROGRAM USING ARRAY OF OBJECTS
Search for a specific booking

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
22JAVA PROGRAM USING ARRAY OF OBJECTS
Booking name not found
Document Page
23JAVA PROGRAM USING ARRAY OF OBJECTS
Output of option 2
Document Page
24JAVA PROGRAM USING ARRAY OF OBJECTS
Option 3 for Statistics

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
25JAVA PROGRAM USING ARRAY OF OBJECTS
Exit option
Document Page
26JAVA PROGRAM USING ARRAY OF OBJECTS
Sorted List of Booking
Document Page
27JAVA PROGRAM USING ARRAY OF OBJECTS
Search result
1 out of 28
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]

Your All-in-One AI-Powered Toolkit for Academic Success.

Available 24*7 on WhatsApp / Email

[object Object]