Banking Management System in Golang

Go, commonly known as Golang, is a statically typed, compiled language with syntax similar to that of C. Its efficiency, concurrency support, and simplicity make it a great choice for various applications, including creating a Banking Management System. In this blog post, we'll build a simple banking system in Golang, breaking down each code segment and discussing its functionality. 

System Overview

Our Banking Management System will offer the following features: 

  • Creating a new bank account. 
  • Checking an account's balance. 
  • Depositing money. 
  • Withdrawing money. 
  • Listing all accounts. 

Implementation

1. Account Struct: 

This struct will define the basic properties and behaviors of a bank account.

type Account struct {
    AccountNumber int
    HolderName    string
    Balance       float64
}

func (a *Account) Deposit(amount float64) {
    a.Balance += amount
}

func (a *Account) Withdraw(amount float64) {
    if amount <= a.Balance {
        a.Balance -= amount
    } else {
        fmt.Println("Insufficient funds!")
    }
}

2. Bank Struct: 

This struct will manage multiple accounts.

type Bank struct {
    accounts []Account
}

func (b *Bank) CreateAccount(accountNumber int, holderName string, initialBalance float64) {
    newAccount := Account{AccountNumber: accountNumber, HolderName: holderName, Balance: initialBalance}
    b.accounts = append(b.accounts, newAccount)
}

func (b *Bank) GetAccount(accountNumber int) *Account {
    for i, acc := range b.accounts {
        if acc.AccountNumber == accountNumber {
            return &b.accounts[i]
        }
    }
    return nil
}

func (b *Bank) ListAccounts() {
    for _, acc := range b.accounts {
        fmt.Printf("AccountNumber: %d, Holder: %s, Balance: $%.2f\n", acc.AccountNumber, acc.HolderName, acc.Balance)
    }
}

3. Main Function: 

This function serves as our application's entry point.

package main

import "fmt"

func main() {
    myBank := Bank{}

    // Create accounts
    myBank.CreateAccount(101, "John Doe", 1000.50)
    myBank.CreateAccount(102, "Jane Smith", 500.75)

    // List accounts
    fmt.Println("Listing all accounts:")
    myBank.ListAccounts()

    // Deposit money
    johnAccount := myBank.GetAccount(101)
    johnAccount.Deposit(200)
    fmt.Printf("\nJohn's updated balance after depositing $200: $%.2f\n", johnAccount.Balance)

    // Withdraw money
    johnAccount.Withdraw(50)
    fmt.Printf("John's updated balance after withdrawing $50: $%.2f\n", johnAccount.Balance)
}

Complete Source Code with Output

package main

import (
	"fmt"
)

type Account struct {
	AccountNumber int
	HolderName    string
	Balance       float64
}

func (a *Account) Deposit(amount float64) {
	a.Balance += amount
}

func (a *Account) Withdraw(amount float64) {
	if amount <= a.Balance {
		a.Balance -= amount
	} else {
		fmt.Println("Insufficient funds!")
	}
}

type Bank struct {
	accounts []Account
}

func (b *Bank) CreateAccount(accountNumber int, holderName string, initialBalance float64) {
	newAccount := Account{AccountNumber: accountNumber, HolderName: holderName, Balance: initialBalance}
	b.accounts = append(b.accounts, newAccount)
}

func (b *Bank) GetAccount(accountNumber int) *Account {
	for i, acc := range b.accounts {
		if acc.AccountNumber == accountNumber {
			return &b.accounts[i]
		}
	}
	return nil
}

func (b *Bank) ListAccounts() {
	for _, acc := range b.accounts {
		fmt.Printf("AccountNumber: %d, Holder: %s, Balance: $%.2f\n", acc.AccountNumber, acc.HolderName, acc.Balance)
	}
}

func main() {
	myBank := Bank{}

	// Create accounts
	myBank.CreateAccount(101, "John Doe", 1000.50)
	myBank.CreateAccount(102, "Jane Smith", 500.75)

	// List accounts
	fmt.Println("Listing all accounts:")
	myBank.ListAccounts()

	// Deposit money
	johnAccount := myBank.GetAccount(101)
	johnAccount.Deposit(200)
	fmt.Printf("\nJohn's updated balance after depositing $200: $%.2f\n", johnAccount.Balance)

	// Withdraw money
	johnAccount.Withdraw(50)
	fmt.Printf("John's updated balance after withdrawing $50: $%.2f\n", johnAccount.Balance)

	// Trying to withdraw more money than available
	fmt.Println("\nAttempting to withdraw $2000 from Jane's account:")
	janeAccount := myBank.GetAccount(102)
	janeAccount.Withdraw(2000)
	fmt.Printf("Jane's balance after attempted withdrawal: $%.2f\n", janeAccount.Balance)
}

Output

Listing all accounts:
AccountNumber: 101, Holder: John Doe, Balance: $1000.50
AccountNumber: 102, Holder: Jane Smith, Balance: $500.75

John's updated balance after depositing $200: $1200.50
John's updated balance after withdrawing $50: $1150.50

Attempting to withdraw $2000 from Jane's account:
Insufficient funds!
Jane's balance after attempted withdrawal: $500.75

This code demonstrates the creation of bank accounts, depositing funds into an account, and withdrawing funds from it. We also have a scenario where a withdrawal attempt exceeds the available balance, leading to an "Insufficient funds!" message.


Comments