Browse Source

Add remaining code of chapter 05

master
T. Meissner 9 months ago
parent
commit
0e57666404
6 changed files with 253 additions and 0 deletions
  1. +116
    -0
      chapter_05/activity_5.02/main.go
  2. +40
    -0
      chapter_05/exercise_5.04/main.go
  3. +23
    -0
      chapter_05/exercise_5.05/main.go
  4. +16
    -0
      chapter_05/exercise_5.06/main.go
  5. +26
    -0
      chapter_05/exercise_5.07/main.go
  6. +32
    -0
      chapter_05/exercise_5.08/main.go

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

@ -0,0 +1,116 @@
package main
import "fmt"
type Employee struct {
Id int
FirstName string
LastName string
}
type Developer struct {
Individual Employee
HourlyRate int
WorkWeek [7]int
}
type Weekday int
const (
Sunday Weekday = iota
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
)
func main() {
d := Developer{Individual: Employee{Id: 1, FirstName: "Tony", LastName: "Stark"}, HourlyRate: 10}
x := nonLoggedHours()
fmt.Println("Tracking hours worked so far today:", x(2))
fmt.Println("Tracking hours worked so far today:", x(3))
fmt.Println("Tracking hours worked so far today:", x(5))
d.LogHours(Monday, 8)
d.LogHours(Tuesday, 10)
d.LogHours(Wednesday, 10)
d.LogHours(Thursday, 10)
d.LogHours(Friday, 6)
d.LogHours(Saturday, 8)
d.PayDetails()
}
func (d *Developer) LogHours(day Weekday, hours int) {
d.WorkWeek[day] = hours
}
func (d *Developer) HoursWorked() int {
total := 0
for _, v := range d.WorkWeek {
total += v
}
return total
}
func (d *Developer) PayDay() (int, bool) {
if d.HoursWorked() > 40 {
hoursOver := d.HoursWorked() - 40
overTime := hoursOver * 2 * d.HourlyRate
regularPay := 40 * d.HourlyRate
return regularPay + overTime, true
}
return d.HoursWorked() * d.HourlyRate, false
}
func nonLoggedHours() func(int) int {
total := 0
return func(i int) int {
total += i
return total
}
}
func (d *Developer) PayDetails() {
for i, v := range d.WorkWeek {
switch i {
case 0:
fmt.Println("Sunday hours:", v)
case 1:
fmt.Println("Monday hours:", v)
case 2:
fmt.Println("Tuesday hours:", v)
case 3:
fmt.Println("Wednesday hours:", v)
case 4:
fmt.Println("Thursday hours:", v)
case 5:
fmt.Println("Friday hours:", v)
case 6:
fmt.Println("Saturday hours:", v)
}
}
fmt.Println("Hours worked this week:", d.HoursWorked())
pay, over := d.PayDay()
fmt.Println("Pay for the week:", pay)
fmt.Println("Is this overtime pay:", over)
}

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

@ -0,0 +1,40 @@
package main
import (
"fmt"
"strings"
)
func main() {
hdr := []string{"empid", "employee", "address", "hours worked",
"hourly rate", "manager"}
result := csvHdrCol(hdr)
fmt.Println("Result:")
fmt.Println(result)
fmt.Println()
hdr2 := []string{"employee", "empid", "hours worked", "address",
"manager", "hourly rate"}
result2 := csvHdrCol(hdr2)
fmt.Println("Result2:")
fmt.Println(result2)
fmt.Println()
}
func csvHdrCol(hdr []string) map[int]string {
csvIdxToCol := make(map[int]string)
for i, v := range hdr {
v = strings.TrimSpace(v)
switch strings.ToLower(v) {
case "employee", "hours worked", "hourly rate":
csvIdxToCol[i] = v
}
}
return csvIdxToCol
}

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

@ -0,0 +1,23 @@
package main
import "fmt"
func main() {
i := []int{5, 10, 15}
fmt.Println(sum(5, 4))
fmt.Println(sum(i...))
}
func sum(nums ...int) int {
total := 0
for _, num := range nums {
total += num
}
return total
}

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

@ -0,0 +1,16 @@
package main
import "fmt"
func main() {
j := 9
x := func(i int) int {
return i * i
}
fmt.Printf("The square of %d is %d\n", j, x(j))
fmt.Printf("The square of %d is %d\n", j, func(i int) int { return i * i }(j))
}

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

@ -0,0 +1,26 @@
package main
import "fmt"
func main() {
counter := 4
x := decrement(counter)
fmt.Println(x())
fmt.Println(x())
fmt.Println(x())
fmt.Println(x())
fmt.Println("counter:", counter)
}
func decrement(i int) func() int {
return func() int {
i--
return i
}
}

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

@ -0,0 +1,32 @@
package main
import "fmt"
func main() {
devSalary := salary(50, 2080, developerSalary)
bossSalary := salary(150000, 25000, managerSalary)
fmt.Printf("Boss salary: %d\n", bossSalary)
fmt.Printf("Developer salary: %d\n", devSalary)
}
func salary(x, y int, f func(int, int) int) int {
pay := f(x, y)
return pay
}
func developerSalary(hourlyRate, hoursWorked int) int {
return hourlyRate * hoursWorked
}
func managerSalary(baseSalary, bonus int) int {
return baseSalary + bonus
}

Loading…
Cancel
Save