Skip to content

Instantly share code, notes, and snippets.

View mkock's full-sized avatar
💭
Currently building stuff in Go and Vue.js

Martin Kock mkock

💭
Currently building stuff in Go and Vue.js
View GitHub Profile
@mkock
mkock / busride_code_snippet_12.go
Created August 20, 2020 19:08
Bus Ride, code snippet 12
// Prospect is a potential Passenger. Prospects wait at BusStops to board Buses.
type Prospect struct {
SSN string
Destination *BusStop
}
// BusStop represents a place where a Bus can stop and signal to prospects (future passengers)
// that they may board.
type BusStop struct {
Name string
@mkock
mkock / busride_code_snippet_13.go
Created August 20, 2020 19:10
Bus Ride, code snippet 13
// NotifyProspectArrival is called whenever a prospect arrives at Busstop.
func (b *BusStop) NotifyProspectArrival(p Prospect) {
b.prospects = append(b.prospects, p)
// Notify all Busses on this route.
for _, bus := range b.busses {
if bus.StopsAt(p.Destination) {
bus.NotifyBoardingIntent(b)
}
}
}
@mkock
mkock / busride_code_snippet_14.go
Created August 20, 2020 19:11
Bus Ride, code snippet 14
// NotifyBoardingIntent is called by BusStop every time a Prospect arrives and instructs the Bus to signal its arrival at that BusStop.
func (b *Bus) NotifyBoardingIntent(busStop *BusStop) {
if b.StopsAt(busStop) {
return // We already intend to stop here.
}
b.addBusStop(busStop)
}
@mkock
mkock / busride_code_snippet_15.go
Created August 20, 2020 19:12
Bus Ride, code snippet 15
// NotifyArrival notifies the current BusStop that the Bus has arrived.
func (b *Bus) NotifyArrival() {
curr := b.stops[b.currentStop]
curr.NotifyBusArrival(b)
}
@mkock
mkock / busride_code_snippet_16.go
Created August 20, 2020 19:14
Bus Ride, code snippet 16
// Bus carries Passengers from A to B if they have a valid bus ticket.
type Bus struct {
name string
passengers Passengers
stops []*BusStop
currentStop int16
}
@mkock
mkock / busride_code_snippet_17.go
Created August 20, 2020 19:15
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
}
@mkock
mkock / busride_code_snippet_18.go
Created August 20, 2020 19:16
Bus Ride, code snippet 18
// NewBus returns a new Bus with an empty passenger set.
func NewBus(name string) Bus {
b := Bus{}
        b.name = name
b.currentStop = -1
b.passengers = NewPassengerSet()
return b
}
@mkock
mkock / busride_code_snippet_19.go
Created August 20, 2020 19:17
Bus Ride, code snippet 19
// NotifyBusArrival is called by Bus upon arrival.
func (b *BusStop) NotifyBusArrival(bus *Bus) {
for _, p := range b.prospects {
if bus.StopsAt(p.Destination) {
bus.Add(p.ToPassenger())
}
}
}
// Equals returns true if the given BusStop is the same as the receiver.
@mkock
mkock / busride_code_snippet_20.go
Created August 20, 2020 19:18
Bus Ride, code snippet 20
// ToPassenger returns a Passenger with the same SSN as his or her Prospect.
func (p Prospect) ToPassenger() Passenger {
return Passenger{SSN: p.SSN, , Destination: p.Destination}
}
@mkock
mkock / busride_code_snippet_21.go
Created August 20, 2020 19:19
Bus Ride, code snippet 21
func main() {
fmt.Println("Starting simulation")
expressLine := busservice.NewBus("Express Line")
s1 := busservice.BusStop{Name: "Downtown"}
s2 := busservice.BusStop{Name: "The University"}
s3 := busservice.BusStop{Name: "The Village"}
expressLine.AddStop(&s1)
expressLine.AddStop(&s2)