Advertisement
PhilDrummer

Assignment_5 03

Oct 19th, 2014
2,727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Eiffel 3.80 KB | None | 0 0
  1. note
  2.     description: "Route information displays."
  3.  
  4. class
  5.     DISPLAY
  6.  
  7. inherit
  8.     ZURICH_OBJECTS
  9.  
  10. feature -- Explore Zurich
  11.  
  12.     add_public_transport
  13.             -- Add a public transportation unit per line.
  14.         do
  15.             across
  16.                 Zurich.lines as line_pointer
  17.             loop
  18.                 line_pointer.item.add_transport
  19.             end
  20.         end
  21.  
  22.     update_transport_display (t: PUBLIC_TRANSPORT)
  23.             -- Update route information display inside transportation unit `t'.
  24.         require
  25.             t_exists: t /= Void
  26.         local
  27.             s: STATION
  28.             total_stations_left: INTEGER
  29.             i: INTEGER
  30.         do
  31.             -- Reset the console
  32.             console.clear
  33.  
  34.             -- Count the number of stations left
  35.             total_stations_left := 0
  36.             from
  37.                 s := Zurich.station (t.arriving.name)
  38.             invariant
  39.                 total_stations_left < Zurich.line (t.line.number).stations.count + 1
  40.             until
  41.                 s = void
  42.             loop
  43.                 total_stations_left := total_stations_left + 1
  44.                 s := Zurich.line (t.line.number).next_station (s, t.destination)
  45.             variant
  46.                 Zurich.line (t.line.number).stations.count + 1 - total_stations_left
  47.             end
  48.  
  49.             -- Output info
  50.             if
  51.                 -- For 1 station left
  52.                 total_stations_left = 1
  53.             then
  54.                 console.output ("Linie/Line " + t.line.number.out + " Willkommen/Welcome.")
  55.                 console.append_line (stop_info (t, t.destination))
  56.             elseif
  57.                 -- For 2 stations left
  58.                 total_stations_left = 2
  59.             then
  60.                 console.output ("Linie/Line " + t.line.number.out + " Willkommen/Welcome.")
  61.                 console.append_line (stop_info (t, t.arriving))
  62.                 console.append_line (stop_info (t, t.destination))
  63.             elseif
  64.                 -- For 3 stations left
  65.                 total_stations_left = 3
  66.             then
  67.                 console.output ("Linie/Line " + t.line.number.out + " Willkommen/Welcome.")
  68.                 from
  69.                     s := Zurich.station (t.arriving.name)
  70.                     i := 1
  71.                 invariant
  72.                     i < Zurich.line (t.line.number).stations.count + 1
  73.                 until
  74.                     i = 3
  75.                 loop
  76.                     i := i + 1
  77.                     console.append_line (stop_info (t, s))
  78.                     s := Zurich.line (t.line.number).next_station (s, t.destination)
  79.                 variant
  80.                     Zurich.line (t.line.number).stations.count + 1 - i
  81.                 end
  82.                 console.append_line (stop_info (t, t.destination))
  83.             elseif
  84.                 -- For more than 3 stations left
  85.                 total_stations_left > 3
  86.             then
  87.                 console.output ("Linie/Line " + t.line.number.out + " Willkommen/Welcome.")
  88.                 from
  89.                     s := Zurich.station (t.arriving.name)
  90.                     i := 1
  91.                 invariant
  92.                     i < Zurich.line (t.line.number).stations.count + 1
  93.                 until
  94.                     i = 4
  95.                 loop
  96.                     i := i + 1
  97.                     console.append_line (stop_info (t, s))
  98.                     s := Zurich.line (t.line.number).next_station (s, t.destination)
  99.                 variant
  100.                     Zurich.line (t.line.number).stations.count + 1 - i
  101.                 end
  102.                 console.append_line ("...")
  103.                 console.append_line (stop_info (t, t.destination))
  104.             else
  105.                 -- The something-is-not-right case!
  106.                 console.append_line ("Oops, something went wrong.. please click again or terminate.")
  107.             end
  108.         end
  109.  
  110.     stop_info (t: PUBLIC_TRANSPORT; s: STATION): STRING
  111.             -- Get the station info for the next stop(s)
  112.         require
  113.             t_is_there: t /= void
  114.             s_is_there: s /= void
  115.         local
  116.             minutes: INTEGER
  117.             looping_line: LINE
  118.         do
  119.             -- Time to station
  120.             if
  121.                 t.time_to_station (s) < 60
  122.             then
  123.                 Result := "<1 Min.%T" + s.name
  124.             else
  125.                 minutes := t.time_to_station (s) // 60
  126.                 Result := minutes.out + " Min.%T" + s.name
  127.             end
  128.  
  129.             -- Connecting lines at station
  130.             if s.lines.is_empty = false then
  131.                 across
  132.                     s.lines as z
  133.                 loop
  134.                     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
  135.                         looping_line := void
  136.                     else
  137.                         looping_line := z.item
  138.                     end
  139.                     if looping_line /= void then
  140.                         Result.append (" " + looping_line.number.out)
  141.                     end
  142.                 end
  143.             end
  144.         ensure
  145.             Result_is_there: Result /= void
  146.         end
  147.  
  148. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement