I can't get my code (below) to execute the exceptions!
1- First, the bank account is empty.
2- 1500 deposit is made
3- Attempt to withdraw 1050 but the overdraft limit is 1000
4- Since the the amount to withdraw is greater than the overdraft limit the OverdraftEx exception should display my message (but it is not).
5- Then an attempt to withdraw 1000 which should be successful and the balance should become 500
6- Now, an attempt to withdraw 50 but second exception message DailyLimitEx should display which is exceeding the daily limit, because the Daily withdraw limit is 1000 and it has already been withdrawn for the day, again this exception is not getting displayed.
7- Then, End of the Day will display which is resetting the DailyLimit back to 0.
8- Then an attempt to withdraw 50 is allowed because it is a new day.
I can't get the exceptions' messages to display.
Can you please help? at the end there is a sample output.
Thanks
Sample output:
Note: the first error message should display: "Sorry, the amount you are trying to withdraw exceeds your allowed overdraft limit" not what is displayed below.
1- First, the bank account is empty.
2- 1500 deposit is made
3- Attempt to withdraw 1050 but the overdraft limit is 1000
4- Since the the amount to withdraw is greater than the overdraft limit the OverdraftEx exception should display my message (but it is not).
5- Then an attempt to withdraw 1000 which should be successful and the balance should become 500
6- Now, an attempt to withdraw 50 but second exception message DailyLimitEx should display which is exceeding the daily limit, because the Daily withdraw limit is 1000 and it has already been withdrawn for the day, again this exception is not getting displayed.
7- Then, End of the Day will display which is resetting the DailyLimit back to 0.
8- Then an attempt to withdraw 50 is allowed because it is a new day.
I can't get the exceptions' messages to display.
Can you please help? at the end there is a sample output.
Thanks
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
Account account = new Account();
try
{
double overdraftLimit = 1000;
double dailyWithdrawlLimit = 1000;
}
catch (OverdraftEx e1)
{
Console.WriteLine(e1.Message);
}
catch (DailyLimitEx e2)
{
Console.WriteLine(e2.Message);
}
account.display();
account.deposit(1500);
account.withdraw(1050);
account.withdraw(1000);
Console.WriteLine("-------------------");
account.withdraw(50);
account.endDay();
account.withdraw(50);
Console.ReadLine();
return;
}
}
class OverdraftEx : Exception
{
private static string msg = " Sorry, the amount you are trying to withdraw exceeds your allowed overdraft limit.";
public OverdraftEx() : base(msg)
{
}
}
class DailyLimitEx : Exception
{
private static string msg1 = " Sorry, you have exceeded your allowed daily withdrawal limit.";
public DailyLimitEx() : base(msg1)
{
}
}
class Account
{
public int accountID { get; set; }
public double amount;
public double balance;
public double Balance
{
get
{
return balance;
}
set
{
if (amount > overdraftLimit)
{
OverdraftEx ex1 = new OverdraftEx();
throw ex1;
}
}
}
public double overdraftLimit { get; set; }
public double dailyWithdrawlLimit
{
get
{
return dailyWithdrawlLimit;
}
set
{
if (amount > dailyWithdrawlLimit - 1000)
{
DailyLimitEx ex2 = new DailyLimitEx();
throw ex2;
}
}
}
public Account()
{
accountID = 10;
balance = 0;
return;
}
public Account(int accID, double accBalance)
{
accountID = accID;
balance = accBalance;
return;
}
public void withdraw(double amount)
{
Console.WriteLine("");
Console.WriteLine(" Attempting to withdraw $" + Math.Round(amount, 2));
balance = balance - amount;
Console.WriteLine(" Account #: " + accountID);
Console.WriteLine(" Your new balance is $" + Math.Round(balance, 2));
}
public void deposit(double amount)
{
// calculating the balance after depositing money
if (amount > 0)
{
Console.WriteLine(" Depositing $" + Math.Round(amount, 2));
balance = balance + amount;
Console.WriteLine(" Account #: " + accountID);
Console.WriteLine(" Balance: $" + Math.Round(balance, 2));
}
else
Console.WriteLine(" Sorry, amount is invalid");
Console.WriteLine("");
return;
}
public void display()
{
Console.WriteLine("");
Console.WriteLine(" Account ID: " + accountID);
Console.WriteLine(" Balance: $" + Math.Round(balance, 2));
Console.WriteLine("");
return;
}
public void endDay()
{
Console.WriteLine("");
Console.WriteLine(" End Day");
Console.WriteLine(" Account ID: " + accountID);
Console.WriteLine(" Balance: $" + Math.Round(balance, 2));
return;
}
}
}
Note: the first error message should display: "Sorry, the amount you are trying to withdraw exceeds your allowed overdraft limit" not what is displayed below.
Last edited by a moderator: