Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Improving the code to produce the same program

  1. #1
    Member
    Join Date
    Apr 2011
    Posts
    32
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Default 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

     
    /**
    *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!!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Improving the code to produce the same program

    You might want to look into the modulus operator.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    u-will-neva-no (April 8th, 2011)

  4. #3
    Member
    Join Date
    Apr 2011
    Posts
    32
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Default Re: Improving the code to produce the same program

    how exactly would that help?

  5. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. The Following User Says Thank You to KevinWorkman For This Useful Post:

    u-will-neva-no (April 8th, 2011)

  7. #5
    Member
    Join Date
    Apr 2011
    Posts
    32
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Default 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?

  8. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

  9. The Following User Says Thank You to helloworld922 For This Useful Post:

    u-will-neva-no (April 8th, 2011)

  10. #7
    Member
    Join Date
    Apr 2011
    Posts
    32
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Default 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!

  11. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Improving the code to produce the same program

    Quote Originally Posted by u-will-neva-no View Post
    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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. The Following User Says Thank You to KevinWorkman For This Useful Post:

    u-will-neva-no (April 8th, 2011)

  13. #9
    Member
    Join Date
    Apr 2011
    Posts
    32
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Default 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)

    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

  14. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  15. The Following User Says Thank You to KevinWorkman For This Useful Post:

    u-will-neva-no (April 8th, 2011)

  16. #11
    Member
    Join Date
    Apr 2011
    Posts
    32
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Default 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...
     
    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"...

  17. #12
    Member
    Join Date
    Apr 2011
    Posts
    32
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Default Re: Improving the code to produce the same program

    ok I solved all of my issues...any advice in improving it further?

     
    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

  18. #13
    Member
    Join Date
    Apr 2011
    Posts
    32
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Default 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!

  19. #14
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

  20. The Following User Says Thank You to helloworld922 For This Useful Post:

    u-will-neva-no (April 9th, 2011)

Similar Threads

  1. [SOLVED] Listen to two JRadioButton + ons JButton to produce output
    By voltaire in forum AWT / Java Swing
    Replies: 1
    Last Post: May 12th, 2010, 03:46 PM
  2. HELP with Java Paint Program Code
    By CheekySpoon in forum AWT / Java Swing
    Replies: 1
    Last Post: April 5th, 2010, 12:03 PM
  3. Jigloo help to produce a text area which only scrolls down
    By rtumatt in forum AWT / Java Swing
    Replies: 0
    Last Post: February 2nd, 2010, 06:09 PM
  4. Convert Java Program to Java ME code
    By rinchan11 in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: October 5th, 2009, 10:18 PM
  5. Cracking password in less number of Trials using brute forcea
    By xisstar in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 10th, 2009, 10:40 AM