-
Notifications
You must be signed in to change notification settings - Fork 0
/
personClass.go
52 lines (44 loc) · 1021 Bytes
/
personClass.go
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
package main
import "fmt"
type person struct {
age int
}
func (p person) NewPerson(initialAge int) person { // constructor
if initialAge > 0 {
p.age = initialAge
} else {
p.age = 0
fmt.Println("Age is not valid, setting age to 0.")
}
return p
}
func (p person) amIOld() {
//Do some computation in here and print out the correct statement to the console
if p.age < 13 {
fmt.Println("You are young.")
} else if 13 <= p.age && p.age < 18 {
fmt.Println("You are a teenager.")
} else {
fmt.Println("You are old.")
}
}
func (p person) yearPasses() person {
//Increment the age of the person in here
p.age++
return p
}
func main() {
var T,age int
fmt.Scan(&T)
for i := 0; i < T; i++ {
fmt.Scan(&age)
p := person{age: age}
p = p.NewPerson(age)
p.amIOld()
for j := 0; j < 3; j++ {
p = p.yearPasses()
}
p.amIOld()
fmt.Println()
}
}