Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language Eiffel

(Single class version with inline agents)

Date:12/06/09
Author:Finnian Reilly
URL:n/a
Comments:1
Info:http://en.wikipedia.org/wiki/Eiffel_(programming_language)
Score: (2.95 in 19 votes)
indexing
	description: "[
		Short application to generate lyrics for the song 99 Bottles of Beer
		See: http://www.99-bottles-of-beer.net/language-eiffel-231.html
	]"
	author: "Finnian Reilly"
	email: "finnian at eiffel-loop dot com"

class
	THE_SHORT_99_BOTTLES_OF_BEER_APPLICATION

create
	make

feature {NONE} -- Initialization

	make is
			--
		do
			print ("Lyrics of the song 99 Bottles of Beer%N%N")
			(0 |..| 99).do_all (agent print_verse)
		end

feature -- Basic operations

	print_verse (count_consumed: INTEGER) is
			--
		local
			count, x, y, z: INTEGER
		do
			count := 99 - count_consumed
			x := count; y := count
			z := 99 - (count_consumed + 1) \\ 100
			verse := replace_verse_template (count)

			(<< x, y, z >>).do_all_with_index (
				agent (value, i: INTEGER)
					do
						verse.replace_substring_all (Substitution_variable_names [i], bottle_expression (value, i))
					end
			)
			print (verse)
		end

feature -- Implementation

	bottle_expression (count, position: INTEGER): STRING is
			--
		do
			inspect count
				when 0 then
					Result := "no more bottles"
					if position = 1 then Result.put ('N', 1) end

				when 1 then
					Result := count.out + " bottle"

				else
					Result := count.out + " bottles"
			end
		end

	replace_verse_template (count: INTEGER): STRING is
			--
		do
			Result := "$x of beer on the wall, $y of beer.%N$action, $z of beer on the wall.%N%N"
			Result.replace_substring_all ("$action", Verse_line_2_alternatives [count.min (1) + 1])
		end

	verse: STRING

feature -- Constants

	Verse_line_2_alternatives: ARRAY [STRING] is
			--
		once
			Result := << "Go to the store and buy some more", "Take one down and pass it around" >>
		end

	Substitution_variable_names: ARRAY [STRING] is
			--
		once
			Result := << "$x", "$y", "$z" >>
		end

end

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
1Anonymous04/20/054

Comments

>>  Author said on 12/12/09 17:03:02

Author Apologies for forgetting to substitute spaces for tabs. I have asked for it to be corrected.

Download Source | Write Comment

Add Comment

Please provide a value for the fields Name, Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.

Please don't post large portions of code here! Use the form to submit new examples or updates instead!

Name:

eMail:

URL:

Security Code:
  
Comment: