trains

Notifications for the new MBTA trains. Probably no longer functional.

git clone https://code.pdelong.com/trains.git

 1package main
 2
 3// Definitions related to the Train type
 4
 5import (
 6	"fmt"
 7)
 8
 9// Train contains all of the information we care about for a single train.
10type Train struct {
11	Line      int
12	Cars      []int
13	Stop      string
14	Direction int
15	Trip      string
16}
17
18func IsCarNew(car, line int) bool {
19	switch line {
20	case Red:
21		return car >= 1900 && car <= 2151
22	case Orange:
23		return car >= 1400 && car <= 1551
24	case GreenB, GreenC, GreenD, GreenE:
25		return car >= 3900 && car <= 3924 && car != 3920
26	default:
27		panic("Got LineUnknown in IsNew")
28	}
29}
30
31// IsNew returns true if any of the cars in the train are new.
32func (t *Train) IsNew() bool {
33	for _, car := range t.Cars {
34		if IsCarNew(car, t.Line) {
35			return true
36		}
37	}
38
39	return false
40}
41
42func (t Train) String() string {
43	var isnew string
44	if t.IsNew() {
45		isnew = "is"
46	} else {
47		isnew = "is not"
48	}
49	return fmt.Sprintf(
50		"%s line train (%v) headed %s at %s. It %s a new train",
51		LineToString(t.Line),
52		t.Cars,
53		DirectionToString(t.Direction),
54		t.Stop,
55		isnew)
56}