Advertisement
PhilDrummer

Assignment_3 03

Oct 3rd, 2014
2,569
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Eiffel 3.29 KB | None | 0 0
  1. class
  2.     BUSINESS_CARD
  3.  
  4. create
  5.     fill_in
  6.  
  7. feature {NONE} -- Initialization
  8.  
  9.     fill_in
  10.             -- Fill in the card and print it.
  11.         do
  12.             io.put_string ("Hi there! %NPlease enter your name here: ")
  13.             io.read_line   
  14.             set_name(io.last_string)
  15.             io.put_string ("Please enter your job here: ")
  16.             io.read_line   
  17.             set_job(io.last_string)
  18.             io.putstring ("Please enter your age here: ")
  19.             io.read_integer
  20.             set_age(io.last_integer)
  21.             print_card()
  22.         end
  23.  
  24. feature -- Access
  25.  
  26.     name: STRING
  27.             -- Owner's name.
  28.  
  29.     job: STRING
  30.             -- Owner's job.
  31.  
  32.     age: INTEGER
  33.             -- Owner's age.
  34.  
  35. feature -- Setting
  36.  
  37.     set_name (a_name: STRING)
  38.             -- Set `name' to `a_name'.
  39.         require
  40.             name_exists: a_name /= Void
  41.         do
  42.             name := a_name.twin
  43.         end
  44.  
  45.     set_job (a_job: STRING)
  46.             -- Set `job' to `a_job'.
  47.         require
  48.             job_exists: a_job /= Void
  49.         do
  50.             job := a_job.twin
  51.         end
  52.  
  53.     set_age (a_age: INTEGER)
  54.             -- Set `age' to `a_age'.
  55.         require
  56.             age_non_negative: a_age >= 0
  57.         do
  58.             age := a_age
  59.         end
  60.  
  61. feature -- Output
  62.     age_info: STRING
  63.             -- Text representation of age on the card.
  64.         do
  65.             Result := age.out + " years old"
  66.         end
  67.  
  68.     Width: INTEGER
  69.             -- Width of the card (in characters), excluding borders.
  70.         do
  71.             -- Checks if the continuous one-line string is too long for a 50 width
  72.             -- (eg. too long name/job)
  73.             if (name.count + age_info.count + job.count + 32) > 50 then            
  74.                 Result := (name.count + age_info.count + job.count + 32 + 5)       
  75.             else
  76.                 Result := 50
  77.             end
  78.         end
  79.  
  80.     alternative_width: INTEGER
  81.             -- Sets alternative width for card (if name/job too long)
  82.         do
  83.             if (name.count + 13) > 50 then
  84.                 Result := (name.count + 13 + 5)
  85.             elseif (age_info.count + 8) > 50 then
  86.                 Result := (age_info.count + 8 + 5)
  87.             elseif (job.count + 9) > 50 then
  88.                 Result := (job.count + 9 + 5)
  89.             else
  90.                 Result := 50
  91.             end
  92.         end
  93.  
  94.     line (n: INTEGER): STRING
  95.             -- Horizontal line on length `n'.
  96.         do
  97.             Result := "-"
  98.             Result.multiply (n)
  99.         end
  100.  
  101.     space_number: INTEGER
  102.             -- Count number of necessary spaces
  103.         do
  104.             Result := Width - (name.count + age_info.count + job.count + 32)
  105.         end
  106.  
  107.     spaces (n: INTEGER): STRING
  108.             -- Repeating Space (Spaceholder) for length `n'.
  109.         do
  110.             Result := " "
  111.             Result.multiply (n)
  112.         end
  113.  
  114.     print_card ()
  115.             -- Print buisness card
  116.         do
  117.             if (name.count + age_info.count + job.count + 32) < 79 then
  118.                 io.new_line
  119.                 io.put_string (line(Width))
  120.                 io.new_line
  121.                 io.put_string ("|My name is " + name + ", I'm " + age_info + " and I am a " + job + "." + spaces(space_number) + "|")
  122.                 io.new_line
  123.                 io.put_string (line(Width))
  124.             else
  125.                 -- Prints alternative design, if name/age/job too long for all in one line.
  126.                 print_alternative_card ()
  127.             end
  128.         end
  129.  
  130.     print_alternative_card ()
  131.             -- Prints an alternative card (if name/job too long)
  132.         do
  133.             io.new_line
  134.             io.put_string (line(alternative_width))
  135.             io.new_line
  136.             io.put_string ("|My name is " + name + spaces(alternative_width - (name.count + 13)) + "|")
  137.             io.new_line
  138.             io.put_string ("|I am a " + job + spaces(alternative_width - (job.count + 9)) + "|")
  139.             io.new_line
  140.             io.put_string ("|I am " + age_info + "." + spaces(alternative_width - (age_info.count + 8)) + "|")
  141.             io.new_line
  142.             io.put_string (line(alternative_width))
  143.         end
  144.  
  145. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement