Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
V
VendingMachine_Robert_Sharp
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
robert.sharp
VendingMachine_Robert_Sharp
Commits
40509fa7
Commit
40509fa7
authored
Jan 07, 2020
by
robert.sharp
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
got file working correctly, now i need to correct the item prices.
parent
9f33a549
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
72 additions
and
57 deletions
+72
-57
temp.java
src/main/java/temp.java
+72
-57
No files found.
src/main/java/temp.java
View file @
40509fa7
...
@@ -27,6 +27,8 @@ public class temp extends javax.swing.JFrame {
...
@@ -27,6 +27,8 @@ public class temp extends javax.swing.JFrame {
//the names of the items
//the names of the items
static
double
basket
[]={
0
,
0
,
0
,
0
,
0
};
static
double
basket
[]={
0
,
0
,
0
,
0
,
0
};
static
boolean
valid
=
false
;
//setting the default values
//setting the default values
static
String
ID
=
""
;
static
String
ID
=
""
;
static
String
Chosen
=
""
;
static
String
Chosen
=
""
;
...
@@ -270,34 +272,32 @@ public class temp extends javax.swing.JFrame {
...
@@ -270,34 +272,32 @@ 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"
;
ID
=
"01"
;
ID
=
"01"
;
break
;
break
;
case
"Water"
:
case
"Water"
:
Price
=
"£0.5"
;
ID
=
"02"
;
ID
=
"02"
;
break
;
break
;
case
"Drink"
:
case
"Drink"
:
Price
=
"£2"
;
ID
=
"03"
;
ID
=
"03"
;
break
;
break
;
case
"Snack"
:
case
"Snack"
:
Price
=
"£2"
;
ID
=
"04"
;
ID
=
"04"
;
break
;
break
;
case
"Sweet"
:
case
"Sweet"
:
Price
=
"£1"
;
ID
=
"05"
;
ID
=
"05"
;
break
;
break
;
default
:
default
:
Price
=
""
;
ID
=
""
;
ID
=
""
;
}
}
if
(
ID
==
""
){
if
(
ID
==
""
){
PriceLabel
.
setText
(
"Please selct an Item"
);
PriceLabel
.
setText
(
"Please selct an Item"
);
}
else
{
}
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
//update price label
...
@@ -305,6 +305,7 @@ public class temp extends javax.swing.JFrame {
...
@@ -305,6 +305,7 @@ public class temp extends javax.swing.JFrame {
private
void
quantBoxActionPerformed
(
java
.
awt
.
event
.
ActionEvent
evt
)
{
private
void
quantBoxActionPerformed
(
java
.
awt
.
event
.
ActionEvent
evt
)
{
// TODO add your handling code here:
// TODO add your handling code here:
findItem
();
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
);
...
@@ -316,22 +317,28 @@ public class temp extends javax.swing.JFrame {
...
@@ -316,22 +317,28 @@ public class temp extends javax.swing.JFrame {
private
void
PurchaseActionPerformed
(
java
.
awt
.
event
.
ActionEvent
evt
)
{
private
void
PurchaseActionPerformed
(
java
.
awt
.
event
.
ActionEvent
evt
)
{
// TODO add your handling code here:
// TODO add your handling code here:
amount
=
Double
.
parseDouble
(
moneyInput
.
getText
());
amount
=
Double
.
parseDouble
(
moneyInput
.
getText
());
payed
=
payed
+
amount
;
findItem
();
jLabel3
.
setText
(
"Amount Given: "
+
amount
);
if
(
PriceLabel
.
getText
()==
"Please selct an Item"
){
if
(
PriceLabel
.
getText
()==
"Please selct an Item"
){
error
.
setText
(
"Error: No item selected"
);
error
.
setText
(
"Error: No item selected"
);
//makes sure that the usr has an item selected before a purchase
}
else
{
}
else
{
error
.
setText
(
""
);
error
.
setText
(
""
);
}
}
if
(
cost
>
amount
){
if
(
cost
>
amount
){
error
.
setText
(
"Error: Payment not valid"
);
error
.
setText
(
"Error: Payment not valid"
);
valid
=
false
;
}
else
if
(
amount
>=
cost
)
{
}
else
if
(
amount
>=
cost
)
{
error
.
setText
(
"Payment valid"
);
error
.
setText
(
"Payment valid"
);
change
+=
amount
-
cost
;
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
));
moneyInput
.
setText
(
String
.
valueOf
(
amount
-
cost
));
purchase
();
purchase
();
valid
=
true
;
}
}
}
}
}
...
@@ -339,15 +346,27 @@ public class temp extends javax.swing.JFrame {
...
@@ -339,15 +346,27 @@ public class temp extends javax.swing.JFrame {
private
void
moneyInputActionPerformed
(
java
.
awt
.
event
.
ActionEvent
evt
)
{
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;
payed
=
Double
.
parseDouble
(
moneyInput
.
getText
());
amount
=
Double
.
parseDouble
(
moneyInput
.
getText
());
amount
=
Double
.
parseDouble
(
moneyInput
.
getText
());
//fins the item's index
jLabel3
.
setText
(
"Amount Given: "
+
amount
);
findItem
();
}
}
private
void
ReceiptActionPerformed
(
java
.
awt
.
event
.
ActionEvent
evt
)
{
private
void
ReceiptActionPerformed
(
java
.
awt
.
event
.
ActionEvent
evt
)
{
// TODO add your handling code here:
// TODO add your handling code here:
if
(
valid
=
true
){
//if the payemnt is valid
//update the change amount
print
();
print
();
//print the receipt
}
else
{
error
.
setText
(
"Error: Payments not valid"
);
//send an error message
}
}
}
/**
/**
...
@@ -399,18 +418,21 @@ public class temp extends javax.swing.JFrame {
...
@@ -399,18 +418,21 @@ public class temp extends javax.swing.JFrame {
}
}
}
}
public
void
purchase
(){
public
boolean
purchase
(){
while
(
true
)
{
while
(
true
)
{
findItem
();
if
((
quantity
[
index
]-
quant
)>=
0
)
{
//if not add the item to the basket
if
((
quantity
[
index
]-
quant
)>=
0
)
{
//if not add the item to the basket
quantity
[
index
]=(
quantity
[
index
]-
quant
);
quantity
[
index
]=(
quantity
[
index
]-
quant
);
//removes the number from the quantity
//removes the number from the quantity
basket
[
index
]=(
basket
[
index
]+
quant
);
basket
[
index
]=(
basket
[
index
]+
quant
);
//adds the number from the quantity to ther basket
//adds the number from the quantity to ther basket
break
;
return
true
;
}
else
{
}
else
{
//if the menu item has no quantity or less than they want, don't let them buy it.
//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."
);
error
.
setText
(
"There are not enough items for that."
);
break
;
amount
=
amount
+
cost
;
jLabel3
.
setText
(
"Amount Given: "
+
amount
);
return
false
;
}
}
}
}
}
}
...
@@ -420,45 +442,38 @@ public class temp extends javax.swing.JFrame {
...
@@ -420,45 +442,38 @@ public class temp extends javax.swing.JFrame {
FileWriter
f
=
new
FileWriter
(
"receipt.txt"
);
FileWriter
f
=
new
FileWriter
(
"receipt.txt"
);
f
.
write
(
"Items purchased: \n"
);
f
.
write
(
"Items purchased: \n"
);
f
.
write
(
"Code\tItem\t\tPrice\tQuantity \n"
);
f
.
write
(
"Code\tItem\t\tPrice\tQuantity \t"
+
"\n"
);
String
line
=
""
;
//displays the menu the the user
for
(
int
i
=
0
;
i
<
price
.
length
;
i
++)
{
if
(
basket
[
i
]!=
0
)
{
line
=
(
itemCode
[
i
]+
"\t"
);
//writes the data to a file.
f
.
write
(
line
);
for
(
int
i
=
0
;
i
<
basket
.
length
;
i
++)
{
//displays the item code
if
(
basket
[
i
]!=
0
)
{
if
(
i
==
0
||
i
==
1
||
i
==
3
)
{
f
.
write
(
itemCode
[
i
]+
"\t"
);
//this adds a second tab for shorter items which would otherwise display incorrectly
if
(
i
==
1
||
i
==
2
||
i
==
3
||
i
==
4
){
line
=
(
itemName
[
i
]+
"
\t"
);
f
.
write
(
itemName
[
i
]+
"\t
\t"
);
f
.
write
(
line
);
//for dealing with shorter names
}
else
{
}
else
{
//if the item name is not short, it only gets one tab
f
.
write
(
itemName
[
i
]+
"\t"
);
line
=
(
itemName
[
i
]+
"\t"
);
//fopr longer names
f
.
write
(
line
);
}
}
line
=
(
""
+
price
[
i
]+
"\t"
);
f
.
write
(
""
+
price
[
i
]+
"\t"
);
f
.
write
(
line
);
//displays the price
//displays the price
if
(
basket
[
i
]!=
0
)
{
f
.
write
(
basket
[
i
]+
"\t"
);
line
=
(
basket
[
i
]+
"\t"
);
//adds a new line to display the next item.
f
.
write
(
line
);
f
.
write
(
"\n"
);
}
else
{
}
f
.
write
(
"Not in Stock"
);
}
}
f
.
write
(
"\n"
+
"Total cost: "
+
cost
+
"\n"
);
f
.
write
(
"\n"
+
"Total payed: "
+
payed
+
"\n"
);
f
.
write
(
"\n"
+
"Change Given: "
+
Double
.
parseDouble
(
moneyInput
.
getText
())
+
"\n"
);
//adds a new line to display the next item.
if
(
change
>=
0
){
line
=
(
"\n"
);
f
.
write
(
"\n"
+
"valid payment: "
+
true
);
f
.
write
(
line
);
}
else
{
f
.
write
(
"\n"
+
"valid payment: "
+
false
);
}
}
f
.
write
(
"Total cost: "
+
cost
+
"\n"
);
f
.
write
(
"Total payed: "
+
amount
+
"\n"
);
f
.
write
(
"Change Given: "
+
change
+
"\n"
);
f
.
close
();
f
.
close
();
}
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
//catches execptions, the program wouldn't let me run it until i wrote this.
//catches execptions, the program wouldn't let me run it until i wrote this.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment