Click to See Complete Forum and Search --> : Please help me!
I need to create a simple console java application that prompts a user for a dollar amount and then calculates the minimum number of pennies, nickels, dimes, quarters, dollar bills, five dollar bills, ten dollar bills, and twenty dollar bills that are needed to make up that amount.
If there is no quantity for a denomonation I don't need to display it but if there is I do. Also, I'd like the program to loop until the user indicates they are done.
Can someone please help me. I'm new at this and lost
chazzy
05-18-2006, 12:24 PM
what do you have so far? i would imagine you need to take the modulus of the input from highest denomination to lowest to determine breakdown.
Truthfully I don't have anything at this time. I am very new to this and only know a few things, like (int =) and System.out.println ("").
I'm guessing that this would be done as some sort of division function in a loop until there is no longer a remainder. This is where I'm at. How do I do it and is my thinking correct? I was hoping someone with experience could get me off in the right direction.
agent_x91
05-18-2006, 04:27 PM
I'm confused about what you want the program to do. Do you mean you want it to calculate the coins which you would use to reach the amount entered, with the fewest amount of coins?
The way it should work is if I enter say 54.31 I should get a response the it takes:
20's = 2
10's = 1
1's = 4
Quarters = 1
Nickles = 1
Pennies = 1
That would be the minimum monetary denomination for that amount. If the input was say $5 the the print out would simply be Five = 1. Does that make sense?
agent_x91
05-18-2006, 04:46 PM
Ok, I'm from england so I don't actually know how the american coinage system works, but I think I understand where you're heading.
My way of dealing with this task would be to store the numerical values of each coin in a separate variable, then loop through each coin, subtracting the value from the total value each time, and incrementing the number of coins used each time, until the total value is 0. (Of course checking each time that the coin value is not greater than the total value)
For example:
double total_value=70.0;
int twenties=20, int tens=10;
int n_twen=0,n_ten=0;
while(total_value>=twenties)
{
total_value-=twenties;
n_twen++;
}
while(total_value>=tens)
{
total_value-=tens;
n_ten++;
}
//etc.
In the case above, n_twen would equal 3, and n_ten would equal 1. Make sense?
agent_x91
05-18-2006, 04:54 PM
No problem. And If you want it to be slightly tidier, you could put them into an array instead, and do this:
int[] coin_values={20,10}; //array of coin values
int[] coin_nums={0,0}; //array of number of coins used for this value
double total_value=70.0;
for(int i=0;i<coin_values.length;i++)
{
while(total_value>=coin_values[i])
{
total_value-=coin_values[i];
coin_nums[i]++;
}
}
How can I allow the user to enter another value and have it run back through the program or press a key, any key would be fine, or yes and no would work too to exit?
Would someone please add it to my code where it goes so it works?
/*
* Main.java
*
* Created on May 18, 2006, 12:09 PM
*
*This program allows the user to enter a numeric value
*and have it break it into its individual denomonations.
*/
package countingcoins;
import java.io.*;
/**
*
* @author Ken Breegle
*/
public class Main {
/** Creates a new instance of Main */
public Main() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader (System.in));
// Ask the user for an input.
System.out.println ("Please enter an amount: ");
String input = in.readLine();
double cash = Double.parseDouble(input);
System.out.println(); //Adds a blank line for appearance.
System.out.println("You've entered $" + cash + " which breaks down into the following:");
System.out.println(); //Adds a blank line for appearance.
cash = cash * 100; //Multiply input by 100 to make the input an integer.
{
//Set the value of money.
int twenty = 0;
int ten = 0;
int five = 0;
int one = 0;
int penny = 0;
int nickle = 0;
int dime = 0;
int quarter = 0;
while (cash >= 2000) // do this while cash is greater than or equal to 2000.
{
cash = cash - 2000; // take off 2000 from total
twenty = twenty + 1; // indicate that we used up "a twenty dollar bill"
}
while (cash >= 1000) // do this while cash is greater than or equal to 1000.
{
cash = cash - 1000; // take off 1000 from total
ten = ten + 1; // indicate that we used up "a ten dollar bill"
}
while (cash >= 500) // do this while cash is greater than or equal to 500.
{
cash = cash - 500; // take off 500 from total
five = five + 1; // indicate that we used up "a five dollar bill"
}
while (cash >= 100) // do this while cash is greater than or equal to 100.
{
cash = cash - 100; // take off 100 from total
one = one + 1; // indicate that we used up "a one dollar bill"
}
while (cash >= 25) // do this while cash is greater than or equal to 25.
{
cash = cash - 25; // take off 25 from total
quarter = quarter + 1; // indicate that we used up "a quater"
}
while (cash >= 10) // do this while cash is greater than or equal to 10.
{
cash = cash - 10; // take off 10 from total
dime = dime + 1; // indicate that we used up "a dime"
}
while (cash >= 5) // do this while cash is greater than or equal to 5.
{
cash = cash - 5; // take off 5 from total
nickle = nickle + 1; // indicate that we used up "a nickle"
}
while (cash >= 1) // do this while cash is greater than or equal to 1.
{
cash = cash - 1; // take off 1 from total
penny = penny + 1; // indicate that we used up "a penny"
}
// Print out the results.
if (twenty != 0) // if we used some twenties
{
System.out.println("Number of Twenty Dollar Bills: " + twenty);
}
if (ten != 0) // if we used some tens
{
System.out.println("Number of Ten Dollar Bills: " + ten);
}
if (five != 0) // if we used some fives
{
System.out.println("Number of Five Dollar Bills: " + five);
}
if (one != 0) // if we used some ones
{
System.out.println("Number of One Dollar Bills: " + one);
}
if (quarter != 0) // if we used some quarters
{
System.out.println("Number of Quarters: " + quarter);
}
if (dime != 0) // if we used some dimes
{
System.out.println("Dimes: " + dime);
}
if (nickle != 0) // if we used some nickles
{
System.out.println("Nickles: " + nickle);
}
if (penny != 0) // if we used some pennies
{
System.out.println("Pennies: " + penny);
}
//Ask the user if they would like to enter another value.
System.out.println("");
System.out.println("");
System.out.println("Please enter another amount or press <ENTER> to exit: ");
}
}
}
Never mind, I got it to work.