From 83dba7282afa2f97045f32d780ff68dc154f89d4 Mon Sep 17 00:00:00 2001
From: Martin Schmollinger <martin.schmollinger@reutlingen-university.de>
Date: Fri, 7 Apr 2023 19:02:01 +0200
Subject: [PATCH] Solution to exercise of unit 04-03

---
 exercise_0403/go.mod  |  3 +++
 exercise_0403/main.go | 53 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+)
 create mode 100644 exercise_0403/go.mod
 create mode 100644 exercise_0403/main.go

diff --git a/exercise_0403/go.mod b/exercise_0403/go.mod
new file mode 100644
index 0000000..e4b477a
--- /dev/null
+++ b/exercise_0403/go.mod
@@ -0,0 +1,3 @@
+module gitlab.reutlingen-university.de/go-exercises/go-starter/exercise_0403
+
+go 1.20
diff --git a/exercise_0403/main.go b/exercise_0403/main.go
new file mode 100644
index 0000000..5d9f214
--- /dev/null
+++ b/exercise_0403/main.go
@@ -0,0 +1,53 @@
+package main
+
+import (
+	"fmt"
+)
+
+type BankAccount struct {
+	balance float32
+	limit   float32
+}
+
+func (ba *BankAccount) Withdraw(amount float32) error {
+
+	if amount > ba.balance+ba.limit {
+		err := BankAccountWithdrawalError{
+			Amount:  amount,
+			Balance: ba.balance,
+			Limit:   ba.limit,
+			Msg:     "amount currently not available",
+		}
+		return err
+	} else {
+		ba.balance -= amount
+		return nil
+	}
+}
+
+type BankAccountWithdrawalError struct {
+	Amount  float32
+	Balance float32
+	Limit   float32
+	Msg     string
+}
+
+func (e BankAccountWithdrawalError) Error() string {
+	return fmt.Sprintf("%s: %f > %f", e.Msg, e.Amount, e.Balance+e.Limit)
+}
+
+func main() {
+	ba := BankAccount{
+		balance: 1000.0,
+		limit:   3000.0,
+	}
+	fmt.Printf("%v\n", ba)
+	if err := ba.Withdraw(5000); err != nil {
+		fmt.Printf("%s\n", err.Error())
+	}
+	fmt.Printf("%v\n", ba)
+	if err := ba.Withdraw(1000); err != nil {
+		fmt.Printf("%s\n", err.Error())
+	}
+	fmt.Printf("%v\n", ba)
+}
-- 
GitLab