72 lines
1.5 KiB
Go
72 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"example.com/bank/fileops"
|
|
"fmt"
|
|
"github.com/Pallinder/go-randomdata"
|
|
)
|
|
|
|
const accountBalanceFile = "balance.txt"
|
|
|
|
|
|
|
|
func main() {
|
|
var accountBalance, err = fileops.GetFloatFromFile(accountBalanceFile)
|
|
|
|
if err != nil {
|
|
fmt.Println("ERROR")
|
|
fmt.Println(err)
|
|
fmt.Println("-------")
|
|
}
|
|
|
|
fmt.Println("Welcome to Go Bank!")
|
|
fmt.Println(randomdata.PhoneNumber())
|
|
|
|
for {
|
|
presentOptions()
|
|
|
|
var choice int
|
|
fmt.Print("Your choice: ")
|
|
fmt.Scan(&choice)
|
|
|
|
switch choice {
|
|
case 1:
|
|
fmt.Println("Your balance is: ", accountBalance)
|
|
case 2:
|
|
fmt.Print("Your deposit: ")
|
|
var depositAmount float64
|
|
fmt.Scan(&depositAmount)
|
|
|
|
if depositAmount <= 0 {
|
|
fmt.Println("Invalid amount. Must be greater than 0.")
|
|
continue
|
|
}
|
|
|
|
accountBalance += accountBalance
|
|
fmt.Println("Balance updated! New amount:", accountBalance)
|
|
fileops.WriteFloatToFile(accountBalance, accountBalanceFile)
|
|
case 3:
|
|
fmt.Print("Your withdrawal")
|
|
var withdrawalAmount float64
|
|
fmt.Scan(&withdrawalAmount)
|
|
|
|
if withdrawalAmount <= 0 {
|
|
fmt.Println("Invalid amount. Must be greater than 0.")
|
|
continue
|
|
}
|
|
|
|
if withdrawalAmount > accountBalance {
|
|
fmt.Println("Invalid amount. You can't withdraw more than you have.")
|
|
continue
|
|
}
|
|
|
|
accountBalance -= withdrawalAmount
|
|
fmt.Println("Balance updated! New amount:", accountBalance)
|
|
fileops.WriteFloatToFile(accountBalance, accountBalanceFile)
|
|
default:
|
|
fmt.Println("Goodbye!")
|
|
fmt.Println("Thanks for choosing our bank")
|
|
return
|
|
}
|
|
}
|
|
} |