Browse Source

Add remaining code of chapter 05

T. Meissner 1 month ago
parent
commit
0e57666404

+ 116
- 0
chapter_05/activity_5.02/main.go View File

@@ -0,0 +1,116 @@
1
+package main
2
+
3
+import "fmt"
4
+
5
+type Employee struct {
6
+  Id        int
7
+  FirstName string
8
+  LastName  string
9
+}
10
+
11
+type Developer struct {
12
+  Individual Employee
13
+  HourlyRate int
14
+  WorkWeek   [7]int
15
+}
16
+
17
+type Weekday int
18
+
19
+const (
20
+  Sunday Weekday = iota
21
+  Monday
22
+  Tuesday
23
+  Wednesday
24
+  Thursday
25
+  Friday
26
+  Saturday
27
+)
28
+
29
+func main() {
30
+
31
+  d := Developer{Individual: Employee{Id: 1, FirstName: "Tony", LastName: "Stark"}, HourlyRate: 10}
32
+
33
+  x := nonLoggedHours()
34
+
35
+  fmt.Println("Tracking hours worked so far today:", x(2))
36
+  fmt.Println("Tracking hours worked so far today:", x(3))
37
+  fmt.Println("Tracking hours worked so far today:", x(5))
38
+
39
+  d.LogHours(Monday, 8)
40
+  d.LogHours(Tuesday, 10)
41
+  d.LogHours(Wednesday, 10)
42
+  d.LogHours(Thursday, 10)
43
+  d.LogHours(Friday, 6)
44
+  d.LogHours(Saturday, 8)
45
+
46
+  d.PayDetails()
47
+
48
+}
49
+
50
+func (d *Developer) LogHours(day Weekday, hours int) {
51
+
52
+  d.WorkWeek[day] = hours
53
+
54
+}
55
+
56
+func (d *Developer) HoursWorked() int {
57
+
58
+  total := 0
59
+  for _, v := range d.WorkWeek {
60
+    total += v
61
+  }
62
+  return total
63
+
64
+}
65
+
66
+func (d *Developer) PayDay() (int, bool) {
67
+
68
+  if d.HoursWorked() > 40 {
69
+    hoursOver := d.HoursWorked() - 40
70
+    overTime := hoursOver * 2 * d.HourlyRate
71
+    regularPay := 40 * d.HourlyRate
72
+    return regularPay + overTime, true
73
+  }
74
+
75
+  return d.HoursWorked() * d.HourlyRate, false
76
+
77
+}
78
+
79
+func nonLoggedHours() func(int) int {
80
+
81
+  total := 0
82
+
83
+  return func(i int) int {
84
+    total += i
85
+    return total
86
+  }
87
+
88
+}
89
+
90
+func (d *Developer) PayDetails() {
91
+
92
+  for i, v := range d.WorkWeek {
93
+    switch i {
94
+    case 0:
95
+      fmt.Println("Sunday hours:", v)
96
+    case 1:
97
+      fmt.Println("Monday hours:", v)
98
+    case 2:
99
+      fmt.Println("Tuesday hours:", v)
100
+    case 3:
101
+      fmt.Println("Wednesday hours:", v)
102
+    case 4:
103
+      fmt.Println("Thursday hours:", v)
104
+    case 5:
105
+      fmt.Println("Friday hours:", v)
106
+    case 6:
107
+      fmt.Println("Saturday hours:", v)
108
+    }
109
+  }
110
+
111
+  fmt.Println("Hours worked this week:", d.HoursWorked())
112
+  pay, over := d.PayDay()
113
+  fmt.Println("Pay for the week:", pay)
114
+  fmt.Println("Is this overtime pay:", over)
115
+
116
+}

+ 40
- 0
chapter_05/exercise_5.04/main.go View File

@@ -0,0 +1,40 @@
1
+package main
2
+
3
+import (
4
+  "fmt"
5
+  "strings"
6
+)
7
+
8
+func main() {
9
+
10
+  hdr := []string{"empid", "employee", "address", "hours worked",
11
+    "hourly rate", "manager"}
12
+  result := csvHdrCol(hdr)
13
+  fmt.Println("Result:")
14
+  fmt.Println(result)
15
+  fmt.Println()
16
+
17
+  hdr2 := []string{"employee", "empid", "hours worked", "address",
18
+    "manager", "hourly rate"}
19
+  result2 := csvHdrCol(hdr2)
20
+  fmt.Println("Result2:")
21
+  fmt.Println(result2)
22
+  fmt.Println()
23
+
24
+}
25
+
26
+func csvHdrCol(hdr []string) map[int]string {
27
+
28
+  csvIdxToCol := make(map[int]string)
29
+
30
+  for i, v := range hdr {
31
+    v = strings.TrimSpace(v)
32
+    switch strings.ToLower(v) {
33
+    case "employee", "hours worked", "hourly rate":
34
+      csvIdxToCol[i] = v
35
+    }
36
+  }
37
+
38
+  return csvIdxToCol
39
+
40
+}

+ 23
- 0
chapter_05/exercise_5.05/main.go View File

@@ -0,0 +1,23 @@
1
+package main
2
+
3
+import "fmt"
4
+
5
+func main() {
6
+
7
+  i := []int{5, 10, 15}
8
+  fmt.Println(sum(5, 4))
9
+  fmt.Println(sum(i...))
10
+
11
+}
12
+
13
+func sum(nums ...int) int {
14
+
15
+  total := 0
16
+
17
+  for _, num := range nums {
18
+    total += num
19
+  }
20
+
21
+  return total
22
+
23
+}

+ 16
- 0
chapter_05/exercise_5.06/main.go View File

@@ -0,0 +1,16 @@
1
+package main
2
+
3
+import "fmt"
4
+
5
+func main() {
6
+
7
+  j := 9
8
+
9
+  x := func(i int) int {
10
+    return i * i
11
+  }
12
+
13
+  fmt.Printf("The square of %d is %d\n", j, x(j))
14
+  fmt.Printf("The square of %d is %d\n", j, func(i int) int { return i * i }(j))
15
+
16
+}

+ 26
- 0
chapter_05/exercise_5.07/main.go View File

@@ -0,0 +1,26 @@
1
+package main
2
+
3
+import "fmt"
4
+
5
+func main() {
6
+
7
+  counter := 4
8
+
9
+  x := decrement(counter)
10
+  fmt.Println(x())
11
+  fmt.Println(x())
12
+  fmt.Println(x())
13
+  fmt.Println(x())
14
+
15
+  fmt.Println("counter:", counter)
16
+
17
+}
18
+
19
+func decrement(i int) func() int {
20
+
21
+  return func() int {
22
+    i--
23
+    return i
24
+  }
25
+
26
+}

+ 32
- 0
chapter_05/exercise_5.08/main.go View File

@@ -0,0 +1,32 @@
1
+package main
2
+
3
+import "fmt"
4
+
5
+func main() {
6
+
7
+  devSalary := salary(50, 2080, developerSalary)
8
+  bossSalary := salary(150000, 25000, managerSalary)
9
+
10
+  fmt.Printf("Boss salary: %d\n", bossSalary)
11
+  fmt.Printf("Developer salary: %d\n", devSalary)
12
+
13
+}
14
+
15
+func salary(x, y int, f func(int, int) int) int {
16
+
17
+  pay := f(x, y)
18
+  return pay
19
+
20
+}
21
+
22
+func developerSalary(hourlyRate, hoursWorked int) int {
23
+
24
+  return hourlyRate * hoursWorked
25
+
26
+}
27
+
28
+func managerSalary(baseSalary, bonus int) int {
29
+
30
+  return baseSalary + bonus
31
+
32
+}