-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Expand file tree
/
Copy pathseat.go
More file actions
62 lines (52 loc) · 1.05 KB
/
seat.go
File metadata and controls
62 lines (52 loc) · 1.05 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
58
59
60
61
62
package concertbookingsystem
import (
"fmt"
"sync"
"time"
)
type Seat struct {
ID string
SeatNumber string
Type SeatType
Price float64
status SeatStatus
mu sync.Mutex
LockUntil time.Time
}
func NewSeat(id, seatNumber string, seatType SeatType, price float64) *Seat {
return &Seat{
ID: id,
SeatNumber: seatNumber,
Type: seatType,
Price: price,
status: StatusAvailable,
}
}
func (s *Seat) Hold() error {
s.mu.Lock()
defer s.mu.Unlock()
if s.status != StatusAvailable {
return NewSeatNotAvailableError(fmt.Sprintf("SeatNo: %s Not Available ", s.ID))
}
s.status = StatusReserved
return nil
}
func (s *Seat) Book() error {
s.mu.Lock()
defer s.mu.Unlock()
if s.status != StatusAvailable {
return NewSeatNotAvailableError("Seat is already booked or reserved")
}
s.status = StatusBooked
return nil
}
func (s *Seat) Release() {
s.mu.Lock()
defer s.mu.Unlock()
s.status = StatusAvailable
}
func (s *Seat) GetStatus() SeatStatus {
s.mu.Lock()
defer s.mu.Unlock()
return s.status
}