Browse Source

Add code of chapter 06

T. Meissner 1 month ago
parent
commit
8d91867ccc

+ 18
- 0
chapter_06/activity_6.01/main.go View File

@@ -0,0 +1,18 @@
1
+package main
2
+
3
+import (
4
+  "errors"
5
+  "fmt"
6
+)
7
+
8
+var (
9
+  ErrInvalidLastName      = errors.New("invalid last name")
10
+  ErrInvalidRoutingNumber = errors.New("invalid routing number")
11
+)
12
+
13
+func main() {
14
+
15
+  fmt.Println(ErrInvalidLastName)
16
+  fmt.Println(ErrInvalidRoutingNumber)
17
+
18
+}

+ 70
- 0
chapter_06/activity_6.02/main.go View File

@@ -0,0 +1,70 @@
1
+package main
2
+
3
+import (
4
+  "errors"
5
+  "fmt"
6
+  "strings"
7
+)
8
+
9
+type directDeposit struct {
10
+  lastName      string
11
+  firstName     string
12
+  bankName      string
13
+  routingNumber int
14
+  accountNumber int
15
+}
16
+
17
+var (
18
+  ErrInvalidLastName      = errors.New("invalid last name")
19
+  ErrInvalidRoutingNumber = errors.New("invalid routing number")
20
+)
21
+
22
+func (d *directDeposit) validateRoutingNumber() error {
23
+
24
+  if d.routingNumber < 100 {
25
+    return ErrInvalidRoutingNumber
26
+  }
27
+  return nil
28
+
29
+}
30
+
31
+func (d *directDeposit) validateLastName() error {
32
+
33
+  d.lastName = strings.TrimSpace(d.lastName)
34
+  if len(d.lastName) == 0 {
35
+    return ErrInvalidLastName
36
+  }
37
+  return nil
38
+
39
+}
40
+
41
+func (d *directDeposit) report() {
42
+
43
+  fmt.Println(strings.Repeat("*", 40))
44
+  fmt.Println("Last Name:     ", d.lastName)
45
+  fmt.Println("First Name:    ", d.firstName)
46
+  fmt.Println("Bank Name:     ", d.bankName)
47
+  fmt.Println("Routing Number:", d.routingNumber)
48
+  fmt.Println("Account Number:", d.accountNumber)
49
+
50
+}
51
+
52
+func main() {
53
+
54
+  dp := directDeposit{
55
+    "",
56
+    "Abe",
57
+    "XYZ Inc",
58
+    17,
59
+    1809,
60
+  }
61
+
62
+  if err := dp.validateRoutingNumber(); err != nil {
63
+    fmt.Println(err)
64
+  }
65
+  if err := dp.validateLastName(); err != nil {
66
+    fmt.Println(err)
67
+  }
68
+  dp.report()
69
+
70
+}

+ 68
- 0
chapter_06/activity_6.03/main.go View File

@@ -0,0 +1,68 @@
1
+package main
2
+
3
+import (
4
+  "errors"
5
+  "fmt"
6
+  "strings"
7
+)
8
+
9
+type directDeposit struct {
10
+  lastName      string
11
+  firstName     string
12
+  bankName      string
13
+  routingNumber int
14
+  accountNumber int
15
+}
16
+
17
+var (
18
+  ErrInvalidLastName      = errors.New("invalid last name")
19
+  ErrInvalidRoutingNumber = errors.New("invalid routing number")
20
+)
21
+
22
+func (d *directDeposit) validateRoutingNumber() error {
23
+
24
+  if d.routingNumber < 100 {
25
+    panic(ErrInvalidRoutingNumber)
26
+  }
27
+  return nil
28
+
29
+}
30
+
31
+func (d *directDeposit) validateLastName() error {
32
+
33
+  d.lastName = strings.TrimSpace(d.lastName)
34
+  if len(d.lastName) == 0 {
35
+    return ErrInvalidLastName
36
+  }
37
+  return nil
38
+
39
+}
40
+
41
+func (d *directDeposit) report() {
42
+
43
+  fmt.Println(strings.Repeat("*", 40))
44
+  fmt.Println("Last Name:     ", d.lastName)
45
+  fmt.Println("First Name:    ", d.firstName)
46
+  fmt.Println("Bank Name:     ", d.bankName)
47
+  fmt.Println("Routing Number:", d.routingNumber)
48
+  fmt.Println("Account Number:", d.accountNumber)
49
+
50
+}
51
+
52
+func main() {
53
+
54
+  dp := directDeposit{
55
+    "",
56
+    "Abe",
57
+    "XYZ Inc",
58
+    17,
59
+    1809,
60
+  }
61
+
62
+  _ = dp.validateRoutingNumber()
63
+  if err := dp.validateLastName(); err != nil {
64
+    fmt.Println(err)
65
+  }
66
+  dp.report()
67
+
68
+}

+ 74
- 0
chapter_06/activity_6.04/main.go View File

@@ -0,0 +1,74 @@
1
+package main
2
+
3
+import (
4
+  "errors"
5
+  "fmt"
6
+  "strings"
7
+)
8
+
9
+type directDeposit struct {
10
+  lastName      string
11
+  firstName     string
12
+  bankName      string
13
+  routingNumber int
14
+  accountNumber int
15
+}
16
+
17
+var (
18
+  ErrInvalidLastName      = errors.New("invalid last name")
19
+  ErrInvalidRoutingNumber = errors.New("invalid routing number")
20
+)
21
+
22
+func (d *directDeposit) validateRoutingNumber() error {
23
+
24
+  defer func() {
25
+    if err := recover(); err == ErrInvalidRoutingNumber {
26
+      fmt.Println(err)
27
+    }
28
+  }()
29
+
30
+  if d.routingNumber < 100 {
31
+    panic(ErrInvalidRoutingNumber)
32
+  }
33
+  return nil
34
+
35
+}
36
+
37
+func (d *directDeposit) validateLastName() error {
38
+
39
+  d.lastName = strings.TrimSpace(d.lastName)
40
+  if len(d.lastName) == 0 {
41
+    return ErrInvalidLastName
42
+  }
43
+  return nil
44
+
45
+}
46
+
47
+func (d *directDeposit) report() {
48
+
49
+  fmt.Println(strings.Repeat("*", 40))
50
+  fmt.Println("Last Name:     ", d.lastName)
51
+  fmt.Println("First Name:    ", d.firstName)
52
+  fmt.Println("Bank Name:     ", d.bankName)
53
+  fmt.Println("Routing Number:", d.routingNumber)
54
+  fmt.Println("Account Number:", d.accountNumber)
55
+
56
+}
57
+
58
+func main() {
59
+
60
+  dp := directDeposit{
61
+    "",
62
+    "Abe",
63
+    "XYZ Inc",
64
+    17,
65
+    1809,
66
+  }
67
+
68
+  _ = dp.validateRoutingNumber()
69
+  if err := dp.validateLastName(); err != nil {
70
+    fmt.Println(err)
71
+  }
72
+  dp.report()
73
+
74
+}

+ 28
- 0
chapter_06/example_1/main.go View File

@@ -0,0 +1,28 @@
1
+package main
2
+
3
+import (
4
+  "errors"
5
+  "fmt"
6
+)
7
+
8
+func main() {
9
+  test()
10
+  fmt.Println("This line will not get printed")
11
+}
12
+func test() {
13
+  n := func() {
14
+    fmt.Println("Defer in test")
15
+  }
16
+  defer n()
17
+  msg := "good-bye"
18
+  message(msg)
19
+}
20
+func message(msg string) {
21
+  f := func() {
22
+    fmt.Println("Defer in message func")
23
+  }
24
+  defer f()
25
+  if msg == "good-bye" {
26
+    panic(errors.New("something went wrong"))
27
+  }
28
+}

+ 26
- 0
chapter_06/example_2/main.go View File

@@ -0,0 +1,26 @@
1
+package main
2
+
3
+import (
4
+  "errors"
5
+  "fmt"
6
+)
7
+
8
+func main() {
9
+  a()
10
+  fmt.Println("This line will now get printed from main() function")
11
+}
12
+func a() {
13
+  b("good-bye")
14
+  fmt.Println("Back in function a()")
15
+}
16
+func b(msg string) {
17
+  defer func() {
18
+    if r := recover(); r != nil {
19
+      fmt.Println("error in func b()", r)
20
+    }
21
+  }()
22
+  if msg == "good-bye" {
23
+    panic(errors.New("something went wrong"))
24
+  }
25
+  fmt.Print(msg)
26
+}

+ 17
- 0
chapter_06/exercise_6.01/main.go View File

@@ -0,0 +1,17 @@
1
+package main
2
+
3
+import "fmt"
4
+
5
+func main() {
6
+
7
+  nums := []int{2, 4, 6, 8}
8
+  total := 0
9
+
10
+  //  for i := 0; i <= 10; i++ {
11
+  for num, _ := range nums {
12
+    total += num
13
+  }
14
+
15
+  fmt.Println("Total:", total)
16
+
17
+}

+ 16
- 0
chapter_06/exercise_6.02/main.go View File

@@ -0,0 +1,16 @@
1
+package main
2
+
3
+import "fmt"
4
+
5
+func main() {
6
+
7
+  km := 2
8
+
9
+  //  if km > 2 {
10
+  if km >= 2 {
11
+    fmt.Println("Take the car")
12
+  } else {
13
+    fmt.Println("Going to walk today")
14
+  }
15
+
16
+}

+ 52
- 0
chapter_06/exercise_6.03/main.go View File

@@ -0,0 +1,52 @@
1
+package main
2
+
3
+import (
4
+  "errors"
5
+  "fmt"
6
+)
7
+
8
+var (
9
+  ErrHourlyRate  = errors.New("invalid hourly rate")
10
+  ErrHoursWorked = errors.New("invalid hours worked per week")
11
+)
12
+
13
+func main() {
14
+
15
+  pay, err := payDay(81, 50)
16
+  if err != nil {
17
+    fmt.Println(err)
18
+  }
19
+
20
+  pay, err = payDay(80, 5)
21
+  if err != nil {
22
+    fmt.Println(err)
23
+  }
24
+
25
+  pay, err = payDay(80, 50)
26
+  if err != nil {
27
+    fmt.Println(err)
28
+  }
29
+
30
+  fmt.Println(pay)
31
+
32
+}
33
+
34
+func payDay(hoursWorked, hourlyRate int) (int, error) {
35
+
36
+  if hourlyRate < 10 || hourlyRate > 75 {
37
+    return 0, ErrHourlyRate
38
+  }
39
+  if hoursWorked < 0 || hoursWorked > 80 {
40
+    return 0, ErrHoursWorked
41
+  }
42
+
43
+  if hoursWorked > 40 {
44
+    hoursOver := hoursWorked - 40
45
+    overTime := hoursOver * 2
46
+    regularPay := 40 * hourlyRate
47
+    return regularPay + overTime, nil
48
+  }
49
+
50
+  return hoursWorked * hourlyRate, nil
51
+
52
+}

+ 43
- 0
chapter_06/exercise_6.04/main.go View File

@@ -0,0 +1,43 @@
1
+package main
2
+
3
+import (
4
+  "errors"
5
+  "fmt"
6
+)
7
+
8
+var (
9
+  ErrHourlyRate  = errors.New("invalid hourly rate")
10
+  ErrHoursWorked = errors.New("invalid hours worked per week")
11
+)
12
+
13
+func main() {
14
+
15
+  pay := payDay(81, 50)
16
+  fmt.Println(pay)
17
+
18
+}
19
+
20
+func payDay(hoursWorked, hourlyRate int) int {
21
+
22
+  report := func() {
23
+    fmt.Printf("hoursWorked: %d\nhourlyRate: %d\n", hoursWorked, hourlyRate)
24
+  }
25
+  defer report()
26
+
27
+  if hourlyRate < 10 || hourlyRate > 75 {
28
+    panic(ErrHourlyRate)
29
+  }
30
+  if hoursWorked < 0 || hoursWorked > 80 {
31
+    panic(ErrHoursWorked)
32
+  }
33
+
34
+  if hoursWorked > 40 {
35
+    hoursOver := hoursWorked - 40
36
+    overTime := hoursOver * 2
37
+    regularPay := 40 * hourlyRate
38
+    return regularPay + overTime
39
+  }
40
+
41
+  return hoursWorked * hourlyRate
42
+
43
+}

+ 55
- 0
chapter_06/exercise_6.05/main.go View File

@@ -0,0 +1,55 @@
1
+package main
2
+
3
+import (
4
+  "errors"
5
+  "fmt"
6
+)
7
+
8
+var (
9
+  ErrHourlyRate  = errors.New("invalid hourly rate")
10
+  ErrHoursWorked = errors.New("invalid hours worked per week")
11
+)
12
+
13
+func main() {
14
+
15
+  pay := payDay(100, 25)
16
+  fmt.Println(pay)
17
+  pay = payDay(100, 200)
18
+  fmt.Println(pay)
19
+  pay = payDay(60, 25)
20
+  fmt.Println(pay)
21
+
22
+}
23
+
24
+func payDay(hoursWorked, hourlyRate int) int {
25
+
26
+  defer func() {
27
+    if r := recover(); r != nil {
28
+      if r == ErrHourlyRate {
29
+        fmt.Printf("hourly rate: %d -> err: %v\n\n", hourlyRate, r)
30
+      }
31
+      if r == ErrHoursWorked {
32
+        fmt.Printf("hours worked: %d -> err: %v\n\n", hoursWorked, r)
33
+      }
34
+    }
35
+    fmt.Printf("Pay was calculated based on:\nhours worked: %d\nhourly rate: %d\n",
36
+      hoursWorked, hourlyRate)
37
+  }()
38
+
39
+  if hourlyRate < 10 || hourlyRate > 75 {
40
+    panic(ErrHourlyRate)
41
+  }
42
+  if hoursWorked < 0 || hoursWorked > 80 {
43
+    panic(ErrHoursWorked)
44
+  }
45
+
46
+  if hoursWorked > 40 {
47
+    hoursOver := hoursWorked - 40
48
+    overTime := hoursOver * 2
49
+    regularPay := 40 * hourlyRate
50
+    return regularPay + overTime
51
+  }
52
+
53
+  return hoursWorked * hourlyRate
54
+
55
+}