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")
|
||||
|
||||
Reference in New Issue
Block a user