From 95391ba7a2198d6be9a2dceca0da0a08531284be Mon Sep 17 00:00:00 2001
From: Martin Schmollinger <martin.schmollinger@reutlingen-university.de>
Date: Fri, 7 Apr 2023 18:47:31 +0200
Subject: [PATCH] Solution to exercise of unit 04-02

---
 exercise_0402/go.mod  |  3 +++
 exercise_0402/main.go | 37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+)
 create mode 100644 exercise_0402/go.mod
 create mode 100644 exercise_0402/main.go

diff --git a/exercise_0402/go.mod b/exercise_0402/go.mod
new file mode 100644
index 0000000..a30505f
--- /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 0000000..8b04b23
--- /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)
+}
-- 
GitLab