Browse Source

Add first activities & exercises of chapter 05

master
T. Meissner 4 years ago
parent
commit
39a55a15ef
5 changed files with 170 additions and 0 deletions
  1. +55
    -0
      chapter_05/activity_5.01/main.go
  2. +29
    -0
      chapter_05/exercise_5.01/main.go
  3. +33
    -0
      chapter_05/exercise_5.02/main.go
  4. +26
    -0
      chapter_05/exercise_5.03/main.go
  5. +27
    -0
      chapter_05/fizzBuzz/main.go

+ 55
- 0
chapter_05/activity_5.01/main.go View File

@ -0,0 +1,55 @@
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}
d.LogHours(Monday, 8)
d.LogHours(Tuesday, 10)
fmt.Println("Hours worked on monday:", d.WorkWeek[Monday])
fmt.Println("Hours worked on tuesday:", d.WorkWeek[Tuesday])
fmt.Printf("Hours worked this week: %d\n", d.HoursWorked())
}
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
}

+ 29
- 0
chapter_05/exercise_5.01/main.go View File

@ -0,0 +1,29 @@
package main
import "fmt"
func main() {
itemsSold()
}
func itemsSold() {
items := make(map[string]int)
items["John"] = 41
items["Celina"] = 109
items["Micah"] = 24
for k, v := range items {
fmt.Printf("%s sold %d items and ", k, v)
if v < 40 {
fmt.Println("is below expectations")
} else if v > 40 && v <= 100 {
fmt.Println("meets expectations")
} else {
fmt.Println("exceeded expectations")
}
}
}

+ 33
- 0
chapter_05/exercise_5.02/main.go View File

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

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

@ -0,0 +1,26 @@
package main
import "fmt"
func main() {
for i := 1; i <= 15; i++ {
n, s := fizzBuzz(i)
fmt.Printf("Results: %d %s\n", n, s)
}
}
func fizzBuzz(i int) (int, string) {
switch {
case i%15 == 0:
return i, "FizzBuzz"
case i%3 == 0:
return i, "Fizz"
case i%5 == 0:
return i, "Buzz"
}
return i, ""
}

+ 27
- 0
chapter_05/fizzBuzz/main.go View File

@ -0,0 +1,27 @@
package main
import "fmt"
func fizzBuzz() {
for i := 1; i <= 30; i++ {
if i%15 == 0 {
fmt.Println("FizzBuzz")
} else if i%3 == 0 {
fmt.Println("Fizz")
} else if i%5 == 0 {
fmt.Println("Buzz")
} else {
fmt.Println(i)
}
}
}
func main() {
fmt.Println("Main is in control")
fizzBuzz()
fmt.Println("Back to main")
}

Loading…
Cancel
Save