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.
- 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 today_date.make_now
- Result := today_date.relative_duration (birth_date).year
- end
- today_date: DATE
- feature -- Status report
- valid_postal_code (pc: STRING): BOOLEAN
- -- Is `pc' a valid postal code in Switzerland?
- do
- if pc /= Void and pc.is_natural and pc.count = 4 then
- Result := True
- end
- 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 then 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 (age < 25) or (in_zurich_city (home) and (not in_zurich_city (work)))
- or (in_zurich_city (work) and (in_zurich_canton (home) and (not in_zurich_city (home)))) then
- Result := True
- 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