Term 3 Assignment

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4462760071454301"

20245 Java Programming Assignment Term 3

The answer to this question is given after the questions

COIT20245, 2019 Term Three
Assessment details COIT 20245
Assessment item 2—JAVA Program using an array of objects
Due date:

Refer below for complete assessment item 2 requirements
(Assignment Two)
This assessment item relates to the unit learning outcomes as stated in the Unit Profile.
For this assignment, you are required to develop a Menu Driven Console Java Program to demonstrate you can use Java constructs including input/output via GUI dialogs, Java primitive and built-in types, Java defined objects, arrays, selection and looping statements and various other Java commands. Your program must produce the correct results.
The code for the menu and option selection is supplied: RockyCateringMenu.java and is available on the unit website, you must write the underlying code to implement the program. The menu selections are linked to appropriate methods in the given code. Please spend a bit of time looking at the given code to familiarise yourself with it and where you have to complete the code. You will need to write comments in the supplied code as well as your own additions.
What to submit for this assignment
The Java source code:
o Booking.java
o RockyCateringMenu.java
If you submit the source code with an incorrect name you will lose marks.
A report including a UML class diagram of your Booking class, how long it took to create the whole program, any problems encountered, and screenshots of the output produced including annotations. (Use Alt-PrtScrn to capture just the application window and you can paste it into your Word document) You should test every possibility in the program.
o ReportAss2.docx
You will submit your files by the due date using the “Assignment 2” link on the Moodle course website under Assessment … Assignment 2 Submission.
COIT20245, 2019 Term Three – Page 2 of 10
Assignment Specification
You have completed the console program for processing bookings for Rocky Catering Service. We
are going to extend this application so the booking names and number of guests per booking can be
stored in an array of objects, do not use ArrayList.
The program will run via a menu of options, the file RockyCateringMenu.java has been supplied
(via the Moodle web site) which supplies the basic functionality of the menu system.
Look at the code supplied and trace the execution and you will see the menu is linked to blank
methods (stubs) which you will implement the various choices in the menu.
Booking class
First step is to create a class called Booking (Booking.java).
The Booking class will be very simple it will contain two private instance variables:
o bookingName as a String
o guests as an integer
You should also have constants for the charge and discount values.
The following public methods will have to be implemented:
o A default constructor
o A parameterised constructor
o Two set methods (mutators)
o Two get methods (accessors)
o A method to calculate and return the charge for the booking. This calculation will be the same
as in assignment one. Use constants for all numeric literals.
Ten to twenty guests will be $29.50 per head.
Twenty-one to forty guests will be the cost of twenty guests (at $29.50 per head) $590.00 plus $24.50
per head after that.
Over forty guests will be the cost of forty guests $1,080.00 plus $19.50 per head after that.
COIT20245, 2019 Term Three – Page 3 of 10
Note: Following basic database principles, calculated values are not usually stored, so in this case we
will not store the charge as a instance variable, but use the calculateCharge() method when we
want to determine the charge.
RockyCaterMenu class
Once the Booking class is implemented and fully tested we can now start to implement the
functionality of the menu system.
Data structures
For this assignment we are going to store the booking names and number of guests in an array of
Booking objects.
Declare an array of Booking objects as an instance variable of RockyCateringMenu class the array
should hold ten bookings.
You will need another instance variable (integer) to keep track of the number of the bookings being
entered and use this for the index into the array of Booking objects.
Menu options

  1. Enter booking name and number of guests: enterBooking()
    For assignment two we are going to use the GUI dialog showInputDialog() for our input. You will
    need to create the following two dialogs to receive the input from the user. You will not implement
    the functionality of the cancel key (need to use exceptions for this). Use Integer.parseInt() to
    convert the number as a string into an integer.
    Data validation (you can implement this after you have got the basic functionality implemented)
    You will need to validate the user input using a validation loop.
    The booking name cannot be blank i.e. not null and the number of guests cannot be blank and the
    number of guests needs to be greater than or equal to ten, the same as assignment one.
    Output the following error dialogs:
    COIT20245, 2019 Term Three – Page 4 of 10
    When the booking name and number of guests have been entered successfully into two local variables
    you will need to add these values into the bookings object array, you will also need to increment a
    counter to keep track of the number of bookings you have entered and the position in the array of the
    next booking to be entered.
    When the maximum number of bookings is reached do not attempt to add any more bookings and
    give the following error message:
    When the booking details have been successfully entered, display the details of the booking and the
    charge on the console screen.
    Note: For the next three options, display all, statistics and search you should ensure at least one
    booking has been entered and give an appropriate error message if it there are no bookings entered
    and for the sorting option you must ensure at least two bookings have been entered, for example:
  2. Display all booking names, number of guests and charges: displayAllBookings()
    When this option is selected display all of the bookings which have been entered so far.
  3. Display statistics: displayStatistics()
    When this option is selected you will display the statistics as per assignment one. You can loop
    through your array of objects to calculate this information.
  4. Search for a booking: searchBookings()
    You can just use a simple linear search which will be case insensitive. Use the showInputDialog()
    method to input the booking name (you can share this functionality from enter booking).
    If the search is successful display the details about the booking.
    If the search is unsuccessful display an appropriate message.
  5. Sort the bookings: sortBookings()
    This option you will sort the bookings alphabetically (ignore case) by the booking names, you can use
    any sorting algorithm which you like, do not use any in-built sort methods. Display the sorted list
    after the sort is complete.
    Remember the welcome and exit messages as per assignment one.

    Extra Hints
    Your program should be well laid out, commented and uses appropriate and consistent names (camel
    notation) for all variables, methods and objects.
    Make sure you have no repeated code (even writing headings in the output)
    Constants must be used for all numbers (numeric literals) in your code.
    Look at the marking criteria to ensure you have completed all of the necessary items
    Refer to a Java reference textbook and the unit and lecture material (available on the course web
    site) for further information about the Java programming topics required to complete this assignment.
    Check output, check code and add all of your comments, complete report and the UML class diagram.
    Supplied Code
    Download, compile and run the supplied code available from the course web site.
    You will see the menu interface has been implemented and you have to implement the underlying
    code, use the supplied method stubs and add your own methods.
    Follow the // TODO comments in the supplied code.
    Again no code should be repeated in your program.
    If you just submit the supplied code you will receive zero marks.

    Marking scheme is on the following page.
    COIT20245, 2019 Term Three – Page 10 of 10
    Variables, constants and types
    Variables have meaningful names and use camel notation 0.5
    Variables are the correct type and constants are used 0.5
    Array of objects is used 1
    Code in general
    Code is indented and aligned correctly 1
    Code is easy to read (use of vertical whitespace) 0.5
    Code has header comment which includes name, student ID,
    date, file name and purpose of the class 0.5
    Code is fully commented including all variables and methods 0.5
    No repeated code 1
    Booking class
    Instance variables are correct and private 0.5
    Default and parameterised constructors are correct 0.5
    Method for calculating charges is correct 1
    Get and set methods are correct 1
    RockyCateringMenu class — Enter Booking
    Booking name is read correctly 0.5
    Number of guests is read correctly 0.5
    Data is added to the object array correctly 2
    Output resembles the specification (two decimal points) 1
    Charges calculated correctly 0.5
    Error dialog when maximum bookings is reached 0.5
    Error dialog when booking name not entered 0.5
    Error dialog when number of guests is blank or is out of range 0.75
    Data validation loops are correct 0.25
    Display all bookings
    All records displayed 2
    Output resembles the specification 1
    Display statistics
    Maximum and minimum are correct 0.5
    Average is correct 0.5
    Total charges is correct 0.5
    Output resembles the specification (two decimal points) 1
    Search is correct and correct details returned 2
    Search is case insensitive 0.5
    “ not found” is correct 0.5
    Sort is correct and inbuilt sorting methods are not used 2
    Sort is case insensitive 0.5
    Sorted list is displayed after sort 0.5
    Welcome and exit message (as per assignment one) 0.5
    No bookings entered is handled correctly 0.5
    Correct files submitted including types and names 0.5
    UML class diagram of Booking class is correct 1
    Screen shot(s) of testing and annotations 0.5
    Report presentation and comments including how long it took
    and any problems encountered 0.5
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4462760071454301"



Term – 3 2019

Assignment 2

Rocky catering Service





Prepared by

Student Name: xxxxx

Student ID: xxxx

Table of Contents






Booking.java. 5

RockyCateringMenu.java. 8


Rocky Catering Service is an application that allows the customer to enter the details of their booking with the name of the booking and the guest number. The guest number cannot be blank and should enter more than 10. The statistics should be displayed and the bookings should be searched and sorted accordingly in the particular program.


UML diagram


Entering the booking name in the box below shown

Booking Name

Entering the number of guests for the booking

Guest number

Displaying the booking name and guest number according to the details given

Displaying the booking name and guest number according to the details given
Displaying the statistics for the Rocky Catering Service

Displaying the statistics for the Rocky Catering Service

Searching and sorting the booking details


It takes a few moments to write the comments and by writing the comments it is helpful to understand the usage of the code in the Rocky Catering Project.

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4462760071454301"




 * To change this license header, choose License Headers in Project Properties.

 * To change this template file, choose Tools | Templates

 * and open the template in the editor.


package rockycateringproject;//this name is the project name



 * @author Varsha Chowdary Bachina

 * Student id 12121600


public class Booking {

    private String bookingName;//declaring bookingName and guests for the Rocky Catering Project

    private int guests;

    public Booking(){


    public Booking(String bookingName, int guests){

      this.bookingName = bookingName;// used for booking name

      this.guests = guests;// used for guests to enter the number


    public String getBookingName() {

        return bookingName;


    public void setBookingName(String bookingName) {

        this.bookingName = bookingName;


    public int getGuests() {

        return guests;


    public void setGuests(int guests) {

        this.guests = guests;


    public double calculateCharge(){//todo calculation for the booking charges 

        double T_Charge = 0.0;

        final double CHARGE_20_GUESTS = 29.50;//the details are given in the specification

        final double CHARGE_20_TO_40_GUESTS = 24.50;

        final double CHARGE_FOR_40_GUESTS = 19.50;

        final double PRICE_FOR_20_GUESTS = 590.00;

        final double PRICE_FOR_40_GUESTS = 1080.00;

        final int FIRST_BATCH = 20;

        final int SECOND_BATCH = 40;

        if(getGuests()< FIRST_BATCH){

            T_Charge = getGuests() * CHARGE_20_GUESTS;


        if(getGuests()>FIRST_BATCH && getGuests()<=SECOND_BATCH){// conditions are to be applied if it is more 20 guests

            T_Charge = PRICE_FOR_20_GUESTS + (getGuests()-FIRST_BATCH) * CHARGE_20_TO_40_GUESTS;// charge for guests between 20 to 40


        if(getGuests()>SECOND_BATCH){//charge for more than or equal to 40 guests

            T_Charge = PRICE_FOR_40_GUESTS + (getGuests()-FIRST_BATCH) * CHARGE_FOR_40_GUESTS;


        return T_Charge;//returning the T_Charge value




package rockycateringproject;

// TODO — create header comments

import java.util.Scanner;

import javax.swing.JOptionPane;

import javax.swing.JFrame;

// 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;

    int testBook = 0;

    // TODO — declare any further constants

    // TODO — declare array of booking objects

    // TODO — declare variable for the current booking entered (integer)

    Scanner inputMenuChoice = new Scanner(System.in);

    JFrame frame;

    final int COUNT = 9;    //declaration of constant

    Booking[] bookings = new Booking[COUNT];//declaration of array of booking object

    private int getMenuItem() {//displaying the menu item for the Rocky Catering Project

        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)) {

            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() {//the booking process is displayed

        int choice = getMenuItem();

        while (choice != EXIT) {//while is used for subordinate clauses

            switch (choice) {// this is used to check the values with each and every case

                case ENTER_BOOKING:


                    break;//to break the current flow

                case DISPLAY_BOOKINGS:



                case DISPLAY_STATISTICS:



                case SEARCH_BOOKINGS:



                case SORT_BOOKINGS:




                    System.out.println(“ERROR choice not recognised”);


            choice = getMenuItem();



    private void enterBooking() {//entering the booking details

        String bookingName;

        int i = 0;

        do {

            bookingName = JOptionPane.showInputDialog(null, “Please enter booking name:”, JOptionPane.PLAIN_MESSAGE);

            if (bookingName.trim().isEmpty()) {

                JOptionPane.showMessageDialog(null, “Booking name cannot be blank.”, “Invalid input”, JOptionPane.ERROR_MESSAGE);


        } while (bookingName.trim().isEmpty());

        //b.setBookingName(JOptionPane.showInputDialog(null, “Name of Booking Name: “));

        String guestNumber;

        do {

            guestNumber = JOptionPane.showInputDialog(null, “Enter guests”, “Invalid input”, JOptionPane.OK_CANCEL_OPTION);

            if (guestNumber.trim().isEmpty()) {

                JOptionPane.showMessageDialog(null, “Guest number cannot be blank.”, “Invalid input”, JOptionPane.ERROR_MESSAGE);


        } while (guestNumber.trim().isEmpty());

        int guest = Integer.parseInt(guestNumber);

//                  if(guest<10){


//                        JOptionPane.showMessageDialog(null,”Guest should be greater than or equal to 10″,”Invalid input”,JOptionPane.ERROR_MESSAGE);


//                  }

        bookings[testBook] = new Booking(bookingName, guest);

        System.out.printf(“%-30s%-17s%-6s\n”, “Booking Name”, “Guests”, “Charge”);

        System.out.printf(“%-30s%-17s%-6s\n”, bookings[testBook].getBookingName(), bookings[testBook].getGuests(), bookings[testBook].calculateCharge());



    private void displayHeading() {

        System.out.printf(“%-30s%-17s%-6s\n”, “Booking Name”, “Guests”, “Charge”);


    private void displayAllBookings() {

        for (int i = 0; i < testBook; i++) {

            System.out.printf(“%-30s%-17s%-6s\n”, bookings[i].getBookingName(), bookings[i].getGuests(), bookings[i].calculateCharge());


        // 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)


    private void displayStatistics() {//to display statistics

        // TODO — check if there has been a booking entered (do this after getting the other functionality working)

        // TODO — loop though the current entries in the array and calculate and display the statistics

        int maxValue = 0;

        String maxName = “”;

        double avgValue;

        int i;

        for (i = 0; i < testBook ; i++) {

            maxValue = bookings[0].getGuests();

            for (i = 1; i < testBook; i++) {

                if (bookings[i].getGuests() > maxValue) {

                    maxValue = bookings[i].getGuests();

                    maxName = bookings[i].getBookingName();





        System.out.println(“Statistics for the Rocky Catering Service”);

        System.out.println(maxName + ” has the maximum number of ” + maxValue + ” guests”);

        int minValue = 0;

        String minName = “”;

        for (i = 0; i < testBook; i++) {

            minValue = bookings[0].getGuests();

            for (i = 1; i < testBook; i++) {

                if (bookings[i].getGuests() < minValue) {

                    minValue = bookings[i].getGuests();

                    minName = bookings[i].getBookingName();




        System.out.println(minName + ” has the minimum number of ” + minValue + ” guests”);

        double TValue = 0;

        double TCharges = 0;

        for (i = 0; i < testBook; i++) {

            TValue = TValue + bookings[i].getGuests();

            TCharges = TCharges + bookings[i].calculateCharge();


        avgValue = TValue / testBook;

        System.out.println(“Average number of guests is ” + avgValue + ” guests”);

        System.out.println(“The total charges are: $” + TCharges);


    private void searchBookings() {//booking is searched if it exists in the list of booking names

        String toSearch = JOptionPane.showInputDialog(null, “enter search”, “Invalid input”, JOptionPane.OK_CANCEL_OPTION);

        for (int i = 0; i < testBook; i++) {

            if (toSearch.compareTo(bookings[i].getBookingName()) == 0) {

                System.out.printf(“%-30s%-17s%-6s\n”, bookings[i].getBookingName(), bookings[i].getGuests(), bookings[i].calculateCharge());

            } else {

                JOptionPane.showMessageDialog(null, “enter search”, “Invalid input”, JOptionPane.ERROR_MESSAGE);




    private void sortBookings() {//sorting is done in this method

        String tempName;

        int tempGuest;

        for (int i = 0; i < testBook; i++) {

            for (int j = i + 1; j < testBook; j++) {

                if (bookings[i].getBookingName().compareToIgnoreCase(bookings[j].getBookingName()) > 0) {

                    tempName = bookings[i].getBookingName();

                    tempGuest = bookings[i].getGuests();






                tempName = bookings[i].getBookingName();





    private void handleBookingNameData(Booking[] bookings) {

        System.out.println(“Printing from handleBookingNameData method()…”);

        for (int i = 0; i < bookings.length; i++) {

//                System.out.println(bookings[i].getBookingName());




    public static void main(String[] args) {

        RockyCateringMenu app = new RockyCateringMenu();

        app.frame = new JFrame(“Rocky Catering Service”);