validation of code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • soty
    New Member
    • Mar 2008
    • 25

    validation of code

    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.sho wInputDialog("P ay:");

    try {
    cash = Double.parseDou ble(bill);
    System.out.prin tln(cash);

    if (cash != Dime || cash != Nickel || cash != Quarter || cash != OneDollar || cash != FiveDollar) {
    throw new NumberFormatExc eption();
    }
    else
    {
    done = true;
    }
    } catch (NumberFormatEx ception e) {
    JOptionPane.sho wMessageDialog( null, "MACHINE ACCEPTS: $.05,$.10,$.25, $1,$5\n",
    "Error", JOptionPane.INF ORMATION_MESSAG E);

    }
    }

    return cash;

    }
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    Originally posted by soty
    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.sho wInputDialog("P ay:");

    try {
    cash = Double.parseDou ble(bill);
    System.out.prin tln(cash);

    if (cash != Dime || cash != Nickel || cash != Quarter || cash != OneDollar || cash != FiveDollar) {
    throw new NumberFormatExc eption();
    }
    else
    {
    done = true;
    }
    } catch (NumberFormatEx ception e) {
    JOptionPane.sho wMessageDialog( null, "MACHINE ACCEPTS: $.05,$.10,$.25, $1,$5\n",
    "Error", JOptionPane.INF ORMATION_MESSAG E);

    }
    }

    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,

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

    Comment

    • sukatoa
      Contributor
      • Nov 2007
      • 539

      #3
      Dont forget to inclose your code with codetags.... ;-)

      just reminding
      sukatoa...

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by soty
        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:

        [code=java]
        int Nickel = 5;
        int Dime = 10;
        int Quarter = 25;
        int OneDollar = 100;
        int FiveDollar = 500;
        [/code]

        i.e. the monetary unit is the penny/cent instead of the dollar.

        kind regards,

        Jos

        Comment

        • soty
          New Member
          • Mar 2008
          • 25

          #5
          hi

          av finished the code but i'm having one slight problem.....eit her 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

          Code:
          
          /*
           *  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();}
                      }
          
                  }
          }

          Comment

          Working...