From 39a55a15ef499c9df79e83cf0fbb3b1fc7de9f73 Mon Sep 17 00:00:00 2001 From: tmeissner Date: Wed, 5 Aug 2020 23:55:20 +0200 Subject: [PATCH] Add first activities & exercises of chapter 05 --- chapter_05/activity_5.01/main.go | 55 ++++++++++++++++++++++++++++++++ chapter_05/exercise_5.01/main.go | 29 +++++++++++++++++ chapter_05/exercise_5.02/main.go | 33 +++++++++++++++++++ chapter_05/exercise_5.03/main.go | 26 +++++++++++++++ chapter_05/fizzBuzz/main.go | 27 ++++++++++++++++ 5 files changed, 170 insertions(+) create mode 100644 chapter_05/activity_5.01/main.go create mode 100644 chapter_05/exercise_5.01/main.go create mode 100644 chapter_05/exercise_5.02/main.go create mode 100644 chapter_05/exercise_5.03/main.go create mode 100644 chapter_05/fizzBuzz/main.go diff --git a/chapter_05/activity_5.01/main.go b/chapter_05/activity_5.01/main.go new file mode 100644 index 0000000..e4bf544 --- /dev/null +++ b/chapter_05/activity_5.01/main.go @@ -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 + +} diff --git a/chapter_05/exercise_5.01/main.go b/chapter_05/exercise_5.01/main.go new file mode 100644 index 0000000..cddf846 --- /dev/null +++ b/chapter_05/exercise_5.01/main.go @@ -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") + } + } + +} diff --git a/chapter_05/exercise_5.02/main.go b/chapter_05/exercise_5.02/main.go new file mode 100644 index 0000000..8b1a48a --- /dev/null +++ b/chapter_05/exercise_5.02/main.go @@ -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) + +} diff --git a/chapter_05/exercise_5.03/main.go b/chapter_05/exercise_5.03/main.go new file mode 100644 index 0000000..2aa0202 --- /dev/null +++ b/chapter_05/exercise_5.03/main.go @@ -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, "" + +} diff --git a/chapter_05/fizzBuzz/main.go b/chapter_05/fizzBuzz/main.go new file mode 100644 index 0000000..76bb382 --- /dev/null +++ b/chapter_05/fizzBuzz/main.go @@ -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") + +}