Golang:如何实现转账方法/函数

huangapple go评论77阅读模式
英文:

Golang: How to implement the transfer method/function

问题

我有以下领域。我们如何实现Transfer功能,可以将amount从一个账户转移到另一个账户。我应该能够从储蓄账户转账到支票账户,反之亦然。在面向对象的世界中,超类型使得这更容易。我想知道我们如何在Go语言中实现这一点。

type AccountData struct {
    Num      string
    Name     string
    OpenDate time.Time
    Balance  float64
}

type SavingsAccount struct {
    InterestRate float32
    AccountData
}

type CheckingAccount struct {
    TransactionFee float32
    AccountData
}

type Account interface {
    Deposit(amount float64) error
    Withdraw(amount float64) error
}

//构造函数
func OpenSavingsAccount(no string, name string, openingDate time.Time) SavingsAccount {
    return SavingsAccount{
        AccountData: AccountData{Num: no,
            Name:     name,
            OpenDate: openingDate,
        },
        InterestRate: 0.9,
    }
}

func OpenCheckingAccount(no string, name string, openingDate time.Time) CheckingAccount {
    return CheckingAccount{
        AccountData: AccountData{Num: no,
            Name:     name,
            OpenDate: openingDate,
        },
        TransactionFee: 0.15,
    }
}

//账户方法
func (acct *SavingsAccount) Withdraw(amount float64) error {
    if acct.Balance < amount {
        return errors.New("Not enough money to withdraw")
    }
    acct.Balance = acct.Balance - amount
    return nil
}

func (acct *SavingsAccount) Deposit(amount float64) error {
    fmt.Printf("Depositing %f \n", amount)
    acct.Balance = acct.Balance + amount
    return nil
}
func (acct *CheckingAccount) Deposit(amount float64) error {
    fmt.Printf("Depositing %f \n", amount)
    acct.Balance = acct.Balance + amount
    return nil
}
func (acct *CheckingAccount) Withdraw(amount float64) error {
    if acct.Balance < amount {
        return errors.New("Not enough money to withdraw")
    }
    acct.Balance = acct.Balance - amount
    return nil
}

以上是你提供的代码。

英文:

I've the following domain. How do we implement the Transfer functionality that can transfer an amount from one account to another account. I should be able be transfer from a savings to checking and vice versa. I OOP world, a super type makes it easier. I am wondering how we accomplish this in Go.

type AccountData struct {
Num      string
Name     string
OpenDate time.Time
Balance  float64
}
type SavingsAccount struct {
InterestRate float32
AccountData
}
type CheckingAccount struct {
TransactionFee float32
AccountData
}
type Account interface {
Deposit(amount float64) error
Withdraw(amount float64) error
}
//constructor functions
func OpenSavingsAccount(no string, name string, openingDate time.Time) SavingsAccount {
return SavingsAccount{
AccountData: AccountData{Num: no,
Name:     name,
OpenDate: openingDate,
},
InterestRate: 0.9,
}
}
func OpenCheckingAccount(no string, name string, openingDate time.Time) CheckingAccount {
return CheckingAccount{
AccountData: AccountData{Num: no,
Name:     name,
OpenDate: openingDate,
},
TransactionFee: 0.15,
}
}
//Account methods
func (acct *SavingsAccount) Withdraw(amount float64) error {
if acct.Balance &lt; amount {
return errors.New(&quot;Not enough money to withdraw&quot;)
}
acct.Balance = acct.Balance - amount
return nil
}
func (acct *SavingsAccount) Deposit(amount float64) error {
fmt.Printf(&quot;Depositing %f \n&quot;, amount)
acct.Balance = acct.Balance + amount
return nil
}
func (acct *CheckingAccount) Deposit(amount float64) error {
fmt.Printf(&quot;Depositing %f \n&quot;, amount)
acct.Balance = acct.Balance + amount
return nil
}
func (acct *CheckingAccount) Withdraw(amount float64) error {
if acct.Balance &lt; amount {
return errors.New(&quot;Not enough money to withdraw&quot;)
}
acct.Balance = acct.Balance - amount
return nil
}

答案1

得分: 3

func Transfer(to, from Account, amount float64) error {
if err := from.Withdraw(amount); err != nil {
return err
}
if err := to.Deposit(amount); err != nil {
if err := from.Deposit(amount); err != nil {
// from 应该被警告,他们的钱刚刚消失了
}
return err
}
return nil
}

作为一项练习,设计一个接口,其中交易是原子性的,可能是值得的。

英文:
func Transfer(to, from Account, amount float64) error {
if err := from.Withdraw(amount); err != nil {
return err
}
if err := to.Deposit(amount); err != nil {
if err := from.Deposit(amount); err != nil {
// `from` should be alerted that their money
// just vanished into thin air
}
return err
}
return nil
}

As an exercise, it might be worthwhile to design an interface in which transactions are atomic.

huangapple
  • 本文由 发表于 2017年1月6日 09:20:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/41497477.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定