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

Update temp.java

parent 3f48aeac
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
* To change this template file, choose Tools | Templates * To change this template file, choose Tools | Templates
* and open the template in the editor. * and open the template in the editor.
*/ */
import java.io.*;
/** /**
* *
...@@ -13,6 +14,7 @@ public class temp extends javax.swing.JFrame { ...@@ -13,6 +14,7 @@ public class temp extends javax.swing.JFrame {
* Creates new form temp * Creates new form temp
*/ */
static double price[]={2, 0.5, 1, 2, 1}; static double price[]={2, 0.5, 1, 2, 1};
//the prices //the prices
static double quantity[]={20, 20, 20, 20, 20}; static double quantity[]={20, 20, 20, 20, 20};
...@@ -36,6 +38,10 @@ public class temp extends javax.swing.JFrame { ...@@ -36,6 +38,10 @@ public class temp extends javax.swing.JFrame {
static double amount = 0; static double amount = 0;
//expected cost //expected cost
static double cost = 0; static double cost = 0;
//returned change
static double change = 0;
//total payed
static double payed = 0;
public temp() { public temp() {
initComponents(); initComponents();
...@@ -47,7 +53,7 @@ public class temp extends javax.swing.JFrame { ...@@ -47,7 +53,7 @@ public class temp extends javax.swing.JFrame {
* regenerated by the Form Editor. * regenerated by the Form Editor.
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents // <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() { private void initComponents() {
ItemSelect = new javax.swing.JPanel(); ItemSelect = new javax.swing.JPanel();
...@@ -59,12 +65,14 @@ public class temp extends javax.swing.JFrame { ...@@ -59,12 +65,14 @@ public class temp extends javax.swing.JFrame {
quantLabelchange = new javax.swing.JLabel(); quantLabelchange = new javax.swing.JLabel();
quantBox = new javax.swing.JComboBox<>(); quantBox = new javax.swing.JComboBox<>();
Purchase = new javax.swing.JButton(); Purchase = new javax.swing.JButton();
Total = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel(); jPanel2 = new javax.swing.JPanel();
moneyInput = new javax.swing.JTextField(); moneyInput = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel();
error = new javax.swing.JLabel(); error = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel();
Receipt = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
...@@ -84,7 +92,7 @@ public class temp extends javax.swing.JFrame { ...@@ -84,7 +92,7 @@ public class temp extends javax.swing.JFrame {
quantLabelchange.setText("Selected quantity:"); 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() { quantBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) { public void actionPerformed(java.awt.event.ActionEvent evt) {
quantBoxActionPerformed(evt); quantBoxActionPerformed(evt);
...@@ -98,6 +106,8 @@ public class temp extends javax.swing.JFrame { ...@@ -98,6 +106,8 @@ public class temp extends javax.swing.JFrame {
} }
}); });
Total.setText("Total:");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout); jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup( jPanel1Layout.setHorizontalGroup(
...@@ -113,10 +123,12 @@ public class temp extends javax.swing.JFrame { ...@@ -113,10 +123,12 @@ public class temp extends javax.swing.JFrame {
.addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createSequentialGroup()
.addGap(20, 20, 20) .addGap(20, 20, 20)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .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() .addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(Purchase) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 6, Short.MAX_VALUE)) .addComponent(Total)
.addComponent(quantLabelchange, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))) .addComponent(Purchase))
.addGap(0, 6, Short.MAX_VALUE)))))
.addContainerGap()) .addContainerGap())
); );
jPanel1Layout.setVerticalGroup( jPanel1Layout.setVerticalGroup(
...@@ -128,9 +140,10 @@ public class temp extends javax.swing.JFrame { ...@@ -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) .addComponent(quantBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(11, 11, 11) .addGap(11, 11, 11)
.addComponent(quantLabelchange) .addComponent(quantLabelchange)
.addGap(18, 18, 18) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Purchase) .addComponent(Total)
.addContainerGap()) .addGap(9, 9, 9)
.addComponent(Purchase))
); );
javax.swing.GroupLayout ItemSelectLayout = new javax.swing.GroupLayout(ItemSelect); javax.swing.GroupLayout ItemSelectLayout = new javax.swing.GroupLayout(ItemSelect);
...@@ -207,46 +220,49 @@ public class temp extends javax.swing.JFrame { ...@@ -207,46 +220,49 @@ public class temp extends javax.swing.JFrame {
jLabel3.setText("Amount Given:"); 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()); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout); getContentPane().setLayout(layout);
layout.setHorizontalGroup( layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup() .addGroup(layout.createSequentialGroup()
.addComponent(ItemSelect, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .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.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup() .addComponent(error, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel3)))
.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() .addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18) .addGap(18, 18, 18)
.addComponent(jLabel3))) .addComponent(Receipt, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(92, Short.MAX_VALUE))
); );
layout.setVerticalGroup( layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup() .addGroup(layout.createSequentialGroup()
.addContainerGap() .addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .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) .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))
.addGroup(layout.createSequentialGroup() .addGroup(layout.createSequentialGroup()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(31, 31, 31) .addGap(31, 31, 31)
.addComponent(jLabel3) .addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(error) .addComponent(error)))
.addGap(25, 25, 25)))) .addGap(18, 18, 18)
.addComponent(Receipt)
.addContainerGap(56, Short.MAX_VALUE))
); );
pack(); 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: // TODO add your handling code here:
Chosen = ItemSelectBox.getSelectedItem().toString(); Chosen = ItemSelectBox.getSelectedItem().toString();
//gets the item selected //gets the item selected
...@@ -254,23 +270,23 @@ public class temp extends javax.swing.JFrame { ...@@ -254,23 +270,23 @@ public class temp extends javax.swing.JFrame {
switch(Chosen){ switch(Chosen){
//give the value of the item back //give the value of the item back
case "Chocolate": case "Chocolate":
Price = "£2"; Price = "£2";
ID = "01"; ID = "01";
break; break;
case "Water": case "Water":
Price = "£0.5"; Price = "£0.5";
ID = "02"; ID = "02";
break; break;
case "Drink": case "Drink":
Price = "£1"; Price = "£1";
ID = "03"; ID = "03";
break; break;
case "Snack": case "Snack":
Price = "£2"; Price = "£2";
ID = "04"; ID = "04";
break; break;
case "Sweet": case "Sweet":
Price = "£1"; Price = "£1";
ID = "05"; ID = "05";
break; break;
default : default :
...@@ -285,37 +301,54 @@ public class temp extends javax.swing.JFrame { ...@@ -285,37 +301,54 @@ public class temp extends javax.swing.JFrame {
} }
//update price label //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: // TODO add your handling code here:
quant = Double.parseDouble(quantBox.getSelectedItem().toString()); quant = Double.parseDouble(quantBox.getSelectedItem().toString());
//find the slected item and convet it into a double //find the slected item and convet it into a double
quantLabelchange.setText("Selected quantity: "+quant); quantLabelchange.setText("Selected quantity: "+quant);
//update the display to show the quanity of item wanted //update the display to show the quanity of item wanted
}//GEN-LAST:event_quantBoxActionPerformed cost = price[index]*quant;
Total.setText("Total: "+ cost);
}
private void PurchaseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PurchaseActionPerformed private void PurchaseActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here: // TODO add your handling code here:
cost = price[index]*quant; amount = Double.parseDouble(moneyInput.getText());
payed = payed + amount;
jLabel3.setText("Amount Given: "+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){ if (cost > amount){
error.setText("Error: Payment not valid"); error.setText("Error: Payment not valid");
} else if(amount >= cost) { } else if(amount >= cost) {
error.setText("Payment valid"); 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: // TODO add your handling code here:
//convert contents to a double; //convert contents to a double;
amount = Double.parseDouble(moneyInput.getText()); amount = Double.parseDouble(moneyInput.getText());
//fins the item's index //fins the item's index
findItem(); 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 * @param args the command line arguments
...@@ -349,6 +382,7 @@ public class temp extends javax.swing.JFrame { ...@@ -349,6 +382,7 @@ public class temp extends javax.swing.JFrame {
new temp().setVisible(true); new temp().setVisible(true);
} }
}); });
} }
public void findItem(){ public void findItem(){
...@@ -365,11 +399,64 @@ public class temp extends javax.swing.JFrame { ...@@ -365,11 +399,64 @@ public class temp extends javax.swing.JFrame {
} }
} }
// Variables declaration - do not modify//GEN-BEGIN:variables 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
private javax.swing.JPanel ItemSelect; private javax.swing.JPanel ItemSelect;
private javax.swing.JComboBox<String> ItemSelectBox; private javax.swing.JComboBox<String> ItemSelectBox;
private javax.swing.JLabel PriceLabel; private javax.swing.JLabel PriceLabel;
private javax.swing.JButton Purchase; private javax.swing.JButton Purchase;
private javax.swing.JButton Receipt;
private javax.swing.JLabel Total;
private javax.swing.JLabel error; private javax.swing.JLabel error;
private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel2;
...@@ -381,6 +468,6 @@ public class temp extends javax.swing.JFrame { ...@@ -381,6 +468,6 @@ public class temp extends javax.swing.JFrame {
private javax.swing.JComboBox<String> quantBox; private javax.swing.JComboBox<String> quantBox;
private javax.swing.JLabel quantLabel; private javax.swing.JLabel quantLabel;
private javax.swing.JLabel quantLabelchange; 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