By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,543 Members | 2,037 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,543 IT Pros & Developers. It's quick & easy.

validation of code

P: 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
Share this Question
Share on Google+
4 Replies


100+
P: 539
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

100+
P: 539
Dont forget to inclose your code with codetags.... ;-)

just reminding
sukatoa...
Mar 26 '08 #3

Expert 10K+
P: 11,448
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

P: 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

Post your reply

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