Language Dart
(An object oriented version of the song.)
Date: | 10/15/11 |
Author: | Ville Saalo |
URL: | http://netbooksandeverything.blogspot.com/ |
Comments: | 0 |
Info: | http://www.dartlang.org |
Score: | (2.75 in 4 votes) |
class BeerWall { int _beers; String _container; BeerWall.withBeers(this._beers, [this._container = "bottle"]); int get beers() => _beers; String get container() => _container; void takeBeer() { if (_beers > 0) --_beers; else throw "Go to the store and buy some more"; } void set beers(int amount) { if (amount == null) _beers = 0; else if (amount < 0) throw "Cannot have a negative amount of ${container}s!"; _beers = amount; } } class Singer { BeerWall _wall; sing() { _wall = new BeerWall.withBeers(99); try { while (true) { print('${_singAmount(true)} of beer on the wall, ${_singAmount()} of beer.'); _wall.takeBeer(); print('Take one down and pass it around, ${_singAmount()} of beer on the wall.\n'); } } catch ( var beerException ) { _wall.beers = 99; print('$beerException, ${_singAmount()} of beer on the wall.'); } } String _singAmount([bool startWithUppercase = false]) { int amount = _wall.beers; if (amount > 1) return "$amount ${_wall.container}s"; else if (amount == 1) return "$amount ${_wall.container}"; else if (startWithUppercase) return "No more ${_wall.container}s"; else return "no more ${_wall.container}s"; } } main() { var singer = new Singer(); singer.sing(); }
Download Source | Write Comment
Alternative Versions
Version | Author | Date | Comments | Rate |
---|---|---|---|---|
1 | houmei | 10/13/11 | 0 | |
Class used version | Jimin Park | 10/12/11 | 0 |
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!
Comments