Skip to content

Instantly share code, notes, and snippets.

@mkock
Created August 20, 2020 19:15
Show Gist options
  • Select an option

  • Save mkock/e054dc9db33ef34df86230511bd7d268 to your computer and use it in GitHub Desktop.

Select an option

Save mkock/e054dc9db33ef34df86230511bd7d268 to your computer and use it in GitHub Desktop.
Bus Ride, code snippet 17
// StopsAt checks if Bus stops at the given BusStop, and returns true if it does, and false otherwise.
func (b Bus) StopsAt(busStop *BusStop) bool {
for _, stop := range b.stops {
if stop.Equals(busStop) { // Some kind of equality check, anyway.
return true
}
}
return false
}
// CurrentStop returns the BusStop that the Bus is currently stopped at.
func (b Bus) CurrentStop() *BusStop {
return b.stops[b.currentStop]
}
// AddStop adds the given BusStop to the list of stops that the Bus will stop at. Each stop is visited in order.
func (b *Bus) AddStop(busStop *BusStop) {
b.stops = append(b.stops, busStop)
}
// Go takes the Bus to the next BusStop. Go returns true if there are still more stops to visit.
func (b *Bus) Go() bool {
b.currentStop++
lastIndex := int16(len(b.stops) - 1)
if b.currentStop == lastIndex {
fmt.Printf("%s: reached the end of the line, everybody out\n", b.name)
b.VisitPassengers(func(p Passenger) {
b.Remove(p)
})
return false
}
if b.currentStop == 0 {
        fmt.Printf("%s: starting\n", b.name)
} else {
fmt.Printf("%s: carrying %d passengers: heading for next stop\n", b.name, len(b.passengers))
}
curr := b.stops[b.currentStop]
fmt.Printf("%s: arriving at %q\n", b.name, curr.Name)
curr.NotifyBusArrival(b)
return b.currentStop < lastIndex
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment