Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- note
- description: "Route information displays."
- class
- DISPLAY
- inherit
- ZURICH_OBJECTS
- feature -- Explore Zurich
- add_public_transport
- -- Add a public transportation unit per line.
- do
- across
- Zurich.lines as line_pointer
- loop
- line_pointer.item.add_transport
- end
- end
- update_transport_display (t: PUBLIC_TRANSPORT)
- -- Update route information display inside transportation unit `t'.
- require
- t_exists: t /= Void
- local
- s: STATION
- total_stations_left: INTEGER
- i: INTEGER
- do
- -- Reset the console
- console.clear
- -- Count the number of stations left
- total_stations_left := 0
- from
- s := Zurich.station (t.arriving.name)
- invariant
- total_stations_left < Zurich.line (t.line.number).stations.count + 1
- until
- s = void
- loop
- total_stations_left := total_stations_left + 1
- s := Zurich.line (t.line.number).next_station (s, t.destination)
- variant
- Zurich.line (t.line.number).stations.count + 1 - total_stations_left
- end
- -- Output info
- if
- -- For 1 station left
- total_stations_left = 1
- then
- console.output ("Linie/Line " + t.line.number.out + " Willkommen/Welcome.")
- console.append_line (stop_info (t, t.destination))
- elseif
- -- For 2 stations left
- total_stations_left = 2
- then
- console.output ("Linie/Line " + t.line.number.out + " Willkommen/Welcome.")
- console.append_line (stop_info (t, t.arriving))
- console.append_line (stop_info (t, t.destination))
- elseif
- -- For 3 stations left
- total_stations_left = 3
- then
- console.output ("Linie/Line " + t.line.number.out + " Willkommen/Welcome.")
- from
- s := Zurich.station (t.arriving.name)
- i := 1
- invariant
- i < Zurich.line (t.line.number).stations.count + 1
- until
- i = 3
- loop
- i := i + 1
- console.append_line (stop_info (t, s))
- s := Zurich.line (t.line.number).next_station (s, t.destination)
- variant
- Zurich.line (t.line.number).stations.count + 1 - i
- end
- console.append_line (stop_info (t, t.destination))
- elseif
- -- For more than 3 stations left
- total_stations_left > 3
- then
- console.output ("Linie/Line " + t.line.number.out + " Willkommen/Welcome.")
- from
- s := Zurich.station (t.arriving.name)
- i := 1
- invariant
- i < Zurich.line (t.line.number).stations.count + 1
- until
- i = 4
- loop
- i := i + 1
- console.append_line (stop_info (t, s))
- s := Zurich.line (t.line.number).next_station (s, t.destination)
- variant
- Zurich.line (t.line.number).stations.count + 1 - i
- end
- console.append_line ("...")
- console.append_line (stop_info (t, t.destination))
- else
- -- The something-is-not-right case!
- console.append_line ("Oops, something went wrong.. please click again or terminate.")
- end
- end
- stop_info (t: PUBLIC_TRANSPORT; s: STATION): STRING
- -- Get the station info for the next stop(s)
- require
- t_is_there: t /= void
- s_is_there: s /= void
- local
- minutes: INTEGER
- looping_line: LINE
- do
- -- Time to station
- if
- t.time_to_station (s) < 60
- then
- Result := "<1 Min.%T" + s.name
- else
- minutes := t.time_to_station (s) // 60
- Result := minutes.out + " Min.%T" + s.name
- end
- -- Connecting lines at station
- if s.lines.is_empty = false then
- across
- s.lines as z
- loop
- if z.item.has_station (t.line.next_station (s, t.destination)) or z.item.has_station (t.line.next_station (s, t.line.first)) then
- looping_line := void
- else
- looping_line := z.item
- end
- if looping_line /= void then
- Result.append (" " + looping_line.number.out)
- end
- end
- end
- ensure
- Result_is_there: Result /= void
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement