Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C# I need to make a program that follows the given prompts: ### Prompt 1 - Instantiate a Bank Write a code snippet to create

C# I need to make a program that follows the given prompts: ### Prompt 1- Instantiate a Bank
Write a code snippet to create an instance of a `Bank` with a specified name and maximum number of accounts.
### Prompt 2- Create Bank Accounts
Instantiate several `BankAccount` objects with different account numbers and associate them with the bank instance created in prompt 1.
### Prompt 3- Deposit Money
Write a method to deposit a certain amount of money into one of the `BankAccount` instances.
### Prompt 4- Withdraw Money
Implement a code snippet to withdraw an amount from a `BankAccount`. Check if the withdrawal is successful.
### Prompt 5- Check Account Balance
Display the balance of a specific `BankAccount` after performing deposit and withdrawal operations.
### Prompt 6- Handle Insufficient Funds
Attempt a withdrawal that exceeds the account balance and handle the `-1` that is returned by displaying an appropriate message.
### Prompt 7- List All Accounts in a Bank
Loop through all accounts in a bank and display their details (account number and balance).
### Prompt 8- Transfer Funds Between Accounts
Transfer funds from one account to another within the same bank. ### Prompt 9- Bank Capacity Check
Try adding more `BankAccount` instances to the bank than its maximum capacity allows and handle the error gracefully. This is the code I have now, but I am getting an error saying : An unhandled exception of type 'System.NullReferenceException' occurred in Bank.dll: 'Object reference npublic 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("Insufficient funds.");
return false;
}
}
public double CheckBalance()
{
return Balance;
}
}
class Program
{
static void Main()
{
// Prompt 1- Instantiate a Bank
Bank myBank = new Bank("MyBank",5);
// 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 willot set to an instance of an object.'

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

Database Theory And Application Bio Science And Bio Technology International Conferences DTA And BSBT 2011 Held As Part Of The Future Generation In Computer And Information Science 258

Authors: Tai-hoon Kim ,Hojjat Adeli ,Alfredo Cuzzocrea ,Tughrul Arslan ,Yanchun Zhang ,Jianhua Ma ,Kyo-il Chung ,Siti Mariyam ,Xiaofeng Song

2011th Edition

3642271561, 978-3642271564

Students also viewed these Databases questions