From 55dfd1b8aa30cd5842276432372f8d32304cd899 Mon Sep 17 00:00:00 2001 From: steev Date: Sun, 23 Feb 2025 22:22:43 +0100 Subject: [PATCH] feat: :sparkles: added mvc code --- .vscode/launch.json | 15 +++++++ .vscode/settings.json | 6 +++ go.mod | 3 ++ interfaces/IObserver.go | 5 +++ main.go | 14 +++++++ objects/BankAccount.Model.go | 69 ++++++++++++++++++++++++++++++++ objects/BankAccountController.go | 69 ++++++++++++++++++++++++++++++++ objects/BankAccountView.go | 25 ++++++++++++ 8 files changed, 206 insertions(+) create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json create mode 100644 go.mod create mode 100644 interfaces/IObserver.go create mode 100644 main.go create mode 100644 objects/BankAccount.Model.go create mode 100644 objects/BankAccountController.go create mode 100644 objects/BankAccountView.go diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..44544db --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Launch", + "type": "go", + "request": "launch", + "mode": "debug", + "program": "${workspaceFolder}/main.go", + "env": {}, + "args": [] + } + ] + } + \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..ff6e752 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "cSpell.words": [ + "Betrag", + "Kontobewegung" + ] +} \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b34b201 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module mvc + +go 1.23.2 diff --git a/interfaces/IObserver.go b/interfaces/IObserver.go new file mode 100644 index 0000000..a200ea2 --- /dev/null +++ b/interfaces/IObserver.go @@ -0,0 +1,5 @@ +package interfaces + +type IObserver interface { + Update() error +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..0af61b1 --- /dev/null +++ b/main.go @@ -0,0 +1,14 @@ +package main + +import "mvc/objects" + +func main() { + model := objects.NewBankAccountModel() + view := objects.NewBankAccountView(model) + controller := objects.NewBankAccountController(model, view) + + model.AddObserver(&view) + + controller.Loop() + +} diff --git a/objects/BankAccount.Model.go b/objects/BankAccount.Model.go new file mode 100644 index 0000000..cb5a06e --- /dev/null +++ b/objects/BankAccount.Model.go @@ -0,0 +1,69 @@ +package objects + +import ( + "errors" + "fmt" + "mvc/interfaces" +) + +type BankAccountModel struct { + kontostand float64 + observer []interfaces.IObserver +} + +func NewBankAccountModel() BankAccountModel { + return BankAccountModel{} +} + +func (bm *BankAccountModel) GetBalance() float64 { + return bm.kontostand +} + +func (bm *BankAccountModel) AddObserver(observer interfaces.IObserver) error { + bm.observer = append(bm.observer, observer) + return nil +} + +func (bm *BankAccountModel) NotifyObserver() error { + + if len(bm.observer) <= 0 { + return errors.New("no observer found") + } + + for _, observer := range bm.observer { + observer.Update() + } + + return nil +} +func (bm *BankAccountModel) Deposit(balance float64) error { + + if balance <= 0 { + return errors.New("balance cannot be 0") + } + + fmt.Printf("balance: %v", balance) + + bm.kontostand += balance + + fmt.Printf("balance: %v", bm.kontostand) + + bm.NotifyObserver() + return nil +} + +func (bm *BankAccountModel) Withdraw(balance float64) error { + + if balance <= 0 { + return errors.New("balance cannot be 0") + } + + fmt.Printf("balance: %v", balance) + + bm.kontostand -= balance + + fmt.Printf("balance: %v", bm.kontostand) + + bm.NotifyObserver() + return nil +} diff --git a/objects/BankAccountController.go b/objects/BankAccountController.go new file mode 100644 index 0000000..dbeb1b7 --- /dev/null +++ b/objects/BankAccountController.go @@ -0,0 +1,69 @@ +package objects + +import ( + "bufio" + "fmt" + "os" + "strconv" + "strings" +) + +type BankAccountController struct { + model BankAccountModel + view BankAccountView + scanner *bufio.Scanner +} + +func NewBankAccountController(model BankAccountModel, view BankAccountView) BankAccountController { + return BankAccountController{ + scanner: bufio.NewScanner(os.Stdin), + model: model, + view: view, + } +} + +func (bc *BankAccountController) Loop() { + for { + // Update View + if err := bc.view.Update(); err != nil { + fmt.Println("Error updating view:", err) + return + } + + // Eingabeaufforderung + fmt.Print("Enter command: ") + + // Warte auf Benutzereingabe + if !bc.scanner.Scan() { + fmt.Println("Error reading input or EOF reached") + break + } + + scannedString := strings.TrimSpace(bc.scanner.Text()) + if len(scannedString) == 0 { + continue // Ignoriere leere Eingaben + } + + fmt.Printf("First char: %c\n", scannedString[0]) + + // Befehl ausführen + switch scannedString[0] { + case '+': + amount, err := strconv.ParseFloat(scannedString[1:], 64) + if err != nil { + panic(fmt.Sprintf("Failed to parse float: %v\n", err)) + } + + bc.model.Deposit(amount) + case '-': + amount, err := strconv.ParseFloat(scannedString[1:], 64) + if err != nil { + panic(fmt.Sprintf("Failed to parse float: %v\n", err)) + } + + bc.model.Withdraw(amount) + default: + fmt.Println("No command recognized") + } + } +} diff --git a/objects/BankAccountView.go b/objects/BankAccountView.go new file mode 100644 index 0000000..62cf289 --- /dev/null +++ b/objects/BankAccountView.go @@ -0,0 +1,25 @@ +package objects + +import ( + "fmt" + "mvc/interfaces" +) + +type BankAccountView struct { + model BankAccountModel +} + +var _ interfaces.IObserver = &BankAccountView{} + +func NewBankAccountView(model BankAccountModel) BankAccountView { + return BankAccountView{ + model: model, + } +} + +func (bv *BankAccountView) Update() error { + fmt.Print("\033[H\033[2J") + fmt.Printf("Kontostand: %v\n", bv.model.GetBalance()) + fmt.Println("Bitte Kontobewegung eingeben: [+ = Einzahlung / - = Abhebung] + Betrag (ex. +100.0)") + return nil +}