|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "math" |
| 7 | +) |
| 8 | + |
| 9 | +/* -- exercise */ |
| 10 | +/* incorrect */ |
| 11 | + |
| 12 | +type Invoice struct { |
| 13 | + Amount float64 |
| 14 | + IsDiscounted bool |
| 15 | +} |
| 16 | + |
| 17 | +func (i *Invoice) CalculateTotal() float64 { |
| 18 | + if i.IsDiscounted { |
| 19 | + return i.Amount * 0.9 |
| 20 | + } |
| 21 | + return i.Amount |
| 22 | +} |
| 23 | + |
| 24 | +/* correct */ |
| 25 | + |
| 26 | +type InvoiceC interface { |
| 27 | + CalculateTotal() float64 |
| 28 | +} |
| 29 | + |
| 30 | +type RegularInvoice struct { |
| 31 | + Amount float64 |
| 32 | +} |
| 33 | + |
| 34 | +func (i RegularInvoice) CalculateTotal() float64 { |
| 35 | + return i.Amount |
| 36 | +} |
| 37 | + |
| 38 | +type DiscountedInvoice struct { |
| 39 | + Amount float64 |
| 40 | + DiscountRate float64 |
| 41 | +} |
| 42 | + |
| 43 | +func (i DiscountedInvoice) CalculateTotal() float64 { |
| 44 | + return i.Amount * (1 - i.DiscountRate) |
| 45 | +} |
| 46 | + |
| 47 | +/* -- extra challenge */ |
| 48 | + |
| 49 | +type Operation interface { |
| 50 | + Calculate(a, b float64) (float64, error) |
| 51 | +} |
| 52 | + |
| 53 | +type Addition struct{} |
| 54 | + |
| 55 | +func (Addition) Calculate(a, b float64) (float64, error) { |
| 56 | + return a + b, nil |
| 57 | +} |
| 58 | + |
| 59 | +type Subtraction struct{} |
| 60 | + |
| 61 | +func (Subtraction) Calculate(a, b float64) (float64, error) { |
| 62 | + return a - b, nil |
| 63 | +} |
| 64 | + |
| 65 | +type Multiplication struct{} |
| 66 | + |
| 67 | +func (Multiplication) Calculate(a, b float64) (float64, error) { |
| 68 | + return a * b, nil |
| 69 | +} |
| 70 | + |
| 71 | +type Division struct{} |
| 72 | + |
| 73 | +func (Division) Calculate(a, b float64) (float64, error) { |
| 74 | + if b == 0 { |
| 75 | + return 0, errors.New("cannot divide by zero") |
| 76 | + } |
| 77 | + return a / b, nil |
| 78 | +} |
| 79 | + |
| 80 | +type Calculator struct { |
| 81 | + operations map[string]Operation |
| 82 | +} |
| 83 | + |
| 84 | +func NewCalculator() *Calculator { |
| 85 | + return &Calculator{ |
| 86 | + operations: make(map[string]Operation), |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func (c *Calculator) AddOperation(name string, operation Operation) { |
| 91 | + c.operations[name] = operation |
| 92 | +} |
| 93 | + |
| 94 | +func (c *Calculator) Calculate(name string, a, b float64) (float64, error) { |
| 95 | + operation, found := c.operations[name] |
| 96 | + if !found { |
| 97 | + return 0, fmt.Errorf("operation %s not found", name) |
| 98 | + } |
| 99 | + return operation.Calculate(a, b) |
| 100 | +} |
| 101 | + |
| 102 | +type Power struct{} |
| 103 | + |
| 104 | +func (Power) Calculate(a, b float64) (float64, error) { |
| 105 | + return math.Pow(a, b), nil |
| 106 | +} |
| 107 | + |
| 108 | +func main() { |
| 109 | + /* -- exercise */ |
| 110 | + /* exercise -- incorrect */ |
| 111 | + invoice := Invoice{Amount: 100, IsDiscounted: true} |
| 112 | + fmt.Printf("Total: %.2f\n", invoice.CalculateTotal()) |
| 113 | + |
| 114 | + /* exercise -- correct */ |
| 115 | + regularInvoice := RegularInvoice{Amount: 100} |
| 116 | + discountedInvoice := DiscountedInvoice{Amount: 100, DiscountRate: 0.1} |
| 117 | + |
| 118 | + invoices := []InvoiceC{regularInvoice, discountedInvoice} |
| 119 | + |
| 120 | + for _, invoice := range invoices { |
| 121 | + fmt.Printf("Total: %.2f\n", invoice.CalculateTotal()) |
| 122 | + } |
| 123 | + |
| 124 | + /* extra challenge */ |
| 125 | + calculator := NewCalculator() |
| 126 | + |
| 127 | + calculator.AddOperation("add", Addition{}) |
| 128 | + calculator.AddOperation("subtract", Subtraction{}) |
| 129 | + calculator.AddOperation("multiply", Multiplication{}) |
| 130 | + calculator.AddOperation("divide", Division{}) |
| 131 | + calculator.AddOperation("power", Power{}) |
| 132 | + |
| 133 | + a, b := 6.0, 2.0 |
| 134 | + |
| 135 | + result, _ := calculator.Calculate("add", a, b) |
| 136 | + fmt.Printf("Add: %.2f + %.2f = %.2f\n", a, b, result) |
| 137 | + |
| 138 | + result, _ = calculator.Calculate("subtract", a, b) |
| 139 | + fmt.Printf("Subtract: %.2f - %.2f = %.2f\n", a, b, result) |
| 140 | + |
| 141 | + result, _ = calculator.Calculate("multiply", a, b) |
| 142 | + fmt.Printf("Multiply: %.2f * %.2f = %.2f\n", a, b, result) |
| 143 | + |
| 144 | + result, err := calculator.Calculate("divide", a, b) |
| 145 | + if err != nil { |
| 146 | + fmt.Println("Error:", err) |
| 147 | + } else { |
| 148 | + fmt.Printf("Divide: %.2f / %.2f = %.2f\n", a, b, result) |
| 149 | + } |
| 150 | + |
| 151 | + result, _ = calculator.Calculate("power", a, b) |
| 152 | + fmt.Printf("Power: %.2f ^ %.2f = %.2f\n", a, b, result) |
| 153 | +} |
0 commit comments