英文:
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 < 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
}
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论