Advertisement
Iamlugano

ticket

Oct 9th, 2011
2,421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Eiffel 2.80 KB | None | 0 0
  1. note
  2.     description : "ZVV information system."
  3.  
  4. class
  5.     APPLICATION
  6.  
  7. create
  8.     execute
  9.  
  10. feature {NONE} -- Initialization
  11.  
  12.     execute
  13.             -- Run application.
  14.         do
  15.             read_data
  16.             if not read_error then
  17.                 Io.new_line
  18.                 print ("Eligible for discount: ")
  19.                 print (gets_discount)
  20.             end
  21.         end
  22.  
  23. feature -- Access
  24.  
  25.     birth_date: DATE
  26.             -- Birth date.
  27.  
  28.     home: STRING
  29.             -- Home postal code.
  30.  
  31.     work: STRING
  32.             -- Work postal code.
  33.  
  34.     age: INTEGER
  35.             -- Age (difference in years between today's date and `birth_date').
  36.  
  37.  
  38.         require
  39.             birth_date_exists: birth_date /= Void
  40.         do
  41.             create today_date.make_now
  42.             Result := today_date.relative_duration (birth_date).year
  43.         end
  44.  
  45.         today_date: DATE
  46.  
  47. feature -- Status report
  48.  
  49.     valid_postal_code (pc: STRING): BOOLEAN
  50.             -- Is `pc' a valid postal code in Switzerland?
  51.  
  52.         do
  53.             if pc /= Void and pc.is_natural and pc.count = 4  then
  54.                 Result := True
  55.             end
  56.         end
  57.  
  58.     in_zurich_canton (pc: STRING): BOOLEAN
  59.             -- Is postal code `pc' inside the canton of Zurich?
  60.         require
  61.             valid_code: valid_postal_code (pc)
  62.         do
  63.             Result := pc [1] = '8'
  64.         end
  65.  
  66.     in_zurich_city (pc: STRING): BOOLEAN
  67.             -- Is postal code `pc' inside the city of Zurich?
  68.         require
  69.             valid_code: valid_postal_code (pc)
  70.         do
  71.             Result := pc [1] = '8' and then pc [2] =  '0'
  72.         end
  73.  
  74.     gets_discount: BOOLEAN
  75.             -- Is a customer with the current `birth_date', `home' and `work' eligible for a discounted seasonal ticket?
  76.         require
  77.             birth_date_exists: birth_date /= Void
  78.             valid_home_code: valid_postal_code (home)
  79.             valid_work_code: valid_postal_code (work)
  80.         do
  81.             if (age < 25) or (in_zurich_city (home) and (not in_zurich_city (work)))
  82.             or (in_zurich_city (work) and (in_zurich_canton (home) and (not in_zurich_city (home)))) then
  83.                 Result := True
  84.  
  85.             end
  86.         end
  87.  
  88. feature {NONE} -- Implementation
  89.  
  90.     read_error: BOOLEAN
  91.             -- Did an error occur while reading user data?
  92.  
  93.     read_data
  94.             -- Read user input.
  95.         local
  96.             date_format: STRING
  97.         do
  98.             date_format := "[0]dd/[0]mm/yyyy"
  99.             print ("Enter birth date as dd/mm/yyyy: ")
  100.             Io.read_line
  101.             if not (create {DATE_VALIDITY_CHECKER}).date_valid (Io.last_string, date_format) then
  102.                 print ("Invalid date")
  103.                 read_error := True
  104.             else
  105.                 create birth_date.make_from_string (Io.last_string, date_format)
  106.             end
  107.  
  108.             if not read_error then
  109.                 print ("Enter home postal code: ")
  110.                 Io.read_line
  111.                 home := Io.last_string.twin
  112.                 if not valid_postal_code (home)  then
  113.                     print ("Invalid postal code")
  114.                     read_error := True
  115.                 end
  116.             end
  117.  
  118.             if not read_error then
  119.                 print ("Enter work postal code: ")
  120.                 Io.read_line
  121.                 work := Io.last_string.twin
  122.                 if not valid_postal_code (work)  then
  123.                     print ("Invalid postal code")
  124.                     read_error := True
  125.                 end
  126.             end
  127.         end
  128. end
  129.  
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement