This commit is contained in:
2024-10-20 21:07:51 +00:00
parent ac623d6d49
commit ed60288a2a
8 changed files with 82 additions and 32 deletions

View File

@@ -1,36 +1,17 @@
package main package main
import "errors" import (
import "fmt" "example.com/bank/fileops"
import "os" "fmt"
import "strconv" "github.com/Pallinder/go-randomdata"
)
const accountBalanceFile = "balance.txt" const accountBalanceFile = "balance.txt"
func getBalanceFromFile() (float64, error) {
data, err := os.ReadFile(accountBalanceFile, )
if err != nil {
return 1000, errors.New("Failed to find balance file.")
}
balanceText := string(data)
balance, err :=strconv.ParseFloat(balanceText, 64)
if err != nil {
return 1000, errors.New("Failed to parse stored balance value.")
}
return balance, nil
}
func writeBalanceToFile(balance float64) {
balanceText := fmt.Sprint(balance)
os.WriteFile(accountBalanceFile, []byte(balanceText), 0644)
}
func main() { func main() {
var accountBalance, err = getBalanceFromFile() var accountBalance, err = fileops.GetFloatFromFile(accountBalanceFile)
if err != nil { if err != nil {
fmt.Println("ERROR") fmt.Println("ERROR")
@@ -39,13 +20,10 @@ func main() {
} }
fmt.Println("Welcome to Go Bank!") fmt.Println("Welcome to Go Bank!")
fmt.Println(randomdata.PhoneNumber())
for { for {
fmt.Println("What do you want to do?") presentOptions()
fmt.Println("1. Check balance")
fmt.Println("2. Deposit money")
fmt.Println("3. Withdraw money")
fmt.Println("4. Exit")
var choice int var choice int
fmt.Print("Your choice: ") fmt.Print("Your choice: ")
@@ -66,7 +44,7 @@ func main() {
accountBalance += accountBalance accountBalance += accountBalance
fmt.Println("Balance updated! New amount:", accountBalance) fmt.Println("Balance updated! New amount:", accountBalance)
writeBalanceToFile(accountBalance) fileops.WriteFloatToFile(accountBalance, accountBalanceFile)
case 3: case 3:
fmt.Print("Your withdrawal") fmt.Print("Your withdrawal")
var withdrawalAmount float64 var withdrawalAmount float64
@@ -84,7 +62,7 @@ func main() {
accountBalance -= withdrawalAmount accountBalance -= withdrawalAmount
fmt.Println("Balance updated! New amount:", accountBalance) fmt.Println("Balance updated! New amount:", accountBalance)
writeBalanceToFile(accountBalance) fileops.WriteFloatToFile(accountBalance, accountBalanceFile)
default: default:
fmt.Println("Goodbye!") fmt.Println("Goodbye!")
fmt.Println("Thanks for choosing our bank") fmt.Println("Thanks for choosing our bank")

11
bank/communication.go Normal file
View File

@@ -0,0 +1,11 @@
package main
import "fmt"
func presentOptions() {
fmt.Println("What do you want to do?")
fmt.Println("1. Check balance")
fmt.Println("2. Deposit money")
fmt.Println("3. Withdraw money")
fmt.Println("4. Exit")
}

30
bank/fileops/fileops.go Normal file
View File

@@ -0,0 +1,30 @@
package fileops
import (
"errors"
"fmt"
"os"
"strconv"
)
func GetFloatFromFile(fileName string) (float64, error) {
data, err := os.ReadFile(fileName)
if err != nil {
return 1000, errors.New("Failed to find file.")
}
valueText := string(data)
value, err :=strconv.ParseFloat(valueText, 64)
if err != nil {
return 1000, errors.New("Failed to parse stored value.")
}
return value, nil
}
func WriteFloatToFile(value float64, fileName string) {
valueText := fmt.Sprint(value)
os.WriteFile(fileName, []byte(valueText), 0644)
}

View File

@@ -1,3 +1,5 @@
module example.com/bank module example.com/bank
go 1.23.1 go 1.23.1
require github.com/Pallinder/go-randomdata v1.2.0 // indirect

2
bank/go.sum Normal file
View File

@@ -0,0 +1,2 @@
github.com/Pallinder/go-randomdata v1.2.0 h1:DZ41wBchNRb/0GfsePLiSwb0PHZmT67XY00lCDlaYPg=
github.com/Pallinder/go-randomdata v1.2.0/go.mod h1:yHmJgulpD2Nfrm0cR9tI/+oAgRqCQQixsA8HyRZfV9Y=

2
go.sum Normal file
View File

@@ -0,0 +1,2 @@
github.com/Pallinder/go-randomdata v1.2.0 h1:DZ41wBchNRb/0GfsePLiSwb0PHZmT67XY00lCDlaYPg=
github.com/Pallinder/go-randomdata v1.2.0/go.mod h1:yHmJgulpD2Nfrm0cR9tI/+oAgRqCQQixsA8HyRZfV9Y=

3
pointers/go.mod Normal file
View File

@@ -0,0 +1,3 @@
module example.com/pointers
go 1.23.1

22
pointers/pointers.go Normal file
View File

@@ -0,0 +1,22 @@
package main
import "fmt"
func main() {
age := 32
var agePointer *int
agePointer = &age
fmt.Println("Age:", *agePointer)
editAgeToAdultYears(agePointer)
fmt.Println(age)
}
func editAgeToAdultYears(age *int) {
// return *age - 18
*age = *age - 18
}