Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Database Technology Edbt 88 International Conference On Extending Database Technology Venice Italy March 14 18 1988 Proceedings Lncs 303

Authors: Joachim W. Schmidt ,Stefano Ceri ,Michele Missikoff

1988th Edition

3540190740, 978-3540190745

More Books

Students also viewed these Databases questions

Question

Compare the different types of employee separation actions.

Answered: 1 week ago

Question

Assess alternative dispute resolution methods.

Answered: 1 week ago

Question

Distinguish between intrinsic and extrinsic rewards.

Answered: 1 week ago