I decided to write an ATM program in my favourite programming language, C#.
Tested with a few numbers and I am confident it will give correct output for any valid input:
class Program
{
const int MIN_WITHDRAW = 20;
const int MAX_WITHDRAW = 1000;
const int INVALID_WITHDRAW = 30;
const int TWENTIES = 0;
const int FIFTIES = 1;
static void Main(string[] args)
{
int withdrawAmount = 0;
int[] notesGiven = { 0, 0 };
Console.Write("Please enter the amount you would like to withdraw: ");
int.TryParse(Console.ReadLine(), out withdrawAmount);
if (withdrawAmount >= MIN_WITHDRAW &&
withdrawAmount <= MAX_WITHDRAW &&
withdrawAmount != INVALID_WITHDRAW &&
IsMultipleOfTen(withdrawAmount))
{
notesGiven[FIFTIES] += withdrawAmount / 50;
withdrawAmount -= notesGiven[FIFTIES] * 50;
notesGiven[TWENTIES] += withdrawAmount / 20;
withdrawAmount -= notesGiven[TWENTIES] * 20;
if (withdrawAmount == 10)
{
notesGiven[FIFTIES]--;
withdrawAmount += 50;
notesGiven[TWENTIES] += withdrawAmount / 20;
withdrawAmount -= notesGiven[TWENTIES] * 20;
}
Console.WriteLine("Operation complete. You were given: {0} $50 notes and {1} $20 notes",
notesGiven[FIFTIES], notesGiven[TWENTIES]);
Console.Write("Press any key to exit");
Console.ReadKey();
}
else
{
Console.WriteLine("Please enter a valid number");
Console.Write("Press any key to exit");
Console.ReadKey();
}
}
static bool IsMultipleOfTen(int amount)
{
return (amount % 10) == 0;
}
}