In the code below, which does work, I would like to handle 5 different exceptions that could be created by user input.
I understand I should use an IF
statement to handle these exceptions but the requirement is to handle the errors with exception handlers. So please I am only looking for input in doing that and not alternatives.
I would like to handle them with exception handlers.
The problem I am having is where to put the exception handling code.
Also being I have 5 exceptions I want to check for does that mean I need 5 different try/catch
blocks or can I handle them all in the same block?
The exceptions I am looking for are, trying to create more than 19 accounts, trying to create an account with an initial balance below $300
, withdrawing more than the current balance from an account, attempting a transaction on an account that hasn't been created and entering anything other than a number in the TextBox
.
So if a user makes one of these errors I would like to throw the error and display a message to the user of the error they have made.
Any assistance is greatly appreciated.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MoreRobustBankGUI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int _nextIndex = 0;
List<Account> accounts = new List<Account>();
decimal balance = 0;
private void createButton1_Click(object sender, EventArgs e)
{
if (accounts.Count < 19 && balance > 300)
{
_nextIndex++;
int accountId = _nextIndex;
decimal.TryParse(amountTextBox2.Text, out balance);
transactionLabel3.Text = "Account: #" + accountId + " created with a starting balance of $" + balance;
accountTextBox1.Text = "" + accountId;
accounts.Add(new Account(balance)
{
AccountId = accountId
});
}
else
{
transactionLabel3.Text = "Can only create up to 19 accounts and starting balance must be $300";
}
}
private void executeButton2_Click(object sender, EventArgs e)
{
decimal amount = 0;
int accountID;
string textAmount = amountTextBox2.Text == "" ? "0" : amountTextBox2.Text;
if (depositRadioButton3.Checked == true)
{
if (string.IsNullOrEmpty(accountTextBox1.Text)) return;
bool accountCanBeConverted = int.TryParse(accountTextBox1?.Text, out accountID);
bool ammountCanBeConverted = decimal.TryParse(amountTextBox2?.Text, out amount);
if (accountCanBeConverted && ammountCanBeConverted && amount > 0)
{
var selectedAccount = GetAccount(accountID);
selectedAccount.DepositFunds(amount);
transactionLabel3.Text = $"Account: #{selectedAccount.AccountId} You made a deposit of ${amount}";
}
}
else if (withdrawRadioButton2.Checked == true)
{
if (string.IsNullOrEmpty(accountTextBox1.Text)) return;
bool accountCanBeConverted = int.TryParse(accountTextBox1?.Text, out accountID);
bool ammountCanBeConverted = decimal.TryParse(amountTextBox2?.Text, out amount);
if (accountCanBeConverted && ammountCanBeConverted && amount > 0)
{
var selectedAccount = GetAccount(accountID);
if (selectedAccount.HasAvailableFunds)
{
selectedAccount.WithdrawFromAccount(amount);
transactionLabel3.Text = $"Account: #{selectedAccount.AccountId} You made a withdrawal of ${amount}";
}
else
{
transactionLabel3.Text = $"Account: #{selectedAccount.AccountId} Does not have available funds to withdraw";
}
}
}
else if (balanceRadioButton3.Checked == true)
{
if (string.IsNullOrEmpty(accountTextBox1.Text)) return;
bool accountCanBeConverted = int.TryParse(accountTextBox1?.Text, out accountID);
var selectedAccount = GetAccount(accountID);
var balance = selectedAccount.GetAvailableBalanceForAccount(accountID);
if (balance == -1234567890)
{
transactionLabel3.Text = $"Invalid account number passed.";
}
else
{
transactionLabel3.Text = $"Account: #{selectedAccount.AccountId} Balance: $ {selectedAccount.GetAvailableBalanceForAccount(accountID)}";
}
}
clearFields();
}
public void clearFields()
{
amountTextBox2.Text = "";
}
public Account GetAccount(int id)
{
return accounts.Where(x => x.AccountId == id).FirstOrDefault();
}
public class Account
{
public Account(decimal balance)
{
Balance = balance;
}
public int AccountId { get; set; }
public decimal Balance { get; set; }
public void WithdrawFromAccount(decimal deductionAmount)
{
Balance -= deductionAmount;
}
public void DepositFunds(decimal depositAmount)
{
Balance += depositAmount;
}
public bool HasAvailableFunds => Balance > 0;
public decimal GetAvailableBalanceForAccount(int accountId)
{
if (accountId == AccountId)
{
return Balance;
}
else
{
return -1234567890;
}
}
}
}
}