HI
i wrote a code to validate cash for a vending machine.... the problem am having is dat dat my code failed to validate any ammount.
please help!!!!
the code is below
public static double validDollarNote() {
boolean done = false;
double cash = 0.0;
String bill;
double Nickel = 0.05;
double Dime = 0.10;
double Quarter = 0.25;
double OneDollar = 1.0;
double FiveDollar = 5.0;
while (!done) {
bill = JOptionPane.showInputDialog("Pay:");
try {
cash = Double.parseDouble(bill);
System.out.println(cash);
if (cash != Dime || cash != Nickel || cash != Quarter || cash != OneDollar || cash != FiveDollar) {
throw new NumberFormatException();
}
else
{
done = true;
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "MACHINE ACCEPTS: $.05,$.10,$.25,$1,$5\n",
"Error", JOptionPane.INFORMATION_MESSAGE);
}
}
return cash;
}
4 1863
HI
i wrote a code to validate cash for a vending machine.... the problem am having is dat dat my code failed to validate any ammount.
please help!!!!
the code is below
public static double validDollarNote() {
boolean done = false;
double cash = 0.0;
String bill;
double Nickel = 0.05;
double Dime = 0.10;
double Quarter = 0.25;
double OneDollar = 1.0;
double FiveDollar = 5.0;
while (!done) {
bill = JOptionPane.showInputDialog("Pay:");
try {
cash = Double.parseDouble(bill);
System.out.println(cash);
if (cash != Dime || cash != Nickel || cash != Quarter || cash != OneDollar || cash != FiveDollar) {
throw new NumberFormatException();
}
else
{
done = true;
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "MACHINE ACCEPTS: $.05,$.10,$.25,$1,$5\n",
"Error", JOptionPane.INFORMATION_MESSAGE);
}
}
return cash;
}
Hello soty, i've test your code... There is something strange about your if statement....
Assume true = 1 and false = 0....
If you convert your if statement into logic...
OR = ||
AND = &&
@ OR, if one of the equality test is true, then it should return true/1
@ AND, if at least one of the equality test is false, it should return false/0
OR represents in logic as + (addition)
AND represents in logic as * (multiply)
the if statement encounters 1/true inside of ( here ), then it will allow to execute the statements inside of it....
@ your if statement,
if ( (cash != Dime || cash != Nickel || cash != Quarter || cash != OneDollar || cash != FiveDollar)
if i will enter a value .05
it is equivalent to
if ( 0 + 1 + 0 + 0 + 0) and the total is 1...
obviously it will execute all the statements inside of it....
try this one, - if((cash==Dime)||(cash==Nickel)||(cash==Quarter)||(cash==OneDollar)||(cash==FiveDollar)) {
-
done = true;
-
}
-
else {
-
throw new NumberFormatException();
-
}
Have some experiments on it.
update us,
sukatoa
Dont forget to inclose your code with codetags.... ;-)
just reminding
sukatoa...
double Nickel = 0.05;
double Dime = 0.10;
double Quarter = 0.25;
double OneDollar = 1.0;
double FiveDollar = 5.0;
Don't do that. The IEEE floating point number system can't represent values
such as 0.1 exactly. Only numbers that are a sum of negative powers of two
times an exponent value can be represented exactly.
e.g. try to make 1/10 given the numbers 1/2, 1/4, 1/8, 1/16, 1/32 ... etc by
using each number at most once. For a thorough explanation read this.
Use the following monetary units instead: -
int Nickel = 5;
-
int Dime = 10;
-
int Quarter = 25;
-
int OneDollar = 100;
-
int FiveDollar = 500;
-
i.e. the monetary unit is the penny/cent instead of the dollar.
kind regards,
Jos
hi
av finished the code but i'm having one slight problem.....either av lost it or i'm jus not thinking straight.... either way i guess my logic is not coming out well.
the problem am having wif this code is that each time i wanna calculate my total sales its resets it to present price. it doesn't add it up.... please can u look into it?
thanks -
-
-
/*
-
* Name :Uju
-
* Project: Vending Machine
-
* Program: This Program uses JOptionPane to assimulate a vending machine
-
* that can allow ther user select more that one item at each time and displays
-
* the total expenses in that transaction
-
*/
-
-
import javax.swing.JOptionPane;
-
import javax.swing.*;
-
import java.util.Date;
-
import java.text.DecimalFormat;
-
-
public class vending {
-
-
public static void main(String[] args) {
-
-
vencalc();
-
-
-
}
-
-
public static int getoption() { // this method get the displays the menue
-
//and validates the range for the option
-
-
int option = 0;
-
String choice;
-
boolean done = false;
-
-
Date currentdate = new Date();
-
-
while (!done) {
-
choice = JOptionPane.showInputDialog(null, "UJU'S VENDING MACHINE\n" + currentdate + "\n\n" + "SELECT YOUR CHOICE\n\n1 CHIPS\n2 MINT GUM\n3 NACHOS\n4 COOKIES" +
-
"\n5 LIFESAVER\n6 M&M\n7 WALNUT\n8 POPCORN\n9 MIXED NUT\n");
-
-
if (choice == null) {
-
finish();
-
}
-
-
try {
-
-
option = Integer.parseInt(choice);
-
if (option < 1 || option > 9) {
-
throw new NumberFormatException();
-
} else {
-
done = true;
-
}
-
-
} catch (NumberFormatException e) {
-
JOptionPane.showMessageDialog(null, "your entery was not in proper format.",
-
"Error", JOptionPane.INFORMATION_MESSAGE);
-
}
-
}
-
return option;
-
-
}
-
-
public static void vencalc() {// this method calculates the vend machine.
-
int val = 0;
-
double price = 0.0;
-
int outOfStock = 0;
-
int chipInStock = 2;
-
int mintgumInStock = 5;
-
int nachoInStock = 5;
-
int cookiesInStock = 5;
-
int lifesaverInStock = 5;
-
int mandmInStock = 5;
-
int walnutInStock = 5;
-
int popcornInStock = 5;
-
int mixednutInStock = 5;
-
double total = 0;
-
-
boolean instock = true;
-
-
while (true) {
-
val = getoption();
-
-
switch (val) {
-
case 1:
-
price = 1.85;
-
instock = (chipInStock > 0);
-
break;
-
case 2:
-
price = 0.95;
-
instock = (mintgumInStock > 0);
-
break;
-
case 3:
-
price = 1.00;
-
instock = (nachoInStock > 0);
-
break;
-
case 4:
-
price = 2.15;
-
instock = (cookiesInStock > 0);
-
break;
-
case 5:
-
price = 1.05;
-
instock = (lifesaverInStock > 0);
-
break;
-
case 6:
-
price = 0.85;
-
instock = (mandmInStock > 0);
-
break;
-
case 7:
-
price = 4.20;
-
instock = (walnutInStock > 0);
-
break;
-
case 8:
-
price = 1.15;
-
instock = (popcornInStock > 0);
-
break;
-
case 9:
-
price = 1.15;
-
instock = (mixednutInStock > 0);
-
break;
-
}
-
-
-
Continue(price, instock,total);// this calls the continue method
-
switch (val) {
-
case 1:
-
chipInStock -= 1;
-
break;
-
case 2:
-
mintgumInStock -= 1;
-
break;
-
case 3:
-
nachoInStock -= 1;
-
break;
-
case 4:
-
cookiesInStock -= 1;
-
break;
-
case 5:
-
lifesaverInStock -= 1;
-
break;
-
case 6:
-
mandmInStock -= 1;
-
break;
-
case 7:
-
walnutInStock -= 1;
-
break;
-
case 8:
-
popcornInStock -= 1;
-
break;
-
case 9:
-
mixednutInStock -= 1;
-
break;
-
}
-
-
if (instock == false) {
-
outOfStock = JOptionPane.showConfirmDialog(null, "Sorry, out of this item" +
-
"\nWould you like to choose another item?", "Question", JOptionPane.YES_NO_OPTION);
-
}
-
if (outOfStock == 1) {
-
JOptionPane.showMessageDialog(null, "Please pick up your money and" +
-
"come back soon", "\nGOOD BYE!!!", JOptionPane.INFORMATION_MESSAGE);
-
finish();
-
}
-
}
-
-
-
}
-
-
public static int getAnswer(double price) {// method that confirms if
-
//the customer wants to purshace the item
-
-
int ans;
-
-
ans = JOptionPane.showConfirmDialog(null, "Do you want to purchase this item?" +
-
"\nItem Price: $" + price, "Confirmation", JOptionPane.YES_NO_OPTION);
-
-
return ans;
-
}
-
-
public static void Continue(double price, boolean instock,double total) {// this method confirms for
-
//the second time if the user wants to exit
-
int ans;
-
-
ans = getAnswer(price);
-
if (ans == 0) {
-
collect(price, instock,total);
-
} else if (ans == 1) {
-
ans = JOptionPane.showConfirmDialog(null, "Do you want to purchase " +
-
"another item?", "Question", JOptionPane.YES_NO_OPTION);
-
if (ans == 1) {
-
finish();
-
}
-
}
-
-
}
-
-
public static double validDollarNote(double price) {// this method validates the cash input
-
-
boolean done = false;
-
double cash = 0.0;
-
String bill;
-
-
double Nickel = 0.05;
-
double Dime = 0.10;
-
double Quarter = 0.25;
-
double OneDollar = 1.0;
-
double FiveDollar = 5.0;
-
-
-
while (!done) {
-
-
bill = JOptionPane.showInputDialog("Pay:" + price);
-
-
try {
-
cash = Double.parseDouble(bill);
-
System.out.println(cash);
-
-
if (cash == Dime || cash == Nickel || cash == Quarter || cash == OneDollar || cash == FiveDollar) {
-
done = true;
-
} else {
-
throw new NumberFormatException();
-
}
-
} catch (NumberFormatException e) {
-
JOptionPane.showMessageDialog(null, "MACHINE ACCEPTS: $.05,$.10,$.25,$1,$5\n",
-
"Error", JOptionPane.INFORMATION_MESSAGE);
-
-
}
-
}
-
-
return cash;
-
-
}
-
-
public static void finish() {// this is an exit method
-
System.exit(0);
-
}
-
-
public static void collect(double price, boolean instock,double total) {// this this method calculates and
-
//and validates cash input.
-
boolean done = false;
-
double Nickel = 0.05;
-
double Dime = 0.10;
-
double Quarter = 0.25;
-
double OneDollar = 1.0;
-
double FiveDollar = 5.0;
-
double amtToPay;
-
String cash = null;
-
double money;
-
int reply;
-
-
-
DecimalFormat twoDigits = new DecimalFormat("$0.00");
-
-
amtToPay = validDollarNote(price);
-
-
total+= price;
-
-
-
while (amtToPay < price) {
-
-
while (!done) {
-
-
cash = JOptionPane.showInputDialog(null, "Not enough Cash for the purshase\n\n" +
-
"Please pay " + twoDigits.format(price - amtToPay) + " to complete your transaction");
-
-
try {
-
money = Double.parseDouble(cash);
-
-
System.out.println(cash);
-
-
if (money == Dime || money == Nickel || money == Quarter || money == OneDollar || money == FiveDollar) {
-
done = true;
-
} else {
-
throw new NumberFormatException();
-
}
-
} catch (NumberFormatException e) {
-
JOptionPane.showMessageDialog(null, "MACHINE ACCEPTS: $.05,$.10,$.25,$1,$5\n",
-
"Error", JOptionPane.INFORMATION_MESSAGE);
-
-
}
-
}
-
-
if (cash == null)
-
{
-
vencalc();;
-
}
-
else
-
{
-
amtToPay += Double.parseDouble(cash);
-
}
-
-
}
-
-
if (amtToPay == price && instock) {
-
-
reply =JOptionPane.showConfirmDialog(null, "Do you want to purchase " +
-
"another item?", "Question", JOptionPane.YES_NO_OPTION);
-
-
if (reply == 1){
-
JOptionPane.showMessageDialog(null, "please pick up you item\n" + "\nTOTAL: "
-
+ twoDigits.format(total) + "\nThank you");
-
finish();}
-
}
-
-
else if (amtToPay > price && instock) {
-
-
reply =JOptionPane.showConfirmDialog(null, "Do you want to purchase " +
-
"another item?", "Question", JOptionPane.YES_NO_OPTION);
-
if (reply == 1){
-
JOptionPane.showMessageDialog(null, "please pick up you item\n" + "\nTOTAL: "
-
+ twoDigits.format(total) + "\nThank you");
-
finish();}
-
}
-
-
}
-
}
-
-
-
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Stefan Richter |
last post by:
Hi, after coding for days on stupid form validations -
Like:
strings (min / max length), numbers(min / max value), money(min / max
value), postcodes(min / max value), telefon numbers,
email...
|
by: |
last post by:
Hello Guys,
I am using the validation controls to validate my data.
But the problem is "The page is still being posted to
server".
I want to get rid of the round trips to server. Are there...
|
by: A.M |
last post by:
Hi,
I have a validation control in my page that upon any invalid data, it
disables all buttons in the page. basicly i don't have any postback in the
page if the validator finds any error.
How...
|
by: Matt |
last post by:
I want to know if ASP.NET Web Forms Validation Controls are Server-Side or
Client-Side form validation? Since I think each validator control can select
either 1) JavaScript based error dialog or 2)...
|
by: usl2222 |
last post by:
Hi folks,
I appreciate any assistance in the following problem:
I have a form with a bunch of dynamic controls on it. All the controls
are dynamically generated on a server, including all...
|
by: Barbara Alderton |
last post by:
I setup some standard Required Field Validation controls and one Custom
validation control on an ASP.NET page (within a user control) to validate
text entry. I also setup a Summary Control to post...
|
by: Tim Frawley |
last post by:
Source code attached indicates my problem with validation and a button
bar save button.
Fill the Textbox with some text then tab off the control. The message
box will display the text in the...
|
by: dustbort |
last post by:
I recently had a problem where my required field validator stopped working.
But, the page still posted back and tried to insert a record into the
database without performing server-side validation....
|
by: Jon Paal |
last post by:
validation doesn't fire
what's missing ?????
/////// ---- code -----///////////////////////////
Sub btnSubmit_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) 'Handles...
|
by: Bryan |
last post by:
I want my business objects to be able to do this:
class Person(base):
def __init__(self):
self.name = None
@base.validator
def validate_name(self):
if not self.name: return
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |