82 lines
1.8 KiB
Go
82 lines
1.8 KiB
Go
package main
|
|
|
|
import "fmt"
|
|
import "os"
|
|
import "strconv"
|
|
|
|
const accountBalanceFile = "balance.txt"
|
|
|
|
func getBalanceFromFile() float64 {
|
|
data, err := os.ReadFile(accountBalanceFile, )
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
balanceText := string(data)
|
|
balance, _ :=strconv.ParseFloat(balanceText, 64)
|
|
return balance
|
|
}
|
|
|
|
func writeBalanceToFile(balance float64) {
|
|
balanceText := fmt.Sprint(balance)
|
|
os.WriteFile(accountBalanceFile, []byte(balanceText), 0644)
|
|
}
|
|
|
|
func main() {
|
|
var accountBalance = getBalanceFromFile()
|
|
|
|
fmt.Println("Welcome to Go Bank!")
|
|
|
|
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")
|
|
|
|
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)
|
|
writeBalanceToFile(accountBalance)
|
|
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)
|
|
writeBalanceToFile(accountBalance)
|
|
default:
|
|
fmt.Println("Goodbye!")
|
|
fmt.Println("Thanks for choosing our bank")
|
|
return
|
|
}
|
|
}
|
|
} |