× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Java Money Counter to Count the Large Sums of Money You Wish You Had

Ugo A
Apr 21, 2017
<p><a href="http://i1.kym-cdn.com/photos/images/facebook/000/264/200/acb.jpg"><img alt="" src="http://i1.kym-cdn.com/photos/images/facebook/000/264/200/acb.jpg" style="height:337px; width:600px"></a></p> <p>A few years ago in college,  I was given a homework assignment to create a class (from scratch) that would properly separate money when a given value was entered by the end user  Money is separated into bills of $5 and $20 denominations.</p> <p>The code can be found here:  <a href="https://gist.github.com/muxor/7965307">https://gist.github.com/muxor/7965307</a> </p> <p> </p> <p>I've also listed the code below. For anyone that needs a digital money counter to count the money you dream of having, you can have some fun with this.</p> <p> </p> <pre><code> import java.util.Scanner; /** * This class will give an assortment of bills which match a given entered amount. * The addition of the product of each bill type (multiplied by its necessary amount) * will equal the total amount initially entered. */ public class ChangeCalculator { public static void main(String[] args) { //double money; Scanner yup = new Scanner(System.in); System.out.print("Please enter the amount of money: "); double money = yup.nextDouble(); if ( money &gt; 0 ){ System.out.println("the money was accepted! \n"); ChangeCalculator cc = new ChangeCalculator(); // must have an object in order to use // the 'theBills' method below double nxtAmnt; // will hold the leftover amount of money after each bill type // has been counted until it reaches zero double billValue;// the current value of the bill to be checked billValue = 20; if ( money &gt; billValue){ System.out.println("The number of $20 Bills is "); } nxtAmnt = cc.theBills(money, billValue); // the method requires the value of an initial amount // of money and the current type of bill to be calculated billValue = 10; if ( nxtAmnt &gt; billValue){ System.out.println("The number of $10 Bills is "); } nxtAmnt = cc.theBills(nxtAmnt, billValue); billValue = 5; if ( nxtAmnt &gt; billValue){ System.out.println("The number of $5 Bills is "); } nxtAmnt = cc.theBills(nxtAmnt, billValue); billValue = 1; if ( nxtAmnt &gt; billValue){ System.out.println("The number of $1 Bills is "); } nxtAmnt = cc.theBills(nxtAmnt, billValue); billValue = 0.25; if ( nxtAmnt &gt; billValue){ System.out.println("The number of quarters is "); } nxtAmnt = cc.theBills(nxtAmnt, billValue); billValue = 0.10; if ( nxtAmnt &gt; billValue){ System.out.println("The number of dimes is "); } nxtAmnt = cc.theBills(nxtAmnt, billValue); billValue = 0.05; if ( nxtAmnt &gt; billValue){ System.out.println("The number of nickels is "); } nxtAmnt = cc.theBills(nxtAmnt, billValue); billValue = 0.01; if ( nxtAmnt &gt; billValue){ System.out.println("The number of pennies is "); } nxtAmnt = cc.theBills(nxtAmnt, billValue); //System.out.println("The result is: "+ nxtAmnt); }// closes the first 'if' statement }// closees the main method public double theBills(double amnt, double curValue){ // will return value of 'amnt' for the next bill // to be processed //double amnt = 78.0; int fv;// will be used in 'for' loops int twbill; // will eventually contain the number of total twenties in this amount int loops = 2;// the limit of the for loop if ( amnt &gt; curValue){ for (fv=1; fv &lt; loops ; fv++){ double twroll = (fv*curValue);// multiple of the bill (ie $5, $20, etc) value if ((amnt/twroll) &gt;= 1 ){ // will check to see that the result of the division is greater than or equal to one ++loops; }// closes the above if statment if ( (amnt/twroll) &lt; 1 ){ // fv should be decremented and the total value created from the multiple of the given bill // should be subtracted from the 'amnt' variable --fv; // in order to have a multiple of the bill which isn't more than // original entered 'amnt' variable twroll = (fv*curValue);// have to update twroll b/c fv was updated twbill = fv; // stores the final number of this type of bill amnt = amnt-twroll; // updates the value of the remaining amount loops = fv; // will end the 'for' loop System.out.println(twbill+" and the leftover is:"+amnt+"\n"); }// closes the 'if' statement }// closes the 'for' loop }// closes the main Bill 'if' statement return amnt; }// closes the 'theBills' method }// closes the change calculator class </code></pre> <p> </p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus