Commit 40509fa7 authored by robert.sharp's avatar robert.sharp

got file working correctly, now i need to correct the item prices.

parent 9f33a549
......@@ -26,7 +26,9 @@ public class temp extends javax.swing.JFrame {
static String itemName[]={"Chocolate","Water","Drink", "Snack", "Sweet"};
//the names of the items
static double basket[]={0, 0, 0, 0, 0};
static boolean valid = false;
//setting the default values
static String ID = "";
static String Chosen = "";
......@@ -270,34 +272,32 @@ public class temp extends javax.swing.JFrame {
switch(Chosen){
//give the value of the item back
case "Chocolate":
Price = "£2";
ID = "01";
break;
case "Water":
Price = "£0.5";
ID = "02";
break;
case "Drink":
Price = "£2";
ID = "03";
break;
case "Snack":
Price = "£2";
ID = "04";
break;
case "Sweet":
Price = "£1";
ID = "05";
break;
default :
Price = "";
ID = "";
}
if (ID == ""){
PriceLabel.setText("Please selct an Item");
} else {
PriceLabel.setText("Price: " + Price);
findItem();
//find the items index
PriceLabel.setText("Price: " + price[index]);
//returns it's price.
cost = price[index]*quant;
Total.setText("Total: "+ cost);
}
//update price label
......@@ -305,6 +305,7 @@ public class temp extends javax.swing.JFrame {
private void quantBoxActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
findItem();
quant = Double.parseDouble(quantBox.getSelectedItem().toString());
//find the slected item and convet it into a double
quantLabelchange.setText("Selected quantity: "+quant);
......@@ -316,22 +317,28 @@ public class temp extends javax.swing.JFrame {
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);
findItem();
if(PriceLabel.getText()=="Please selct an Item"){
error.setText("Error: No item selected");
//makes sure that the usr has an item selected before a purchase
} else {
error.setText("");
}
if (cost > amount){
error.setText("Error: Payment not valid");
error.setText("Error: Payment not valid");
valid = false;
} else if(amount >= cost) {
error.setText("Payment valid");
change += amount - cost;
moneyInput.setText(String.valueOf(amount - cost));
purchase();
if(purchase()==true){
//checks to see if there are enough still items left
//if yes, it charges the account
//calculates the user's change
moneyInput.setText(String.valueOf(amount - cost));
purchase();
valid = true;
}
}
}
......@@ -339,15 +346,27 @@ public class temp extends javax.swing.JFrame {
private void moneyInputActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//convert contents to a double;
payed = Double.parseDouble(moneyInput.getText());
amount = Double.parseDouble(moneyInput.getText());
//fins the item's index
findItem();
jLabel3.setText("Amount Given: "+amount);
}
private void ReceiptActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
print();
if (valid = true){
//if the payemnt is valid
//update the change amount
print();
//print the receipt
} else {
error.setText("Error: Payments not valid");
//send an error message
}
}
/**
......@@ -399,66 +418,62 @@ public class temp extends javax.swing.JFrame {
}
}
public void purchase(){
public boolean purchase(){
while (true) {
findItem();
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;
return true;
} 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;
amount = amount + cost;
jLabel3.setText("Amount Given: "+amount);
return false;
}
}
}
public void print(){
try {
FileWriter f = new FileWriter("receipt.txt");
f.write("Items purchased: \n");
f.write("Code\tItem\t\tPrice\tQuantity \n");
String line = "";
f.write("Code\tItem\t\tPrice\tQuantity \t" + "\n");
//displays the menu the the user
for(int i=0; i < price.length; i++) {
//writes the data to a file.
for(int i=0; i < basket.length; i++) {
if(basket[i]!=0) {
line = (itemCode[i]+"\t");
f.write(line);
//displays the item code
if (i==0||i==1||i==3) {
//this adds a second tab for shorter items which would otherwise display incorrectly
line = (itemName[i]+"\t");
f.write(line);
} else {
//if the item name is not short, it only gets one tab
line = (itemName[i]+"\t");
f.write(line);
}
line = (""+price[i]+"\t");
f.write(line);
//displays the price
if(basket[i]!=0) {
line = (basket[i]+"\t");
f.write(line);
} else {
f.write("Not in Stock");
}
f.write(itemCode[i]+"\t");
if(i==1||i==2||i==3||i==4){
f.write(itemName[i]+"\t\t");
//for dealing with shorter names
} else {
f.write(itemName[i]+"\t");
//fopr longer names
}
f.write(""+price[i]+"\t");
//displays the price
f.write(basket[i]+"\t");
//adds a new line to display the next item.
line = ("\n");
f.write(line);
}
f.write("Total cost: " + cost + "\n");
f.write("Total payed: " + amount + "\n");
f.write("Change Given: " + change + "\n");
f.close();
f.write("\n");
}
}
f.write("\n" + "Total cost: " + cost + "\n");
f.write("\n" + "Total payed: " + payed + "\n");
f.write("\n" + "Change Given: " + Double.parseDouble(moneyInput.getText()) + "\n");
if (change >= 0){
f.write("\n" + "valid payment: " + true);
} else {
f.write("\n" + "valid payment: " + false);
}
f.close();
} catch(Exception e) {
e.printStackTrace();
//catches execptions, the program wouldn't let me run it until i wrote this.
......
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