Browse Source

Add code of chapter 06

master
T. Meissner 4 years ago
parent
commit
8d91867ccc
11 changed files with 467 additions and 0 deletions
  1. +18
    -0
      chapter_06/activity_6.01/main.go
  2. +70
    -0
      chapter_06/activity_6.02/main.go
  3. +68
    -0
      chapter_06/activity_6.03/main.go
  4. +74
    -0
      chapter_06/activity_6.04/main.go
  5. +28
    -0
      chapter_06/example_1/main.go
  6. +26
    -0
      chapter_06/example_2/main.go
  7. +17
    -0
      chapter_06/exercise_6.01/main.go
  8. +16
    -0
      chapter_06/exercise_6.02/main.go
  9. +52
    -0
      chapter_06/exercise_6.03/main.go
  10. +43
    -0
      chapter_06/exercise_6.04/main.go
  11. +55
    -0
      chapter_06/exercise_6.05/main.go

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

@ -0,0 +1,18 @@
package main
import (
"errors"
"fmt"
)
var (
ErrInvalidLastName = errors.New("invalid last name")
ErrInvalidRoutingNumber = errors.New("invalid routing number")
)
func main() {
fmt.Println(ErrInvalidLastName)
fmt.Println(ErrInvalidRoutingNumber)
}

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

@ -0,0 +1,70 @@
package main
import (
"errors"
"fmt"
"strings"
)
type directDeposit struct {
lastName string
firstName string
bankName string
routingNumber int
accountNumber int
}
var (
ErrInvalidLastName = errors.New("invalid last name")
ErrInvalidRoutingNumber = errors.New("invalid routing number")
)
func (d *directDeposit) validateRoutingNumber() error {
if d.routingNumber < 100 {
return ErrInvalidRoutingNumber
}
return nil
}
func (d *directDeposit) validateLastName() error {
d.lastName = strings.TrimSpace(d.lastName)
if len(d.lastName) == 0 {
return ErrInvalidLastName
}
return nil
}
func (d *directDeposit) report() {
fmt.Println(strings.Repeat("*", 40))
fmt.Println("Last Name: ", d.lastName)
fmt.Println("First Name: ", d.firstName)
fmt.Println("Bank Name: ", d.bankName)
fmt.Println("Routing Number:", d.routingNumber)
fmt.Println("Account Number:", d.accountNumber)
}
func main() {
dp := directDeposit{
"",
"Abe",
"XYZ Inc",
17,
1809,
}
if err := dp.validateRoutingNumber(); err != nil {
fmt.Println(err)
}
if err := dp.validateLastName(); err != nil {
fmt.Println(err)
}
dp.report()
}

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

@ -0,0 +1,68 @@
package main
import (
"errors"
"fmt"
"strings"
)
type directDeposit struct {
lastName string
firstName string
bankName string
routingNumber int
accountNumber int
}
var (
ErrInvalidLastName = errors.New("invalid last name")
ErrInvalidRoutingNumber = errors.New("invalid routing number")
)
func (d *directDeposit) validateRoutingNumber() error {
if d.routingNumber < 100 {
panic(ErrInvalidRoutingNumber)
}
return nil
}
func (d *directDeposit) validateLastName() error {
d.lastName = strings.TrimSpace(d.lastName)
if len(d.lastName) == 0 {
return ErrInvalidLastName
}
return nil
}
func (d *directDeposit) report() {
fmt.Println(strings.Repeat("*", 40))
fmt.Println("Last Name: ", d.lastName)
fmt.Println("First Name: ", d.firstName)
fmt.Println("Bank Name: ", d.bankName)
fmt.Println("Routing Number:", d.routingNumber)
fmt.Println("Account Number:", d.accountNumber)
}
func main() {
dp := directDeposit{
"",
"Abe",
"XYZ Inc",
17,
1809,
}
_ = dp.validateRoutingNumber()
if err := dp.validateLastName(); err != nil {
fmt.Println(err)
}
dp.report()
}

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

@ -0,0 +1,74 @@
package main
import (
"errors"
"fmt"
"strings"
)
type directDeposit struct {
lastName string
firstName string
bankName string
routingNumber int
accountNumber int
}
var (
ErrInvalidLastName = errors.New("invalid last name")
ErrInvalidRoutingNumber = errors.New("invalid routing number")
)
func (d *directDeposit) validateRoutingNumber() error {
defer func() {
if err := recover(); err == ErrInvalidRoutingNumber {
fmt.Println(err)
}
}()
if d.routingNumber < 100 {
panic(ErrInvalidRoutingNumber)
}
return nil
}
func (d *directDeposit) validateLastName() error {
d.lastName = strings.TrimSpace(d.lastName)
if len(d.lastName) == 0 {
return ErrInvalidLastName
}
return nil
}
func (d *directDeposit) report() {
fmt.Println(strings.Repeat("*", 40))
fmt.Println("Last Name: ", d.lastName)
fmt.Println("First Name: ", d.firstName)
fmt.Println("Bank Name: ", d.bankName)
fmt.Println("Routing Number:", d.routingNumber)
fmt.Println("Account Number:", d.accountNumber)
}
func main() {
dp := directDeposit{
"",
"Abe",
"XYZ Inc",
17,
1809,
}
_ = dp.validateRoutingNumber()
if err := dp.validateLastName(); err != nil {
fmt.Println(err)
}
dp.report()
}

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

@ -0,0 +1,28 @@
package main
import (
"errors"
"fmt"
)
func main() {
test()
fmt.Println("This line will not get printed")
}
func test() {
n := func() {
fmt.Println("Defer in test")
}
defer n()
msg := "good-bye"
message(msg)
}
func message(msg string) {
f := func() {
fmt.Println("Defer in message func")
}
defer f()
if msg == "good-bye" {
panic(errors.New("something went wrong"))
}
}

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

@ -0,0 +1,26 @@
package main
import (
"errors"
"fmt"
)
func main() {
a()
fmt.Println("This line will now get printed from main() function")
}
func a() {
b("good-bye")
fmt.Println("Back in function a()")
}
func b(msg string) {
defer func() {
if r := recover(); r != nil {
fmt.Println("error in func b()", r)
}
}()
if msg == "good-bye" {
panic(errors.New("something went wrong"))
}
fmt.Print(msg)
}

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

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

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

@ -0,0 +1,16 @@
package main
import "fmt"
func main() {
km := 2
// if km > 2 {
if km >= 2 {
fmt.Println("Take the car")
} else {
fmt.Println("Going to walk today")
}
}

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

@ -0,0 +1,52 @@
package main
import (
"errors"
"fmt"
)
var (
ErrHourlyRate = errors.New("invalid hourly rate")
ErrHoursWorked = errors.New("invalid hours worked per week")
)
func main() {
pay, err := payDay(81, 50)
if err != nil {
fmt.Println(err)
}
pay, err = payDay(80, 5)
if err != nil {
fmt.Println(err)
}
pay, err = payDay(80, 50)
if err != nil {
fmt.Println(err)
}
fmt.Println(pay)
}
func payDay(hoursWorked, hourlyRate int) (int, error) {
if hourlyRate < 10 || hourlyRate > 75 {
return 0, ErrHourlyRate
}
if hoursWorked < 0 || hoursWorked > 80 {
return 0, ErrHoursWorked
}
if hoursWorked > 40 {
hoursOver := hoursWorked - 40
overTime := hoursOver * 2
regularPay := 40 * hourlyRate
return regularPay + overTime, nil
}
return hoursWorked * hourlyRate, nil
}

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

@ -0,0 +1,43 @@
package main
import (
"errors"
"fmt"
)
var (
ErrHourlyRate = errors.New("invalid hourly rate")
ErrHoursWorked = errors.New("invalid hours worked per week")
)
func main() {
pay := payDay(81, 50)
fmt.Println(pay)
}
func payDay(hoursWorked, hourlyRate int) int {
report := func() {
fmt.Printf("hoursWorked: %d\nhourlyRate: %d\n", hoursWorked, hourlyRate)
}
defer report()
if hourlyRate < 10 || hourlyRate > 75 {
panic(ErrHourlyRate)
}
if hoursWorked < 0 || hoursWorked > 80 {
panic(ErrHoursWorked)
}
if hoursWorked > 40 {
hoursOver := hoursWorked - 40
overTime := hoursOver * 2
regularPay := 40 * hourlyRate
return regularPay + overTime
}
return hoursWorked * hourlyRate
}

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

@ -0,0 +1,55 @@
package main
import (
"errors"
"fmt"
)
var (
ErrHourlyRate = errors.New("invalid hourly rate")
ErrHoursWorked = errors.New("invalid hours worked per week")
)
func main() {
pay := payDay(100, 25)
fmt.Println(pay)
pay = payDay(100, 200)
fmt.Println(pay)
pay = payDay(60, 25)
fmt.Println(pay)
}
func payDay(hoursWorked, hourlyRate int) int {
defer func() {
if r := recover(); r != nil {
if r == ErrHourlyRate {
fmt.Printf("hourly rate: %d -> err: %v\n\n", hourlyRate, r)
}
if r == ErrHoursWorked {
fmt.Printf("hours worked: %d -> err: %v\n\n", hoursWorked, r)
}
}
fmt.Printf("Pay was calculated based on:\nhours worked: %d\nhourly rate: %d\n",
hoursWorked, hourlyRate)
}()
if hourlyRate < 10 || hourlyRate > 75 {
panic(ErrHourlyRate)
}
if hoursWorked < 0 || hoursWorked > 80 {
panic(ErrHoursWorked)
}
if hoursWorked > 40 {
hoursOver := hoursWorked - 40
overTime := hoursOver * 2
regularPay := 40 * hourlyRate
return regularPay + overTime
}
return hoursWorked * hourlyRate
}

Loading…
Cancel
Save