feat: ✨ added mvc code
This commit is contained in:
commit
55dfd1b8aa
15
.vscode/launch.json
vendored
Normal file
15
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Launch",
|
||||||
|
"type": "go",
|
||||||
|
"request": "launch",
|
||||||
|
"mode": "debug",
|
||||||
|
"program": "${workspaceFolder}/main.go",
|
||||||
|
"env": {},
|
||||||
|
"args": []
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
6
.vscode/settings.json
vendored
Normal file
6
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"cSpell.words": [
|
||||||
|
"Betrag",
|
||||||
|
"Kontobewegung"
|
||||||
|
]
|
||||||
|
}
|
5
interfaces/IObserver.go
Normal file
5
interfaces/IObserver.go
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package interfaces
|
||||||
|
|
||||||
|
type IObserver interface {
|
||||||
|
Update() error
|
||||||
|
}
|
14
main.go
Normal file
14
main.go
Normal file
@ -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()
|
||||||
|
|
||||||
|
}
|
69
objects/BankAccount.Model.go
Normal file
69
objects/BankAccount.Model.go
Normal file
@ -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
|
||||||
|
}
|
69
objects/BankAccountController.go
Normal file
69
objects/BankAccountController.go
Normal file
@ -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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
25
objects/BankAccountView.go
Normal file
25
objects/BankAccountView.go
Normal file
@ -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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user