Commit 68356156 authored by harry's avatar harry

First and main submission of assessment program with GUI.

parent 226b6014
// This a program with a GUI which aims to simulte using a self checkout system, commonly found in supermarkets and other similar retail environments
//The program uses a relatively simple looking GUI which is clear and user friendly.
//Below are the different java utilities I made use of throughout this program.
import static java.io.File.separator;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.JOptionPane;
import java.util.Random;
import java.io.FileWriter;
......@@ -21,7 +21,7 @@ import java.io.FileWriter;
// this is the class in which my program is built
public class NewJFrame extends javax.swing.JFrame {
/**
......@@ -38,15 +38,16 @@ public class NewJFrame extends javax.swing.JFrame {
// this is the function where key components for the program to use are created.
public NewJFrame() {
initComponents();
}
//here are my two array that I have used effectively to assist with recording the products and prices selected by the user in my program.
ArrayList<Integer> pricelist = new ArrayList<Integer>();
ArrayList<String> shoplist = new ArrayList<String>();
//below I have initialised the available stock of 20 for each menu item. This gets updated upon a successful transaction within the system.
int penstock = 20;
int bookstock = 20;
int waterstock = 20;
......@@ -57,11 +58,13 @@ public class NewJFrame extends javax.swing.JFrame {
int biscstock = 20;
int cakestock = 20;
//below are 3 messages used at the end of the program which I have stored here for both ease and in case they need to be used multiple times in different areas.
String message1 = "Thankyou. Press below to print your reciept. Please take your items.";
String error1 = "You have not provided enough for today's transaction. It will be cancelled";
String message2 = "Thankyou. Press below to print your reciept, your change will be processed now. Please take your items.";
String default1 = "Welcome to SCS Shopping.";
//here are a few variables used in different methods that I have places here for ease when building the payment section.
String strfinalcost;
String strpaygiven;
int finalcost;
......@@ -419,11 +422,11 @@ public class NewJFrame extends javax.swing.JFrame {
private void baskActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_baskActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_baskActionPerformed
// this is the method area for actions performed by pressing the add to basket button.
private void add2baskActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_add2baskActionPerformed
// TODO add your handling code here:
//Here i have defined a handful of variables that I have made use of within this section of the program
String item;
String Qty;
String Cost;
......@@ -435,56 +438,58 @@ public class NewJFrame extends javax.swing.JFrame {
String tempitem;
//in the 2 lines below I am setting the text for the text areas that show what items have been selected.
bask.setText((String)ComboItems.getSelectedItem());
Itemqty.setText((String)pickqty.getSelectedItem());
// here I am storing the quantity selected in the dropdown box to a variable which can be used in different ways.
Qty = Itemqty.getText();
// converting the qty variable to an intege to assist with calculations.
int quantity = Integer.parseInt(Qty);
// a couple more definitions of variables for filling out text areas
item = bask.getText();
Cost = price.getText();
// conerting the cost variable for calculations
int cost = Integer.parseInt(Cost);
itemtotal = cost * quantity;
itemtotal = cost * quantity;// calculating the total for the item selected
tempitem = Qty + "x " + item;
tempitem = Qty + "x " + item;//setting variable for text to be added to the shopping list array
shoplist.add(tempitem);
shoplist.add(tempitem);//adding the item selected to the shopping list array
basketview.append(tempitem + "\n");
basketview.append(tempitem + "\n");//adding the selected item to the text area for diplaying "scanned" items
String ItemTotal = String.valueOf(itemtotal);
String ItemTotal = String.valueOf(itemtotal);// converting the total value of an item to a string to be shown in a pice text area
prodprice.setText(ItemTotal);
prodprice.setText(ItemTotal);//setting the text area to an item's value
pickcost = prodprice.getText();
pickcost = prodprice.getText();//taking that price and adding to a new variable
int pickedcost = Integer.parseInt(pickcost) ;
int pickedcost = Integer.parseInt(pickcost) ;//converting that new variable to integer
pricelist.add(pickedcost);
pricelist.add(pickedcost);// adding this integer to the pricelist array for use later when total is needed
int sum = 0;
for(int i = 0; i < pricelist.size(); i++){
sum = sum + pricelist.get(i);
}
}// calculating an overall total for the basket by getting the sum of prices in the array
priceview.append(pickedcost + "\n");
priceview.append(pickedcost + "\n");//adding the cost of the selected item to the prices text area, corresponds to scanned items text area
total = String.valueOf(sum);
totalscreen.setText(total);
System.out.println("Total: " + total);
System.out.println("Total: " + total);//converting basket sum to string to show in total text area
System.out.println("Selected items array: " + pricelist);
System.out.println("Selected items array: " + pricelist);// backup line shown in standard output to show up to date cost, assisted when building the program
String startprice = totalscreen.getText();
String startprice = totalscreen.getText();// fetching total value to be added to discount price text area until updated manually
totalscreen2.setText(startprice);
totalscreen2.setText(startprice);//adding total to 2nd text area
switch (item){
......@@ -526,9 +531,9 @@ public class NewJFrame extends javax.swing.JFrame {
break;
}
}//switch case used to recognise item selected and update quantities
//tools used to assist development and check to see stock changes
System.out.println("Pen stock: " + penstock);
System.out.println("Book stock: " + bookstock);
System.out.println("Water stock: " + waterstock);
......@@ -564,12 +569,12 @@ public class NewJFrame extends javax.swing.JFrame {
private void ComboItemsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ComboItemsActionPerformed
//definition of variables for this method
String item;
check.setText((String)ComboItems.getSelectedItem());
check.setText((String)ComboItems.getSelectedItem());//filling a text area to show recognition of item selected in dropdown box
item = check.getText();
item = check.getText();// getting a string variable from box filled above for use in switch case
switch (item){
......@@ -611,7 +616,7 @@ public class NewJFrame extends javax.swing.JFrame {
}
//switch case used to update the price listing for an item once it has been selected in the dropdown box, shows corresponding price for item curently selected
......@@ -634,7 +639,7 @@ public class NewJFrame extends javax.swing.JFrame {
private void pickqtyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pickqtyActionPerformed
// TODO add your handling code here:
check2.setText((String)pickqty.getSelectedItem());
check2.setText((String)pickqty.getSelectedItem());//text area filled with selected quantity, changes when different quantities are selected.
}//GEN-LAST:event_pickqtyActionPerformed
private void ItemqtyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ItemqtyActionPerformed
......@@ -651,9 +656,10 @@ public class NewJFrame extends javax.swing.JFrame {
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
//Discount button method
//random number generator between 1 and 10 for use with discount value
Random random = new Random();
int rand = 0;
while(true){
......@@ -661,8 +667,8 @@ while(true){
if(rand !=0) break;
}
System.out.println(rand);
System.out.println(rand);//assistng development by showing the raw value of the generator before switch case is applied
//definition of more variables for switch case discount calculations
double discount;
int newprice;
String total = totalscreen.getText();
......@@ -672,7 +678,10 @@ int calc1;
String dcnum;
switch (rand){
switch (rand){//case 1 - 5, calculations to apply discounts of 10%-50%
// case 6-10, same calculation used to take £5 off basket total
//price calculated based on discount value and applied to discount toal, resulting in a new final total
case 1 :
discount = 0.1;
......@@ -840,28 +849,30 @@ String dcnum;
//reassigning some variables defined at the start of the program for use as follows
strfinalcost = totalscreen2.getText();
strpaygiven = payentry.getText();
finalcost = Integer.parseInt(strfinalcost);
paygiven = Integer.parseInt(strpaygiven);
//setting conditions for different payment outcomes
//shows message based on payment being equal to total
if (finalcost == paygiven){
payresult.setText(message1);
payresult2.setText(default1);
}
//shows message based on payment being less tha total required
else if(finalcost > paygiven){
payresult.setText(error1);
payresult2.setText(default1);
}
//shows message based on payment being more than required total
else if(finalcost < paygiven){
payresult.setText(message2);
//defining and calculating change total and storing in variable
int change = paygiven - finalcost;
Integer.toString(change);
......@@ -877,7 +888,7 @@ String dcnum;
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
// TODO add your handling code here:
//defining variables first to simplify writing to a text file
String Items = basketview.getText();
String Cost = totalscreen.getText();
String discount = discountvalue.getText();
......@@ -885,7 +896,7 @@ String dcnum;
String Payment = payentry.getText();
String Change = Integer.toString(paygiven - finalcost);
try{
try{//micro function to write the variables defined above into a text file named reciept.txt
FileWriter receipt = new FileWriter("receipt.txt");
receipt.write(Items);
receipt.write("\n" + "Cost before discount: " + Cost);
......@@ -900,7 +911,7 @@ String dcnum;
}//GEN-LAST:event_jButton4ActionPerformed
// automated definitions here created when I have added GUI elements
/**
* @param args the command line arguments
*/
......
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