Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class
- BUSINESS_CARD
- create
- fill_in
- feature {NONE} -- Initialization
- fill_in
- -- Fill in the card and print it.
- do
- io.put_string ("Hi there! %NPlease enter your name here: ")
- io.read_line
- set_name(io.last_string)
- io.put_string ("Please enter your job here: ")
- io.read_line
- set_job(io.last_string)
- io.putstring ("Please enter your age here: ")
- io.read_integer
- set_age(io.last_integer)
- print_card()
- end
- feature -- Access
- name: STRING
- -- Owner's name.
- job: STRING
- -- Owner's job.
- age: INTEGER
- -- Owner's age.
- feature -- Setting
- set_name (a_name: STRING)
- -- Set `name' to `a_name'.
- require
- name_exists: a_name /= Void
- do
- name := a_name.twin
- end
- set_job (a_job: STRING)
- -- Set `job' to `a_job'.
- require
- job_exists: a_job /= Void
- do
- job := a_job.twin
- end
- set_age (a_age: INTEGER)
- -- Set `age' to `a_age'.
- require
- age_non_negative: a_age >= 0
- do
- age := a_age
- end
- feature -- Output
- age_info: STRING
- -- Text representation of age on the card.
- do
- Result := age.out + " years old"
- end
- Width: INTEGER
- -- Width of the card (in characters), excluding borders.
- do
- -- Checks if the continuous one-line string is too long for a 50 width
- -- (eg. too long name/job)
- if (name.count + age_info.count + job.count + 32) > 50 then
- Result := (name.count + age_info.count + job.count + 32 + 5)
- else
- Result := 50
- end
- end
- alternative_width: INTEGER
- -- Sets alternative width for card (if name/job too long)
- do
- if (name.count + 13) > 50 then
- Result := (name.count + 13 + 5)
- elseif (age_info.count + 8) > 50 then
- Result := (age_info.count + 8 + 5)
- elseif (job.count + 9) > 50 then
- Result := (job.count + 9 + 5)
- else
- Result := 50
- end
- end
- line (n: INTEGER): STRING
- -- Horizontal line on length `n'.
- do
- Result := "-"
- Result.multiply (n)
- end
- space_number: INTEGER
- -- Count number of necessary spaces
- do
- Result := Width - (name.count + age_info.count + job.count + 32)
- end
- spaces (n: INTEGER): STRING
- -- Repeating Space (Spaceholder) for length `n'.
- do
- Result := " "
- Result.multiply (n)
- end
- print_card ()
- -- Print buisness card
- do
- if (name.count + age_info.count + job.count + 32) < 79 then
- io.new_line
- io.put_string (line(Width))
- io.new_line
- io.put_string ("|My name is " + name + ", I'm " + age_info + " and I am a " + job + "." + spaces(space_number) + "|")
- io.new_line
- io.put_string (line(Width))
- else
- -- Prints alternative design, if name/age/job too long for all in one line.
- print_alternative_card ()
- end
- end
- print_alternative_card ()
- -- Prints an alternative card (if name/job too long)
- do
- io.new_line
- io.put_string (line(alternative_width))
- io.new_line
- io.put_string ("|My name is " + name + spaces(alternative_width - (name.count + 13)) + "|")
- io.new_line
- io.put_string ("|I am a " + job + spaces(alternative_width - (job.count + 9)) + "|")
- io.new_line
- io.put_string ("|I am " + age_info + "." + spaces(alternative_width - (age_info.count + 8)) + "|")
- io.new_line
- io.put_string (line(alternative_width))
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement