Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- note
- description: "A default business model."
- author: "Jackie Wang"
- date: "$Date$"
- revision: "$Revision$"
- class
- ETF_MODEL
- inherit
- ANY
- redefine
- out
- end
- create {ETF_MODEL_ACCESS}
- make
- feature {NONE} -- Initialization
- make
- do
- create {SORTED_TWO_WAY_LIST [ETF_PERSON]} persons.make
- create person.make(1000)
- model_error_status := "EMPTY"
- end
- feature -- model attributes
- persons : LIST[ETF_PERSON]
- person : HASH_TABLE[ETF_PERSON, INTEGER]
- model_error_status : STRING assign update_model_error
- feature -- model operations
- update_model_error (error_msg : STRING)
- do
- model_error_status := error_msg
- end
- put (id: INTEGER_64; name: STRING; dob: TUPLE[d: INTEGER_64; m: INTEGER_64; y: INTEGER_64])
- require
- id_is_positive: id > 0
- id_is_not_taken: not person.has (id.as_integer_32)
- valid_name_start: char_start_proper (name)
- valid_date: date_is_valid (dob)
- local
- new_person : ETF_PERSON
- do
- create new_person.make(id, name, dob, "Canada")
- persons.extend (new_person)
- person.put (new_person, new_person.person_id)
- ensure
- added_people : person.has (id.as_integer_32)
- end
- put_alien (id: INTEGER_64; name: STRING; dob: TUPLE[d: INTEGER_64; m: INTEGER_64; y: INTEGER_64]; country: STRING)
- require
- id_positive: id > 0
- id_is_not_taken: not person.has (id.as_integer_32)
- valid_name_start: char_start_proper (name)
- valid_date: date_is_valid (dob)
- valid_country_start: char_start_proper (country)
- local
- new_person: ETF_PERSON
- do
- create new_person.make (id, name, dob, country)
- persons.extend (new_person)
- person.put (new_person, new_person.person_id)
- ensure
- added_people : person.has (id.as_integer_32)
- end
- marry (id1: INTEGER_64 ; id2: INTEGER_64 ; date: TUPLE[d: INTEGER_64; m: INTEGER_64; y: INTEGER_64])
- require
- ids_not_equal : (id1 ~ id2) = false
- ids_non_negative : id1 > 0 and id2 > 0
- ids_not_used : (person.has (id1.as_integer_32) or person.has (id2.as_integer_32) ) = false
- is_ids_marriage_valid : marriage_valid (id1, id2, date)
- valid_date: date_is_valid (date)
- do
- marrying(id1.as_integer_64,id2.as_integer_64, date)
- end
- divorce (id1: INTEGER_64 ;id2: INTEGER_64) -- command divorce checks all the preconditions
- require
- ids_are_different : (id1 ~ id2) = false
- ids_are_greater_than_zero : id1 > 0 and id2 > 0
- ids_not_used : (person.has (id1.as_integer_32) or person.has (id2.as_integer_32) ) = false
- is_valid_divorce : divorce_valid (id1, id2)
- do
- divorcing(id1,id2)
- end
- die (id: INTEGER_64)
- require
- id_is_greater_than_zero: id > 0
- id_is_not_taken: not person.has (id.as_integer_32)
- not_dead_already: not is_dead (id)
- do
- dying(id)
- end
- reset
- do
- make
- end
- char_start_proper (test_name : STRING) : BOOLEAN
- do
- Result := false
- if (test_name ~ "%U") then
- Result := false
- elseif (test_name.is_empty) then
- Result := false
- elseif ((test_name[1] >= 'A' and test_name[1] <= 'Z') or (test_name[1] >= 'a' and test_name[1] <= 'z')) then
- Result := true
- end
- end
- date_is_valid (test_date : TUPLE[d : INTEGER_64; m : INTEGER_64; y : INTEGER_64]) : BOOLEAN
- do
- Result := false
- if (test_date.y >= 1900 and test_date.y <= 3000) then
- if (test_date.m ~ 1 or test_date.m ~ 3 or test_date.m ~ 5 or test_date.m ~ 7 or test_date.m ~ 8 or test_date.m ~ 10 or test_date.m ~ 12) then
- if (test_date.d >= 1 and test_date.d <= 31) then
- Result := true
- end
- elseif (test_date.m ~ 2) then
- if (is_leap_year (test_date.y)) then
- if (test_date.d >= 1 and test_date.d <= 29) then
- Result := true
- end
- elseif (test_date.d >= 1 and test_date.d <= 28) then
- Result := true
- end
- elseif (test_date.m ~ 4 or test_date.m ~ 6 or test_date.m ~ 9 or test_date.m ~ 11) then
- if (test_date.d >= 1 and test_date.d <= 30) then
- Result := true
- end
- end
- end
- end
- is_leap_year (test_year : INTEGER_64) : BOOLEAN
- do
- Result := false
- if ((test_year \\ 4 ~ 0) and (test_year \\ 100 /= 0) or (test_year \\ 400 ~ 0)) then
- Result := true
- end
- end
- is_dead (id : INTEGER_64): BOOLEAN
- require
- id_is_positive: id > 0
- id_exist : person.has (id.as_integer_32)
- do
- Result := false
- if attached person.at (id.as_integer_32) as person_dead then
- if ((person_dead.person_status ~ "Deceased")) then
- Result := true
- end
- end
- end
- marriage_valid (id1: INTEGER_64 ; id2: INTEGER_64; date: TUPLE[d: INTEGER_64; m: INTEGER_64; y: INTEGER_64]) : BOOLEAN
- require
- local
- legal_age : BOOLEAN
- married_not_valid : BOOLEAN
- list_of_spouse : BOOLEAN
- date_of_birth_18 : DATE
- date_of_birth_2_18 : DATE
- date_of_valid_marraige : DATE
- do
- Result := false
- check attached person.at (id1.as_integer_32) as p1 then
- check attached person.at (id2.as_integer_32) as p2 then
- create date_of_birth_18.make (p1.person_dob.y.as_integer_32 + 18, p1.person_dob.m.as_integer_32, p1.person_dob.d.as_integer_32)
- create date_of_birth_2_18.make (p2.person_dob.y.as_integer_32 + 18, p2.person_dob.m.as_integer_32, p2.person_dob.d.as_integer_32)
- create date_of_valid_marraige.make (date.y.as_integer_32, date.m.as_integer_32, date.d.as_integer_32)
- if ((date_of_valid_marraige >= date_of_birth_18) and (date_of_valid_marraige >= date_of_birth_2_18)) then
- result := true
- if (((p1.person_status ~ "Single") and (p2.person_status ~ "Single")) and (p1.person_spouse ~ p1.person_id) and (p2.person_spouse ~ p2.person_id)) then
- result := true
- end
- end
- if ((p1.person_status ~ "Deceased") or (p2.person_status ~ "Deceased")) then
- result := false
- end
- if ((p1.person_status ~ "Married") or (p2.person_status ~ "Married")) then
- result := false
- end
- end
- end
- end
- divorce_valid (id1: INTEGER_64 ; id2: INTEGER_64) : BOOLEAN
- do
- Result := false
- if attached person.at (id1.as_integer_32) as p1 then
- if attached person.at (id2.as_integer_32) as p2 then
- if ((p1.person_spouse ~ p2.person_id) and (p2.person_spouse ~ p1.person_id)) then
- result := true
- end
- end
- end
- end
- marrying(id1: INTEGER_64 ; id2: INTEGER_64 ; date: TUPLE[d: INTEGER_64; m: INTEGER_64; y: INTEGER_64])
- do
- if attached person.at (id1.as_integer_32) as p1 then
- if attached person.at (id2.as_integer_32) as p2 then
- p1.person_spouse := p2.person_id
- p2.person_spouse := p1.person_id
- p1.person_status := "Married"
- p2.person_status := "Married"
- p1.set_date_of_marriage(date)
- p2.set_date_of_marriage (date)
- end
- end
- end
- divorcing(id1: INTEGER_64 ; id2: INTEGER_64 )
- do
- if attached person.at (id1.as_integer_32) as p1 then
- if attached person.at (id2.as_integer_32) as p2 then
- p1.person_spouse := p1.person_id
- p2.person_spouse := p2.person_id
- p1.person_status := "Single"
- p2.person_status := "Single"
- end
- end
- end
- dying(id: INTEGER_64)
- do
- if attached person.at (id.as_integer_32) as person_obj then
- if (person_obj.person_status ~ "Married") then
- if attached person.at (person_obj.person_spouse) as obj_spouse then
- obj_spouse.person_status := "Single"
- obj_spouse.person_spouse := obj_spouse.person_id
- end
- end
- person_obj.person_spouse := person_obj.person_id
- person_obj.person_status := "Deceased"
- end
- end
- feature -- queries
- out : STRING
- do
- create Result.make_from_string ("")
- if (model_error_status ~ "EMPTY") then
- Result.append(" ok%N")
- end
- if not (model_error_status ~ "EMPTY") then
- Result.append(" " + model_error_status + "%N")
- model_error_status := "EMPTY"
- end
- from persons.start
- until persons.after
- loop
- Result.append(" " + persons[persons.index].person_name + "; ID: " + persons[persons.index].person_id.out + "; Born: ")
- Result.append(persons[persons.index].person_dob.y.out + "-")
- if (persons[persons.index].person_dob.m < 10) then
- Result.append("0")
- end
- Result.append(persons[persons.index].person_dob.m.out + "-")
- if (persons[persons.index].person_dob.d < 10) then
- Result.append("0")
- end
- Result.append(persons[persons.index].person_dob.d.out + "; ")
- Result.append("Citizen: " + persons[persons.index].person_country + "; ")
- if (persons[persons.index].person_status ~ "Single") then
- Result.append("Single%N")
- elseif (persons[persons.index].person_status ~ "Deceased") then
- Result.append("Deceased%N")
- elseif (persons[persons.index].person_status ~ "Married") then
- if attached person.at (persons[persons.index].person_spouse.as_integer_32) as spouse then
- Result.append("Spouse: " + spouse.person_name + "," + spouse.person_id.out + ",[" + spouse.person_date_of_marriage.y.out + "-")
- if (spouse.person_date_of_marriage.m < 10) then
- Result.append("0")
- end
- Result.append(spouse.person_date_of_marriage.m.out + "-")
- if (spouse.person_date_of_marriage.d < 10) then
- Result.append("0")
- end
- Result.append(spouse.person_date_of_marriage.d.out + "]%N")
- end
- end
- persons.forth
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement