Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Cocoa
- protocol Accountable {
- var ownerName : String { get }
- var balance : Double { get set }
- func deposit(amount : Double)
- func withdrow (amount : Double)
- }
- protocol Overdraftable {
- var overdraftLimit : Double { get set }
- }
- protocol Loanable {
- var commission : Double { get set }
- func getLoan (amount : Double)
- }
- class Account : Accountable {
- internal var ownerName: String
- internal var balance: Double
- var Balance: Double {
- get {
- return self.balance
- }
- set {
- self.balance = newValue
- }
- }
- var Owner: String {
- get {
- return self.ownerName
- }
- }
- init(owner : String, balance : Double) {
- self.ownerName = owner
- self.balance = balance
- }
- init(owner : String) {
- self.ownerName = owner
- self.balance = 0
- }
- func deposit(amount: Double) {
- if amount > 0 {
- Balance += amount
- } else {
- print("Cannot deposit amount less then 1")
- }
- }
- func withdrow(amount: Double) {
- if amount <= 0 {
- print("Cannot withdrow amount less then 1")
- } else if Balance - amount >= 0 {
- self.Balance -= amount
- } else {
- print("Withdrowing \(amount) from the account will exceed the account overdraft limit for student or soldier account - Overdraft limit: 0")
- }
- }
- func details() -> String {
- return "Owner name: \(self.Owner); Balance: \(self.Balance)"
- }
- }
- class StudentAccount : Account, Loanable {
- internal var commission: Double
- var Commission : Double {
- get {
- return self.commission
- }
- set {
- self.commission = newValue
- }
- }
- override init(owner: String) {
- self.commission = 0
- super.init(owner: owner)
- }
- init(owner: String, balance :Double, commission :Double) {
- self.commission = commission
- super.init(owner: owner, balance: balance)
- }
- func getLoan(amount: Double) {
- if amount > 0 {
- self.Balance += amount
- } else {
- print("Cannot get a negative or aero loan")
- }
- }
- func printDetails() -> String {
- return "Account type: Student acount; \(super.details()); Commission: \(Commission)"
- }
- }
- class SoldierAccount : Account {
- func printDetails() -> String {
- return "Account type: Soldier acount; \(super.details())"
- }
- }
- class RegularAccount : Account, Loanable, Overdraftable {
- internal var commission: Double
- internal var overdraftLimit: Double
- var Commission : Double {
- get {
- return self.commission
- }
- set {
- self.commission = newValue
- }
- }
- var OverdraftLimit : Double {
- get {
- return self.overdraftLimit
- }
- set {
- self.overdraftLimit = newValue
- }
- }
- override init(owner: String) {
- self.commission = 10.2
- self.overdraftLimit = -10_000
- super.init(owner: owner)
- }
- func getLoan(amount: Double) {
- if amount > 0 {
- self.Balance += (amount * ((100 - Commission) / 100))
- } else {
- print("Cannot get a negative or aero loan")
- }
- }
- override func withdrow(amount: Double) {
- if amount <= 0 {
- print("Cannot withdrow amount less then 1")
- } else if Balance - amount >= OverdraftLimit {
- self.Balance -= amount
- } else {
- print("Withdrowing \(amount) from the account will exceed the account overdraft limit for your regular account - Overdraft limit: \(self.OverdraftLimit)")
- }
- }
- func printDetails() -> String {
- return "Account type: Regular acount; \(super.details()); Overdraft limit: \(OverdraftLimit); Commission: \(Commission)"
- }
- }
- class BusinessAccount : Account, Loanable, Overdraftable {
- internal var commission: Double
- internal var overdraftLimit: Double
- var Commission : Double {
- get {
- return self.commission
- }
- set {
- self.commission = newValue
- }
- }
- var OverdraftLimit : Double {
- get {
- return self.overdraftLimit
- }
- set {
- self.overdraftLimit = newValue
- }
- }
- override init(owner: String) {
- self.commission = 5.8
- self.overdraftLimit = -100_000
- super.init(owner: owner)
- }
- func getLoan(amount: Double) {
- if amount > 0 {
- self.Balance += (amount * ((100 - Commission) / 100))
- } else {
- print("Cannot get a negative or aero loan")
- }
- }
- override func withdrow(amount: Double) {
- if amount <= 0 {
- print("Cannot withdrow amount less then 1")
- } else if Balance - amount >= OverdraftLimit {
- self.Balance -= amount
- } else {
- print("Withdrowing \(amount) from the account will exceed the account overdraft limit for your business account - Overdraft limit: \(self.OverdraftLimit)")
- }
- }
- func printDetails() -> String {
- return "Account type: Business acount; \(super.details()); Overdraft limit: \(OverdraftLimit); Commission: \(Commission)"
- }
- }
- class FileWriter {
- // Variables
- // For creating string filename location
- private var documentPath, fileName, fullPath : String!
- // For getting the ios file manager
- private var fileManager = FileManager.default
- init(fileName : String) {
- self.fileName = fileName
- fileSettings()
- }
- func fileSettings() {
- // For getting document path, we need to use our file manager...
- self.documentPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0]
- self.fullPath = "\(documentPath!)/\(fileName!)"
- }
- func test() {
- print(self.fullPath!)
- }
- func addToFile(_ txt: String) {
- var myData = readFromFile()
- do {
- myData += txt
- try myData.write(toFile: self.fullPath, atomically: true, encoding: String.Encoding.utf8)
- print("Wrote to file")
- }
- catch {
- print("Error writing to file")
- }
- }
- func writeToFile(_ txt: String) {
- do {
- try txt.write(toFile: self.fullPath, atomically: true, encoding: String.Encoding.utf8)
- print("Wrote to file")
- }
- catch {
- print("Error writing to file")
- }
- }
- func readFromFile() -> String {
- var temp = ""
- if fileManager.fileExists(atPath: fullPath) {
- do {
- try temp = String(contentsOfFile: fullPath)
- } catch {
- print("Cannot read from file \(self.fileName ?? "")") /// In string we can add ?? "with default content"
- }
- } else {
- print("No such file in the system")
- }
- return temp
- }
- func deleteFile() {
- do {
- try fileManager.removeItem(atPath: fullPath)
- print("File \(fileName!) deleted successfully")
- } catch {
- print("Failed to delete file \(fileName ?? "Bad file name")")
- }
- }
- }
- class Bank {
- private var accounts : [Account]
- private let fileWriter = FileWriter(fileName: "MyBank.txt")
- init() {
- accounts = []
- }
- public func addAccount(_ owner: String, _ accountType: String) {
- switch accountType {
- case "Regular":
- let regularAccount = RegularAccount(owner: owner)
- accounts.append(regularAccount)
- fileWriter.addToFile(regularAccount.printDetails())
- case "Student":
- let studentAccount = StudentAccount(owner: owner)
- accounts.append(StudentAccount(owner: owner))
- fileWriter.addToFile(studentAccount.printDetails())
- case "Soldier":
- let soldierAccount = SoldierAccount(owner: owner)
- accounts.append(SoldierAccount(owner: owner))
- fileWriter.addToFile(soldierAccount.printDetails())
- case "Business":
- let businessAccount = BusinessAccount(owner: owner)
- accounts.append(BusinessAccount(owner: owner))
- fileWriter.addToFile(businessAccount.printDetails())
- default:
- print("No such account type exists!!!")
- }
- }
- public func getALoan(owner: String, amount: Double) {
- for account in accounts {
- if account.Owner == owner {
- if account is Loanable {
- if let regularAccount = account as? RegularAccount {
- regularAccount.getLoan(amount: amount)
- printToFile()
- return
- } else if let businessAccount = account as? BusinessAccount {
- businessAccount.getLoan(amount: amount)
- printToFile()
- return
- }
- }
- print("Cannot get a loan in soldier or student account")
- }
- }
- print("Account not found!")
- }
- public func withdrow(owner: String, amount: Double) {
- for account in accounts {
- if account.ownerName == owner {
- if account is Overdraftable {
- if let regularAccount = account as? RegularAccount {
- regularAccount.withdrow(amount: amount)
- printToFile()
- return
- } else if let businessAccount = account as? BusinessAccount {
- businessAccount.withdrow(amount: amount)
- printToFile()
- return
- }
- } else {
- account.withdrow(amount: amount)
- }
- }
- }
- }
- public func deposit(owner: String, amount: Double) {
- for account in accounts {
- if account.ownerName == owner {
- account.deposit(amount: amount)
- }
- }
- printToFile()
- }
- public func printAllAccounts() {
- // Passing the map function a closure expression
- let strToPrint = accounts.map { (account) -> String in
- return account.details() + "\n"
- }
- print(strToPrint.joined())
- }
- public func printAllOverdraftableAccounts() {
- print("Acounts with negative balance:")
- let overdraftableAccounts = accounts.filter { $0 is Overdraftable && $0.balance < 0 }
- // Passing the map function a closure expression
- let strToPrint = overdraftableAccounts.map { (account) -> String in
- return account.details() + "\n"
- }
- print(strToPrint.joined())
- }
- public func printAllOverdraftableAccountsCloseToLimit() {
- print("Acounts that are 1000 shekels away from the overdraft limit:")
- for account in accounts {
- if account is Overdraftable {
- if account is Overdraftable {
- if let regularAccount = account as? RegularAccount {
- if regularAccount.balance - regularAccount.overdraftLimit < 1000 {
- print(regularAccount.printDetails())
- }
- } else if let businessAccount = account as? BusinessAccount {
- if businessAccount.balance - businessAccount.overdraftLimit < 1000 {
- print(businessAccount.printDetails())
- }
- }
- }
- }
- }
- }
- public func printSpecificAccount(_ owner: String) {
- let account = accounts.filter { $0.ownerName == owner }[0]
- if let regularAccount = account as? RegularAccount {
- print(regularAccount.printDetails())
- } else if let businessAccount = account as? BusinessAccount {
- print(businessAccount.printDetails())
- } else if let studentAccount = account as? StudentAccount {
- print(studentAccount.printDetails())
- } else if let soldierAccount = account as? SoldierAccount {
- print(soldierAccount.printDetails())
- } else {
- print("Something went wrong!")
- }
- }
- private func printToFile() {
- var str = ""
- for account in accounts {
- if let studentAccount = account as? StudentAccount {
- str += studentAccount.printDetails() + "\n"
- }
- if let soldierAccount = account as? SoldierAccount {
- str += soldierAccount.printDetails() + "\n"
- }
- if let regularAccount = account as? RegularAccount {
- str += regularAccount.printDetails() + "\n"
- }
- if let businessAccount = account as? BusinessAccount {
- str += businessAccount.printDetails() + "\n"
- }
- }
- fileWriter.writeToFile(str)
- }
- }
- var bank = Bank()
- bank.addAccount("Yossi", "Soldier")
- bank.addAccount("Moshe", "Soldier")
- bank.addAccount("Eli", "Soldier")
- bank.addAccount("Hadar", "Regular")
- bank.addAccount("Yoav", "Regular")
- bank.addAccount("Avi", "Regular")
- bank.addAccount("Haim", "Student")
- bank.addAccount("Ofri", "Student")
- bank.addAccount("Benny", "Student")
- bank.addAccount("Riki", "Business")
- bank.addAccount("Noa", "Business")
- bank.addAccount("Yaron", "Business")
- bank.deposit(owner: "Yossi", amount: 20_000)
- bank.deposit(owner: "Moshe", amount: 25_000)
- bank.deposit(owner: "Eli", amount: 30_000)
- bank.deposit(owner: "Hadar", amount: 35_000)
- bank.deposit(owner: "Yoav", amount: 40_000)
- bank.deposit(owner: "Avi", amount: 33_000)
- bank.deposit(owner: "Haim", amount: 50_000)
- bank.deposit(owner: "Ofri", amount: 27_000)
- bank.deposit(owner: "Benny", amount: 60_000)
- bank.deposit(owner: "Riki", amount: 43_000)
- bank.deposit(owner: "Noa", amount: 79_000)
- bank.deposit(owner: "Yaron", amount: 15_000)
- bank.printAllAccounts()
- bank.withdrow(owner: "Yossi", amount: 10_000)
- bank.withdrow(owner: "Avi", amount: 10_000)
- bank.withdrow(owner: "Noa", amount: 10_000)
- bank.withdrow(owner: "Ofri", amount: 10_000)
- bank.withdrow(owner: "Haim", amount: 51_000)
- bank.withdrow(owner: "Avi", amount: 32_600)
- bank.withdrow(owner: "Noa", amount: 100_000)
- bank.withdrow(owner: "Riki", amount: 100_000)
- bank.withdrow(owner: "Yoav", amount: 139_800)
- bank.withdrow(owner: "Yoav", amount: 49_800)
- bank.printAllAccounts()
- print("\n")
- bank.printAllOverdraftableAccounts()
- print("\n")
- bank.printAllOverdraftableAccountsCloseToLimit()
- print("\n")
- bank.printSpecificAccount("Yaron")
- bank.getALoan(owner: "Avi", amount: 20_000)
- bank.printSpecificAccount("Avi")
- bank.getALoan(owner: "Yaron", amount: 20_000)
- bank.printSpecificAccount("Yaron")
Add Comment
Please, Sign In to add comment