Commit 6f763fc2 authored by addya's avatar addya

Test cases written. Finished product ?? (Will double check)

parent fa5c4206
......@@ -5,6 +5,26 @@
<artifactId>SelfCheckout</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.6.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>15</maven.compiler.source>
......
......@@ -39,12 +39,18 @@
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="jPanel1" alignment="0" pref="615" max="32767" attributes="0"/>
<Group type="102" alignment="0" attributes="0">
<Component id="jPanel1" min="-2" pref="569" max="-2" attributes="0"/>
<EmptySpace min="0" pref="0" max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="jPanel1" min="-2" pref="378" max="-2" attributes="0"/>
<Group type="102" alignment="0" attributes="0">
<Component id="jPanel1" min="-2" pref="381" max="-2" attributes="0"/>
<EmptySpace min="0" pref="19" max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
</Layout>
......@@ -65,6 +71,27 @@
<Group type="102" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="lblBasket" min="-2" pref="127" max="-2" attributes="0"/>
<Component id="lblTotal" alignment="0" min="-2" pref="127" max="-2" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="lblDiscount" min="-2" pref="127" max="-2" attributes="0"/>
<Component id="lbl£" alignment="1" min="-2" pref="18" max="-2" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<Component id="txfPayment" min="-2" pref="134" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="btnPay" min="-2" pref="66" max="-2" attributes="0"/>
</Group>
<Component id="lblMessage" min="-2" pref="238" max="-2" attributes="0"/>
<Component id="lblMessage2" min="-2" pref="238" max="-2" attributes="0"/>
</Group>
</Group>
<Component id="jLabel2" alignment="0" min="-2" max="-2" attributes="0"/>
<Group type="102" alignment="0" attributes="0">
<Component id="jScrollPane1" min="-2" pref="156" max="-2" attributes="0"/>
......@@ -92,28 +119,8 @@
</Group>
</Group>
</Group>
<Group type="102" alignment="0" attributes="0">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="lblBasket" min="-2" pref="127" max="-2" attributes="0"/>
<Component id="lblTotal" alignment="0" min="-2" pref="127" max="-2" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="lblDiscount" min="-2" pref="127" max="-2" attributes="0"/>
<Component id="lbl£" alignment="1" min="-2" pref="18" max="-2" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<Component id="txfPayment" min="-2" pref="134" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="btnPay" min="-2" pref="66" max="-2" attributes="0"/>
</Group>
<Component id="lblErrorMessage" min="-2" pref="150" max="-2" attributes="0"/>
</Group>
</Group>
</Group>
<EmptySpace min="194" pref="133" max="32767" attributes="0"/>
<EmptySpace pref="55" max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
......@@ -123,8 +130,10 @@
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="1" attributes="0">
<Group type="102" attributes="0">
<Component id="lblErrorMessage" min="-2" max="-2" attributes="0"/>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Component id="lblMessage" min="-2" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="10" max="-2" attributes="0"/>
<Component id="lblMessage2" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="txfPayment" alignment="3" min="-2" pref="30" max="-2" attributes="0"/>
<Component id="btnPay" alignment="3" min="-2" max="-2" attributes="0"/>
......@@ -289,6 +298,7 @@
<SubComponents>
<Component class="javax.swing.JTextArea" name="txaBasket">
<Properties>
<Property name="editable" type="boolean" value="false"/>
<Property name="columns" type="int" value="20"/>
<Property name="rows" type="int" value="5"/>
</Properties>
......@@ -334,6 +344,7 @@
<Color blue="0" green="ff" red="ff" type="rgb"/>
</Property>
<Property name="text" type="java.lang.String" value="Pay"/>
<Property name="enabled" type="boolean" value="false"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="btnPayActionPerformed"/>
......@@ -358,7 +369,7 @@
<Property name="text" type="java.lang.String" value="6-10&gt;&gt;&gt;&#xa3;5 off"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="lblErrorMessage">
<Component class="javax.swing.JLabel" name="lblMessage">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Tahoma" size="14" style="2"/>
......@@ -366,7 +377,7 @@
<Property name="foreground" type="java.awt.Color" editor="org.netbeans.beaninfo.editors.ColorEditor">
<Color blue="0" green="ff" red="ff" type="rgb"/>
</Property>
<Property name="text" type="java.lang.String" value=".........."/>
<Property name="verticalAlignment" type="int" value="1"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="lbl&#xa3;">
......@@ -380,6 +391,17 @@
<Property name="text" type="java.lang.String" value="&#xa3;"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="lblMessage2">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Tahoma" size="14" style="2"/>
</Property>
<Property name="foreground" type="java.awt.Color" editor="org.netbeans.beaninfo.editors.ColorEditor">
<Color blue="0" green="ff" red="ff" type="rgb"/>
</Property>
<Property name="verticalAlignment" type="int" value="1"/>
</Properties>
</Component>
</SubComponents>
</Container>
</SubComponents>
......
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import java.text.DecimalFormat;
import java.math.RoundingMode;
......@@ -16,11 +19,38 @@ import java.math.RoundingMode;
*/
public class CheckOut extends javax.swing.JFrame {
public double totalCost;
public String discountStr="";
/**
* Creates new form CheckOut
*/
public CheckOut() {
//variable and array defined publicly
double basketTotal;
String[] basket;
int[] stock;
int[] tempStock;
String[] products;
public CheckOut(double ttl, String[] bskt, String[] prdt, int[] stck, int[] tmpStck) {
//value for price before discount
basketTotal=ttl;
//array of items in basket
basket=bskt;
//array of products
products=prdt;
//number of items in stock
stock=stck;
//temporary stock
tempStock=tmpStck;
initComponents();
//on opening the form the basket label shows the price before discount is applied
lblBasket.setText("Basket: £"+basketTotal);
for (int i=0; i<basket.length; i++){
//for each item in the basket
//we display the current item on a new line
txaBasket.append(basket[i]+"\n");
}
}
/**
......@@ -51,8 +81,9 @@ public class CheckOut extends javax.swing.JFrame {
btnPay = new javax.swing.JButton();
txfPayment = new javax.swing.JTextField();
lbl7 = new javax.swing.JLabel();
lblErrorMessage = new javax.swing.JLabel();
lblMessage = new javax.swing.JLabel();
lbl£ = new javax.swing.JLabel();
lblMessage2 = new javax.swing.JLabel();
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
jLabel1.setForeground(new java.awt.Color(255, 255, 0));
......@@ -104,6 +135,7 @@ public class CheckOut extends javax.swing.JFrame {
}
});
txaBasket.setEditable(false);
txaBasket.setColumns(20);
txaBasket.setRows(5);
jScrollPane1.setViewportView(txaBasket);
......@@ -122,6 +154,7 @@ public class CheckOut extends javax.swing.JFrame {
btnPay.setBackground(new java.awt.Color(255, 255, 0));
btnPay.setText("Pay");
btnPay.setEnabled(false);
btnPay.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnPayActionPerformed(evt);
......@@ -139,14 +172,18 @@ public class CheckOut extends javax.swing.JFrame {
lbl7.setForeground(new java.awt.Color(255, 255, 0));
lbl7.setText("6-10>>>£5 off");
lblErrorMessage.setFont(new java.awt.Font("Tahoma", 2, 14)); // NOI18N
lblErrorMessage.setForeground(new java.awt.Color(255, 255, 0));
lblErrorMessage.setText("..........");
lblMessage.setFont(new java.awt.Font("Tahoma", 2, 14)); // NOI18N
lblMessage.setForeground(new java.awt.Color(255, 255, 0));
lblMessage.setVerticalAlignment(javax.swing.SwingConstants.TOP);
lbl£.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
lbl£.setForeground(new java.awt.Color(255, 255, 0));
lbl£.setText("£");
lblMessage2.setFont(new java.awt.Font("Tahoma", 2, 14)); // NOI18N
lblMessage2.setForeground(new java.awt.Color(255, 255, 0));
lblMessage2.setVerticalAlignment(javax.swing.SwingConstants.TOP);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
......@@ -154,6 +191,22 @@ public class CheckOut extends javax.swing.JFrame {
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblBasket, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lblTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblDiscount, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lbl£, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(txfPayment, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnPay, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(lblMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lblMessage2, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(jLabel2)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
......@@ -174,23 +227,8 @@ public class CheckOut extends javax.swing.JFrame {
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addComponent(lbl1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(lblYouGot, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblBasket, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lblTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblDiscount, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lbl£, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(txfPayment, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnPay, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(lblErrorMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGap(133, 133, Short.MAX_VALUE))
.addComponent(lblYouGot, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))))))
.addContainerGap(55, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
......@@ -198,8 +236,10 @@ public class CheckOut extends javax.swing.JFrame {
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(lblErrorMessage)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(lblMessage)
.addGap(10, 10, 10)
.addComponent(lblMessage2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txfPayment, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnPay)))
......@@ -245,11 +285,15 @@ public class CheckOut extends javax.swing.JFrame {
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 615, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 569, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 378, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 381, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 19, Short.MAX_VALUE))
);
pack();
......@@ -276,22 +320,130 @@ public class CheckOut extends javax.swing.JFrame {
return shortened;
}
public String payment(double cost, double payment){
payment=shortenValue(payment);
//we will return the message string to be displayed to the user
String message="";
//if the user gave more money than the total price..
if (payment>=cost){
//the system checks if the price is equal..
if (payment==cost){
//if it is they aren't given any change.
message="Thank you.";
}
else{
//else the users change is worked out and they are given change
double change=payment-cost;
message="Thank you. Your change is £"+change+".";
}
lblMessage2.setText("Have a nice day :)");
//the pay button is disabled once the user has payed
btnPay.setEnabled(false);
//prints receipt to file
printReceipt(basket, totalCost, basketTotal, discountStr, payment);
//the stock change is finalised
stock=tempStock;
}
else {
//if they haven't given enough money they get this message
message="Insufficient payment >.<";
lblMessage2.setText("Please offer more money.");
}
//the message is returned to be displayed
return message;
}
public void printReceipt(String[] basket, double totalCost, double costBefore, String discount, double enteredAmount){
//create new file called receipt.txt
//createNewFile("recipt.txt");
File file = new File("receipt.txt");
try {
FileWriter fr = new FileWriter(file, true);
//write to file '----------Items bought----------'
fr.write("----------Items bought----------");
//for each item bought, add the next item to the file
for (int i=0; i<basket.length; i++){
fr.write("\n"+basket[i]);
} //this string is what we want to append to the .txt receipt file
fr.write("\n--------------------------------");
String add;
add="\nCost before discount: £"+costBefore;
fr.write(add);
add="\nCost after discount: £"+totalCost;
fr.write(add);
add="\nDiscount: "+discount;
fr.write(add);
add="\nMoney given: £"+enteredAmount;
fr.write(add);
//the change is worked out
double change=enteredAmount-totalCost;
add="\nChange: £"+change;
fr.write(add);
fr.close();
}
catch (IOException ex){
System.out.println("Error");
}
}
public void removeStock(String[] basket){
//for each item in user's basket
for (int i=0; i<basket.length; i++){
//index is what we're trying to find
int index=0;
int j=0;
boolean loop=true;
while (loop==true){
//we iterate through the products array to find the product
//that matches the one in the basket
if (basket[i]==products[j]){
index=j;
//when we find the right index, the loop condition is set to false
loop=false;
}
else{
//if they dont match we keep iterating through the products array
j++;
}
}
//this line gets the number of items in stock for the current item
int thisStock=stock[index];
//we take away 1 from the stock and replace the one in the list
thisStock=thisStock-1;
stock[index]=thisStock;
}
}
private void btnPayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPayActionPerformed
// TODO add your handling code here:
double moneyGiven=Double.parseDouble(txfPayment.getText());
boolean loop=true;
//while (loop==true){
String message=payment(totalCost, moneyGiven);
lblMessage.setText(message);
//}
}//GEN-LAST:event_btnPayActionPerformed
private void btnDrawActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDrawActionPerformed
// TODO add your handling code here:
//the user should only be able to pay after they have drawn
//so the pay button is initially disabled until they draw
btnPay.setEnabled(true);
Random rand = new Random();
//a random integer from 1-10 is used to pick the random discount
//rand.nextInt(9) would pick a number from 0-9, so we need to add 1
//this is because the random draw is from 1-10
int r=rand.nextInt(9)+1;
String discountStr="";
double discountPer=1;
lblYouGot.setText("You got "+r+"!");
//we disable use of the button so the user can't draw more than once
//btnDraw.setEnabled(false);
btnDraw.setEnabled(false);
//the random number from 1-10 decides how much discount is received
switch (r){
//if the variable r is 1 then a 10% discount is applied
......@@ -328,10 +480,10 @@ public class CheckOut extends javax.swing.JFrame {
discountStr="£5";
}
lblDiscount.setText("Discount: "+discountStr);
double totalCost=calcTotal(11.11, discountPer, r);
totalCost=109.777;
totalCost=calcTotal(basketTotal, discountPer, r);
totalCost=shortenValue(totalCost);
lblTotal.setText("Total: "+totalCost);
//removeStock()
}//GEN-LAST:event_btnDrawActionPerformed
private void txfPaymentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txfPaymentActionPerformed
......@@ -368,7 +520,7 @@ public class CheckOut extends javax.swing.JFrame {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CheckOut().setVisible(true);
//new CheckOut().setVisible(true);
}
});
}
......@@ -389,7 +541,8 @@ public class CheckOut extends javax.swing.JFrame {
private javax.swing.JLabel lblBasket;
private javax.swing.JLabel lblChance;
private javax.swing.JLabel lblDiscount;
private javax.swing.JLabel lblErrorMessage;
private javax.swing.JLabel lblMessage;
private javax.swing.JLabel lblMessage2;
private javax.swing.JLabel lblTotal;
private javax.swing.JLabel lblYouGot;
private javax.swing.JLabel lbl£;
......
......@@ -169,7 +169,7 @@
<Property name="editable" type="boolean" value="false"/>
<Property name="columns" type="int" value="20"/>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Tahoma" size="18" style="2"/>
<Font name="Tahoma" size="14" style="2"/>
</Property>
<Property name="rows" type="int" value="5"/>
</Properties>
......@@ -232,6 +232,9 @@
</Property>
<Property name="text" type="java.lang.String" value="Checkout"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="btnCheckoutActionPerformed"/>
</Events>
</Component>
</SubComponents>
</Container>
......
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.DefaultComboBoxModel;
/*
......@@ -18,22 +19,34 @@ public class SelectProducts extends javax.swing.JFrame {
public String[] products = {"Pen", "Book", "Water", "Drink", "Snack", "Sweet", "Chocolate", "Biscuits", "Cake"};
//array of prices
public double[] prices = {1, 1, 1, 1, 2, 1, 1, 1, 2};
//array of how many of each item is in stock
//arraylist of amount of each item left in stock
//its an arraylist so it can be initialised
public ArrayList<Integer> stockArL = new ArrayList<Integer>();
//array for the quantity of each item left
public int[] stock;
//temporary array for the quantity of each item left
//this will update each time the user adds an item to their basket
//after they checkout it will finalise and
public int[] tempStock;
//index will be the position of the currently selected item in these arrays
public int index;
//the basket array must be an arraylist since we add new things to it dynamically using a button
public ArrayList<String> basketArL= new ArrayList<String>();
/**
* Creates new form SelectProducts
*/
public SelectProducts() {
initComponents();
//------------------Initialising stocks-------------------
//this variable is how many iterations the loop performs
//one iteration is done for eacah product
int amountOfItems=products.length;
int amountOfItems=products.length-1;
for (int i=0;i==9;i++){
for (int i=0;i<=amountOfItems;i++){
String z="0";
//if int i is below 10 then a "0" is added to the front
//e.g. "01, "02", "03"
......@@ -54,11 +67,20 @@ public class SelectProducts extends javax.swing.JFrame {
//so 20 is added to the stock arraylist
stockArL.add(20);
}
Integer[] s = stockArL.toArray(new Integer[9]);
int[] st = Arrays.stream(s).mapToInt(Integer::intValue).toArray();
stock=st;
tempStock=stock;
//-----------------------------------------------
//on the form starting the combobox cmbItems
//is set to display the items in the products array
DefaultComboBoxModel dm=new DefaultComboBoxModel(products);
cmbProducts.setModel(dm);
//the button to add items to basket is enabled at first
//so the user can't add anything without selecting it
btnAddItem.setEnabled(false);
}
/**
......@@ -101,7 +123,7 @@ public class SelectProducts extends javax.swing.JFrame {
txaShowSelected.setEditable(false);
txaShowSelected.setColumns(20);
txaShowSelected.setFont(new java.awt.Font("Tahoma", 2, 18)); // NOI18N
txaShowSelected.setFont(new java.awt.Font("Tahoma", 2, 14)); // NOI18N
txaShowSelected.setRows(5);
jScrollPane1.setViewportView(txaShowSelected);
......@@ -128,6 +150,11 @@ public class SelectProducts extends javax.swing.JFrame {
btnCheckout.setBackground(new java.awt.Color(255, 255, 0));
btnCheckout.setText("Checkout");
btnCheckout.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCheckoutActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
......@@ -207,6 +234,7 @@ public class SelectProducts extends javax.swing.JFrame {
private void cmbProductsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbProductsActionPerformed
// TODO add your handling code here:
btnAddItem.setEnabled(true);
String display;
String find=cmbProducts.getSelectedItem().toString();
String current="";
......@@ -216,47 +244,18 @@ public class SelectProducts extends javax.swing.JFrame {
//index is equal to i
//this is the position of the product we want to find
index=i;
if (tempStock[index]<=0){
display="Item: "+products[i]+"\nPrice: £"+prices[i]+"\nSorry, out of stock.";
btnAddItem.setEnabled(false);
}
else{
display="Item: "+products[i]+"\nPrice: £"+prices[i];
}
txaShowSelected.setText(display);
}
else {
}
}
/*
String display;
int index=4;
display="Code: "+codesArL.get(index)+"\nPrice: " + products[index]
+ "\n£" + prices[index] + "\nIn stock: "+stockArL.get(index);
//method .setText() will set the text area to the display string
txaShowSelected.append(display);
//we want to find the selected item in the products array
String find=cmbProducts.getSelectedItem().toString();
//find the index of the selected item
String current="";
//index wil be the index of the item when we find it
//int index=0;
for (int i=0;i<products.length; i++){
current=products[i];
if (current==find){
index=i;
}
else{
}
}
//String display is what we will show in the text area txaShowSelected
//we take the corresponding code, product, price and quantity from each array
display="Code: "+codesArL.get(index)+"\nPrice: " + products[index]
+ "\n£" + prices[index] + "\nIn stock: "+stockArL.get(index);
//method .setText() will set the text area to the display string
txaShowSelected.append(display);
*/
}//GEN-LAST:event_cmbProductsActionPerformed
//the variable for the total cost must be defined outside of the action event
......@@ -264,22 +263,43 @@ public class SelectProducts extends javax.swing.JFrame {
double total=0;
private void btnAddItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddItemActionPerformed
// TODO add your handling code here:
//when the 'Add Item' button is pressed
//when the 'Add Item' button is pressed, if there is enough stock left
if (tempStock[index]>0){
//take away 1 from the stock of the added item
int ts=tempStock[index];
ts=ts-1;
tempStock[index]=ts;
//the item in the display should go into the basket
txaBasket.append(products[index]+"\t"+prices[index]+"\n");
//we add the item to an arraylist for the basket too
//somehow we need to pass this arraylist to the second JFrameForm
ArrayList<String> BasketArL= new ArrayList<String>();
BasketArL.add(products[index]);
//we add the item to the arraylist for the basket too
basketArL.add(products[index]);
//we will add the price of the added item to the total cost
total= total + prices[index];
//then display it with the total label
lblTotalCost.setText("Total: £"+total);
}
else {
String display="Item: "+products[index]+"\nPrice: £"+prices[index]+"\nSorry, out of stock.";
txaShowSelected.setText(display);
btnAddItem.setEnabled(false);
}
}//GEN-LAST:event_btnAddItemActionPerformed
private void btnCheckoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCheckoutActionPerformed
// TODO add your handling code here:
//when they're ready to check out they click this button
//to open the CheckOut JFrame form
this.setVisible(false);
String[] basketArr = new String[basketArL.size()];
basketArr = basketArL.toArray(basketArr);
//the total cost of the items and the array of items in the basket
//are both passed to the next jFrame form
new CheckOut(total, basketArr, products, stock, tempStock).setVisible(true);
}//GEN-LAST:event_btnCheckoutActionPerformed
/**
* @param args the command line arguments
*/
......
/*
* 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.
*/
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/**
*
* @author addya
*/
public class CheckOutTest {
public CheckOutTest() {
}
@BeforeAll
public static void setUpClass() {
}
@AfterAll
public static void tearDownClass() {
}
@BeforeEach
public void setUp() {
}
@AfterEach
public void tearDown() {
}
/**
* Test of payment when the user gives more money than the cost
*/
@Test
public void testPaymentSuccess() {
System.out.println("payment success");
double cost = 10.50;
double payment = 11.0;
String message="";
if (payment>=cost){
if (payment==cost){
message="Thank you.";
}
else{
double change=payment-cost;
message="Thank you. Your change is £"+change+".";
}
}
else {
message="Insufficient payment >.<";
fail("Message says 'Insufficient payment'.");
}
CheckOut instance = null;
String expResult = "Thank you. Your change is £0.5.";
//String result = instance.payment(cost, payment);
String result = message;
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
//fail("The test case is a prototype.");
}
/**
* Test of payment when the user gives more money than the cost
*/
@Test
public void testPaymentFailure() {
System.out.println("payment failure");
double cost = 10.50;
double payment = 10.0;
String message="";
if (payment>=cost){
if (payment==cost){
message="Thank you.";
}
else{
double change=payment-cost;
message="Thank you. Your change is £"+change+".";
}
fail("System accepts payment.");
}
else {
message="Insufficient payment >.<";
}
CheckOut instance = null;
String expResult = "Insufficient payment >.<";
//String result = instance.payment(cost, payment);
String result = message;
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
//fail("The test case is a prototype.");
}
}
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