-
Notifications
You must be signed in to change notification settings - Fork 0
/
Town.swift
62 lines (52 loc) · 1.38 KB
/
Town.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// Town.swift
// MonsterTown
//
// Created by Matthew D. Mathias on 8/22/14.
// Copyright (c) 2014 BigNerdRanch. All rights reserved.
//
struct Town {
static let region = "South"
var mayor = Mayor()
var population = 5422 {
// Bronze Challenge
willSet(newPopulation) {
if ( newPopulation < population ) {
print("warning! population has lowered")
//Silver Challenge
mayor.listen(to: .decrease)
}
}
/////////////////
didSet(oldPoppulation) {
print("The population has changed to \(population) from \(oldPoppulation)")
}
}
var numberOfStoplights = 4
enum Size {
case Small
case Medium
case Large
}
var townSize: Size {
get {
switch self.population {
case 0...10000:
return .Small
case 10001...100000:
return .Medium
default:
return .Large
}
}
}
func printDescription() {
print("Population: \(population); number of stop lights: \(numberOfStoplights)")
}
mutating func changePopulation(by amount: Int) {
population += amount
if ( amount < 0 ) {
mayor.listenZombieAttact()
}
}
}