1
0
Fork 0
pr3-sose2026-fork/go/06-airport/airport/securityCheck.go

69 lines
1.5 KiB
Go

package airport
import (
"fmt"
"time"
)
var prohibitedItems = map[string]bool{"Knife": true, "Gun": true, "Explosives": true}
type SecurityCheck struct {
processingTime time.Duration
queue chan securityCheckData
}
type securityCheckData struct {
p Passenger
err chan error
}
func NewSecurityCheck(processingTime time.Duration) SecurityCheck {
return SecurityCheck{
processingTime: processingTime,
queue: make(chan securityCheckData),
}
}
// processes one passenger at a time. Each passenger must at least spend the processingTime at the security check
func (sc *SecurityCheck) Start() {
defer fmt.Println("security terminated")
for el := range sc.queue {
time.Sleep(sc.processingTime)
if el.p.BoardingPass == nil {
el.err <- fmt.Errorf("no boarding pass")
continue
}
if el.p.Name != el.p.BoardingPass.Name {
el.err <- fmt.Errorf("boarding pass does not match passenger name")
continue
}
prohibited := false
for _, item := range el.p.CarryOnItems {
if prohibitedItems[item] {
el.err <- fmt.Errorf("prohibited item detected: %v", item)
prohibited = true
break
}
}
if prohibited {
continue
}
el.err <- nil
}
}
// returns an error if
// - the passenger has no boarding pass
// - the passenger's boardings pass does not match the name
// - the passenger carries a prohibited item
func (sc SecurityCheck) Process(p Passenger) error {
errc := make(chan error)
sc.queue <- securityCheckData{p: p, err: errc}
return <-errc
}