Commit 6daad40d authored by robert.sharp's avatar robert.sharp

Update temp.java

parent 3f48aeac
......@@ -3,6 +3,7 @@
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
/**
*
......@@ -13,6 +14,7 @@ public class temp extends javax.swing.JFrame {
* Creates new form temp
*/
static double price[]={2, 0.5, 1, 2, 1};
//the prices
static double quantity[]={20, 20, 20, 20, 20};
......@@ -36,6 +38,10 @@ public class temp extends javax.swing.JFrame {
static double amount = 0;
//expected cost
static double cost = 0;
//returned change
static double change = 0;
//total payed
static double payed = 0;
public temp() {
initComponents();
......@@ -47,7 +53,7 @@ public class temp extends javax.swing.JFrame {
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
ItemSelect = new javax.swing.JPanel();
......@@ -59,12 +65,14 @@ public class temp extends javax.swing.JFrame {
quantLabelchange = new javax.swing.JLabel();
quantBox = new javax.swing.JComboBox<>();
Purchase = new javax.swing.JButton();
Total = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
moneyInput = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
error = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
Receipt = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
......@@ -84,7 +92,7 @@ public class temp extends javax.swing.JFrame {
quantLabelchange.setText("Selected quantity:");
quantBox.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "0", "1", "2", "3", "4", "5" }));
quantBox.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "1", "2", "3", "4", "5" }));
quantBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
quantBoxActionPerformed(evt);
......@@ -98,6 +106,8 @@ public class temp extends javax.swing.JFrame {
}
});
Total.setText("Total:");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
......@@ -113,10 +123,12 @@ public class temp extends javax.swing.JFrame {
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(20, 20, 20)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(quantLabelchange, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(Purchase)
.addGap(0, 6, Short.MAX_VALUE))
.addComponent(quantLabelchange, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Total)
.addComponent(Purchase))
.addGap(0, 6, Short.MAX_VALUE)))))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
......@@ -128,9 +140,10 @@ public class temp extends javax.swing.JFrame {
.addComponent(quantBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(11, 11, 11)
.addComponent(quantLabelchange)
.addGap(18, 18, 18)
.addComponent(Purchase)
.addContainerGap())
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Total)
.addGap(9, 9, 9)
.addComponent(Purchase))
);
javax.swing.GroupLayout ItemSelectLayout = new javax.swing.GroupLayout(ItemSelect);
......@@ -207,46 +220,49 @@ public class temp extends javax.swing.JFrame {
jLabel3.setText("Amount Given:");
Receipt.setText("Print receipt");
Receipt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ReceiptActionPerformed(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()
.addComponent(ItemSelect, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(22, 22, 22)
.addComponent(error, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(jLabel3)))
.addContainerGap(92, Short.MAX_VALUE))
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(error, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3)))
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(Receipt, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(ItemSelect, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(ItemSelect, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(31, 31, 31)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(error)
.addGap(25, 25, 25))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(error)))
.addGap(18, 18, 18)
.addComponent(Receipt)
.addContainerGap(56, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
}// </editor-fold>
private void ItemSelectBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ItemSelectBoxActionPerformed
private void ItemSelectBoxActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Chosen = ItemSelectBox.getSelectedItem().toString();
//gets the item selected
......@@ -254,23 +270,23 @@ public class temp extends javax.swing.JFrame {
switch(Chosen){
//give the value of the item back
case "Chocolate":
Price = "£2";
Price = "£2";
ID = "01";
break;
case "Water":
Price = "£0.5";
Price = "£0.5";
ID = "02";
break;
case "Drink":
Price = "£1";
Price = "£1";
ID = "03";
break;
case "Snack":
Price = "£2";
Price = "£2";
ID = "04";
break;
case "Sweet":
Price = "£1";
Price = "£1";
ID = "05";
break;
default :
......@@ -285,37 +301,54 @@ public class temp extends javax.swing.JFrame {
}
//update price label
}//GEN-LAST:event_ItemSelectBoxActionPerformed
}
private void quantBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_quantBoxActionPerformed
private void quantBoxActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
quant = Double.parseDouble(quantBox.getSelectedItem().toString());
//find the slected item and convet it into a double
quantLabelchange.setText("Selected quantity: "+quant);
//update the display to show the quanity of item wanted
}//GEN-LAST:event_quantBoxActionPerformed
private void PurchaseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PurchaseActionPerformed
// TODO add your handling code here:
cost = price[index]*quant;
Total.setText("Total: "+ cost);
}
private void PurchaseActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
amount = Double.parseDouble(moneyInput.getText());
payed = payed + amount;
jLabel3.setText("Amount Given: "+amount);
if(PriceLabel.getText()=="Please selct an Item"){
error.setText("Error: No item selected");
} else {
error.setText("");
}
if (cost > amount){
error.setText("Error: Payment not valid");
error.setText("Error: Payment not valid");
} else if(amount >= cost) {
error.setText("Payment valid");
amount = amount - cost;
change = change + (amount - cost);
moneyInput.setText(String.valueOf(amount - cost));
purchase();
}
}//GEN-LAST:event_PurchaseActionPerformed
}
private void moneyInputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moneyInputActionPerformed
private void moneyInputActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//convert contents to a double;
amount = Double.parseDouble(moneyInput.getText());
//fins the item's index
findItem();
}//GEN-LAST:event_moneyInputActionPerformed
}
private void ReceiptActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
print();
}
/**
* @param args the command line arguments
......@@ -349,6 +382,7 @@ public class temp extends javax.swing.JFrame {
new temp().setVisible(true);
}
});
}
public void findItem(){
......@@ -364,12 +398,65 @@ public class temp extends javax.swing.JFrame {
}
}
}
public void purchase(){
while (true) {
if((quantity[index]-quant)>=0) { //if not add the item to the basket
quantity[index]=(quantity[index]-quant);
//removes the number from the quantity
basket[index]=(basket[index]+quant);
//adds the number from the quantity to ther basket
break;
} else {
//if the menu item has no quantity or less than they want, don't let them buy it.
error.setText("There are not enough items for that.");
break;
}
}
}
public void print(){
File f = new File("receipt.txt");
//creates a file
f.setWritable(rootPaneCheckingEnabled);
//makes sure the file can be written to
f.createNewFile();
//f. ("Code\tItem\t\tPrice\tQuantity \t");
//displays the menu the the user
for(int i=0; i < price.length; i++) {
if(basket[i]!=0) {
f.write(itemCode[i]+"\t");
//displays the item code
if (i==0||i==1||i==3) {
//this adds a second tab for shorter items which would otherwise display incorrectly
writer.write(itemName[i]+"\t\t");
} else {
//if the item name is not short, it only gets one tab
writer.write(itemName[i]+"\t");
}
writer.write(""+price[i]+"\t");
//displays the price
if(basket[i]!=0) {
writer.write(basket[i]+"\t");
} else {
writer.write("Not in Stock");
}
//adds a new line to display the next item.
writer.write("\n");
}
}
}
// Variables declaration - do not modify//GEN-BEGIN:variables
// Variables declaration - do not modify
private javax.swing.JPanel ItemSelect;
private javax.swing.JComboBox<String> ItemSelectBox;
private javax.swing.JLabel PriceLabel;
private javax.swing.JButton Purchase;
private javax.swing.JButton Receipt;
private javax.swing.JLabel Total;
private javax.swing.JLabel error;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
......@@ -381,6 +468,6 @@ public class temp extends javax.swing.JFrame {
private javax.swing.JComboBox<String> quantBox;
private javax.swing.JLabel quantLabel;
private javax.swing.JLabel quantLabelchange;
// End of variables declaration//GEN-END:variables
// End of variables declaration
}
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