pointers
This commit is contained in:
42
bank/bank.go
42
bank/bank.go
@@ -1,36 +1,17 @@
|
||||
package main
|
||||
|
||||
import "errors"
|
||||
import "fmt"
|
||||
import "os"
|
||||
import "strconv"
|
||||
import (
|
||||
"example.com/bank/fileops"
|
||||
"fmt"
|
||||
"github.com/Pallinder/go-randomdata"
|
||||
)
|
||||
|
||||
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() {
|
||||
var accountBalance, err = getBalanceFromFile()
|
||||
var accountBalance, err = fileops.GetFloatFromFile(accountBalanceFile)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("ERROR")
|
||||
@@ -39,13 +20,10 @@ func main() {
|
||||
}
|
||||
|
||||
fmt.Println("Welcome to Go Bank!")
|
||||
fmt.Println(randomdata.PhoneNumber())
|
||||
|
||||
for {
|
||||
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")
|
||||
presentOptions()
|
||||
|
||||
var choice int
|
||||
fmt.Print("Your choice: ")
|
||||
@@ -66,7 +44,7 @@ func main() {
|
||||
|
||||
accountBalance += accountBalance
|
||||
fmt.Println("Balance updated! New amount:", accountBalance)
|
||||
writeBalanceToFile(accountBalance)
|
||||
fileops.WriteFloatToFile(accountBalance, accountBalanceFile)
|
||||
case 3:
|
||||
fmt.Print("Your withdrawal")
|
||||
var withdrawalAmount float64
|
||||
@@ -84,7 +62,7 @@ func main() {
|
||||
|
||||
accountBalance -= withdrawalAmount
|
||||
fmt.Println("Balance updated! New amount:", accountBalance)
|
||||
writeBalanceToFile(accountBalance)
|
||||
fileops.WriteFloatToFile(accountBalance, accountBalanceFile)
|
||||
default:
|
||||
fmt.Println("Goodbye!")
|
||||
fmt.Println("Thanks for choosing our bank")
|
||||
|
||||
11
bank/communication.go
Normal file
11
bank/communication.go
Normal 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
30
bank/fileops/fileops.go
Normal 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)
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
module example.com/bank
|
||||
|
||||
go 1.23.1
|
||||
|
||||
require github.com/Pallinder/go-randomdata v1.2.0 // indirect
|
||||
|
||||
2
bank/go.sum
Normal file
2
bank/go.sum
Normal 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=
|
||||
Reference in New Issue
Block a user