Question: C#: I am getting an error on line 5 6 with the ListAccounts ( ) Constructor. Why am I getting a thrown execption. Code Below:

C#: I am getting an error on line 56 with the ListAccounts() Constructor. Why am I getting a thrown execption. Code Below: public class Bank
{
public string Name { get; set; }
public BankAccount[] AccountList { get; set; }
public int NextOpenIndex { get; set; }
public Bank(string bankName, int maxNumAccounts)
{
Name = bankName;
AccountList = new BankAccount[maxNumAccounts];
NextOpenIndex =0;
}
public Bank(string bankName, BankAccount[] bankAccounts)
{
Name = bankName;
AccountList = bankAccounts;
NextOpenIndex = bankAccounts.Length;
}
public bool AddAccount(BankAccount account)
{
if (NextOpenIndex < AccountList.Length && !AccountList.Contains(account))
{
AccountList[NextOpenIndex]= account;
NextOpenIndex++;
return true;
}
else
{
Console.WriteLine("Cannot add account. Either the bank is full or the account already exists.");
return false;
}
}
public bool Transfer(BankAccount accTransferFrom, BankAccount accTransferTo, double amount)
{
if (accTransferFrom.Balance >= amount)
{
accTransferFrom.Withdraw(amount);
accTransferTo.Deposit(amount);
return true;
}
else
{
Console.WriteLine("There are insufficient funds in account.");
return false;
}
}
public void ListAllAccounts()
{
Console.WriteLine($"Bank: {Name}");
foreach (var account in AccountList)
{
Console.WriteLine($"Account Number: {account.AccountNumber}, Balance: {account.CheckBalance()}");
}
}
}
public class BankAccount
{
public string AccountNumber { get; set; }
public double Balance { get; set; }
public BankAccount(string accountNumber, double initialBalance)
{
AccountNumber = accountNumber;
Balance = initialBalance;
}
public void Deposit(double amount)
{
Balance += amount;
}
public bool Withdraw(double amount)
{
if (Balance >= amount)
{
Balance -= amount;
return true;
}
else
{
Console.WriteLine("-1");
Console.WriteLine("Insufficient funds.");
return false;
}
}
public double CheckBalance()
{
return Balance;
}
}
class Program
{
static void Main()
{
// Prompt 1- Instantiate a Bank
Bank myBank = new Bank("MyBank",7);
// Prompt 2- Create Bank Accounts
BankAccount account1= new BankAccount("001",1000);
BankAccount account2= new BankAccount("002",500);
BankAccount account3= new BankAccount("003",1500);
myBank.AddAccount(account1);
myBank.AddAccount(account2);
myBank.AddAccount(account3);
// Prompt 3- Deposit Money
account1.Deposit(200);
// Prompt 4- Withdraw Money
account2.Withdraw(100);
// Prompt 5- Check Account Balance
Console.WriteLine($"Account1 Balance: {account1.CheckBalance()}");
// Prompt 6- Handle Insufficient Funds
account3.Withdraw(2000); // This will display "Insufficient funds."
// Prompt 7- List All Accounts in a Bank
myBank.ListAllAccounts();
// Prompt 8- Transfer Funds Between Accounts
myBank.Transfer(account1, account2,300);
// Prompt 9- Bank Capacity Check
BankAccount account4= new BankAccount("004",2000);
BankAccount account5= new BankAccount("005",3000);
myBank.AddAccount(account4); // This will display an error message as the bank is full.
myBank.AddAccount(account5); // This will display an error message as the bank is full.
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!