-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Expand file tree
/
Copy pathbooking.go
More file actions
57 lines (50 loc) · 1.2 KB
/
booking.go
File metadata and controls
57 lines (50 loc) · 1.2 KB
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
package concertbookingsystem
import "fmt"
type Booking struct {
ID string
User *User
Concert *Concert
Seats []*Seat
TotalPrice float64
Status BookingStatus
}
func NewBooking(id string, user *User, concert *Concert, seats []*Seat) *Booking {
totalPrice := calculateTotalPrice(seats)
return &Booking{
ID: id,
User: user,
Concert: concert,
Seats: seats,
TotalPrice: totalPrice,
Status: BookingStatusPending,
}
}
func (b *Booking) ConfirmBooking() error {
if b.Status == BookingStatusPending {
b.Status = BookingStatusConfirmed
// TODO: Send booking confirmation to user
for _, seat := range b.Seats {
if seat.status == StatusBooked {
return NewSeatNotAvailableError(fmt.Sprintf("Seat %s is already booked", seat.ID))
}
seat.status = StatusBooked
}
}
return nil
}
func (b *Booking) CancelBooking() {
if b.Status == BookingStatusConfirmed {
b.Status = BookingStatusCancelled
for _, seat := range b.Seats {
seat.Release()
}
// TODO: Send cancellation notification to user
}
}
func calculateTotalPrice(seats []*Seat) float64 {
var total float64
for _, seat := range seats {
total += seat.Price
}
return total
}