Skip to content

Instantly share code, notes, and snippets.

@ali2210
Created August 27, 2020 16:24
Show Gist options
  • Select an option

  • Save ali2210/eb75ee7f44c93cfe0a818c96ebbe80a5 to your computer and use it in GitHub Desktop.

Select an option

Save ali2210/eb75ee7f44c93cfe0a818c96ebbe80a5 to your computer and use it in GitHub Desktop.
Transfer Money (Interface)
package main
import (
"fmt"
)
type Self bool
type Person struct{
name string
accNo string
transactionStatus bool
balance int
}
type PersonRec interface {
SetName(name string)
SetAccountNo(acc string)
SetTransactionStatus(state bool)
SetBalance(amount int)
GetName()string
GetBalance()int
GetTransactStatus()bool
GetAccNo()string
}
type Current interface{
Deposite(balance int, state Self)bool
Return(balance int, state Self)(bool, int)
Transfer(balance int , a *Person, state Self)(bool, int)
}
func main() {
p := [3]Person{}
p[0].SetName("Ali")
p[0].SetAccountNo("00x1")
p[0].SetTransactionStatus(false)
p[0].SetBalance(25000)
p[0].Deposite(2000,true)
p[0].Return(3000, true)
fmt.Println("Person:", p[0])
p[1].SetName("Marraim")
p[1].SetAccountNo("00x20")
p[1].SetTransactionStatus(false)
p[1].SetBalance(30000)
p[1].Deposite(2000,true)
p[1].Return(3000, true)
p[1].Transfer(1000,&p[0],true)
fmt.Println("Person:", p[1])
}
func(p *Person)SetName(name string) {(*p).name = name}
func(p *Person)SetAccountNo(acc string) {(*p).accNo = acc}
func(p *Person)SetTransactionStatus(state bool){(*p).transactionStatus = state}
func(p *Person)SetBalance(amount int){(*p).balance = amount}
func(p *Person)GetName()string{return (*p).name}
func(p *Person)GetBalance()int{return (*p).balance}
func(p *Person)GetTransactStatus()bool{return (*p).transactionStatus}
func(p *Person)GetAccNo()string{return (*p).accNo}
func(p *Person)Deposite(balance int, state Self)bool{
if state{
(*p).balance +=balance
(*p).SetBalance((*p).balance)
}
(*p).SetTransactionStatus(true)
return (*p).GetTransactStatus()
}
func(p *Person)Return(balance int, state Self)(bool, int){
if state{
(*p).balance -= balance
(*p).SetBalance((*p).balance)
}
(*p).SetTransactionStatus(true)
return (*p).GetTransactStatus(),(*p).GetBalance()
}
func(p *Person)Transfer(balance int , a *Person, state Self)(bool, int){
if !state{
(*p).balance -= balance
(*p).SetBalance((*p).balance)
(*a).balance += balance
(*a).SetBalance((*a).balance)
}
(*a).SetTransactionStatus(true)
return (*a).GetTransactStatus(),(*a).GetBalance()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment