This is purely testing my skills; I am not actually coding for a bank. This is part of a bank account class.
public void withDraw(double amount)
{
if(balance - amount >= odLimit)
{
balance -= amount;
}
else
{
Console.WriteLine("Error. Withdrawing will take you below overdraft limit. Enter a new amount: ");
double amount2;
while (!double.TryParse(Console.ReadLine(), out amount2) || (balance - amount2 < odLimit))
{
Console.WriteLine("Enter a valid number: ");
}
balance -= amount2;
}
}
odLimit
is the overdraft limit. The code checks if the balance - amount is greater than or equal to the odLimit
(say -900), and if so, it simply takes that money from the account. If that's not the case then it keeps asking the user to enter an amount they wish to withdraw, and when they enter a value that won't take the account below the overdraft limit and is a double.
Just checking that this is the most efficient way to do this. I prefer minimum lines of code.
Also in the main program class. Would you find it more efficient to create a menu function and call the withDraw
function from the menu using a switch statement within the menu, or call a method in the program class from the switch statement that asks the user for their value, and then calls the withDraw
function in the bank account class?
Decimal
instead ofdouble
. \$\endgroup\$Withdraw
. \$\endgroup\$