Created
September 16, 2025 15:34
-
-
Save hmorgado/e572cf6faeb8f3320679e72c9b3e811a to your computer and use it in GitHub Desktop.
Object Oriented Bank Account With Overrides
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // C# code below | |
| using System; | |
| // Write your answer here, and then test your code. | |
| // Your job is to implement the findLargest() method. | |
| public class Answer { | |
| // Change these Boolean values to control whether you see | |
| // the expected result and/or hints. | |
| public static Boolean ShowExpectedResult = false; | |
| public static Boolean ShowHints = false; | |
| } | |
| public class BankAccount { | |
| private string _FirstName; | |
| private string _LastName; | |
| private decimal _Balance = 0.0m; | |
| public BankAccount(string firstName, string lastName, decimal balance = 0.0m) { | |
| this._FirstName = firstName; | |
| this._LastName = lastName; | |
| this._Balance = balance; | |
| } | |
| public decimal Balance(){ | |
| return this._Balance; | |
| } | |
| public void Balance(decimal b){ | |
| this._Balance = b; | |
| } | |
| public string AccountOwner(){ | |
| return $"{_FirstName} {_LastName}"; | |
| } | |
| public void Deposit(decimal deposit){ | |
| this._Balance += deposit; | |
| } | |
| public virtual void Withdraw(decimal withdraw) { } | |
| } | |
| public class CheckingAcct : BankAccount { | |
| public CheckingAcct(string firstName, string lastName, decimal balance) | |
| :base(firstName, lastName, balance){ | |
| } | |
| public override void Withdraw(decimal withdraw){ | |
| if (withdraw > Balance()){ | |
| Balance(Balance() - (withdraw + 35)); | |
| } else { | |
| Balance(Balance() - withdraw); | |
| } | |
| } | |
| } | |
| public class SavingsAcct : BankAccount { | |
| private decimal _InterestRate; | |
| private int _wdcount = 0; | |
| public SavingsAcct(string firstName, string lastName, decimal ir, decimal balance) | |
| :base(firstName, lastName, balance) | |
| { | |
| this._InterestRate = ir; | |
| } | |
| public decimal InterestRate(){ | |
| return _InterestRate; | |
| } | |
| public void ApplyInterest(){ | |
| Balance(Balance() + (Balance() * _InterestRate)); | |
| } | |
| public override void Withdraw(decimal withdraw) | |
| { | |
| if (withdraw > Balance()){ | |
| return; | |
| } else { | |
| if (_wdcount >= 3){ | |
| Balance(Balance() - (withdraw + 2)); | |
| return; | |
| } | |
| _wdcount++; | |
| Balance(Balance() - withdraw); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment