Last active
March 16, 2022 15:56
-
-
Save rhenz/1dc7342481e8cfef52ad8cede433bed9 to your computer and use it in GitHub Desktop.
SwiftBank Project Solution
This file contains 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
// Write your code below 🏦 | |
struct SwiftBank { | |
private let password: String | |
private var balance: Double = 0 { | |
didSet { | |
if balance < 100 { | |
displayLowBalanceMessage() | |
} | |
} | |
} | |
static let depositBonusRate = 0.01 | |
var depositBonusClaimed: Bool = false | |
init(password: String, initialDeposit: Double) { | |
self.password = password | |
makeDeposit(ofAmount: initialDeposit) | |
} | |
private func isValid(_ enteredPassword: String) -> Bool { | |
if enteredPassword == password { | |
return true | |
} else { | |
return false | |
} | |
} | |
private func finalDepositWithBonus(fromInitialDeposit deposit: Double) -> Double { | |
return deposit + (deposit * SwiftBank.depositBonusRate) | |
} | |
mutating func makeDeposit(ofAmount depositAmount: Double) { | |
// Should not take negative values | |
if depositAmount <= 0 { | |
print("Cannot deposit $\(depositAmount)") | |
return | |
} | |
if depositAmount >= 1000 && !depositBonusClaimed { | |
let depositWithBonus = finalDepositWithBonus(fromInitialDeposit: depositAmount) | |
print("Making a deposit of $\(depositAmount) with a bonus rate. The final amount deposited is $\(depositWithBonus)") | |
balance += depositWithBonus | |
depositBonusClaimed = true // Bonus can only be claimed once | |
} else { | |
print("Making a deposit of $\(depositAmount) without a bonus rate. The final amount deposited is $\(depositAmount)") | |
balance += depositAmount | |
} | |
} | |
func displayBalance(usingPassword password: String) { | |
if !isValid(password) { | |
print("Error: Invalid password. Cannot retrieve balance.") | |
return | |
} | |
print("Your current balance is $\(balance)") | |
} | |
mutating func makeWithdrawal(ofAmount withdrawalAmount: Double, usingPassword password: String) { | |
if !isValid(password) { | |
print("Error: Invalid password. Cannot make withdrawal.") | |
return | |
} | |
if withdrawalAmount <= 0 { | |
print("Can't withdraw amount less than or equal to Zero.") | |
return | |
} | |
if withdrawalAmount > balance { | |
print("You can't withdraw an amount more than your current balance.") | |
displayBalance(usingPassword: password) | |
return | |
} | |
balance -= withdrawalAmount | |
print("Making a $\(withdrawalAmount)") | |
} | |
private func displayLowBalanceMessage() { | |
print("Alert: Your balance is under $100") | |
} | |
} | |
// ============= | |
var myAccount = SwiftBank(password: "MyPassword", initialDeposit: 500) | |
myAccount.makeDeposit(ofAmount: 50) | |
// myAccount.makeWithdrawal(ofAmount: 100, usingPassword: "LucasPass") | |
myAccount.makeWithdrawal(ofAmount: 100, usingPassword: "MyPassword") | |
// Challenge #18 | |
// Deposit with a bonus | |
myAccount.makeDeposit(ofAmount: 1000) | |
// Deposit $1000 or more again. Deposit bonus can only be claimed once | |
myAccount.makeDeposit(ofAmount: 1000) | |
// Challenge #19 | |
// Deposit negative amount or zero amount | |
myAccount.makeDeposit(ofAmount: -100) | |
myAccount.makeDeposit(ofAmount: 0) | |
// Withdraw negative amount or zero amount | |
myAccount.makeWithdrawal(ofAmount: -100, usingPassword: "MyPassword") | |
myAccount.makeWithdrawal(ofAmount: 0, usingPassword: "MyPassword") | |
// Withdraw more cash than they have in the bank | |
myAccount.makeWithdrawal(ofAmount: 50000, usingPassword: "MyPassword") | |
// Check balance | |
// myAccount.displayBalance(usingPassword: "MyPassword") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment