You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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 1320xx51-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 130000
// 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!")
}
}