diff --git a/exercise_0402/go.mod b/exercise_0402/go.mod new file mode 100644 index 0000000000000000000000000000000000000000..a30505f322832c191f81ba98c163fc591628853d --- /dev/null +++ b/exercise_0402/go.mod @@ -0,0 +1,3 @@ +module gitlab.reutlingen-university.de/go-exercises/go-starter/exercise_0402 + +go 1.20 diff --git a/exercise_0402/main.go b/exercise_0402/main.go new file mode 100644 index 0000000000000000000000000000000000000000..8b04b237aa75b270fa31473d35350f9e353e990d --- /dev/null +++ b/exercise_0402/main.go @@ -0,0 +1,37 @@ +package main + +import ( + "errors" + "fmt" +) + +type BankAccount struct { + balance float32 + limit float32 +} + +func (ba *BankAccount) Withdraw(amount float32) error { + + if amount > ba.balance+ba.limit { + return errors.New("amount currently not available") + } else { + ba.balance -= amount + return nil + } +} + +func main() { + ba := BankAccount{ + balance: 1000.0, + limit: 3000.0, + } + fmt.Printf("%v\n", ba) + if err := ba.Withdraw(5000); err != nil { + fmt.Println(err.Error()) + } + fmt.Printf("%v\n", ba) + if err := ba.Withdraw(1000); err != nil { + fmt.Println(err.Error()) + } + fmt.Printf("%v\n", ba) +}