Results 1 to 6 of 6
  1. #1
    incxx is offline Member
    Join Date
    Mar 2013
    Posts
    21
    Rep Power
    0

    Default Problem with creating a program that calculates money

    My program is supposed to ask the user to enter a dollar value from up to 100 dollars, and output the amount of bills and coins required to create that amount with the least change possible. I'm not completely finished, however when I test run the program and I enter values in the 60s and 90s, it doesnt seem to give me any info past the 50 dollar bill. Why is this happening? Thanks
    XML Code:
    import java.io.*;
    
    public class ChangeProgram
    {
      public static void main (String [] args) throws IOException
      {
        BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));
        
        String inputA; 
        double inputB = 0.0, input = 0.0, remainder = 0, remainderB = 0, remainderD = 0, remainderF = 0, remainderH = 0, remainderJ = 0, remainderL = 0;
        
        
        System.out.println("Please enter a dollar amount: ");
        inputA = myInput.readLine();
        input = Double.parseDouble(inputA);
        
        if (input <= 100 && input >= 50){
          double answer = input / 50;
          double fifty = Math.floor(answer);
          
          System.out.println(fifty + " $50 bill(s)");
          remainder = input - 50;
          
          if(remainder >= 20 && remainder < 50){
            double answerB = remainder / 20;
            double remainderA = Math.floor(answerB);
            
            System.out.println(remainderA + " $20 bill(s)");
            
            double twenty = remainderA * 20;
            remainderB = remainder - twenty;
          
          
          if(remainderB >= 10 && remainderB < 20){
            double answerC = remainderB / 10;
            double remainderC = Math.floor(answerC);
            
            System.out.println(remainderC + " $10 bill(s)");
            
            double ten = remainderC * 10;
            remainderD = remainderB - ten;
          
          if(remainderD >= 5 && remainderD < 10){
            double answerD = remainderD / 5;
            double remainderE = Math.floor(answerD);
            
            System.out.println(remainderE + " $5 bill(s)");
            
            double five = remainderE * 5;
            remainderF = remainderD - five;
          
          if(remainderF >= 2 && remainderF < 5){
            double answerE = remainderF / 2;
            double remainderG = Math.floor(answerE);
            
            System.out.println(remainderG + " toonie(s)");
            
            double two = remainderG * 2;
            remainderH = remainderF - two;
          
          if(remainderH >= 1 && remainderH < 2){
            double answerF = remainderH / 1;
            double remainderI = Math.floor(answerF);
            
            System.out.println(remainderI + " loonie(s)");
            
            
            double one = remainderI * 1;
            remainderJ = remainderH - one;
            System.out.println(remainderJ);
          
          if(remainderJ >= 1/4 && remainderJ < 1){
            double answerG = remainderJ / 1/4;
            double remainderK = Math.floor(answerG);
            
            System.out.println(remainderK + " quarter(s)");
            
            double quarter = remainderK * 1/4;
            remainderL = remainderJ - quarter;
          }
          }
        }
       }
      }
     }
          
    }
        
    }
      
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,389
    Rep Power
    5

    Default Re: Problem with creating a program that calculates money

    All of your if statements are nested. That won't work. What happens if you enter 20? The first test for 50 fails so the test for 20 never gets executed.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    incxx is offline Member
    Join Date
    Mar 2013
    Posts
    21
    Rep Power
    0

    Default Re: Problem with creating a program that calculates money

    Hi Jim,
    I removed the nested loops, however, it is still skipping over certain statements. Why is this? Thanks

    XML Code:
    import java.io.*;
    
    public class ChangeProgram
    {
      public static void main (String [] args) throws IOException
      {
        BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));
        
        String inputA; 
        double inputB = 0.0, input = 0.0, remainder = 0, remainderB = 0, remainderD = 0, remainderF = 0, remainderH = 0, remainderJ = 0, remainderL = 0;
        
        
        System.out.println("Please enter a dollar amount: ");
        inputA = myInput.readLine();
        input = Double.parseDouble(inputA);
        
        if (input >= 50 && input < 100){
          double answer = input / 50;
          double fifty = Math.floor(answer);
          
          System.out.println(fifty + " $50 bill(s)");
          remainder = input - 50;
        }
          
          if(remainder >= 20 && remainder < 50){
            double answerB = remainder / 20;
            double remainderA = Math.floor(answerB);
            
            System.out.println(remainderA + " $20 bill(s)");
            
            double twenty = remainderA * 20;
            remainderB = remainder - twenty;
          }
          
          
           if(remainderB >= 10 && remainderB < 20){
            double answerC = remainderB / 10;
            double remainderC = Math.floor(answerC);
            
            System.out.println(remainderC + " $10 bill(s)");
            
            double ten = remainderC * 10;
            remainderD = remainderB - ten;
           }
          
            if(remainderD >= 5 && remainderD < 10){
            double answerD = remainderD / 5;
            double remainderE = Math.floor(answerD);
            
            System.out.println(remainderE + " $5 bill(s)");
            
            double five = remainderE * 5;
            remainderF = remainderD - five;
            }
          
           if(remainderF >= 2 && remainderF < 5){
            double answerE = remainderF / 2;
            double remainderG = Math.floor(answerE);
            
            System.out.println(remainderG + " toonie(s)");
            
            double two = remainderG * 2;
            remainderH = remainderF - two;
           }
          
          if(remainderH >= 1 && remainderH < 2){
            double answerF = remainderH / 1;
            double remainderI = Math.floor(answerF);
            
            System.out.println(remainderI + " loonie(s)");
            
            
            double one = remainderI * 1;
            remainderJ = remainderH - one;
            System.out.println(remainderJ);
         }
          
       
       
          
    
        
    }
      
    }

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,389
    Rep Power
    5

    Default Re: Problem with creating a program that calculates money

    Look at this section of code.

    Java Code:
    if (remainder >= 20 && remainder < 50) {
             double answerB = remainder / 20;
             double remainderA = Math.floor(answerB);
    
             System.out.println(remainderA + " $20 bill(s)");
    
             double twenty = remainderA * 20;
             remainderB = remainder - twenty;
          }
    
          if (remainderB >= 10 && remainderB < 20) {
             double answerC = remainderB / 10;
             double remainderC = Math.floor(answerC);
    
             System.out.println(remainderC + " $10 bill(s)");
    
             double ten = remainderC * 10; 
             remainderD = remainderB - ten;
          }
    Say you enter 76 as a value.
    You already computed at least one $50.
    It gets to the $20 and figure it needs one $20. So you have $6 left. That means the test for $10 to $19 will fail. But that is where you set the remainderD. Now when you check for $5 to $9, it will fail because remainderD is 0.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    incxx is offline Member
    Join Date
    Mar 2013
    Posts
    21
    Rep Power
    0

    Default Re: Problem with creating a program that calculates money

    I understand what your saying, I played around with the code a bit and got it to work with numbers 50 and above but not below. Does it have something to do with th eif statements?
    XML Code:
    import java.io.*;
    
    public class ChangeProgram
    {
      public static void main (String [] args) throws IOException
      {
        BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));
        
        String inputA; 
        double inputB = 0.0, input = 0.0, remainder = 0, remainderB = 0, remainderD = 0, remainderF = 0, remainderH = 0, remainderJ = 0, remainderL = 0, remainderN = 0, remainderP = 0, remainderR = 0;
        
        
        System.out.println("Please enter a dollar amount: ");
        inputA = myInput.readLine();
        input = Double.parseDouble(inputA);
        
        
        if (input >= 50 || input < 100){
          double answer = input / 50;
          double fifty = Math.floor(answer);
          
          System.out.println(fifty + " $50 bill(s)");
          remainder = input - 50;
        }
          
          if(remainder >= 20 || remainder < 50){
            double answerB = remainder / 20;
            double remainderA = Math.floor(answerB);
            
            System.out.println(remainderA + " $20 bill(s)");
            
            double twenty = remainderA * 20;
            remainderB = remainder - twenty;
          }
          
          
           if(remainderB >= 10 || remainderB < 20){
            double answerC = remainderB / 10;
            double remainderC = Math.floor(answerC);
            
            System.out.println(remainderC + " $10 bill(s)");
            
            double ten = remainderC * 10;
            remainderD = remainderB - ten;
           }
          
           if(remainderD >= 5 || remainderD < 10){
            double answerD = remainderD / 5;
            double remainderE = Math.floor(answerD);
            
            System.out.println(remainderE + " $5 bill(s)");
            
            double five = remainderE * 5;
            remainderF = remainderD - five;
            }
          
           if(remainderF >= 2 || remainderF < 5){
            double answerE = remainderF / 2;
            double remainderG = Math.floor(answerE);
            
            System.out.println(remainderG + " toonie(s)");
            
            double two = remainderG * 2;
            remainderH = remainderF - two;
           }
          
         if(remainderH >= 1 || remainderH < 2){
            double answerF = remainderH / 1;
            double remainderI = Math.floor(answerF);
            
            System.out.println(remainderI + " loonie(s)");
            
            
            double one = remainderI * 1;
            remainderJ = remainderH - one;
            
         }
          
         if (remainderJ >= 0.25 || remainderJ < 1){
            double answerG = remainderJ / 0.25;
            double remainderK = Math.floor(answerG);
            
            System.out.println(remainderK + " quarter(s)");
            
            double quarter = remainderK * 0.25;
            remainderL = remainderJ - quarter;
          
          }
          
        
        if(remainderL >= 0.10 || remainderL < 0.25){
            double answerH = remainderL / 0.10;
            double remainderM = Math.floor(answerH);
          
            System.out.println(remainderM + " dime(s)");
          
            double dime = remainderM * 0.10;
            remainderN = remainderL - dime;
              }
          
          if(remainderN >= 0.05 || remainderN < 0.10){
            double answerI = remainderN / 0.10;
            double remainderO = Math.floor(answerI);
          
            System.out.println(remainderO + " nickel(s)");
          
            double nickel = remainderO * 0.10;
            remainderP = remainderN - nickel;
              }
          
         if (remainderP >= 0.01 || remainderP < 0.05){
            double answerJ = remainderP / 0.01;
            double remainderQ = Math.floor(answerJ);
          
            System.out.println(remainderQ + " penny(ies)");
          
            double penny = remainderQ * 0.01;
            remainderR = remainderP - penny;
              }
        
        
       
       
          
    
        
    }
      
    }

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,389
    Rep Power
    5

    Default Re: Problem with creating a program that calculates money

    You don't need separate remainders for you values. Just figure out how many of the largest denomination you can use. Then, using what is left over, do the same for the next highest denomination. When you have a count of a certain denomination which is zero, ignore it.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 0
    Last Post: 05-16-2013, 07:25 PM
  2. Replies: 14
    Last Post: 11-06-2012, 09:56 AM
  3. Replies: 12
    Last Post: 08-04-2012, 09:35 PM
  4. program ,deposit of an amount of money in a bank
    By aou in forum Advanced Java
    Replies: 1
    Last Post: 08-03-2012, 09:41 PM
  5. Work & Earn money from home, use rebate lower price save money $$$
    By arturmoniswork in forum Reviews / Advertising
    Replies: 0
    Last Post: 12-30-2008, 05:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •