Improving the code to produce the same program
Hello everyone, I have made a program that is to act like a cash machine. While it is unrealistic, it works...I was wondering if anyone would like to comment on how to improve my method for printing out the notes to the screen.. ok here is the code, run it to understand what im talking about XD
Code Java:
/**
*Program that simulates a cash machine by asking the user how much money he/she wants and prints
*the value of the notes to be given to the user. (This assumes uk banknotes) This cash machine only
gives out £5,£10,£20,and £50 and assumes an infinite supply of notes and prints to the nearest
note value.
**/
import java.util.Scanner;
public class cashMachine
{
public static void main(String args[])
{
Scanner input = new Scanner (System.in);
System.out.println("How much money would you like to withdraw? ");
System.out.println("Also, enter your number ending with 0 or 5 otherwise you will loose money :)");
int n = input.nextInt();
cash(n);
}//end main
public static int cash(int k)
{
int total = k;
//lots of if statements but goes through every scenario
if(total>=5)
{
while(total>=5 && total <=45)
{
total = k-5;
System.out.println("$5");
k -= 5;
}//end while
if( total>=50)
{
while(k>=50)
{
total = k-50;
System.out.println("$50");
k -= 50;
}//end while
}//end if
if(total>=20)
{
while(total>=20)
{
total = k-20;
System.out.println("$20");
k -= 20;
}//end while
}//end if
if(total>=10)
{
while(total>=10)
{
total = k-10;
System.out.println("$10");
k -= 10;
}//end while
}//end if
if(total>=5)
{
while(total>=5)
{
total = k-5;
System.out.println("$5");
k -= 5;
}//end while
}//end if
}//end main if
return total;
}//end method
}//end class
As you can see, the method is horrible! Im sure there are even a few bugs on certain numbers although it seemed ok for the numbers I tested..Anyways if anyone has a better suggestion to my current method it will be greatly appreciated!!
Re: Improving the code to produce the same program
You might want to look into the modulus operator.
Re: Improving the code to produce the same program
how exactly would that help?
Re: Improving the code to produce the same program
You could accomplish your goal with basic division and modulus instead of a series of while loops. This is a pretty common algorithm.
As for how you have it, if I request 45, isn't it going to give me a bunch of 5s? That doesn't seem right, does it?
Re: Improving the code to produce the same program
Yes i see your point, for £45 you would rather have £20,£20,£5...So i only need division and modulus...not even a for loop or any loop for that matter?
Re: Improving the code to produce the same program
Yep, no loops should be required (luckily, most monetary systems aren't NP-hard, which can't be said for the general case). In addition to division and modulus, you'll also need addition/subtraction.
Re: Improving the code to produce the same program
While I have still not found the solution, i would just like to thank you two for helping me!
Re: Improving the code to produce the same program
Quote:
Originally Posted by
u-will-neva-no
While I have still not found the solution, i would just like to thank you two for helping me!
Hint: What happens when you divide 45 by 20 and cast it to an int? How many 20s do you want to give out? How much do you have left over? What happens when you use the modulus operator?
Re: Improving the code to produce the same program
oh I see... I have been able to simplify it alot more. However, I and using one for loop...my other issue is that when i enter a number such as $20, my output when compiled is $20, $0 because i am printing both the £20 and my modulus...let me put the code below because even that made no sence to me! (im just putting the method below)
Code Java:
public static int cash(int k)
{
//variables
int mod;
int total;
total =k/20;
mod = k%20;
for(int i=0; i<total; i++)
{
System.out.println("$20");
}
System.out.println("$" +mod);
return total;
}//end method
Re: Improving the code to produce the same program
Well, you're only using the for loop to print out multiple copies of the same value, which is fine if that's what you want to print out.
Why are you printing out the result of the modulus in that loop?
Hint: The result of the modulus is what's leftover after taking out the twenties. Don't you want to split that up into tens and fives? What happens if k is 55?
Re: Improving the code to produce the same program
I found a temporary solution to splitting up the remainder of $15...just using an if else statement...
Code Java:
public static int cash(int k)
{
//variables
int mod;
int total;
total =k/20;
mod = k%20;
for(int i=0; i<total; i++)
{
System.out.println("$20");
}
if(mod==15)
{
System.out.println("$10" + "\n" +"$5");
}
else
System.out.println("$" +mod);
return total;
now I just need to fix the problem of printing "$0"...
Re: Improving the code to produce the same program
ok I solved all of my issues...any advice in improving it further?
Code Java:
public static int cash(int k)
{
//variables
int mod;
int total;
total =k/20;
mod = k%20;
for(int i=0; i<total; i++)
{
System.out.println("$20");
}
if(mod==15)
{
System.out.println("$10" + "\n" +"$5");
}
else
if(mod==0)
{
System.out.println("");
}
else
System.out.println("$" +mod);
return total;
}//end method
Re: Improving the code to produce the same program
Ok I found issues with my previous code..I have resorted back to my while loops but have integrated the modulus, which solves the issue of $45 printing all $5...Im happy with my code, its does what it should do...I would really like to know your method for this just because I like to know different approaches...anyways thankyou for your help!
Re: Improving the code to produce the same program
The greedy method (using modulo/division):
1. Start with the current denomination = largest denomination available
2. amount of current denomination = amount / current denomination (note: using integer math. Can also use Math.floor() for floating point math)
3. Remaining amount = amount % current denomination
4. Move to next largest denomination. Repeat 2-4 until amount is zero, or there are no smaller denominations.