patternjavaMinor
Pizza ordering program recursion on the GUI
Viewed 0 times
therecursionpizzaprogramorderinggui
Problem
This program is a basic pizza ordering program with seven topping options, four size options, and five crust options. It pulls the options from a database and populates the GUI on start.
I connected the images saved in the program to the strings associated with each topping. I used a recursive method to stack the topping images that will update each time a topping selection is made. The recursive method was the only solution I found that would restack the images without a blank space when a topping was deselected. It is working really well but I wanted to make it available for review.
The recursive method I used for the GUI is on the bottom of the code. There is also a lot of code written for the check box buttons. I'm not sure if I could have made that any shorter.
This is how the program looks:
```
public class FXMLDocumentController implements Initializable {
// variables with fx:id names for image views
@FXML
private ImageView ivOne;
@FXML
private ImageView ivTwo;
@FXML
private ImageView ivThree;
@FXML
private ImageView ivFour;
@FXML
private ImageView ivFive;
@FXML
private ImageView ivSix;
@FXML
private ImageView ivSeven;
@FXML
private ImageView ivSize;
@FXML
private ImageView ivCrust;
// create an image view array for the recursive method used to update the
// toppings images.
@FXML
private final ImageView[] iva = new ImageView[7];
// array for toppings, size, and crust images
private Image[] topImgs = new Image[8];
private Image[] sizeImgs = new Image[4];
private Image[] crustImgs = new Image[5];
// variables for java text fields displaying price breakdown and total of pizza
@FXML
private TextField tfSizePrice;
@FXML
private TextField tfToppingsPrice;
@FXML
private TextField tfExtrasPrice;
@FXML
private TextField tfTotalPrice;
@FXML
private TextArea taPizzaOverview;
// variables for java radio button elements in display for pizza size
@FXML
private RadioButton radioButtonSizeOne;
@FXML
private RadioButton radioButtonSizeTwo;
@FXML
private RadioButto
I connected the images saved in the program to the strings associated with each topping. I used a recursive method to stack the topping images that will update each time a topping selection is made. The recursive method was the only solution I found that would restack the images without a blank space when a topping was deselected. It is working really well but I wanted to make it available for review.
The recursive method I used for the GUI is on the bottom of the code. There is also a lot of code written for the check box buttons. I'm not sure if I could have made that any shorter.
This is how the program looks:
```
public class FXMLDocumentController implements Initializable {
// variables with fx:id names for image views
@FXML
private ImageView ivOne;
@FXML
private ImageView ivTwo;
@FXML
private ImageView ivThree;
@FXML
private ImageView ivFour;
@FXML
private ImageView ivFive;
@FXML
private ImageView ivSix;
@FXML
private ImageView ivSeven;
@FXML
private ImageView ivSize;
@FXML
private ImageView ivCrust;
// create an image view array for the recursive method used to update the
// toppings images.
@FXML
private final ImageView[] iva = new ImageView[7];
// array for toppings, size, and crust images
private Image[] topImgs = new Image[8];
private Image[] sizeImgs = new Image[4];
private Image[] crustImgs = new Image[5];
// variables for java text fields displaying price breakdown and total of pizza
@FXML
private TextField tfSizePrice;
@FXML
private TextField tfToppingsPrice;
@FXML
private TextField tfExtrasPrice;
@FXML
private TextField tfTotalPrice;
@FXML
private TextArea taPizzaOverview;
// variables for java radio button elements in display for pizza size
@FXML
private RadioButton radioButtonSizeOne;
@FXML
private RadioButton radioButtonSizeTwo;
@FXML
private RadioButto
Solution
There's a lot that can be said here, but I'll keep it to the main points of attention.
- Yes, some of the data is loaded from the database, but it doesn't really make any difference; how many options there are per category is hard coded, and the toppings must match the hard coded images. The solution would be to add the options to the UI dynamically (this may be challenging).
- One big class that does almost everything : manage presentation logic, get data from the DB, keep track of the pizza composition, pricing. You should take a look at MVC or MVP patterns.
- The behavior of every single button is squeezed into one event handling method. Each buttons can have its own eventHandler, this will avoid the parade of if elses to determine which button it is. Similar event handlers can be instances of the same class with different parameters.
- Empty catch block for PatternSyntaxException. It probably cannot occur, but if a bug is introduced and it does occur, you'll never find it. It's a RuntimeException, so just don't catch it. (I'm not saying RuntimeException should never be caught, I'm saying RuntimeExceptions that should never occur, shouldn't be caught)
Context
StackExchange Code Review Q#154773, answer score: 2
Revisions (0)
No revisions yet.