This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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) | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // NotifyArrival notifies the current BusStop that the Bus has arrived. | |
| func (b *Bus) NotifyArrival() { | |
| curr := b.stops[b.currentStop] | |
| curr.NotifyBusArrival(b) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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} | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |