Commit 830c94c2 authored by Daniel's avatar Daniel

completed

parents
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Vending_machine</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Daniel
*/
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.io.IOException;
public class NewJFrame extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
}
/**
* 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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
//initiate all the components of the GUI
Chocolate_label = new javax.swing.JLabel();
Water_label = new javax.swing.JLabel();
Drink_label = new javax.swing.JLabel();
Snack_label = new javax.swing.JLabel();
Sweet_label = new javax.swing.JLabel();
Choc_quant = new javax.swing.JLabel();
Water_quant = new javax.swing.JLabel();
Drink_quant = new javax.swing.JLabel();
Snack_quant = new javax.swing.JLabel();
Sweet_quant = new javax.swing.JLabel();
Choc_price = new javax.swing.JLabel();
Water_price = new javax.swing.JLabel();
Drink_price = new javax.swing.JLabel();
Snack_price = new javax.swing.JLabel();
Sweet_price = new javax.swing.JLabel();
Item_list = new javax.swing.JComboBox<>();
Quant_list = new javax.swing.JComboBox<>();
jScrollPane1 = new javax.swing.JScrollPane();
Basket = new javax.swing.JTextArea();
Price_entry = new javax.swing.JTextField();
Enter_cash = new javax.swing.JLabel();
Purchase_but = new javax.swing.JButton();
Item_selection = new javax.swing.JLabel();
Quantity_selection = new javax.swing.JLabel();
pound_sign = new javax.swing.JLabel();
Select_but = new javax.swing.JButton();
Confirmation_label = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
Chocolate_label.setText("Chocolate");
Water_label.setText("Water");
Drink_label.setText("Drink");
Snack_label.setText("Snack");
Sweet_label.setText("Sweet");
Choc_quant.setText("20");
Water_quant.setText("20");
Drink_quant.setText("20");
Snack_quant.setText("20");
Sweet_quant.setText("20");
Choc_price.setText("2");
Water_price.setText("0.50");
Drink_price.setText("1");
Snack_price.setText("2");
Sweet_price.setText("1");
Item_list.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Chocolate", "Water", "Drink", "Snack", "Sweet" }));
Quant_list.setModel(new javax.swing.DefaultComboBoxModel<>(new Integer[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}));
Basket.setEditable(false);
Basket.setColumns(20);
Basket.setRows(5);
Basket.setText("=============Basket=============\nChocolate 0\nWater "
+ "0\nDrink 0\nSnack 0\nSweet 0\n Total = ");
jScrollPane1.setViewportView(Basket);
Price_entry.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Price_entryActionPerformed(evt);
}
});
Enter_cash.setText("Enter Cash");
Purchase_but.setText("Purchase");
Purchase_but.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Purchase_butActionPerformed(evt);
}
});
Item_selection.setText("Item Selection");
Quantity_selection.setText("Quantity Selection");
pound_sign.setText("");
Select_but.setText("Select");
Select_but.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Select_butActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(35, 35, 35)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Item_list, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Item_selection)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(Sweet_label, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Water_label, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Chocolate_label, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Drink_label, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Snack_label, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(Quant_list, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Select_but)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(Water_quant, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(Water_price, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(Choc_quant, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(Choc_price, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(Drink_quant, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(Drink_price, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(Snack_quant, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(Snack_price, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(Sweet_quant, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(Sweet_price, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(Quantity_selection))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(53, 53, 53)
.addComponent(Confirmation_label, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(pound_sign)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Price_entry, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Purchase_but))
.addComponent(Enter_cash, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(32, 32, 32))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Chocolate_label)
.addComponent(Choc_quant)
.addComponent(Choc_price))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Water_label)
.addComponent(Water_quant)
.addComponent(Water_price))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Drink_label)
.addComponent(Drink_quant)
.addComponent(Drink_price))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Snack_label)
.addComponent(Snack_quant)
.addComponent(Snack_price))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Sweet_label)
.addComponent(Sweet_quant)
.addComponent(Sweet_price))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Item_selection)
.addComponent(Quantity_selection))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Item_list, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Quant_list, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Select_but))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
.addComponent(Enter_cash)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Price_entry, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Purchase_but)
.addComponent(pound_sign)
.addComponent(Confirmation_label, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(34, 34, 34))
);
pack();
}// </editor-fold>
private void Price_entryActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void Purchase_butActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//reads what is in the entry box for the cash entry and saves it as a double
money = Double.parseDouble(Price_entry.getText());
if(money >= total) {
//if the cash entered is greater than or equal to the price then the order is able to be completed,
//and if the cash is more then there is also change which is shown on the receipt
change_total = money - total;
PrintWriter writer;
try {
//receipt is written to a file called "Receipt.txt"
writer = new PrintWriter("Receipt.txt", "UTF-8");
writer.print("Chocolate "
+String.valueOf(choc_order)+ "\nWater " +String.valueOf(water_order)
+ "\nDrink "+ String.valueOf(drink_order) + "\nSnack " + String.valueOf(snack_order)
+ "\nSweet "+String.valueOf(sweet_order)+"\n Total = " + String.valueOf(total) + "0\n"
+ "=======================\nCash paid = " + money + ""
+ "0\nChange given = " + change_total + "0");
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//resets the basket after the transaction has been completed
choc_order = 0;
water_order = 0;
drink_order = 0;
snack_order = 0;
sweet_order = 0;
total = 0;
//shows a confirmation of the order
Confirmation_label.setText("Order confirmed");
Price_entry.setText("");
//prints the current basket
Basket.setText("=============Basket=============\nChocolate "
+String.valueOf(choc_order)+ "\nWater " +String.valueOf(water_order)
+ "\nDrink "+ String.valueOf(drink_order) + "\nSnack " + String.valueOf(snack_order)
+ "\nSweet "+String.valueOf(sweet_order)+"\n Total = " + String.valueOf(total) + "0");
}
//error shown if the cash given is not enough
else if (money < total) {
Confirmation_label.setText("Insufficient funding");
}
}
private void Select_butActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//when the select button is pressed, checks the item in the item box and the value in the quantity box and
//saves them as variables
String item = (String) Item_list.getSelectedItem();
int quant = (int) Quant_list.getSelectedItem();
//depending on what the item chosen is and the quantity, the quantity of the item decreases and is then displayed
//onto the basket, along with the price
if (item == "Chocolate") {
//calculates the new values after the quantity has been taken/added from various variables
if (quant <= choc_sup) {
choc_sup = choc_sup - quant;
choc_order = choc_order + quant;
total = total + (choc_cost * quant);
Choc_quant.setText(String.valueOf(choc_sup));
//if there was any previous error, the confirmation label is wiped of the error to show it is no longer an error
Confirmation_label.setText("");
//prints the current basket
Basket.setText("=============Basket=============\nChocolate "
+String.valueOf(choc_order)+ "\nWater " +String.valueOf(water_order)
+ "\nDrink "+ String.valueOf(drink_order) + "\nSnack " + String.valueOf(snack_order)
+ "\nSweet "+String.valueOf(sweet_order)+"\n Total = " + String.valueOf(total) + "0");
}
else {
//error message if there is not enough of the product available
Confirmation_label.setText("Unavaliable quantity");
}
}
else if (item == "Water") {
if (quant <= water_sup) {
//calculates the new values after the quantity has been taken/added from various variables
water_sup = water_sup - quant;
water_order = water_order + quant;
total = total + (water_cost * quant);
Water_quant.setText(String.valueOf(water_sup));
//if there was any previous error, the confirmation label is wiped of the error to show it is no longer an error
Confirmation_label.setText("");
//prints the current basket
Basket.setText("=============Basket=============\nChocolate "
+String.valueOf(choc_order)+ "\nWater " +String.valueOf(water_order)
+ "\nDrink "+ String.valueOf(drink_order) + "\nSnack " + String.valueOf(snack_order)
+ "\nSweet "+String.valueOf(sweet_order)+"\n Total = " + String.valueOf(total) + "0");
}
else {
//error message if there is not enough of the product available
Confirmation_label.setText("Unavaliable quantity");
}
}
else if (item == "Drink") {
if (quant <= drink_sup) {
//calculates the new values after the quantity has been taken/added from various variables
drink_sup = drink_sup - quant;
drink_order = drink_order + quant;
total = total + (drink_cost * quant);
Drink_quant.setText(String.valueOf(drink_sup));
//if there was any previous error, the confirmation label is wiped of the error to show it is no longer an error
Confirmation_label.setText("");
//prints the current basket
Basket.setText("=============Basket=============\nChocolate "
+String.valueOf(choc_order)+ "\nWater " +String.valueOf(water_order)
+ "\nDrink "+ String.valueOf(drink_order) + "\nSnack " + String.valueOf(snack_order)
+ "\nSweet "+String.valueOf(sweet_order)+"\n Total = " + String.valueOf(total) + "0");
}
else {
//error message if there is not enough of the product available
Confirmation_label.setText("Unavaliable quantity");
}
}
else if (item == "Snack") {
if (quant <= snack_sup) {
//calculates the new values after the quantity has been taken/added from various variables
snack_sup = snack_sup - quant;
snack_order = snack_order + quant;
total = total + (snack_cost * quant);
Snack_quant.setText(String.valueOf(snack_sup));
//if there was any previous error, the confirmation label is wiped of the error to show it is no longer an error
Confirmation_label.setText("");
//prints the current basket
Basket.setText("=============Basket=============\nChocolate "
+String.valueOf(choc_order)+ "\nWater " +String.valueOf(water_order)
+ "\nDrink "+ String.valueOf(drink_order) + "\nSnack " + String.valueOf(snack_order)
+ "\nSweet "+String.valueOf(sweet_order)+"\n Total = " + String.valueOf(total) + "0");
}
else {
//error message if there is not enough of the product available
Confirmation_label.setText("Unavaliable quantity");
}
}
else if (item == "Sweet") {
if (quant <= sweet_sup) {
//calculates the new values after the quantity has been taken/added from various variables
sweet_sup = sweet_sup - quant;
sweet_order = sweet_order + quant;
total = total + (sweet_cost * quant);
Sweet_quant.setText(String.valueOf(sweet_sup));
//if there was any previous error, the confirmation label is wiped of the error to show it is no longer an error
Confirmation_label.setText("");
//prints the current basket
Basket.setText("=============Basket=============\nChocolate "
+String.valueOf(choc_order)+ "\nWater " +String.valueOf(water_order)
+ "\nDrink "+ String.valueOf(drink_order) + "\nSnack " + String.valueOf(snack_order)
+ "\nSweet "+String.valueOf(sweet_order)+"\n Total = " + String.valueOf(total) + "0");
}
else {
//error message if there is not enough of the product available
Confirmation_label.setText("Unavaliable quantity");
}
}
}
/**
* @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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.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 NewJFrame().setVisible(true);
}
});
}
//declares all of the global variables and the variables for the GUI
int choc_sup = 20, water_sup = 20, drink_sup = 20, snack_sup = 20, sweet_sup = 20,
choc_order = 0, water_order = 0, drink_order = 0, snack_order = 0, sweet_order = 0;
double choc_cost = 2, water_cost = 0.5, drink_cost = 1, snack_cost = 2, sweet_cost = 1, total = 0,
money = 0, change_total = 0;
// Variables declaration - do not modify
private javax.swing.JTextArea Basket;
private javax.swing.JLabel Choc_price;
private javax.swing.JLabel Choc_quant;
private javax.swing.JLabel Chocolate_label;
private javax.swing.JLabel Confirmation_label;
private javax.swing.JLabel Drink_label;
private javax.swing.JLabel Drink_price;
private javax.swing.JLabel Drink_quant;
private javax.swing.JLabel Enter_cash;
private javax.swing.JComboBox<String> Item_list;
private javax.swing.JLabel Item_selection;
private javax.swing.JTextField Price_entry;
private javax.swing.JButton Purchase_but;
private javax.swing.JComboBox<Integer> Quant_list;
private javax.swing.JLabel Quantity_selection;
private javax.swing.JButton Select_but;
private javax.swing.JLabel Snack_label;
private javax.swing.JLabel Snack_price;
private javax.swing.JLabel Snack_quant;
private javax.swing.JLabel Sweet_label;
private javax.swing.JLabel Sweet_price;
private javax.swing.JLabel Sweet_quant;
private javax.swing.JLabel Water_label;
private javax.swing.JLabel Water_price;
private javax.swing.JLabel Water_quant;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel pound_sign;
// End of variables declaration
}
\ No newline at end of file
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Daniel
*/
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.io.IOException;
public class Vending_Program extends javax.swing.JFrame {
/**
* Creates new form Vending_Program
*/
public Vending_Program() {
initComponents();
}
/**
* 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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
//initiate all the components of the GUI
Chocolate_label = new javax.swing.JLabel();
Water_label = new javax.swing.JLabel();
Drink_label = new javax.swing.JLabel();
Snack_label = new javax.swing.JLabel();
Sweet_label = new javax.swing.JLabel();
Choc_quant = new javax.swing.JLabel();
Water_quant = new javax.swing.JLabel();
Drink_quant = new javax.swing.JLabel();
Snack_quant = new javax.swing.JLabel();
Sweet_quant = new javax.swing.JLabel();
Choc_price = new javax.swing.JLabel();
Water_price = new javax.swing.JLabel();
Drink_price = new javax.swing.JLabel();
Snack_price = new javax.swing.JLabel();
Sweet_price = new javax.swing.JLabel();
Item_list = new javax.swing.JComboBox<>();
Quant_list = new javax.swing.JComboBox<>();
jScrollPane1 = new javax.swing.JScrollPane();
Basket = new javax.swing.JTextArea();
Price_entry = new javax.swing.JTextField();
Enter_cash = new javax.swing.JLabel();
Purchase_but = new javax.swing.JButton();
Item_selection = new javax.swing.JLabel();
Quantity_selection = new javax.swing.JLabel();
pound_sign = new javax.swing.JLabel();
Select_but = new javax.swing.JButton();
Confirmation_label = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
Chocolate_label.setText("Chocolate");
Water_label.setText("Water");
Drink_label.setText("Drink");
Snack_label.setText("Snack");
Sweet_label.setText("Sweet");
Choc_quant.setText("20");
Water_quant.setText("20");
Drink_quant.setText("20");
Snack_quant.setText("20");
Sweet_quant.setText("20");
Choc_price.setText("2");
Water_price.setText("0.50");
Drink_price.setText("1");
Snack_price.setText("2");
Sweet_price.setText("1");
Item_list.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Chocolate", "Water", "Drink", "Snack", "Sweet" }));
Quant_list.setModel(new javax.swing.DefaultComboBoxModel<>(new Integer[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}));
Basket.setEditable(false);
Basket.setColumns(20);
Basket.setRows(5);
Basket.setText("=============Basket=============\nChocolate 0\nWater "
+ "0\nDrink 0\nSnack 0\nSweet 0\n Total = ");
jScrollPane1.setViewportView(Basket);
Price_entry.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Price_entryActionPerformed(evt);
}
});
Enter_cash.setText("Enter Cash");
Purchase_but.setText("Purchase");
Purchase_but.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Purchase_butActionPerformed(evt);
}
});
Item_selection.setText("Item Selection");
Quantity_selection.setText("Quantity Selection");
pound_sign.setText("");
Select_but.setText("Select");
Select_but.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Select_butActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(35, 35, 35)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Item_list, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Item_selection)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(Sweet_label, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Water_label, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Chocolate_label, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Drink_label, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Snack_label, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(Quant_list, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Select_but)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(Water_quant, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(Water_price, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(Choc_quant, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(Choc_price, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(Drink_quant, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(Drink_price, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(Snack_quant, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(Snack_price, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(Sweet_quant, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(Sweet_price, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(Quantity_selection))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(53, 53, 53)
.addComponent(Confirmation_label, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(pound_sign)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Price_entry, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Purchase_but))
.addComponent(Enter_cash, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(32, 32, 32))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Chocolate_label)
.addComponent(Choc_quant)
.addComponent(Choc_price))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Water_label)
.addComponent(Water_quant)
.addComponent(Water_price))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Drink_label)
.addComponent(Drink_quant)
.addComponent(Drink_price))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Snack_label)
.addComponent(Snack_quant)
.addComponent(Snack_price))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Sweet_label)
.addComponent(Sweet_quant)
.addComponent(Sweet_price))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Item_selection)
.addComponent(Quantity_selection))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Item_list, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Quant_list, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Select_but))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
.addComponent(Enter_cash)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Price_entry, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Purchase_but)
.addComponent(pound_sign)
.addComponent(Confirmation_label, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(34, 34, 34))
);
pack();
}// </editor-fold>
private void Price_entryActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void Purchase_butActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//reads what is in the entry box for the cash entry and saves it as a double
money = Double.parseDouble(Price_entry.getText());
if(money >= total) {
//if the cash entered is greater than or equal to the price then the order is able to be completed,
//and if the cash is more then there is also change which is shown on the receipt
change_total = money - total;
PrintWriter writer;
try {
//receipt is written to a file called "Receipt.txt"
writer = new PrintWriter("Receipt.txt", "UTF-8");
writer.print("Chocolate "
+String.valueOf(choc_order)+ "\nWater " +String.valueOf(water_order)
+ "\nDrink "+ String.valueOf(drink_order) + "\nSnack " + String.valueOf(snack_order)
+ "\nSweet "+String.valueOf(sweet_order)+"\n Total = " + String.valueOf(total) + "0\n"
+ "=======================\nCash paid = " + money + ""
+ "0\nChange given = " + change_total + "0");
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//resets the basket after the transaction has been completed
choc_order = 0;
water_order = 0;
drink_order = 0;
snack_order = 0;
sweet_order = 0;
total = 0;
//shows a confirmation of the order
Confirmation_label.setText("Order confirmed");
Price_entry.setText("");
//prints the current basket
Basket.setText("=============Basket=============\nChocolate "
+String.valueOf(choc_order)+ "\nWater " +String.valueOf(water_order)
+ "\nDrink "+ String.valueOf(drink_order) + "\nSnack " + String.valueOf(snack_order)
+ "\nSweet "+String.valueOf(sweet_order)+"\n Total = " + String.valueOf(total) + "0");
}
//error shown if the cash given is not enough
else if (money < total) {
Confirmation_label.setText("Insufficient funding");
}
}
private void Select_butActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//when the select button is pressed, checks the item in the item box and the value in the quantity box and
//saves them as variables
String item = (String) Item_list.getSelectedItem();
int quant = (int) Quant_list.getSelectedItem();
//depending on what the item chosen is and the quantity, the quantity of the item decreases and is then displayed
//onto the basket, along with the price
if (item == "Chocolate") {
//calculates the new values after the quantity has been taken/added from various variables
if (quant <= choc_sup) {
choc_sup = choc_sup - quant;
choc_order = choc_order + quant;
total = total + (choc_cost * quant);
Choc_quant.setText(String.valueOf(choc_sup));
//if there was any previous error, the confirmation label is wiped of the error to show it is no longer an error
Confirmation_label.setText("");
//prints the current basket
Basket.setText("=============Basket=============\nChocolate "
+String.valueOf(choc_order)+ "\nWater " +String.valueOf(water_order)
+ "\nDrink "+ String.valueOf(drink_order) + "\nSnack " + String.valueOf(snack_order)
+ "\nSweet "+String.valueOf(sweet_order)+"\n Total = " + String.valueOf(total) + "0");
}
else {
//error message if there is not enough of the product available
Confirmation_label.setText("Unavaliable quantity");
}
}
else if (item == "Water") {
if (quant <= water_sup) {
//calculates the new values after the quantity has been taken/added from various variables
water_sup = water_sup - quant;
water_order = water_order + quant;
total = total + (water_cost * quant);
Water_quant.setText(String.valueOf(water_sup));
//if there was any previous error, the confirmation label is wiped of the error to show it is no longer an error
Confirmation_label.setText("");
//prints the current basket
Basket.setText("=============Basket=============\nChocolate "
+String.valueOf(choc_order)+ "\nWater " +String.valueOf(water_order)
+ "\nDrink "+ String.valueOf(drink_order) + "\nSnack " + String.valueOf(snack_order)
+ "\nSweet "+String.valueOf(sweet_order)+"\n Total = " + String.valueOf(total) + "0");
}
else {
//error message if there is not enough of the product available
Confirmation_label.setText("Unavaliable quantity");
}
}
else if (item == "Drink") {
if (quant <= drink_sup) {
//calculates the new values after the quantity has been taken/added from various variables
drink_sup = drink_sup - quant;
drink_order = drink_order + quant;
total = total + (drink_cost * quant);
Drink_quant.setText(String.valueOf(drink_sup));
//if there was any previous error, the confirmation label is wiped of the error to show it is no longer an error
Confirmation_label.setText("");
//prints the current basket
Basket.setText("=============Basket=============\nChocolate "
+String.valueOf(choc_order)+ "\nWater " +String.valueOf(water_order)
+ "\nDrink "+ String.valueOf(drink_order) + "\nSnack " + String.valueOf(snack_order)
+ "\nSweet "+String.valueOf(sweet_order)+"\n Total = " + String.valueOf(total) + "0");
}
else {
//error message if there is not enough of the product available
Confirmation_label.setText("Unavaliable quantity");
}
}
else if (item == "Snack") {
if (quant <= snack_sup) {
//calculates the new values after the quantity has been taken/added from various variables
snack_sup = snack_sup - quant;
snack_order = snack_order + quant;
total = total + (snack_cost * quant);
Snack_quant.setText(String.valueOf(snack_sup));
//if there was any previous error, the confirmation label is wiped of the error to show it is no longer an error
Confirmation_label.setText("");
//prints the current basket
Basket.setText("=============Basket=============\nChocolate "
+String.valueOf(choc_order)+ "\nWater " +String.valueOf(water_order)
+ "\nDrink "+ String.valueOf(drink_order) + "\nSnack " + String.valueOf(snack_order)
+ "\nSweet "+String.valueOf(sweet_order)+"\n Total = " + String.valueOf(total) + "0");
}
else {
//error message if there is not enough of the product available
Confirmation_label.setText("Unavaliable quantity");
}
}
else if (item == "Sweet") {
if (quant <= sweet_sup) {
//calculates the new values after the quantity has been taken/added from various variables
sweet_sup = sweet_sup - quant;
sweet_order = sweet_order + quant;
total = total + (sweet_cost * quant);
Sweet_quant.setText(String.valueOf(sweet_sup));
//if there was any previous error, the confirmation label is wiped of the error to show it is no longer an error
Confirmation_label.setText("");
//prints the current basket
Basket.setText("=============Basket=============\nChocolate "
+String.valueOf(choc_order)+ "\nWater " +String.valueOf(water_order)
+ "\nDrink "+ String.valueOf(drink_order) + "\nSnack " + String.valueOf(snack_order)
+ "\nSweet "+String.valueOf(sweet_order)+"\n Total = " + String.valueOf(total) + "0");
}
else {
//error message if there is not enough of the product available
Confirmation_label.setText("Unavaliable quantity");
}
}
}
/**
* @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(Vending_Program.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Vending_Program.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Vending_Program.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Vending_Program.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 Vending_Program().setVisible(true);
}
});
}
//declares all of the global variables and the variables for the GUI
int choc_sup = 20, water_sup = 20, drink_sup = 20, snack_sup = 20, sweet_sup = 20,
choc_order = 0, water_order = 0, drink_order = 0, snack_order = 0, sweet_order = 0;
double choc_cost = 2, water_cost = 0.5, drink_cost = 1, snack_cost = 2, sweet_cost = 1, total = 0,
money = 0, change_total = 0;
// Variables declaration - do not modify
private javax.swing.JTextArea Basket;
private javax.swing.JLabel Choc_price;
private javax.swing.JLabel Choc_quant;
private javax.swing.JLabel Chocolate_label;
private javax.swing.JLabel Confirmation_label;
private javax.swing.JLabel Drink_label;
private javax.swing.JLabel Drink_price;
private javax.swing.JLabel Drink_quant;
private javax.swing.JLabel Enter_cash;
private javax.swing.JComboBox<String> Item_list;
private javax.swing.JLabel Item_selection;
private javax.swing.JTextField Price_entry;
private javax.swing.JButton Purchase_but;
private javax.swing.JComboBox<Integer> Quant_list;
private javax.swing.JLabel Quantity_selection;
private javax.swing.JButton Select_but;
private javax.swing.JLabel Snack_label;
private javax.swing.JLabel Snack_price;
private javax.swing.JLabel Snack_quant;
private javax.swing.JLabel Sweet_label;
private javax.swing.JLabel Sweet_price;
private javax.swing.JLabel Sweet_quant;
private javax.swing.JLabel Water_label;
private javax.swing.JLabel Water_price;
private javax.swing.JLabel Water_quant;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel pound_sign;
// End of variables declaration
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment