|
|
import Foundation
|
|
|
|
|
|
enum InsufficientEnergy:Error {
|
|
|
case crazyThursdayVMe50
|
|
|
}
|
|
|
|
|
|
class Entity {
|
|
|
let ID, name: String
|
|
|
init?(id ID: String, name: String) {
|
|
|
self.ID = ID
|
|
|
self.name = name
|
|
|
let reFormula = try! NSRegularExpression(pattern: "^\\d{13}$", options: [NSRegularExpression.Options.anchorsMatchLines])
|
|
|
let matchRange = NSRange(ID.startIndex..<ID.endIndex, in: ID)
|
|
|
let matchList = reFormula.matches(in: ID, range: matchRange)
|
|
|
if matchList.count != 1 {
|
|
|
return nil
|
|
|
}
|
|
|
}
|
|
|
convenience init(id ID: String) {
|
|
|
self.init(id: ID, name: "Unknown")!
|
|
|
}
|
|
|
}
|
|
|
|
|
|
class People: Entity {
|
|
|
func work() throws -> Void {
|
|
|
if Int.random(in: 1...7) == 7 {
|
|
|
throw InsufficientEnergy.crazyThursdayVMe50
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
class Student: People {
|
|
|
override init?(id ID: String, name: String){
|
|
|
super.init(id: ID, name: name)
|
|
|
let reFormula = try! NSRegularExpression(pattern: "^20\\d{2}[1-3]\\d{8}$", options: [NSRegularExpression.Options.anchorsMatchLines])
|
|
|
let matchRange = NSRange(ID.startIndex..<ID.endIndex, in: ID)
|
|
|
let matchList = reFormula.matches(in: ID, range: matchRange)
|
|
|
if matchList.count != 1 {
|
|
|
return nil
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
class Cat: Entity {
|
|
|
override init?(id ID: String, name: String){
|
|
|
super.init(id: ID, name: name)
|
|
|
let reFormula = try! NSRegularExpression(pattern: "^20\\d{2}[1-3]\\d{8}$", options: [NSRegularExpression.Options.anchorsMatchLines])
|
|
|
let matchRange = NSRange(ID.startIndex..<ID.endIndex, in: ID)
|
|
|
let matchList = reFormula.matches(in: ID, range: matchRange)
|
|
|
if matchList.count != 1 {
|
|
|
return nil
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 以下的内容将能够判断名为 ID 的字符串是否满足13位数字,以20xx开头,第5位为1-3的情况,可以直接照搬
|
|
|
// let reFormula = try! NSRegularExpression(pattern: "^20\\d{2}[1-3]\\d{8}$", options: [NSRegularExpression.Options.anchorsMatchLines])
|
|
|
// let matchRange = NSRange(ID.startIndex..<ID.endIndex, in: ID)
|
|
|
// let matchList = reFormula.matches(in: ID, range: matchRange)
|
|
|
// if matchList.count != 1 {
|
|
|
// return false
|
|
|
// }
|
|
|
// return true
|
|
|
|
|
|
// 以下的内容将能够判断名为 ID 的字符串是否满足13位数字的情况,可以直接照搬
|
|
|
// let reFormula = try! NSRegularExpression(pattern: "^\\d{13}$", options: [NSRegularExpression.Options.anchorsMatchLines])
|
|
|
// let matchRange = NSRange(ID.startIndex..<ID.endIndex, in: ID)
|
|
|
// let matchList = reFormula.matches(in: ID, range: matchRange)
|
|
|
// if matchList.count != 1 {
|
|
|
// return false
|
|
|
// }
|
|
|
// return true
|
|
|
|
|
|
// 以下的内容将能够判断名为 ID 的字符串是否满足13位数字,开头为0000的情况,可以直接照搬
|
|
|
// let reFormula = try! NSRegularExpression(pattern: "^0000\\d{9}$", options: [NSRegularExpression.Options.anchorsMatchLines])
|
|
|
// let matchRange = NSRange(ID.startIndex..<ID.endIndex, in: ID)
|
|
|
// let matchList = reFormula.matches(in: ID, range: matchRange)
|
|
|
// if matchList.count != 1 {
|
|
|
// return false
|
|
|
// }
|
|
|
// return true
|
|
|
|
|
|
var siuziu = Student(id: "2022202121000", name: "小趙")
|
|
|
var siuchow = Student(id: "2000503205613", name: "小周")
|
|
|
var studentList = [siuziu, siuchow]
|
|
|
|
|
|
for item in studentList {
|
|
|
guard item != nil else {
|
|
|
print("Invalid students")
|
|
|
continue
|
|
|
}
|
|
|
do {
|
|
|
var count = 0
|
|
|
while true{
|
|
|
count += 1
|
|
|
try item!.work()
|
|
|
print("Siu Ziu has worked for \(String(count)) \(count <= 1 ? "hour" : "hours").")
|
|
|
}
|
|
|
}
|
|
|
catch InsufficientEnergy.crazyThursdayVMe50 {
|
|
|
print("I'm hungry, please v me 50!")
|
|
|
}
|
|
|
}
|
|
|
|