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 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.00 in 1 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

VersionAuthorDateCommentsRate
Class used versionJimin Park10/12/110
2houmei10/13/110

Comments

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: