Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- mport java.awt.Color;
- import javax.swing.JOptionPane;
- import java.text.*;
- import java.io.*;
- import java.awt.Image;
- import javax.swing.ImageIcon;
- /**
- *
- * @author student
- */
- public class vendingMachine extends javax.swing.JFrame {
- /**
- * Creates new form vendingMachine
- */
- public vendingMachine() {
- initComponents();
- }
- public static double totalMoneyEntered = 0; //Assigning a value of zero to the total money/balance global variable
- //Public array with the stock set to 10 by default for each coin
- public static int[] coinStock = {10, 10, 10, 10, 10}; //Two euro, One Euro, Fifty Cent, Twenty cent, Ten cent
- public static double[] coinValue = {2, 1, 0.5, 0.2, 0.1}; //Two euro, One Euro, Fifty Cent, Twenty cent, Ten cent
- public static double[] productPrice = {1.1, 1.2, 1.3, 1.4};
- public static int[] productStock = {3, 5, 2, 5}; //colastock, diet cola, sprite stock, fanta stock
- DecimalFormat df = new DecimalFormat("#.##"); //Two decimal places object
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- private void jButtonTwoEuroActionPerformed(java.awt.event.ActionEvent evt) {
- addCoin(0,0); //Add Two euro
- }
- private void jButtonSpriteActionPerformed(java.awt.event.ActionEvent evt) {
- if (totalMoneyEntered < productPrice[3]) { //Checking if the totalMoneyEntered variable has the a value below 1.3 and calling a method
- insufficient();
- }else{
- if (productStock[2] < 1) { //Checking if a product is in stock
- outOfStock("Sprite");
- jButtonSprite.setEnabled(false);
- } else {
- buying(2,2);
- }
- }
- }
- private void jButtonOneEuroActionPerformed(java.awt.event.ActionEvent evt) {
- addCoin(1,1); //Add One euro
- }
- private void jButtonFiftyCentActionPerformed(java.awt.event.ActionEvent evt) {
- addCoin(2,2); //Add Fity cent
- }
- private void jButtonTwentyCentActionPerformed(java.awt.event.ActionEvent evt) {
- addCoin(3,3); //Add Tenty cent
- }
- private void jButtonCheckStockActionPerformed(java.awt.event.ActionEvent evt) {
- list1.clear(); //Checks the stock of products
- list1.setForeground(Color.white);
- list1.add("Cola in stock: " + productStock[0]);
- list1.add("Diet Cola in stock: " + productStock[1]);
- list1.add("Sprite in stock: " + productStock[2]);
- list1.add("Fanta in stock: " + productStock[3]);
- }
- private void jButtonColaActionPerformed(java.awt.event.ActionEvent evt) {
- if (totalMoneyEntered < productPrice[0]) { //Checking if the totalMoneyEntered variable has the a value below 1.1 and calling a method
- insufficient();
- } else {
- if (productStock[0] < 1) { //Checking if a product is in stock
- outOfStock("Cola");
- jButtonCola.setEnabled(false);
- } else {
- buying(0,0);
- }
- }
- }
- private void formWindowActivated(java.awt.event.WindowEvent evt) {
- this.setResizable(false); //Setting the resizing of the window to false at activation
- stockEnable();
- }
- private void jButtonDietActionPerformed(java.awt.event.ActionEvent evt) {
- if (totalMoneyEntered < productPrice[1]) { //Checking if the totalMoneyEntered variable has the a value below 1.2 and calling a method
- insufficient();
- } else {
- if (productStock[1] < 1) { //Checking if a product is in stock
- outOfStock("Diet Cola");
- jButtonDiet.setEnabled(false);
- }else{
- buying(1,1);
- }
- }
- }
- private void jButtonFantaActionPerformed(java.awt.event.ActionEvent evt) {
- if (totalMoneyEntered < productPrice[3]) { //Checking if the totalMoneyEntered variable has the a value below 1.4 and calling a method
- insufficient();
- }else{
- if (productStock[3] < 1) { //Checking if a product is in stock
- outOfStock("Fanta");
- jButtonFanta.setEnabled(false);
- } else {
- buying(3,3);
- }
- }
- }
- private void jButtonRefilStockActionPerformed(java.awt.event.ActionEvent evt) {
- stockFrame sf = new stockFrame(); //New frame object and setting it to visible button
- sf.setVisible(true);
- }
- private void jButtonTenCentActionPerformed(java.awt.event.ActionEvent evt) {
- addCoin(4,4); //Add Ten cent
- }
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- //Change loop
- int x = 0; //Variables to count the ammount of coins that have been removed from coin stock/An if statement has executed
- int y = 0;
- int z = 0;
- int v = 0;
- int t = 0;
- list1.clear();
- list1.setForeground(Color.green);
- enable();
- while (totalMoneyEntered>0.0){ //checking if totalMoneyEntered has a value above of zero
- if(totalMoneyEntered >= coinValue[0] && coinStock[0]>0){ //Checking if totalMoneyEntered is 2 or above and if a coin is still in stock
- deduct(0,0);
- x++; //counter
- }
- else if(totalMoneyEntered >= coinValue[1] && coinStock[1]>0){ //Checking if totalMoneyEntered is 1 or above and if a coin is still in stock
- deduct(1,1);
- y++; //counter
- }
- else if(totalMoneyEntered >= coinValue[2] && coinStock[2]>0){ //Checking if totalMoneyEntered is 0.5 or above and if a coin is still in stock
- deduct(2,2);
- z++; //counter
- }
- else if(totalMoneyEntered >= coinValue[3] && coinStock[3]>0){ //Checking if totalMoneyEntered is 0.2 or above and if a coin is still in stock
- deduct(3,3);
- v++; //counter
- }
- else if(totalMoneyEntered >= coinValue[4] && coinStock[4]>0){ //Checking if totalMoneyEntered is 0.1 or above and if a coin is still in stock
- deduct(4,4);
- t++; //counter
- }
- }
- //Displaying how many coins have been returned
- list1.add(x + " Two Euro coins returned");
- list1.add(y + " One Euro coins returned");
- list1.add(z + " Fifty cent coins returned");
- list1.add(v + " Twenty cent coins returned");
- list1.add(t + " Ten cent coins returned");
- }
- //methods
- public void insufficient() { //Method for the display of insufficient funds
- list1.clear();
- list1.setForeground(Color.red);
- list1.add("Insufficient funds, your balance is: " + df.format(totalMoneyEntered));
- }
- public void boughtDisplay() { //Method for the display of buying of a product
- list1.clear();
- list1.setForeground(Color.green);
- list1.add("Thanks, your balance is now: " + df.format(totalMoneyEntered));
- }
- public void fundsAddedDipsplay() { //Method for the display of funds added
- list1.clear();
- list1.setForeground(Color.white);
- list1.add("Balance has increased to: " + df.format(totalMoneyEntered));
- }
- public void outOfStock(String product) { //Method for the display of an out of stock product
- list1.clear();
- list1.setForeground(Color.red);
- list1.add("Sorry " + product + " is out of stock");
- }
- public void disabled(){ //Limit method, disabling of buttons
- jButtonTwoEuro.setEnabled(false);
- jButtonOneEuro.setEnabled(false);
- jButtonFiftyCent.setEnabled(false);
- jButtonTwentyCent.setEnabled(false);
- jButtonTenCent.setEnabled(false);
- list1.add("Please choose a product you wish to purchase");
- }
- public void enable(){ //Enabling of buttons method
- jButtonTwoEuro.setEnabled(true);
- jButtonOneEuro.setEnabled(true);
- jButtonFiftyCent.setEnabled(true);
- jButtonTwentyCent.setEnabled(true);
- jButtonTenCent.setEnabled(true);
- }
- public void addCoin(int x, int y){
- if(totalMoneyEntered >= 10.0){ //Checking if totalMoneyEntered variable is above or equal to 10 and calling a disabled method
- disabled();
- }else{
- totalMoneyEntered = totalMoneyEntered + coinValue[x]; //Addition of coin
- fundsAddedDipsplay();
- coinStock[y]++;
- }
- }
- public void deduct(int x, int y){
- totalMoneyEntered = totalMoneyEntered - coinValue[x]; //Deducton of variables
- totalMoneyEntered = Math.round(totalMoneyEntered * 100) / 100.0;
- coinStock[y]--;
- }
- public void buying(int x, int y){
- totalMoneyEntered = totalMoneyEntered - productPrice[x]; //Buying and the deduction of totalMoneyEntered and stock
- totalMoneyEntered = Math.round(totalMoneyEntered * 100) / 100.0;
- productStock[y]--;
- boughtDisplay();
- enable();
- }
- public void stockEnable(){
- if(productStock[0]>0){
- jButtonCola.setEnabled(true);
- }
- if(productStock[1]>0){
- jButtonDiet.setEnabled(true);
- }
- if(productStock[2]>0){
- jButtonSprite.setEnabled(true);
- }
- if(productStock[3]>0){
- jButtonFanta.setEnabled(true);
- }}
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(vendingMachine.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(vendingMachine.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(vendingMachine.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(vendingMachine.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new vendingMachine().setVisible(true);
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement