Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Eiffel 9.39 KB | None | 0 0
  1. note
  2.     description: "A default business model."
  3.     author: "Jackie Wang"
  4.     date: "$Date$"
  5.     revision: "$Revision$"
  6.  
  7. class
  8.     ETF_MODEL
  9.  
  10. inherit
  11.     ANY
  12.         redefine
  13.             out
  14.         end
  15.  
  16. create {ETF_MODEL_ACCESS}
  17.     make
  18.  
  19. feature {NONE} -- Initialization
  20.     make
  21.         do
  22.  
  23.             create {SORTED_TWO_WAY_LIST [ETF_PERSON]} persons.make
  24.             create person.make(1000)
  25.             model_error_status := "EMPTY"
  26.         end
  27.  
  28. feature -- model attributes
  29.  
  30.  
  31.     persons : LIST[ETF_PERSON]
  32.     person : HASH_TABLE[ETF_PERSON, INTEGER]
  33.  
  34.     model_error_status : STRING assign update_model_error
  35.  
  36. feature -- model operations
  37.  
  38.     update_model_error (error_msg : STRING)
  39.         do
  40.             model_error_status := error_msg
  41.         end
  42.  
  43.     put (id: INTEGER_64; name: STRING; dob: TUPLE[d: INTEGER_64; m: INTEGER_64; y: INTEGER_64])
  44.         require
  45.  
  46.             id_is_positive: id > 0
  47.             id_is_not_taken: not  person.has (id.as_integer_32)
  48.             valid_name_start: char_start_proper (name)
  49.             valid_date: date_is_valid (dob)
  50.         local
  51.             new_person : ETF_PERSON
  52.         do
  53.             create new_person.make(id, name, dob, "Canada")
  54.  
  55.             persons.extend (new_person)
  56.             person.put (new_person, new_person.person_id)
  57.  
  58.  
  59.             ensure
  60.  
  61.             added_people : person.has (id.as_integer_32)
  62.         end
  63.  
  64.     put_alien (id: INTEGER_64; name: STRING; dob: TUPLE[d: INTEGER_64; m: INTEGER_64; y: INTEGER_64]; country: STRING)
  65.  
  66.         require
  67.             id_positive: id > 0
  68.             id_is_not_taken: not  person.has (id.as_integer_32)
  69.             valid_name_start: char_start_proper (name)
  70.             valid_date: date_is_valid (dob)
  71.             valid_country_start: char_start_proper (country)
  72.         local
  73.             new_person: ETF_PERSON
  74.         do
  75.             create new_person.make (id, name, dob, country)
  76.             persons.extend (new_person)
  77.             person.put (new_person, new_person.person_id)
  78.  
  79.         ensure
  80.  
  81.             added_people : person.has (id.as_integer_32)
  82.         end
  83.  
  84.     marry (id1: INTEGER_64 ; id2: INTEGER_64 ; date: TUPLE[d: INTEGER_64; m: INTEGER_64; y: INTEGER_64])
  85.         require
  86.  
  87.             ids_not_equal : (id1 ~ id2) = false
  88.             ids_non_negative : id1 > 0 and id2 > 0
  89.             ids_not_used : (person.has (id1.as_integer_32) or person.has (id2.as_integer_32) ) = false
  90.             is_ids_marriage_valid : marriage_valid (id1, id2, date)
  91.             valid_date: date_is_valid (date)
  92.         do
  93.                     marrying(id1.as_integer_64,id2.as_integer_64, date)
  94.  
  95.  
  96.         end
  97.  
  98.     divorce (id1: INTEGER_64 ;id2: INTEGER_64)                          -- command divorce checks all the preconditions
  99.         require
  100.             ids_are_different : (id1 ~ id2) = false
  101.             ids_are_greater_than_zero : id1 > 0 and id2 > 0
  102.             ids_not_used : (person.has (id1.as_integer_32) or person.has (id2.as_integer_32) ) = false
  103.             is_valid_divorce : divorce_valid (id1, id2)
  104.         do
  105.             divorcing(id1,id2)
  106.         end
  107.  
  108.     die (id: INTEGER_64)
  109.         require
  110.             id_is_greater_than_zero: id > 0
  111.             id_is_not_taken: not  person.has (id.as_integer_32)
  112.             not_dead_already: not is_dead (id)
  113.  
  114.         do
  115.             dying(id)
  116.         end
  117.  
  118.     reset
  119.         do
  120.             make
  121.         end
  122.  
  123.  
  124.  
  125.     char_start_proper (test_name : STRING) : BOOLEAN
  126.         do
  127.             Result := false
  128.             if (test_name ~ "%U") then
  129.                 Result := false
  130.             elseif (test_name.is_empty) then
  131.                 Result := false
  132.  
  133.             elseif ((test_name[1] >= 'A' and test_name[1] <= 'Z') or (test_name[1] >= 'a' and test_name[1] <= 'z')) then
  134.                 Result := true
  135.             end
  136.         end
  137.  
  138.     date_is_valid (test_date : TUPLE[d : INTEGER_64; m : INTEGER_64; y : INTEGER_64]) : BOOLEAN
  139.         do
  140.             Result := false
  141.             if (test_date.y >= 1900 and test_date.y <= 3000) then
  142.                 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
  143.                     if (test_date.d >= 1 and test_date.d <= 31) then
  144.                         Result := true
  145.                     end
  146.                 elseif (test_date.m ~ 2) then
  147.                     if (is_leap_year (test_date.y)) then
  148.                         if (test_date.d >= 1 and test_date.d <= 29) then
  149.                             Result := true
  150.                         end
  151.                     elseif (test_date.d >= 1 and test_date.d <= 28) then
  152.                             Result := true
  153.                     end
  154.                 elseif (test_date.m ~ 4 or test_date.m ~ 6 or test_date.m ~ 9 or test_date.m ~ 11) then
  155.                     if (test_date.d >= 1 and test_date.d <= 30) then
  156.                         Result := true
  157.                     end
  158.                 end
  159.             end
  160.         end
  161.  
  162.     is_leap_year (test_year : INTEGER_64) : BOOLEAN
  163.         do
  164.             Result := false
  165.             if ((test_year \\ 4 ~ 0) and (test_year \\ 100 /= 0) or (test_year \\ 400 ~ 0)) then
  166.                 Result := true
  167.             end
  168.         end
  169.  
  170.     is_dead (id : INTEGER_64): BOOLEAN
  171.         require
  172.  
  173.             id_is_positive: id > 0
  174.             id_exist : person.has (id.as_integer_32)
  175.         do
  176.             Result := false
  177.             if attached person.at (id.as_integer_32) as person_dead then
  178.                 if ((person_dead.person_status ~ "Deceased")) then
  179.                     Result := true
  180.                 end
  181.             end
  182.         end
  183.  
  184.     marriage_valid (id1: INTEGER_64 ; id2: INTEGER_64; date: TUPLE[d: INTEGER_64; m: INTEGER_64; y: INTEGER_64]) : BOOLEAN
  185.         require
  186.         local
  187.             legal_age : BOOLEAN
  188.             married_not_valid : BOOLEAN
  189.             list_of_spouse : BOOLEAN
  190.             date_of_birth_18 : DATE
  191.             date_of_birth_2_18 : DATE
  192.             date_of_valid_marraige : DATE
  193.         do
  194.             Result := false
  195.             check attached person.at (id1.as_integer_32) as p1 then
  196.                 check attached person.at (id2.as_integer_32) as p2 then
  197.                     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)
  198.                     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)
  199.                     create date_of_valid_marraige.make (date.y.as_integer_32, date.m.as_integer_32, date.d.as_integer_32)
  200.                     if ((date_of_valid_marraige >= date_of_birth_18) and (date_of_valid_marraige >= date_of_birth_2_18)) then
  201.                         result := true
  202.  
  203.                     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
  204.                         result := true
  205.                     end
  206.                     end
  207.                      if ((p1.person_status ~ "Deceased") or (p2.person_status ~ "Deceased")) then
  208.                         result := false
  209.                     end
  210.  
  211.                      if ((p1.person_status ~ "Married") or (p2.person_status ~ "Married")) then
  212.                         result := false
  213.                     end
  214.  
  215.                 end
  216. end
  217.         end
  218.  
  219.     divorce_valid (id1: INTEGER_64 ; id2: INTEGER_64) : BOOLEAN
  220.  
  221.  
  222.         do
  223.             Result := false
  224.             if attached person.at (id1.as_integer_32) as p1 then
  225.                 if attached person.at (id2.as_integer_32) as p2 then
  226.  
  227.                     if ((p1.person_spouse ~ p2.person_id) and (p2.person_spouse ~ p1.person_id)) then
  228.                         result := true
  229.                         end
  230.                     end
  231.             end
  232.         end
  233.  
  234.  
  235.  
  236.  
  237.  
  238.         marrying(id1: INTEGER_64 ; id2: INTEGER_64 ; date: TUPLE[d: INTEGER_64; m: INTEGER_64; y: INTEGER_64])
  239.             do
  240.                 if attached person.at (id1.as_integer_32) as p1 then
  241.                     if attached person.at (id2.as_integer_32) as p2 then
  242.                     p1.person_spouse := p2.person_id
  243.                     p2.person_spouse := p1.person_id
  244.                     p1.person_status := "Married"
  245.                     p2.person_status := "Married"
  246.                     p1.set_date_of_marriage(date)
  247.                     p2.set_date_of_marriage (date)
  248.  
  249.             end
  250.             end
  251.             end
  252.  
  253.  
  254.  
  255.  
  256.  
  257.         divorcing(id1: INTEGER_64 ; id2: INTEGER_64 )
  258.             do
  259.                 if attached person.at (id1.as_integer_32) as p1 then
  260.                     if attached person.at (id2.as_integer_32) as p2 then
  261.                     p1.person_spouse := p1.person_id
  262.                     p2.person_spouse := p2.person_id
  263.                     p1.person_status := "Single"
  264.                     p2.person_status := "Single"
  265.  
  266.  
  267.             end
  268.             end
  269.             end
  270.  
  271.         dying(id: INTEGER_64)
  272.  
  273.         do
  274.  
  275.  
  276.         if attached person.at (id.as_integer_32) as person_obj then
  277.                 if (person_obj.person_status ~ "Married") then
  278.                     if attached person.at (person_obj.person_spouse) as obj_spouse then
  279.                     obj_spouse.person_status := "Single"
  280.                     obj_spouse.person_spouse := obj_spouse.person_id
  281.                     end
  282.                 end
  283.                 person_obj.person_spouse := person_obj.person_id
  284.                 person_obj.person_status := "Deceased"
  285.             end
  286.  
  287.  
  288.  
  289.          end
  290.  
  291. feature -- queries
  292.     out : STRING
  293.         do
  294.             create Result.make_from_string ("")
  295.  
  296.             if (model_error_status ~ "EMPTY") then
  297.                 Result.append("  ok%N")
  298.             end
  299.  
  300.             if not (model_error_status ~ "EMPTY") then
  301.                 Result.append("  " + model_error_status + "%N")
  302.                 model_error_status := "EMPTY"
  303.             end
  304.  
  305.             from persons.start
  306.             until persons.after
  307.             loop
  308.  
  309.                 Result.append("  " + persons[persons.index].person_name + "; ID: " + persons[persons.index].person_id.out + "; Born: ")
  310.                 Result.append(persons[persons.index].person_dob.y.out + "-")
  311.                 if (persons[persons.index].person_dob.m < 10) then
  312.                     Result.append("0")
  313.                 end
  314.                 Result.append(persons[persons.index].person_dob.m.out + "-")
  315.                 if (persons[persons.index].person_dob.d < 10) then
  316.                     Result.append("0")
  317.                 end
  318.                 Result.append(persons[persons.index].person_dob.d.out + "; ")
  319.                 Result.append("Citizen: " + persons[persons.index].person_country + "; ")
  320.                 if (persons[persons.index].person_status ~ "Single") then
  321.                     Result.append("Single%N")
  322.                 elseif (persons[persons.index].person_status ~ "Deceased") then
  323.                     Result.append("Deceased%N")
  324.                 elseif (persons[persons.index].person_status ~ "Married") then
  325.                     if attached person.at (persons[persons.index].person_spouse.as_integer_32) as spouse then
  326.                         Result.append("Spouse: " + spouse.person_name + "," + spouse.person_id.out + ",[" + spouse.person_date_of_marriage.y.out + "-")
  327.                         if (spouse.person_date_of_marriage.m < 10) then
  328.                             Result.append("0")
  329.                         end
  330.                         Result.append(spouse.person_date_of_marriage.m.out + "-")
  331.                         if (spouse.person_date_of_marriage.d < 10) then
  332.                             Result.append("0")
  333.                         end
  334.                         Result.append(spouse.person_date_of_marriage.d.out + "]%N")
  335.                     end
  336.                 end
  337.                 persons.forth
  338.             end
  339.  
  340.         end
  341.  
  342. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement