Question
Task: Create a project called VendingMachine_FirstName_LastName or Lab3_FirstName_LastName. The program will consist of four files: the VendingMachineDemo.java and style.css files provided Soda.java and VendingMachine.java files
Task: Create a project called VendingMachine_FirstName_LastName or Lab3_FirstName_LastName. The program will consist of four files: the VendingMachineDemo.java and style.css files provided Soda.java and VendingMachine.java files that you will implement. Remember to include comments summarizing the program in the two files that you implement.
1. The Soda class has two fields: a String for the name and a double for the price.
2. The Soda class has two constructors. The first is a parameterized constructor that takes a String and a double to be assigned to the fields of the class. The second is a copy constructor that takes a Soda object and assigns the name and price of that object to the newly constructed Soda object.
3. The Soda class has two getters: one to return the name and one to return the price.
4. The VendingMachine has one field for a Soda. This can initially reference a null object by assigning null to the field.
5. The VendingMachine has one constructor that takes a Soda. Remember to assign a new Soda to the Soda field securely.
6. The addSoda method of the VendingMachine class takes a Soda. Check if the field of the class references a null object (you can use the equals operator to check what the variable references). If the field references a null object, assign the new Soda to the Soda field securely and return true. If the field does not reference a null object, just return false.
7. The removeSoda method of the VendingMachine class takes no arguments. Check if the field of the class does not reference a null object (you can use the equals operator to check what the variable references). If the field does not reference a null object, assign null to the Soda field and return true. If the field references a null object, just return false.
8. In the VendingMachineDemo, change the name and price for the Soda being added to the VendingMachine to any other name and price of your choosing.
Criteria: The comments summarizing the program are worth 5 points. The fields of the Soda class are worth 3 points each (6 points total). The constructors for the Soda class are worth 10 points each (20 points total). The getters for the Soda class are worth 5 points each (10 points total). The field of the VendingMachine class is worth 6 points. The constructor, addSoda, and removeSoda methods of the VendingMachine class are worth 16 points each (48 points total). Changing the name and price of the Soda object in the VendingMachineDemo class is worth 5 points. VendingMachineDemo.java
import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene;
import javafx.scene.layout.HBox; import javafx.scene.layout.VBox;
import javafx.scene.control.Label; import javafx.scene.control.Button;
import javafx.geometry.Pos; import javafx.geometry.Insets;
public class VendingMachineDemo extends Application { public static void main(String[] args) { launch(args); }
public void start(Stage primaryStage) { Button addSodaBtn = new Button("Add a Soda"); Button buySodaBtn = new Button("Buy a Soda"); Label output = new Label("The vending machine is set up and has one soda in it."); Soda soda = new Soda("Sunkist", 1.25); VendingMachine vendingMachine = new VendingMachine(soda);
addSodaBtn.setOnAction(event -> { if (vendingMachine.addSoda(soda)) output.setText("A soda was added to the vending machine."); else output.setText("The vending machine already has a soda in it."); });
buySodaBtn.setOnAction(event -> { if (vendingMachine.removeSoda()) output.setText(String.format("You purchased a %s for $%.2f.", soda.getName(), soda.getPrice())); else output.setText("The vending machine is empty."); });
HBox buttonRow = new HBox(20, addSodaBtn, buySodaBtn); buttonRow.setPadding(new Insets(50, 0, 0, 0)); buttonRow.setAlignment(Pos.CENTER);
VBox root = new VBox(20, buttonRow, output); root.setAlignment(Pos.TOP_CENTER); Scene scene = new Scene(root, 700, 300);
scene.getStylesheets().add(this.getClass().getResource("style.css").toExternalForm()); addSodaBtn.getStyleClass().add("add-button"); buySodaBtn.getStyleClass().add("buy-button");
primaryStage.setScene(scene); primaryStage.setTitle("Vending Machine"); primaryStage.show(); } }
style.css
.root { -fx-font-size: 24px; }
.add-button { -fx-background-color: green; -fx-text-fill: white; }
.buy-button { -fx-background-color: yellow; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started