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