新建名为SwiftBank的OSX Comand line 工程,会自动生成main.swift,再新建(⌘ +N)两个文件User.swift和Utils.swift
内容分别为:
main.swift
// // main.swift // SwiftBank // // Created by cyper on 6/1/16. // Copyright © 2016 Moaz Tech. All rights reserved. // var users: [User] = [ User(username: "one",password: "123",name: "Cyper")!,User(username: "two",name: "Green")!,User(username: "three",name: "Kitten")! ] var currentUser: User? func login(){ Utils.clearScree() print(">>>>>> LOGIN") let username = Utils.readInput("Username: ") let password = Utils.readInput("Password: ") var foundUser = false for user in users { if user.username == username && user.password == password { foundUser = true currentUser = user } } if !foundUser { print("Invalid credentials") return } print("Access granted!") Utils.pause() enterUserMenu() } func enterUserMenu(){ while true { Utils.clearScree() print(">>>>>>>> USER MENU") print("1. View Account") print("2. Deposit Money") print("3. Withdraw Money") print("0. Sign out") let response_str = Utils.readInput() let response = Int(response_str) ?? -1 switch response { case 1: viewAccount() case 2: depositMoney() case 3: withdrawMoney() case 0: currentUser = nil return default: continue } } } func viewAccount(){ guard let currentUser = currentUser else { fatalError("currentUser is nil") } currentUser.outputDetails() Utils.pause() } func depositMoney(){ guard let currentUser = currentUser else { fatalError("currentUser is nil") } let amount_str = Utils.readInput("Amount to deposit: ") guard let amount = Double(amount_str) where amount > 0 else { print("number is not valid") return } currentUser.deposit(amount) print("\(amount) depositted successfully") Utils.pause() } func withdrawMoney(){ guard let currentUser = currentUser else { fatalError("currentUser is nil") } let amount_str = Utils.readInput("Amount to withdraw: ") guard let amount = Double(amount_str) where amount > 0 else { print("number is not valid") return } if currentUser.withdraw(amount) { print("\(amount) withdrew successfully") } else { print("not enough balance") } Utils.pause() } func signup(){ Utils.clearScree() print(">>>>>>> SIGNUP") let username = Utils.readInput("Username: ") let password = Utils.readInput("Password: ") let name = Utils.readInput("Name: ") users.append(User(username: username,password: password,name: name)!) print("Signup successful") Utils.pause() } func main(){ repeat { Utils.clearScree() print("Welcome to Swift Bank") print("1. Login") print("2. Signup") print("0. Exit") let response_str = Utils.readInput() let response = Int(response_str) ?? -1 switch response { case 1: login() case 2: signup() case 0: return default: continue } } while true } main()
Utils.swift
// // Utils.swift // SwiftBank // // Created by cyper on 6/1/16. // Copyright © 2016 Moaz Tech. All rights reserved. // import Foundation class Utils { static func readInput(prompt: String = "> ") -> String { print(prompt,terminator: "") repeat { guard let res_str = readLine() where !res_str.isEmpty else { continue } return res_str } while true } static func clearScree() { print("") print("") print("") print("") } static func pause(){ var _ = readLine() } }
User.swift
// // User.swift // SwiftBank // // Created by cyper on 6/2/16. // Copyright © 2016 Moaz Tech. All rights reserved. // import Foundation class User { var username: String! var password: String! var name: String! var savings: Double = 0.0 init?(username: String,password: String,name: String){ if username.isEmpty || password.isEmpty || name.isEmpty { return nil } self.username = username self.password = password self.name = name } func deposit(amount: Double){ savings += amount } func withdraw(amount: Double) -> Bool { let after = savings - amount if after < 0.0 { return false } // ignore negative value savings -= max(0.0,amount) return true } func outputDetails(){ print("username: \(username)") print("password: \(password)") print("name: \(name)") print("savings: \(savings)") } }
程序的入口不是main函数,而是main.swift文件,定义在这个文件中的代码会被顺次执行. 总共有两屏的Menu外层是Welcome screen Menu,内层是登录后的User Menu.. 没什么新的,主要是readLine(..),guard let ... else {} 的用法.,静态的工具类Utils写法,以及定义一个简单的POJO(使用了failable initializer)
原文链接:https://www.f2er.com/swift/323615.html