Introduction
This tutorial will guide you through creating a simple Bank Account Simulator using the Go programming language. The main objective is to simulate basic banking functionalities such as deposits, withdrawals, and checking account balances. You’ll learn how to manage a simple bank account with user input and make updates to the account based on the user’s actions.
Objective
Our goal is to create a basic bank system that supports the following operations:
- Deposit money into the account
- Withdraw money from the account
- Check the current balance of the account
The program will allow the user to interact with these functionalities in a loop, demonstrating a simple, real-world application of a bank account simulator.
Code for Bank Account Simulator in Go
package main import ( "fmt" "errors" ) type BankAccount struct { balance float64 } func (account *BankAccount) Deposit(amount float64) { if amount <= 0 { fmt.Println("Invalid deposit amount. Please enter a positive value.") return } account.balance += amount fmt.Printf("Successfully deposited $%.2f. New balance: $%.2f\n", amount, account.balance) } func (account *BankAccount) Withdraw(amount float64) error { if amount <= 0 { return errors.New("invalid withdrawal amount. Please enter a positive value.") } if amount > account.balance { return errors.New("insufficient funds.") } account.balance -= amount fmt.Printf("Successfully withdrew $%.2f. New balance: $%.2f\n", amount, account.balance) return nil } func (account *BankAccount) CheckBalance() { fmt.Printf("Current balance: $%.2f\n", account.balance) } func main() { account := &BankAccount{balance: 0.0} var choice int for { fmt.Println("\nWelcome to the Bank Account Simulator") fmt.Println("1. Deposit Money") fmt.Println("2. Withdraw Money") fmt.Println("3. Check Balance") fmt.Println("4. Exit") fmt.Print("Please enter your choice: ") fmt.Scanln(&choice) switch choice { case 1: var depositAmount float64 fmt.Print("Enter amount to deposit: $") fmt.Scanln(&depositAmount) account.Deposit(depositAmount) case 2: var withdrawAmount float64 fmt.Print("Enter amount to withdraw: $") fmt.Scanln(&withdrawAmount) err := account.Withdraw(withdrawAmount) if err != nil { fmt.Println(err) } case 3: account.CheckBalance() case 4: fmt.Println("Thank you for using the Bank Account Simulator. Goodbye!") return default: fmt.Println("Invalid choice. Please try again.") } } }
Explanation of the Program
This program defines a BankAccount
struct to represent a bank account with an associated balance
. The following methods are implemented on the BankAccount
struct:
Deposit(amount float64)
: This method adds the given amount to the balance if it’s positive. If the deposit amount is invalid (<= 0), it notifies the user.Withdraw(amount float64)
: This method subtracts the given amount from the balance, but only if the account has sufficient funds. If there are not enough funds, it returns an error.CheckBalance()
: This method simply displays the current balance of the account.
The main()
function contains a loop that continuously prompts the user for input to select one of the four options: deposit, withdraw, check balance, or exit. Based on the user’s choice, the corresponding method is called.
How to Run the Program
- Install Go from here if you don’t have it already.
- Save the code in a file called
bank_account.go
. - Open a terminal or command prompt and navigate to the directory where the file is located.
- Run the command
go run bank_account.go
to execute the program. - Interact with the program by entering the corresponding number for each option.