473,395 Members | 1,488 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

validation of code

25
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;

}
Mar 26 '08 #1
4 1863
sukatoa
539 512MB
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,

Expand|Select|Wrap|Line Numbers
  1. if((cash==Dime)||(cash==Nickel)||(cash==Quarter)||(cash==OneDollar)||(cash==FiveDollar)) {
  2.     done = true;
  3. else {
  4.     throw new NumberFormatException();
  5. }
Have some experiments on it.

update us,
sukatoa
Mar 26 '08 #2
sukatoa
539 512MB
Dont forget to inclose your code with codetags.... ;-)

just reminding
sukatoa...
Mar 26 '08 #3
JosAH
11,448 Expert 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. int Nickel = 5;
  2. int Dime = 10;
  3. int Quarter = 25;
  4. int OneDollar = 100;
  5. int FiveDollar = 500;
  6.  
i.e. the monetary unit is the penny/cent instead of the dollar.

kind regards,

Jos
Mar 26 '08 #4
soty
25
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

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. /*
  4.  *  Name :Uju
  5.  *  Project: Vending Machine 
  6.  *  Program: This Program uses JOptionPane to assimulate a vending machine          
  7.  *  that can allow ther user select more that one item at each time and displays  
  8.  *  the total expenses in that transaction
  9.  */
  10.  
  11. import javax.swing.JOptionPane;
  12. import javax.swing.*;
  13. import java.util.Date;
  14. import java.text.DecimalFormat;
  15.  
  16. public class vending {
  17.  
  18.     public static void main(String[] args) {
  19.  
  20.         vencalc();
  21.  
  22.  
  23.     }
  24.  
  25.     public static int getoption() {     // this method get the displays the menue 
  26.         //and validates the range for the option
  27.  
  28.         int option = 0;
  29.         String choice;
  30.         boolean done = false;
  31.  
  32.         Date currentdate = new Date();
  33.  
  34.         while (!done) {
  35.             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" +
  36.                     "\n5     LIFESAVER\n6     M&M\n7     WALNUT\n8     POPCORN\n9     MIXED NUT\n");
  37.  
  38.             if (choice == null) {
  39.                 finish();
  40.             }
  41.  
  42.             try {
  43.  
  44.                 option = Integer.parseInt(choice);
  45.                 if (option < 1 || option > 9) {
  46.                     throw new NumberFormatException();
  47.                 } else {
  48.                     done = true;
  49.                 }
  50.  
  51.             } catch (NumberFormatException e) {
  52.                 JOptionPane.showMessageDialog(null, "your entery was not in proper format.",
  53.                         "Error", JOptionPane.INFORMATION_MESSAGE);
  54.             }
  55.         }
  56.         return option;
  57.  
  58.     }
  59.  
  60.     public static void vencalc() {// this method calculates the vend machine.
  61.         int val = 0;
  62.         double price = 0.0;
  63.         int outOfStock = 0;
  64.         int chipInStock = 2;
  65.         int mintgumInStock = 5;
  66.         int nachoInStock = 5;
  67.         int cookiesInStock = 5;
  68.         int lifesaverInStock = 5;
  69.         int mandmInStock = 5;
  70.         int walnutInStock = 5;
  71.         int popcornInStock = 5;
  72.         int mixednutInStock = 5;
  73.         double total = 0;
  74.  
  75.         boolean instock = true;
  76.  
  77.         while (true) {
  78.             val = getoption();
  79.  
  80.             switch (val) {
  81.                 case 1:
  82.                     price = 1.85;
  83.                     instock = (chipInStock > 0);
  84.                     break;
  85.                 case 2:
  86.                     price = 0.95;
  87.                     instock = (mintgumInStock > 0);
  88.                     break;
  89.                 case 3:
  90.                     price = 1.00;
  91.                     instock = (nachoInStock > 0);
  92.                     break;
  93.                 case 4:
  94.                     price = 2.15;
  95.                     instock = (cookiesInStock > 0);
  96.                     break;
  97.                 case 5:
  98.                     price = 1.05;
  99.                     instock = (lifesaverInStock > 0);
  100.                     break;
  101.                 case 6:
  102.                     price = 0.85;
  103.                     instock = (mandmInStock > 0);
  104.                     break;
  105.                 case 7:
  106.                     price = 4.20;
  107.                     instock = (walnutInStock > 0);
  108.                     break;
  109.                 case 8:
  110.                     price = 1.15;
  111.                     instock = (popcornInStock > 0);
  112.                     break;
  113.                 case 9:
  114.                     price = 1.15;
  115.                     instock = (mixednutInStock > 0);
  116.                     break;
  117.             }
  118.  
  119.  
  120.             Continue(price, instock,total);// this calls the continue method
  121.             switch (val) {
  122.                 case 1:
  123.                     chipInStock -= 1;
  124.                     break;
  125.                 case 2:
  126.                     mintgumInStock -= 1;
  127.                     break;
  128.                 case 3:
  129.                     nachoInStock -= 1;
  130.                     break;
  131.                 case 4:
  132.                     cookiesInStock -= 1;
  133.                     break;
  134.                 case 5:
  135.                     lifesaverInStock -= 1;
  136.                     break;
  137.                 case 6:
  138.                     mandmInStock -= 1;
  139.                     break;
  140.                 case 7:
  141.                     walnutInStock -= 1;
  142.                     break;
  143.                 case 8:
  144.                     popcornInStock -= 1;
  145.                     break;
  146.                 case 9:
  147.                     mixednutInStock -= 1;
  148.                     break;
  149.             }
  150.  
  151.             if (instock == false) {
  152.                 outOfStock = JOptionPane.showConfirmDialog(null, "Sorry, out of this item" +
  153.                         "\nWould you like to choose another item?", "Question", JOptionPane.YES_NO_OPTION);
  154.             }
  155.             if (outOfStock == 1) {
  156.                 JOptionPane.showMessageDialog(null, "Please pick up your money and" +
  157.                         "come back soon", "\nGOOD BYE!!!", JOptionPane.INFORMATION_MESSAGE);
  158.                 finish();
  159.             }
  160.         }
  161.  
  162.  
  163.     }
  164.  
  165.     public static int getAnswer(double price) {// method that confirms if 
  166.         //the customer wants to purshace the item
  167.  
  168.         int ans;
  169.  
  170.         ans = JOptionPane.showConfirmDialog(null, "Do you want to purchase this item?" +
  171.                 "\nItem Price: $" + price, "Confirmation", JOptionPane.YES_NO_OPTION);
  172.  
  173.         return ans;
  174.     }
  175.  
  176.     public static void Continue(double price, boolean instock,double total) {// this method confirms for 
  177.         //the second time if the user wants to exit
  178.         int ans;
  179.  
  180.         ans = getAnswer(price);
  181.         if (ans == 0) {
  182.             collect(price, instock,total);
  183.         } else if (ans == 1) {
  184.             ans = JOptionPane.showConfirmDialog(null, "Do you want to purchase " +
  185.                     "another item?", "Question", JOptionPane.YES_NO_OPTION);
  186.             if (ans == 1) {
  187.                 finish();
  188.             }
  189.         }
  190.  
  191.     }
  192.  
  193.     public static double validDollarNote(double price) {// this method validates the cash input
  194.  
  195.         boolean done = false;
  196.         double cash = 0.0;
  197.         String bill;
  198.  
  199.         double Nickel = 0.05;
  200.         double Dime = 0.10;
  201.         double Quarter = 0.25;
  202.         double OneDollar = 1.0;
  203.         double FiveDollar = 5.0;
  204.  
  205.  
  206.         while (!done) {
  207.  
  208.             bill = JOptionPane.showInputDialog("Pay:" + price);
  209.  
  210.             try {
  211.                 cash = Double.parseDouble(bill);
  212.                 System.out.println(cash);
  213.  
  214.                 if (cash == Dime || cash == Nickel || cash == Quarter || cash == OneDollar || cash == FiveDollar) {
  215.                     done = true;
  216.                 } else {
  217.                     throw new NumberFormatException();
  218.                 }
  219.             } catch (NumberFormatException e) {
  220.                 JOptionPane.showMessageDialog(null, "MACHINE ACCEPTS: $.05,$.10,$.25,$1,$5\n",
  221.                         "Error", JOptionPane.INFORMATION_MESSAGE);
  222.  
  223.             }
  224.         }
  225.  
  226.         return cash;
  227.  
  228.     }
  229.  
  230.     public static void finish() {// this is an exit method
  231.         System.exit(0);
  232.     }
  233.  
  234.     public static void collect(double price, boolean instock,double total) {// this this method calculates and 
  235.         //and validates cash input.
  236.         boolean done = false;
  237.         double Nickel = 0.05;
  238.         double Dime = 0.10;
  239.         double Quarter = 0.25;
  240.         double OneDollar = 1.0;
  241.         double FiveDollar = 5.0;
  242.         double amtToPay;
  243.         String cash = null;
  244.         double money;
  245.         int reply;
  246.  
  247.  
  248.         DecimalFormat twoDigits = new DecimalFormat("$0.00");
  249.  
  250.         amtToPay = validDollarNote(price);
  251.  
  252.         total+= price;
  253.  
  254.  
  255.         while (amtToPay < price) {
  256.  
  257.             while (!done) {
  258.  
  259.                 cash = JOptionPane.showInputDialog(null, "Not enough Cash for the purshase\n\n" +
  260.                         "Please pay " + twoDigits.format(price - amtToPay) + " to complete your transaction");
  261.  
  262.                 try {
  263.                     money = Double.parseDouble(cash);
  264.  
  265.                     System.out.println(cash);
  266.  
  267.                     if (money == Dime || money == Nickel || money == Quarter || money == OneDollar || money == FiveDollar) {
  268.                         done = true;
  269.                     } else {
  270.                         throw new NumberFormatException();
  271.                     }
  272.                 } catch (NumberFormatException e) {
  273.                     JOptionPane.showMessageDialog(null, "MACHINE ACCEPTS: $.05,$.10,$.25,$1,$5\n",
  274.                             "Error", JOptionPane.INFORMATION_MESSAGE);
  275.  
  276.                 }
  277.             }
  278.  
  279.             if (cash == null) 
  280.             {
  281.                 vencalc();;
  282.             } 
  283.             else 
  284.             {
  285.                 amtToPay += Double.parseDouble(cash);
  286.             }
  287.  
  288.         }
  289.  
  290.         if (amtToPay == price && instock) {
  291.  
  292.             reply =JOptionPane.showConfirmDialog(null, "Do you want to purchase " +
  293.                     "another item?", "Question", JOptionPane.YES_NO_OPTION);
  294.  
  295.              if (reply == 1){
  296.                 JOptionPane.showMessageDialog(null, "please pick up you item\n" + "\nTOTAL: " 
  297.                         + twoDigits.format(total) + "\nThank you");
  298.             finish();}
  299.         }
  300.  
  301.           else if (amtToPay > price && instock) {
  302.  
  303.             reply =JOptionPane.showConfirmDialog(null, "Do you want to purchase " +
  304.                     "another item?", "Question", JOptionPane.YES_NO_OPTION);
  305.             if (reply == 1){
  306.                 JOptionPane.showMessageDialog(null, "please pick up you item\n" + "\nTOTAL: "
  307.                         + twoDigits.format(total) + "\nThank you");
  308.             finish();}
  309.             }
  310.  
  311.         }
  312. }
  313.  
  314.  
  315.  
  316.  
Mar 28 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

21
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...
4
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...
7
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...
14
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)...
4
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...
2
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...
2
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...
2
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....
6
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...
8
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
0
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...
0
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...
0
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
0
BarryA
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...
0
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.