-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
75 lines (60 loc) · 1.31 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"errors"
"fmt"
)
type TicketService struct {
basePrice float64
discountedPrice float64
}
func NewTicketService(basePrice float64) (*TicketService, error) {
if basePrice <= 0 {
return nil, errors.New("base price must be positive")
}
return &TicketService{
basePrice: basePrice,
discountedPrice: basePrice * 0.9,
}, nil
}
func (s *TicketService) CalculatePrice(age int, isStudent bool) (float64, error) {
if age <= 0 {
return 0, errors.New("age must be positive")
}
price := s.basePrice
// Children under 10 get 50% off
if age < 10 {
price *= 0.5
}
// Seniors (70+) get 30% off
if age >= 70 {
price *= 0.7
}
// Students get additional 20% off
if isStudent {
price *= 0.8
}
// Apply an additional discount of 2% for everyone
price *= 0.98
return price, nil
}
func main() {
// Create a new ticket service with a base price of $10.00
s, err := NewTicketService(10.00)
if err != nil {
panic(err)
}
// Calculate the price for a 10-year old
price, err := s.CalculatePrice(10, false)
if err != nil {
panic(err)
}
// Print the price
fmt.Printf("Price: $%.2f\n", price)
// Calculate the price for a 25-year old student
price, err = s.CalculatePrice(25, true)
if err != nil {
panic(err)
}
// Print the price
fmt.Printf("Price: $%.2f\n", price)
}