Advertisement
Nick-O-Rama

VendingMachine

Sep 15th, 2015
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.47 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package vendingmachine;
  7.  
  8. import java.net.URL;
  9. import java.util.ResourceBundle;
  10. import javafx.event.ActionEvent;
  11. import javafx.fxml.FXML;
  12. import javafx.fxml.Initializable;
  13. import javafx.scene.control.*;
  14.  
  15.  
  16. /**
  17.  *
  18.  * @author Compsci
  19.  */
  20. public class FXMLDocumentController implements Initializable {
  21.    
  22.     @FXML
  23.     private Label label;
  24.     @FXML
  25.     private RadioButton cola;
  26.     @FXML
  27.     private RadioButton grape;
  28.     @FXML
  29.     private RadioButton root;
  30.     @FXML
  31.     private RadioButton water;
  32.     @FXML
  33.     private Label change;
  34.     @FXML
  35.     private Label message;
  36.     @FXML
  37.     private TextField moneyInput;
  38.     @FXML
  39.     private Label colaStockLabel;
  40.     @FXML
  41.     private Label grapeStockLabel;
  42.     @FXML
  43.     private Label rootStockLabel;
  44.     @FXML
  45.     private Label waterStockLabel;
  46.    
  47.    
  48.     private int colaStock = 20;
  49.     private int grapeStock = 20;
  50.     private int rootStock = 20;
  51.     private int waterStock = 20;
  52.     private final double COST = 0.75;
  53.    
  54.    
  55.    
  56.    
  57.     @FXML
  58.     private void handleButtonAction(ActionEvent event) {
  59.         System.out.println("You clicked me!");
  60.         checkInput();
  61.         buyDrink();
  62.     }
  63.    
  64.     private void checkInput() {
  65.         try {
  66.             if (Double.parseDouble(moneyInput.getText()) < 0.75) {
  67.                 message.setText("Please enter a number greater than 0.75");
  68.                 change.setText("0.00");
  69.  
  70.             }
  71.             else if (colaStock < 1 || grapeStock < 1 || rootStock < 1 || waterStock < 1){
  72.                 message.setText("Drink is out of stock!");
  73.                 change.setText("0.00");
  74.  
  75.             }
  76.         }
  77.         catch (Exception e) {
  78.             message.setText("Please enter a number greater than 0.75");
  79.             change.setText("0.00");
  80.         }
  81.     }
  82.    
  83.     private void buyDrink() {
  84.         try {
  85.             if (Double.parseDouble(moneyInput.getText()) > 0.75) {
  86.                 if (cola.isSelected() && colaStock > 0) {
  87.                     colaStock--;
  88.                     colaStockLabel.setText(Integer.toString(colaStock));
  89.                     giveChange();
  90.                 }
  91.                 if (grape.isSelected() && grapeStock > 0) {
  92.                     grapeStock--;
  93.                     grapeStockLabel.setText(Integer.toString(grapeStock));
  94.                     giveChange();
  95.                 }
  96.                 if (root.isSelected() && rootStock > 0) {
  97.                     rootStock--;
  98.                     rootStockLabel.setText(Integer.toString(rootStock));
  99.                     giveChange();
  100.                 }
  101.                 if (water.isSelected() && waterStock > 0) {
  102.                     waterStock--;
  103.                     waterStockLabel.setText(Integer.toString(waterStock));
  104.                     giveChange();
  105.                 }
  106.             }
  107.         }
  108.         catch (Exception e) {
  109.            
  110.         }
  111.     }
  112.    
  113.     private void giveChange() {
  114.         if (Double.parseDouble(moneyInput.getText()) > 0.75) {
  115.             double returnChange = Double.parseDouble(moneyInput.getText()) - 0.75;
  116.             change.setText(Double.toString(returnChange));
  117.             message.setText("");
  118.         }
  119.     }
  120.    
  121.     @Override
  122.     public void initialize(URL url, ResourceBundle rb) {
  123.         // TODO
  124.     }    
  125.    
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement