COIT 11222 JAVA Program Using Array of Objects: Assessment 2 Answer
Assessment details COIT 11222
Assessment item 2—JAVA Program using array of objects
This assessment item relates to the unit learning outcomes as stated in the Unit Profile.
What to submit for this assignment
The Java source code:
You will need to submit two source files: Pizza.java and RockyWoodfiredPizzasGUI.java, please submit these files as a single zip file. Only include your source code in the zip file you must submit your report separately.
If you submit the source code with an incorrect name you will lose marks.
A report including an UML diagram of your Pizza class (see text p 468 or p 493 8th edition), how long it took to create the whole program, any problems encountered, and screen shots of the output produced with 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.
You will submit your files by the due date using the “Assignment 2” link on the Moodle unit website under Assessment … Assignment 2 Submission.
For this assignment, you are required to develop a Windowed GUI Java Program to demonstrate you can use Java constructs including input/output via a GUI interface, 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 GUI interface is supplied and is available on the unit website, you must write the underlying code to implement the program. The command buttons 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.
In assignment one we read in multiple customer names pizza details using both Scanner and GUI dialogs. In this assignment we are going to read in the data and output the information via a GUI interface. The code for the GUI interface called RockyWoodfiredPizzasGUI.java is supplied (via the Moodle web site), this has the basic functionality of the interface and you need to use this for your program. We are also going to store the information in an array of Pizza objects.
Look at the code supplied and trace the execution and you will see the buttons are linked to blank methods (stubs) which you will implement the various choices via the buttons.
The GUI interface contains three JLabels for the heading and the prompts.
There are two JTextFields in which the customer name and number of toppings are entered.
There are three JRadioButtons to select a small, medium or large pizza
There is also a JTextArea for displaying the output.
Four JButtons are at the bottom which link to blank methods for implementing the functionality of the application.
First step is to create a class called Pizza (Pizza.java) which will not contain a main method.
The Pizza class will be very simple which will contain three private instance variables:
o customerName as a String
o pizzaSize as a String
o toppings as an integer
The following public methods will have to be implemented:
o A default constructor
o A parameterised constructor
o Three set methods (mutators)
o Three get methods (accessors)
o calculateCharge value returning method*
*You will create a public value returning method calculateCharge(), the pizza charges can be derived from the instance variables pizzaSize and topping. Typically we do not store calculated values in a database so the charge will be derived via your calculateCharge() method. Do not store the charge as an instance variable.
The method will be like calculateCharge method from assignment one, with the same scale of pricing based on the number of toppings and size of pizza. You can modify and use the code from assignment one or use the code from the published solution of assignment (referenced).
Small base: $6.50
Small toppings: 75 cents ($0.75)
Medium base: $10.50
Medium toppings: $1.25
Large base: $14.50
Large toppings: $1.75
You should use the following method header:
public double calculateCharge()
Note: you do not need to pass pizzaSize and toppings as parameters as you can access the instance variables directly within the class.
Once the Pizza class is implemented and fully tested, we can now start to implement the functionality of the GUI interface.
For this assignment we are going to store the customer names, pizza size and number of toppings in an array of Pizza objects. Do not use the ArrayList data structure. Declare an array of Pizza objects as an instance variable of RockyWoodfiredPizzasGUI class, the array should hold ten entries. Use a constant for the maximum entries allowed.
private Pizza  pizzaArray = new Pizza[MAX_PIZZAS];
We need another instance variable (integer) to keep track of the number of pizzas being entered and use this for the index into the array of Pizzas objects.
private int currentPizza = 0;
1. Enter button: enter()
For assignment two we are going to use the JTextFields and the JRadioButtons for our input.
When the enter button is pushed the program will transfer to the method: enter() this is where we read the customer name, the pizza size and the number of toppings and add them to the Pizza array.
Only add one pizza when the enter button is pushed, do not have a loop to add them all
The text in the JTextFields is retrieved using the getText() method:
String customerName = nameField.getText();
When we read the number of toppings input, we are reading a string from the GUI, we will need to convert this to an integer using the Integer wrapper class as per assignment one.
int toppings = Integer.parseInt(toppingsField.getText());
You need to determine which radio button has been selected for the size of pizza, firstly you will notice in the GUI code there has been added a hiddenButton which is not displayed and that it is initially selected: hiddenButton.setSelected(true); You will be able to check if it is selected by: if (hiddenButton.isSelected()) if the hidden button is selected then the user has not selected small, medium or large which they must do, if this is the case then display the following error dialog:
Return the focus to the small radio button: smallButton.requestFocus(); and then exit the method using the return keyword.
If the hidden button is not selected then implement a series of if else statements to determine which of the other buttons have been selected using the isSelected() method as above. Assign the appropriate string to a local String variable e.g. pizzaSize = "Medium";
Note: in the calculateCharge method in your Pizza class you will be comparing the pizza size to "Small", "Medium" and "Large" and not "s", "m" and "l" as in assignment one.
We need to add these values customer name, pizza size and number of toppings to the array of Pizza objects. When we declared our array using the new keyword, only an array of references was created, and we need to create an instance of each of the Pizza objects. When we create the Pizza object we can pass the customer name, pizza size and number of toppings to the object via the parameterised constructor.
pizzaArray[currentPizza] = new Pizza(customerName, pizzaSize, toppings);
Remember to increment currentPizza at the end of the enter method.
Next we will output the entry including the pizza charge in the JTextArea.
The supplied code contains the methods for printing the heading and the line underneath.The font in the text area is “fixed width” so the output can be aligned using column widths in the format string.
displayTextArea.setText(String.format("%-30s%-7s%-15s%-6s\n", "Customer Name", "Size", "Toppings", "Charge"));
Just like the JTextFields the JTextArea has a method setText() to set the text in the text area, note this overwrites the contents of the text area.
You can append text to the text area by using the append() method.
displayTextArea.append("----------- … ----------\n");
Hint: use the format string: "%-30s%-14s%-8d$%5.2f\n" to display one pizza order to match the specification.
To retrieve the values from the array use the get and calculateCharge methods in your Pizza class. Create a separate method to display one line of data and pass the index to this method, this is so you will not be repeating the printing code.
When the data has been entered and displayed, the customer name and number of toppings JTextFields should be cleared. We can clear the contents by using: nameField.setText(""); and do the same for the toppings field. You will also select the hidden radio button (see above).
The focus should then return to the customer name JTextField.
Data validation (you can implement this after you have got the basic functionality implemented, including checking the radio buttons)
When the maximum number of pizzas is reached, do not attempt to add any more pizzas and give the following error message:
Use an if statement at the beginning of the method and after displaying the error dialog use the return statement to exit the method.
Checking the name field:
if (nameField.getText().compareTo("") == 0) // true when blank
Use the above code to check the name field for text and if there is no text then display the following error dialog and use the return statement to exit the method, the focus should return to the name field.The number of toppings field should also be checked for text and appropriate error dialog displayed.The focus should return to toppings field.
We will not worry about checking data types or numeric ranges in this assignment.
2. Display all pizza orders: displayAll()
When this option is selected, display all of the pizza orders which have been entered so far. At the end of the list display the average number of toppings per pizza and the total of the charges collected.
Use a loop structure to iterate through the array, you should be sharing the printing functionality with the enter button, hint: use the method to display a line of data from the enter method.
Only print the entries which have been entered so far and not the whole array, use your instance variable currentPizza as the termination value in your loop.
Sum the number of toppings in the loop for the average calculation and sum the charges for displaying at the end, this is the same as assignment one
Return the focus to the name field after display all.
If no entries have been added then clear the text area and display the following error dialog, repeat this for your search.
3. Search for a customer name: search()
First check if a pizza order has been entered (as above).
You can just use a simple linear search which will be case insensitive. Use the JOptionPane.showInputDialog() method to input the customer name.
If the search is successful display the details about the pizza order.
If the search is unsuccessful display an appropriate message and clear the text area, always return the focus to the nameField.
You need to check if a search name has been entered.
4. Exit the application: exit()
The exit method is called when the user pushes the exit button or when they select the system close (X at top right hand corner), try and find the code which does this (within the supplied code).
During a typical exiting process we may want to ask the user if they want to save any files they have opened or if they really want to exit, in this assignment we will just print an exit message.
Your program should be well laid out, commented and uses appropriate and consistent names (camel notation) for all variables, methods and objects.
Ensure you have header comments in both source files, include name, ID, filename, date and purpose of the class.Make sure you have no repeated code (even writing headings and lines in the output), you should try and write a method which accepts an error message as a parameter and displays the error message in a message dialog within the method; use JOptionPane.ERROR_MESSAGE as the last parameter in the message dialog.
Constants must be used for all literal numbers in your code (calculateCharge method and maximum entries).