Advertisement
PhilDrummer

Assignment_4 03

Oct 11th, 2014
1,239
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     current_date: DATE
  29.             -- Current date.
  30.  
  31.     date_duration: DATE_DURATION
  32.             -- Difference between dates
  33.  
  34.     home: STRING
  35.             -- Home postal code.
  36.  
  37.     work: STRING
  38.             -- Work postal code.
  39.  
  40.     age: INTEGER
  41.             -- Age (difference in years between today's date and `birth_date').
  42.         require
  43.             birth_date_exists: birth_date /= Void
  44.         do
  45.             create current_date.make_now
  46.             create date_duration.make (0, 0, 0)
  47.             date_duration := current_date.relative_duration (birth_date)
  48.             Result := date_duration.days_count
  49.         end
  50.  
  51. feature -- Status report
  52.  
  53.     valid_postal_code (pc: STRING): BOOLEAN
  54.             -- Is `pc' a valid postal code in Switzerland?
  55.         do
  56.             Result := ((pc /= void) and then (pc.is_natural = true) and then (pc.count = 4))
  57.         end
  58.  
  59.     in_zurich_canton (pc: STRING): BOOLEAN
  60.             -- Is postal code `pc' inside the canton of Zurich?
  61.         require
  62.             valid_code: valid_postal_code (pc)
  63.         do
  64.             Result := pc [1] = '8'
  65.         end
  66.  
  67.     in_zurich_city (pc: STRING): BOOLEAN
  68.             -- Is postal code `pc' inside the city of Zurich?
  69.         require
  70.             valid_code: valid_postal_code (pc)
  71.         do
  72.             Result := ((pc [1] = '8') and (pc [2] = '0'))
  73.         end
  74.  
  75.     gets_discount: BOOLEAN
  76.             -- Is a customer with the current `birth_date', `home' and `work' eligible for a discounted seasonal ticket?
  77.         require
  78.             birth_date_exists: birth_date /= Void
  79.             valid_home_code: valid_postal_code (home)
  80.             valid_work_code: valid_postal_code (work)
  81.         do
  82.             if 25 >= (age/365.25) then
  83.                 Result := true
  84.             elseif (in_zurich_city (home) = true) and (in_zurich_city (work) = false) then
  85.                 Result := true
  86.             elseif (in_zurich_city (work) = true) and (in_zurich_canton (home) = true) and (in_zurich_city (home) = false) then
  87.                 Result := true
  88.             else
  89.                 Result := false
  90.             end
  91.         end
  92.  
  93. feature {NONE} -- Implementation
  94.  
  95.     read_error: BOOLEAN
  96.             -- Did an error occur while reading user data?
  97.  
  98.     read_data
  99.             -- Read user input.
  100.         local
  101.             date_format: STRING
  102.         do
  103.             date_format := "[0]dd/[0]mm/yyyy"
  104.             print ("Enter birth date as dd/mm/yyyy: ")
  105.             Io.read_line
  106.             if not (create {DATE_VALIDITY_CHECKER}).date_valid (Io.last_string, date_format) then
  107.                 print ("Invalid date")
  108.                 read_error := True
  109.             else
  110.                 create birth_date.make_from_string (Io.last_string, date_format)
  111.             end
  112.  
  113.             if not read_error then
  114.                 print ("Enter home postal code: ")
  115.                 Io.read_line
  116.                 home := Io.last_string.twin
  117.                 if not valid_postal_code (home)  then
  118.                     print ("Invalid postal code")
  119.                     read_error := True
  120.                 end
  121.             end
  122.  
  123.             if not read_error then
  124.                 print ("Enter work postal code: ")
  125.                 Io.read_line
  126.                 work := Io.last_string.twin
  127.                 if not valid_postal_code (work)  then
  128.                     print ("Invalid postal code")
  129.                     read_error := True
  130.                 end
  131.             end
  132.         end
  133. end
Advertisement
RAW Paste Data Copied
Advertisement