Advertisement
PhilDrummer

Assignment_10 01 NAVIGATOR.e

Dec 16th, 2014
3,135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Eiffel 2.01 KB | None | 0 0
  1. note
  2.     description: "Finding routes in Zurich."
  3.  
  4. class
  5.     NAVIGATOR
  6.  
  7. inherit
  8.     ZURICH_OBJECTS
  9.  
  10. feature -- Explore Zurich
  11.  
  12.     add_event_handlers
  13.             -- Add handlers to mouse-click events on stations
  14.             -- to allow the user to select start and end points of his route.
  15.         do
  16.             across
  17.                 Zurich.stations as i
  18.             loop
  19.                 Zurich_map.station_view (i.item).on_left_click_no_args.extend_back (agent set_origin (i.item))
  20.                 Zurich_map.station_view (i.item).on_right_click_no_args.extend_back (agent set_destination (i.item))
  21.             end
  22.             Zurich_map.on_left_click_no_args.extend_back (agent show_route)
  23.             Zurich_map.on_right_click_no_args.extend_back (agent show_route)
  24.         end
  25.  
  26. feature -- Write / Set
  27.  
  28.     set_origin (s: STATION)
  29.             -- Set new origin
  30.         require
  31.             s_not_void: s /= void
  32.         do
  33.             origin := s
  34.         ensure
  35.             origin_exists: origin /= void
  36.             origin = s
  37.         end
  38.  
  39.     set_destination (s: STATION)
  40.             -- Set new destination
  41.         require
  42.             s_exists: s /= void
  43.         do
  44.             destination := s
  45.         ensure
  46.             destination_exists: destination /= void
  47.             destination = s
  48.         end
  49.  
  50. feature -- Access
  51.  
  52.     origin: STATION
  53.             -- Currently selected start point.
  54.             -- (Void if no start point selected).
  55.  
  56.     destination: STATION
  57.             -- Currently selected end point.
  58.             -- (Void if no end point selected).
  59.  
  60. feature {NONE} -- Implementation
  61.  
  62.     show_route
  63.             -- If both `origin' and `destination' are set, show the route from `origin' to `destination' on the map
  64.             -- and output directions to the console.
  65.             -- Otherwise do nothing.
  66.         local
  67.             find: ROUTE_FINDER
  68.             shortest: ROUTE
  69.         do
  70.             if
  71.                 origin /= void and then destination /= void
  72.             then
  73.                 zurich_map.update
  74.                 console.clear
  75.                 console.output ("From " + origin.name + " to " + destination.name + ":")
  76.                 create find.make (zurich)
  77.                 shortest := find.shortest_route (origin, destination)
  78.                 zurich.add_route (shortest)
  79.                 zurich_map.update
  80.                 shortest.minimize_lines
  81.                 console.append_line (shortest.out)
  82.                 zurich.remove_route (shortest)
  83.             end
  84.         end
  85. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement