解答:方法與介面
您可以在這裡找到先前挑戰的解答。
商店套件
以下是商店套件的程式碼:
package store
import (
"errors"
"fmt"
)
type Account struct {
FirstName string
LastName string
}
type Employee struct {
Account
Credits float64
}
func (a *Account) ChangeName(newname string) {
a.FirstName = newname
}
func (e Employee) String() string {
return fmt.Sprintf("Name: %s %s\nCredits: %.2f\n", e.FirstName, e.LastName, e.Credits)
}
func CreateEmployee(firstName, lastName string, credits float64) (*Employee, error) {
return &Employee{Account{firstName, lastName}, credits}, nil
}
func (e *Employee) AddCredits(amount float64) (float64, error) {
if amount > 0.0 {
e.Credits += amount
return e.Credits, nil
}
return 0.0, errors.New("Invalid credit amount.")
}
func (e *Employee) RemoveCredits(amount float64) (float64, error) {
if amount > 0.0 {
if amount <= e.Credits {
e.Credits -= amount
return e.Credits, nil
}
return 0.0, errors.New("You can't remove more credits than the account has.")
}
return 0.0, errors.New("You can't remove negative numbers.")
}
func (e *Employee) CheckCredits() float64 {
return e.Credits
}
以下是用來測試所有功能的主要程式程式碼:
package main
import (
"fmt"
"store"
)
func main() {
bruce, _ := store.CreateEmployee("Bruce", "Lee", 500)
fmt.Println(bruce.CheckCredits())
credits, err := bruce.AddCredits(250)
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("New Credits Balance = ", credits)
}
_, err = bruce.RemoveCredits(2500)
if err != nil {
fmt.Println("Can't withdraw or overdrawn!", err)
}
bruce.ChangeName("Mark")
fmt.Println(bruce)
}