eranseg

Swift final project - Bank

Apr 5th, 2020
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 15.32 KB | None | 0 0
  1. import Cocoa
  2.  
  3. protocol Accountable {
  4.     var ownerName : String { get }
  5.     var balance : Double { get set }
  6.     func deposit(amount : Double)
  7.     func withdrow (amount : Double)
  8. }
  9.  
  10. protocol Overdraftable {
  11.     var overdraftLimit : Double { get set }
  12. }
  13.  
  14. protocol Loanable {
  15.     var commission : Double { get set }
  16.     func getLoan (amount : Double)
  17. }
  18.  
  19. class Account : Accountable {
  20.    
  21.     internal var ownerName: String
  22.     internal var balance: Double
  23.    
  24.     var Balance: Double {
  25.         get {
  26.             return self.balance
  27.         }
  28.         set {
  29.             self.balance = newValue
  30.         }
  31.     }
  32.    
  33.     var Owner: String {
  34.         get {
  35.             return self.ownerName
  36.         }
  37.     }
  38.    
  39.     init(owner : String, balance : Double) {
  40.         self.ownerName = owner
  41.         self.balance = balance
  42.     }
  43.    
  44.     init(owner : String) {
  45.         self.ownerName = owner
  46.         self.balance = 0
  47.     }
  48.    
  49.     func deposit(amount: Double) {
  50.         if amount > 0 {
  51.             Balance += amount
  52.         } else {
  53.             print("Cannot deposit amount less then 1")
  54.         }
  55.     }
  56.    
  57.     func withdrow(amount: Double) {
  58.         if amount <= 0 {
  59.             print("Cannot withdrow amount less then 1")
  60.         } else if Balance - amount >= 0 {
  61.             self.Balance -= amount
  62.         } else {
  63.             print("Withdrowing \(amount) from the account will exceed the account overdraft limit for student or soldier account - Overdraft limit: 0")
  64.         }
  65.     }
  66.    
  67.     func details() -> String {
  68.         return "Owner name: \(self.Owner); Balance: \(self.Balance)"
  69.     }
  70. }
  71.  
  72. class StudentAccount : Account, Loanable {
  73.    
  74.     internal var commission: Double
  75.    
  76.     var Commission : Double {
  77.         get {
  78.             return self.commission
  79.         }
  80.         set {
  81.             self.commission = newValue
  82.         }
  83.     }
  84.    
  85.     override init(owner: String) {
  86.         self.commission = 0
  87.         super.init(owner: owner)
  88.     }
  89.    
  90.     init(owner: String, balance :Double, commission :Double) {
  91.         self.commission = commission
  92.         super.init(owner: owner, balance: balance)
  93.     }
  94.    
  95.     func getLoan(amount: Double) {
  96.         if amount > 0 {
  97.             self.Balance += amount
  98.         } else {
  99.             print("Cannot get a negative or aero loan")
  100.         }
  101.     }
  102.    
  103.     func printDetails() -> String {
  104.         return "Account type: Student acount; \(super.details()); Commission: \(Commission)"
  105.     }
  106. }
  107.  
  108. class SoldierAccount : Account {
  109.    
  110.     func printDetails() -> String {
  111.         return "Account type: Soldier acount; \(super.details())"
  112.     }
  113. }
  114.  
  115. class RegularAccount : Account, Loanable, Overdraftable {
  116.    
  117.     internal var commission: Double
  118.     internal var overdraftLimit: Double
  119.    
  120.     var Commission : Double {
  121.         get {
  122.             return self.commission
  123.         }
  124.         set {
  125.             self.commission = newValue
  126.         }
  127.     }
  128.    
  129.     var OverdraftLimit : Double {
  130.         get {
  131.             return self.overdraftLimit
  132.         }
  133.         set {
  134.             self.overdraftLimit = newValue
  135.         }
  136.     }
  137.    
  138.     override init(owner: String) {
  139.         self.commission = 10.2
  140.         self.overdraftLimit = -10_000
  141.         super.init(owner: owner)
  142.     }
  143.    
  144.     func getLoan(amount: Double) {
  145.         if amount > 0 {
  146.             self.Balance += (amount * ((100 - Commission) / 100))
  147.         } else {
  148.             print("Cannot get a negative or aero loan")
  149.         }
  150.     }
  151.    
  152.     override func withdrow(amount: Double) {
  153.         if amount <= 0 {
  154.             print("Cannot withdrow amount less then 1")
  155.         } else if Balance - amount >= OverdraftLimit {
  156.             self.Balance -= amount
  157.         } else {
  158.             print("Withdrowing \(amount) from the account will exceed the account overdraft limit for your regular account - Overdraft limit: \(self.OverdraftLimit)")
  159.         }
  160.     }
  161.    
  162.     func printDetails() -> String {
  163.         return "Account type: Regular acount; \(super.details()); Overdraft limit: \(OverdraftLimit); Commission: \(Commission)"
  164.     }
  165. }
  166.  
  167. class BusinessAccount : Account, Loanable, Overdraftable {
  168.    
  169.     internal var commission: Double
  170.     internal var overdraftLimit: Double
  171.    
  172.     var Commission : Double {
  173.         get {
  174.             return self.commission
  175.         }
  176.         set {
  177.             self.commission = newValue
  178.         }
  179.     }
  180.    
  181.     var OverdraftLimit : Double {
  182.         get {
  183.             return self.overdraftLimit
  184.         }
  185.         set {
  186.             self.overdraftLimit = newValue
  187.         }
  188.     }
  189.    
  190.     override init(owner: String) {
  191.         self.commission = 5.8
  192.         self.overdraftLimit = -100_000
  193.         super.init(owner: owner)
  194.     }
  195.    
  196.     func getLoan(amount: Double) {
  197.         if amount > 0 {
  198.             self.Balance += (amount * ((100 - Commission) / 100))
  199.         } else {
  200.             print("Cannot get a negative or aero loan")
  201.         }
  202.     }
  203.    
  204.     override func withdrow(amount: Double) {
  205.         if amount <= 0 {
  206.             print("Cannot withdrow amount less then 1")
  207.         } else if Balance - amount >= OverdraftLimit {
  208.             self.Balance -= amount
  209.         } else {
  210.             print("Withdrowing \(amount) from the account will exceed the account overdraft limit for your business account - Overdraft limit: \(self.OverdraftLimit)")
  211.         }
  212.     }
  213.    
  214.     func printDetails() -> String {
  215.         return "Account type: Business acount; \(super.details()); Overdraft limit: \(OverdraftLimit); Commission: \(Commission)"
  216.     }
  217. }
  218.  
  219. class FileWriter {
  220.     // Variables
  221.     // For creating string filename location
  222.     private var documentPath, fileName, fullPath : String!
  223.     // For getting the ios file manager
  224.     private var fileManager = FileManager.default
  225.    
  226.     init(fileName : String) {
  227.         self.fileName = fileName
  228.         fileSettings()
  229.     }
  230.    
  231.     func fileSettings() {
  232.         // For getting document path, we need to use our file manager...
  233.         self.documentPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0]
  234.         self.fullPath = "\(documentPath!)/\(fileName!)"
  235.     }
  236.    
  237.     func test() {
  238.         print(self.fullPath!)
  239.     }
  240.    
  241.     func addToFile(_ txt: String) {
  242.         var myData = readFromFile()
  243.         do {
  244.             myData += txt
  245.             try myData.write(toFile: self.fullPath, atomically: true, encoding: String.Encoding.utf8)
  246.             print("Wrote to file")
  247.         }
  248.         catch {
  249.             print("Error writing to file")
  250.         }
  251.     }
  252.    
  253.     func writeToFile(_ txt: String) {
  254.         do {
  255.             try txt.write(toFile: self.fullPath, atomically: true, encoding: String.Encoding.utf8)
  256.             print("Wrote to file")
  257.         }
  258.         catch {
  259.             print("Error writing to file")
  260.         }
  261.     }
  262.    
  263.     func readFromFile() -> String {
  264.         var temp = ""
  265.         if fileManager.fileExists(atPath: fullPath) {
  266.             do {
  267.                 try temp = String(contentsOfFile: fullPath)
  268.             } catch {
  269.                 print("Cannot read from file \(self.fileName ?? "")") /// In string we can add ?? "with default content"
  270.             }
  271.         } else {
  272.             print("No such file in the system")
  273.         }
  274.         return temp
  275.     }
  276.    
  277.     func deleteFile() {
  278.         do {
  279.             try fileManager.removeItem(atPath: fullPath)
  280.             print("File \(fileName!) deleted successfully")
  281.         } catch {
  282.             print("Failed to delete file \(fileName ?? "Bad file name")")
  283.         }
  284.     }
  285. }
  286.  
  287. class Bank {
  288.    
  289.     private var accounts : [Account]
  290.     private let fileWriter = FileWriter(fileName: "MyBank.txt")
  291.    
  292.     init() {
  293.         accounts = []
  294.     }
  295.    
  296.     public func addAccount(_ owner: String, _ accountType: String) {
  297.         switch accountType {
  298.         case "Regular":
  299.             let regularAccount = RegularAccount(owner: owner)
  300.             accounts.append(regularAccount)
  301.             fileWriter.addToFile(regularAccount.printDetails())
  302.         case "Student":
  303.             let studentAccount = StudentAccount(owner: owner)
  304.             accounts.append(StudentAccount(owner: owner))
  305.             fileWriter.addToFile(studentAccount.printDetails())
  306.         case "Soldier":
  307.             let soldierAccount = SoldierAccount(owner: owner)
  308.             accounts.append(SoldierAccount(owner: owner))
  309.             fileWriter.addToFile(soldierAccount.printDetails())
  310.         case "Business":
  311.             let businessAccount = BusinessAccount(owner: owner)
  312.             accounts.append(BusinessAccount(owner: owner))
  313.             fileWriter.addToFile(businessAccount.printDetails())
  314.         default:
  315.             print("No such account type exists!!!")
  316.         }
  317.     }
  318.    
  319.     public func getALoan(owner: String, amount: Double) {
  320.         for account in accounts {
  321.             if account.Owner == owner {
  322.                 if account is Loanable {
  323.                     if let regularAccount = account as? RegularAccount {
  324.                         regularAccount.getLoan(amount: amount)
  325.                         printToFile()
  326.                         return
  327.                     } else if let businessAccount = account as? BusinessAccount {
  328.                         businessAccount.getLoan(amount: amount)
  329.                         printToFile()
  330.                         return
  331.                     }
  332.                 }
  333.                 print("Cannot get a loan in soldier or student account")
  334.             }
  335.         }
  336.         print("Account not found!")
  337.     }
  338.    
  339.     public func withdrow(owner: String, amount: Double) {
  340.         for account in accounts {
  341.             if account.ownerName == owner {
  342.                 if account is Overdraftable {
  343.                     if let regularAccount = account as? RegularAccount {
  344.                         regularAccount.withdrow(amount: amount)
  345.                         printToFile()
  346.                         return
  347.                     } else if let businessAccount = account as? BusinessAccount {
  348.                         businessAccount.withdrow(amount: amount)
  349.                         printToFile()
  350.                         return
  351.                     }
  352.                 } else {
  353.                     account.withdrow(amount: amount)
  354.                 }
  355.             }
  356.         }
  357.     }
  358.    
  359.     public func deposit(owner: String, amount: Double) {
  360.         for account in accounts {
  361.             if account.ownerName == owner {
  362.                 account.deposit(amount: amount)
  363.             }
  364.         }
  365.         printToFile()
  366.     }
  367.    
  368.     public func printAllAccounts() {
  369.         // Passing the map function a closure expression
  370.         let strToPrint = accounts.map { (account) -> String in
  371.             return account.details() + "\n"
  372.         }
  373.         print(strToPrint.joined())
  374.     }
  375.    
  376.     public func printAllOverdraftableAccounts() {
  377.         print("Acounts with negative balance:")
  378.         let overdraftableAccounts = accounts.filter { $0 is Overdraftable && $0.balance < 0 }
  379.         // Passing the map function a closure expression
  380.         let strToPrint = overdraftableAccounts.map { (account) -> String in
  381.             return account.details() + "\n"
  382.         }
  383.         print(strToPrint.joined())
  384.     }
  385.    
  386.     public func printAllOverdraftableAccountsCloseToLimit() {
  387.         print("Acounts that are 1000 shekels away from the overdraft limit:")
  388.         for account in accounts {
  389.             if account is Overdraftable {
  390.                 if account is Overdraftable {
  391.                     if let regularAccount = account as? RegularAccount {
  392.                         if regularAccount.balance - regularAccount.overdraftLimit < 1000 {
  393.                             print(regularAccount.printDetails())
  394.                         }
  395.                     } else if let businessAccount = account as? BusinessAccount {
  396.                         if businessAccount.balance - businessAccount.overdraftLimit < 1000 {
  397.                             print(businessAccount.printDetails())
  398.                         }
  399.                     }
  400.                 }
  401.             }
  402.         }
  403.     }
  404.    
  405.     public func printSpecificAccount(_ owner: String) {
  406.         let account = accounts.filter { $0.ownerName == owner }[0]
  407.         if let regularAccount = account as? RegularAccount {
  408.             print(regularAccount.printDetails())
  409.         } else if let businessAccount = account as? BusinessAccount {
  410.             print(businessAccount.printDetails())
  411.         } else if let studentAccount = account as? StudentAccount {
  412.             print(studentAccount.printDetails())
  413.         } else if let soldierAccount = account as? SoldierAccount {
  414.             print(soldierAccount.printDetails())
  415.         } else {
  416.             print("Something went wrong!")
  417.         }
  418.     }
  419.    
  420.     private func printToFile() {
  421.         var str = ""
  422.         for account in accounts {
  423.             if let studentAccount = account as? StudentAccount {
  424.                 str += studentAccount.printDetails() + "\n"
  425.             }
  426.             if let soldierAccount = account as? SoldierAccount {
  427.                 str += soldierAccount.printDetails() + "\n"
  428.             }
  429.             if let regularAccount = account as? RegularAccount {
  430.                 str += regularAccount.printDetails() + "\n"
  431.             }
  432.             if let businessAccount = account as? BusinessAccount {
  433.                 str += businessAccount.printDetails() + "\n"
  434.             }
  435.         }
  436.         fileWriter.writeToFile(str)
  437.     }
  438. }
  439.  
  440. var bank = Bank()
  441.  
  442. bank.addAccount("Yossi", "Soldier")
  443. bank.addAccount("Moshe", "Soldier")
  444. bank.addAccount("Eli", "Soldier")
  445. bank.addAccount("Hadar", "Regular")
  446. bank.addAccount("Yoav", "Regular")
  447. bank.addAccount("Avi", "Regular")
  448. bank.addAccount("Haim", "Student")
  449. bank.addAccount("Ofri", "Student")
  450. bank.addAccount("Benny", "Student")
  451. bank.addAccount("Riki", "Business")
  452. bank.addAccount("Noa", "Business")
  453. bank.addAccount("Yaron", "Business")
  454.  
  455. bank.deposit(owner: "Yossi", amount: 20_000)
  456. bank.deposit(owner: "Moshe", amount: 25_000)
  457. bank.deposit(owner: "Eli", amount: 30_000)
  458. bank.deposit(owner: "Hadar", amount: 35_000)
  459. bank.deposit(owner: "Yoav", amount: 40_000)
  460. bank.deposit(owner: "Avi", amount: 33_000)
  461. bank.deposit(owner: "Haim", amount: 50_000)
  462. bank.deposit(owner: "Ofri", amount: 27_000)
  463. bank.deposit(owner: "Benny", amount: 60_000)
  464. bank.deposit(owner: "Riki", amount: 43_000)
  465. bank.deposit(owner: "Noa", amount: 79_000)
  466. bank.deposit(owner: "Yaron", amount: 15_000)
  467.  
  468. bank.printAllAccounts()
  469.  
  470. bank.withdrow(owner: "Yossi", amount: 10_000)
  471. bank.withdrow(owner: "Avi", amount: 10_000)
  472. bank.withdrow(owner: "Noa", amount: 10_000)
  473. bank.withdrow(owner: "Ofri", amount: 10_000)
  474. bank.withdrow(owner: "Haim", amount: 51_000)
  475.  
  476. bank.withdrow(owner: "Avi", amount: 32_600)
  477. bank.withdrow(owner: "Noa", amount: 100_000)
  478. bank.withdrow(owner: "Riki", amount: 100_000)
  479. bank.withdrow(owner: "Yoav", amount: 139_800)
  480. bank.withdrow(owner: "Yoav", amount: 49_800)
  481.  
  482. bank.printAllAccounts()
  483. print("\n")
  484. bank.printAllOverdraftableAccounts()
  485. print("\n")
  486. bank.printAllOverdraftableAccountsCloseToLimit()
  487. print("\n")
  488. bank.printSpecificAccount("Yaron")
  489.  
  490. bank.getALoan(owner: "Avi", amount: 20_000)
  491. bank.printSpecificAccount("Avi")
  492.  
  493. bank.getALoan(owner: "Yaron", amount: 20_000)
  494. bank.printSpecificAccount("Yaron")
Add Comment
Please, Sign In to add comment