Exercises & activities from the go workshop provided by Packt: https://courses.packtpub.com/courses/go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

68 lines
1.2 KiB

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()
}