Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- note
- description : "ZVV information system."
- class
- APPLICATION
- create
- execute
- feature {NONE} -- Initialization
- execute
- -- Run application.
- do
- read_data
- if not read_error then
- Io.new_line
- print ("Eligible for discount: ")
- print (gets_discount)
- end
- end
- feature -- Access
- birth_date: DATE
- -- Birth date.
- current_date: DATE
- -- Current date.
- date_duration: DATE_DURATION
- -- Difference between dates
- home: STRING
- -- Home postal code.
- work: STRING
- -- Work postal code.
- age: INTEGER
- -- Age (difference in years between today's date and `birth_date').
- require
- birth_date_exists: birth_date /= Void
- do
- create current_date.make_now
- create date_duration.make (0, 0, 0)
- date_duration := current_date.relative_duration (birth_date)
- Result := date_duration.days_count
- end
- feature -- Status report
- valid_postal_code (pc: STRING): BOOLEAN
- -- Is `pc' a valid postal code in Switzerland?
- do
- Result := ((pc /= void) and then (pc.is_natural = true) and then (pc.count = 4))
- end
- in_zurich_canton (pc: STRING): BOOLEAN
- -- Is postal code `pc' inside the canton of Zurich?
- require
- valid_code: valid_postal_code (pc)
- do
- Result := pc [1] = '8'
- end
- in_zurich_city (pc: STRING): BOOLEAN
- -- Is postal code `pc' inside the city of Zurich?
- require
- valid_code: valid_postal_code (pc)
- do
- Result := ((pc [1] = '8') and (pc [2] = '0'))
- end
- gets_discount: BOOLEAN
- -- Is a customer with the current `birth_date', `home' and `work' eligible for a discounted seasonal ticket?
- require
- birth_date_exists: birth_date /= Void
- valid_home_code: valid_postal_code (home)
- valid_work_code: valid_postal_code (work)
- do
- if 25 >= (age/365.25) then
- Result := true
- elseif (in_zurich_city (home) = true) and (in_zurich_city (work) = false) then
- Result := true
- elseif (in_zurich_city (work) = true) and (in_zurich_canton (home) = true) and (in_zurich_city (home) = false) then
- Result := true
- else
- Result := false
- end
- end
- feature {NONE} -- Implementation
- read_error: BOOLEAN
- -- Did an error occur while reading user data?
- read_data
- -- Read user input.
- local
- date_format: STRING
- do
- date_format := "[0]dd/[0]mm/yyyy"
- print ("Enter birth date as dd/mm/yyyy: ")
- Io.read_line
- if not (create {DATE_VALIDITY_CHECKER}).date_valid (Io.last_string, date_format) then
- print ("Invalid date")
- read_error := True
- else
- create birth_date.make_from_string (Io.last_string, date_format)
- end
- if not read_error then
- print ("Enter home postal code: ")
- Io.read_line
- home := Io.last_string.twin
- if not valid_postal_code (home) then
- print ("Invalid postal code")
- read_error := True
- end
- end
- if not read_error then
- print ("Enter work postal code: ")
- Io.read_line
- work := Io.last_string.twin
- if not valid_postal_code (work) then
- print ("Invalid postal code")
- read_error := True
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement