Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C#: I need to create instances of the these prompts with the following code: ### Prompt 1 - Instantiate a Bank Write a code snippet

C#: I need to create instances of the these prompts with the following code: ### 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. 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 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;
}
}

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

Students also viewed these Databases questions

Question

c. What were you expected to do when you grew up?

Answered: 1 week ago