Commit 6f763fc2 authored by addya's avatar addya

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

parent fa5c4206
...@@ -5,6 +5,26 @@ ...@@ -5,6 +5,26 @@
<artifactId>SelfCheckout</artifactId> <artifactId>SelfCheckout</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<packaging>jar</packaging> <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> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>15</maven.compiler.source> <maven.compiler.source>15</maven.compiler.source>
......
...@@ -39,12 +39,18 @@ ...@@ -39,12 +39,18 @@
<Layout> <Layout>
<DimensionLayout dim="0"> <DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="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> </Group>
</DimensionLayout> </DimensionLayout>
<DimensionLayout dim="1"> <DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0"> <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> </Group>
</DimensionLayout> </DimensionLayout>
</Layout> </Layout>
...@@ -65,6 +71,27 @@ ...@@ -65,6 +71,27 @@
<Group type="102" attributes="0"> <Group type="102" attributes="0">
<EmptySpace max="-2" attributes="0"/> <EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" 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"/> <Component id="jLabel2" alignment="0" min="-2" max="-2" attributes="0"/>
<Group type="102" alignment="0" attributes="0"> <Group type="102" alignment="0" attributes="0">
<Component id="jScrollPane1" min="-2" pref="156" max="-2" attributes="0"/> <Component id="jScrollPane1" min="-2" pref="156" max="-2" attributes="0"/>
...@@ -92,28 +119,8 @@ ...@@ -92,28 +119,8 @@
</Group> </Group>
</Group> </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> </Group>
<EmptySpace min="194" pref="133" max="32767" attributes="0"/> <EmptySpace pref="55" max="32767" attributes="0"/>
</Group> </Group>
</Group> </Group>
</DimensionLayout> </DimensionLayout>
...@@ -123,8 +130,10 @@ ...@@ -123,8 +130,10 @@
<EmptySpace max="-2" attributes="0"/> <EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="1" attributes="0"> <Group type="103" groupAlignment="1" attributes="0">
<Group type="102" attributes="0"> <Group type="102" attributes="0">
<Component id="lblErrorMessage" min="-2" max="-2" attributes="0"/> <Component id="lblMessage" min="-2" max="-2" attributes="0"/>
<EmptySpace type="unrelated" 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"> <Group type="103" groupAlignment="3" attributes="0">
<Component id="txfPayment" alignment="3" min="-2" pref="30" max="-2" 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"/> <Component id="btnPay" alignment="3" min="-2" max="-2" attributes="0"/>
...@@ -289,6 +298,7 @@ ...@@ -289,6 +298,7 @@
<SubComponents> <SubComponents>
<Component class="javax.swing.JTextArea" name="txaBasket"> <Component class="javax.swing.JTextArea" name="txaBasket">
<Properties> <Properties>
<Property name="editable" type="boolean" value="false"/>
<Property name="columns" type="int" value="20"/> <Property name="columns" type="int" value="20"/>
<Property name="rows" type="int" value="5"/> <Property name="rows" type="int" value="5"/>
</Properties> </Properties>
...@@ -334,6 +344,7 @@ ...@@ -334,6 +344,7 @@
<Color blue="0" green="ff" red="ff" type="rgb"/> <Color blue="0" green="ff" red="ff" type="rgb"/>
</Property> </Property>
<Property name="text" type="java.lang.String" value="Pay"/> <Property name="text" type="java.lang.String" value="Pay"/>
<Property name="enabled" type="boolean" value="false"/>
</Properties> </Properties>
<Events> <Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="btnPayActionPerformed"/> <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="btnPayActionPerformed"/>
...@@ -358,7 +369,7 @@ ...@@ -358,7 +369,7 @@
<Property name="text" type="java.lang.String" value="6-10&gt;&gt;&gt;&#xa3;5 off"/> <Property name="text" type="java.lang.String" value="6-10&gt;&gt;&gt;&#xa3;5 off"/>
</Properties> </Properties>
</Component> </Component>
<Component class="javax.swing.JLabel" name="lblErrorMessage"> <Component class="javax.swing.JLabel" name="lblMessage">
<Properties> <Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor"> <Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Tahoma" size="14" style="2"/> <Font name="Tahoma" size="14" style="2"/>
...@@ -366,7 +377,7 @@ ...@@ -366,7 +377,7 @@
<Property name="foreground" type="java.awt.Color" editor="org.netbeans.beaninfo.editors.ColorEditor"> <Property name="foreground" type="java.awt.Color" editor="org.netbeans.beaninfo.editors.ColorEditor">
<Color blue="0" green="ff" red="ff" type="rgb"/> <Color blue="0" green="ff" red="ff" type="rgb"/>
</Property> </Property>
<Property name="text" type="java.lang.String" value=".........."/> <Property name="verticalAlignment" type="int" value="1"/>
</Properties> </Properties>
</Component> </Component>
<Component class="javax.swing.JLabel" name="lbl&#xa3;"> <Component class="javax.swing.JLabel" name="lbl&#xa3;">
...@@ -380,6 +391,17 @@ ...@@ -380,6 +391,17 @@
<Property name="text" type="java.lang.String" value="&#xa3;"/> <Property name="text" type="java.lang.String" value="&#xa3;"/>
</Properties> </Properties>
</Component> </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> </SubComponents>
</Container> </Container>
</SubComponents> </SubComponents>
......
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random; import java.util.Random;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.math.RoundingMode; import java.math.RoundingMode;
...@@ -15,12 +18,39 @@ import java.math.RoundingMode; ...@@ -15,12 +18,39 @@ import java.math.RoundingMode;
* @author addya * @author addya
*/ */
public class CheckOut extends javax.swing.JFrame { public class CheckOut extends javax.swing.JFrame {
public double totalCost;
public String discountStr="";
/** /**
* Creates new form CheckOut * 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(); 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 { ...@@ -51,8 +81,9 @@ public class CheckOut extends javax.swing.JFrame {
btnPay = new javax.swing.JButton(); btnPay = new javax.swing.JButton();
txfPayment = new javax.swing.JTextField(); txfPayment = new javax.swing.JTextField();
lbl7 = new javax.swing.JLabel(); lbl7 = new javax.swing.JLabel();
lblErrorMessage = new javax.swing.JLabel(); lblMessage = new javax.swing.JLabel();
lbl£ = 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.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
jLabel1.setForeground(new java.awt.Color(255, 255, 0)); jLabel1.setForeground(new java.awt.Color(255, 255, 0));
...@@ -104,6 +135,7 @@ public class CheckOut extends javax.swing.JFrame { ...@@ -104,6 +135,7 @@ public class CheckOut extends javax.swing.JFrame {
} }
}); });
txaBasket.setEditable(false);
txaBasket.setColumns(20); txaBasket.setColumns(20);
txaBasket.setRows(5); txaBasket.setRows(5);
jScrollPane1.setViewportView(txaBasket); jScrollPane1.setViewportView(txaBasket);
...@@ -122,6 +154,7 @@ public class CheckOut extends javax.swing.JFrame { ...@@ -122,6 +154,7 @@ public class CheckOut extends javax.swing.JFrame {
btnPay.setBackground(new java.awt.Color(255, 255, 0)); btnPay.setBackground(new java.awt.Color(255, 255, 0));
btnPay.setText("Pay"); btnPay.setText("Pay");
btnPay.setEnabled(false);
btnPay.addActionListener(new java.awt.event.ActionListener() { btnPay.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) { public void actionPerformed(java.awt.event.ActionEvent evt) {
btnPayActionPerformed(evt); btnPayActionPerformed(evt);
...@@ -139,14 +172,18 @@ public class CheckOut extends javax.swing.JFrame { ...@@ -139,14 +172,18 @@ public class CheckOut extends javax.swing.JFrame {
lbl7.setForeground(new java.awt.Color(255, 255, 0)); lbl7.setForeground(new java.awt.Color(255, 255, 0));
lbl7.setText("6-10>>>£5 off"); lbl7.setText("6-10>>>£5 off");
lblErrorMessage.setFont(new java.awt.Font("Tahoma", 2, 14)); // NOI18N lblMessage.setFont(new java.awt.Font("Tahoma", 2, 14)); // NOI18N
lblErrorMessage.setForeground(new java.awt.Color(255, 255, 0)); lblMessage.setForeground(new java.awt.Color(255, 255, 0));
lblErrorMessage.setText(".........."); lblMessage.setVerticalAlignment(javax.swing.SwingConstants.TOP);
lbl£.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N lbl£.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
lbl£.setForeground(new java.awt.Color(255, 255, 0)); lbl£.setForeground(new java.awt.Color(255, 255, 0));
lbl£.setText("£"); 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); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout); jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup( jPanel1Layout.setHorizontalGroup(
...@@ -154,6 +191,22 @@ public class CheckOut extends javax.swing.JFrame { ...@@ -154,6 +191,22 @@ public class CheckOut extends javax.swing.JFrame {
.addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap() .addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .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) .addComponent(jLabel2)
.addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
...@@ -174,23 +227,8 @@ public class CheckOut extends javax.swing.JFrame { ...@@ -174,23 +227,8 @@ public class CheckOut extends javax.swing.JFrame {
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup() .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addComponent(lbl1) .addComponent(lbl1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .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))))) .addComponent(lblYouGot, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))))))
.addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap(55, Short.MAX_VALUE))
.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))
); );
jPanel1Layout.setVerticalGroup( jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
...@@ -198,8 +236,10 @@ public class CheckOut extends javax.swing.JFrame { ...@@ -198,8 +236,10 @@ public class CheckOut extends javax.swing.JFrame {
.addContainerGap() .addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(lblErrorMessage) .addComponent(lblMessage)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGap(10, 10, 10)
.addComponent(lblMessage2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txfPayment, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(txfPayment, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnPay))) .addComponent(btnPay)))
...@@ -245,11 +285,15 @@ public class CheckOut extends javax.swing.JFrame { ...@@ -245,11 +285,15 @@ public class CheckOut extends javax.swing.JFrame {
getContentPane().setLayout(layout); getContentPane().setLayout(layout);
layout.setHorizontalGroup( layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 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.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 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(); pack();
...@@ -276,22 +320,130 @@ public class CheckOut extends javax.swing.JFrame { ...@@ -276,22 +320,130 @@ public class CheckOut extends javax.swing.JFrame {
return shortened; 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 private void btnPayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPayActionPerformed
// TODO add your handling code here: // 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 }//GEN-LAST:event_btnPayActionPerformed
private void btnDrawActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDrawActionPerformed private void btnDrawActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDrawActionPerformed
// TODO add your handling code here: // 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(); Random rand = new Random();
//a random integer from 1-10 is used to pick the random discount //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 //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 //this is because the random draw is from 1-10
int r=rand.nextInt(9)+1; int r=rand.nextInt(9)+1;
String discountStr="";
double discountPer=1; double discountPer=1;
lblYouGot.setText("You got "+r+"!"); lblYouGot.setText("You got "+r+"!");
//we disable use of the button so the user can't draw more than once //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 //the random number from 1-10 decides how much discount is received
switch (r){ switch (r){
//if the variable r is 1 then a 10% discount is applied //if the variable r is 1 then a 10% discount is applied
...@@ -328,10 +480,10 @@ public class CheckOut extends javax.swing.JFrame { ...@@ -328,10 +480,10 @@ public class CheckOut extends javax.swing.JFrame {
discountStr="£5"; discountStr="£5";
} }
lblDiscount.setText("Discount: "+discountStr); lblDiscount.setText("Discount: "+discountStr);
double totalCost=calcTotal(11.11, discountPer, r); totalCost=calcTotal(basketTotal, discountPer, r);
totalCost=109.777;
totalCost=shortenValue(totalCost); totalCost=shortenValue(totalCost);
lblTotal.setText("Total: "+totalCost); lblTotal.setText("Total: "+totalCost);
//removeStock()
}//GEN-LAST:event_btnDrawActionPerformed }//GEN-LAST:event_btnDrawActionPerformed
private void txfPaymentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txfPaymentActionPerformed private void txfPaymentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txfPaymentActionPerformed
...@@ -368,7 +520,7 @@ public class CheckOut extends javax.swing.JFrame { ...@@ -368,7 +520,7 @@ public class CheckOut extends javax.swing.JFrame {
/* Create and display the form */ /* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() { java.awt.EventQueue.invokeLater(new Runnable() {
public void run() { public void run() {
new CheckOut().setVisible(true); //new CheckOut().setVisible(true);
} }
}); });
} }
...@@ -389,7 +541,8 @@ public class CheckOut extends javax.swing.JFrame { ...@@ -389,7 +541,8 @@ public class CheckOut extends javax.swing.JFrame {
private javax.swing.JLabel lblBasket; private javax.swing.JLabel lblBasket;
private javax.swing.JLabel lblChance; private javax.swing.JLabel lblChance;
private javax.swing.JLabel lblDiscount; 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 lblTotal;
private javax.swing.JLabel lblYouGot; private javax.swing.JLabel lblYouGot;
private javax.swing.JLabel lbl£; private javax.swing.JLabel lbl£;
......
...@@ -169,7 +169,7 @@ ...@@ -169,7 +169,7 @@
<Property name="editable" type="boolean" value="false"/> <Property name="editable" type="boolean" value="false"/>
<Property name="columns" type="int" value="20"/> <Property name="columns" type="int" value="20"/>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor"> <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>
<Property name="rows" type="int" value="5"/> <Property name="rows" type="int" value="5"/>
</Properties> </Properties>
...@@ -232,6 +232,9 @@ ...@@ -232,6 +232,9 @@
</Property> </Property>
<Property name="text" type="java.lang.String" value="Checkout"/> <Property name="text" type="java.lang.String" value="Checkout"/>
</Properties> </Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="btnCheckoutActionPerformed"/>
</Events>
</Component> </Component>
</SubComponents> </SubComponents>
</Container> </Container>
......
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.DefaultComboBoxModel; import javax.swing.DefaultComboBoxModel;
/* /*
...@@ -18,22 +19,34 @@ public class SelectProducts extends javax.swing.JFrame { ...@@ -18,22 +19,34 @@ public class SelectProducts extends javax.swing.JFrame {
public String[] products = {"Pen", "Book", "Water", "Drink", "Snack", "Sweet", "Chocolate", "Biscuits", "Cake"}; public String[] products = {"Pen", "Book", "Water", "Drink", "Snack", "Sweet", "Chocolate", "Biscuits", "Cake"};
//array of prices //array of prices
public double[] prices = {1, 1, 1, 1, 2, 1, 1, 1, 2}; 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>(); 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 //index will be the position of the currently selected item in these arrays
public int index; 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 * Creates new form SelectProducts
*/ */
public SelectProducts() { public SelectProducts() {
initComponents(); initComponents();
//------------------Initialising stocks-------------------
//this variable is how many iterations the loop performs //this variable is how many iterations the loop performs
//one iteration is done for eacah product //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"; String z="0";
//if int i is below 10 then a "0" is added to the front //if int i is below 10 then a "0" is added to the front
//e.g. "01, "02", "03" //e.g. "01, "02", "03"
...@@ -54,11 +67,20 @@ public class SelectProducts extends javax.swing.JFrame { ...@@ -54,11 +67,20 @@ public class SelectProducts extends javax.swing.JFrame {
//so 20 is added to the stock arraylist //so 20 is added to the stock arraylist
stockArL.add(20); 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 //on the form starting the combobox cmbItems
//is set to display the items in the products array //is set to display the items in the products array
DefaultComboBoxModel dm=new DefaultComboBoxModel(products); DefaultComboBoxModel dm=new DefaultComboBoxModel(products);
cmbProducts.setModel(dm); 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 { ...@@ -101,7 +123,7 @@ public class SelectProducts extends javax.swing.JFrame {
txaShowSelected.setEditable(false); txaShowSelected.setEditable(false);
txaShowSelected.setColumns(20); 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); txaShowSelected.setRows(5);
jScrollPane1.setViewportView(txaShowSelected); jScrollPane1.setViewportView(txaShowSelected);
...@@ -128,6 +150,11 @@ public class SelectProducts extends javax.swing.JFrame { ...@@ -128,6 +150,11 @@ public class SelectProducts extends javax.swing.JFrame {
btnCheckout.setBackground(new java.awt.Color(255, 255, 0)); btnCheckout.setBackground(new java.awt.Color(255, 255, 0));
btnCheckout.setText("Checkout"); 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); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout); jPanel1.setLayout(jPanel1Layout);
...@@ -207,6 +234,7 @@ public class SelectProducts extends javax.swing.JFrame { ...@@ -207,6 +234,7 @@ public class SelectProducts extends javax.swing.JFrame {
private void cmbProductsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbProductsActionPerformed private void cmbProductsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbProductsActionPerformed
// TODO add your handling code here: // TODO add your handling code here:
btnAddItem.setEnabled(true);
String display; String display;
String find=cmbProducts.getSelectedItem().toString(); String find=cmbProducts.getSelectedItem().toString();
String current=""; String current="";
...@@ -216,47 +244,18 @@ public class SelectProducts extends javax.swing.JFrame { ...@@ -216,47 +244,18 @@ public class SelectProducts extends javax.swing.JFrame {
//index is equal to i //index is equal to i
//this is the position of the product we want to find //this is the position of the product we want to find
index=i; index=i;
display="Item: "+products[i]+"\nPrice: £"+prices[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); txaShowSelected.setText(display);
} }
else { 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 }//GEN-LAST:event_cmbProductsActionPerformed
//the variable for the total cost must be defined outside of the action event //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 { ...@@ -264,22 +263,43 @@ public class SelectProducts extends javax.swing.JFrame {
double total=0; double total=0;
private void btnAddItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddItemActionPerformed private void btnAddItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddItemActionPerformed
// TODO add your handling code here: // 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
//the item in the display should go into the basket if (tempStock[index]>0){
txaBasket.append(products[index]+"\t"+prices[index]+"\n"); //take away 1 from the stock of the added item
int ts=tempStock[index];
//we add the item to an arraylist for the basket too ts=ts-1;
//somehow we need to pass this arraylist to the second JFrameForm tempStock[index]=ts;
ArrayList<String> BasketArL= new ArrayList<String>(); //the item in the display should go into the basket
BasketArL.add(products[index]); txaBasket.append(products[index]+"\t"+prices[index]+"\n");
//we will add the price of the added item to the total cost //we add the item to the arraylist for the basket too
total= total + prices[index]; basketArL.add(products[index]);
//then display it with the total label
lblTotalCost.setText("Total: £"+total); //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 }//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 * @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