dongqigu0429 2017-01-06 01:20
浏览 85
已采纳

Golang:如何实现传递方法/功能

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 < 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 
", amount)
    acct.Balance = acct.Balance + amount
    return nil
}
func (acct *CheckingAccount) Deposit(amount float64) error {
    fmt.Printf("Depositing %f 
", 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
}
  • 写回答

1条回答 默认 最新

  • doudiyu1639 2017-01-06 01:35
    关注
    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀