Language BeanShell
| Date: | 06/18/05 |
| Author: | Tom Edelson |
| URL: | http://www.well.com/user/edelsont/ |
| Comments: | 0 |
| Info: | http://www.beanshell.org/ |
| Score: |
// 99bottles.bsh
//
// Tom Edelson, last modified 2005-06-18
// This is BeanShell, a scripting language based on, and implemented in,
// Java.
//
// Mostly, the BeanShell language just is Java; but type declarations
// on variables are optional, and some scripting-friendly features
// have been added. One of these features is the ability of a method
// to return a "closure", which is a bundle of [persistent] data and
// code; thus, like an object, but not derived from a class.
//
// In this example, each of the methods "newWall" and "newSinger" creates and
// returns a closure, whose content is defined by statements,
// and nested method definitions, within the "constructor" itself.
// For more information on BeanShell, and to download it, visit
//
// http://www.beanshell.org/
formatBottleCount (number) {
if (number == 0) {
return "No bottles ";
}
if (number == 1) {
return "One bottle ";
}
return number + " bottles ";
} // end method formatBottleCount
newWall (initialCount) {
count = initialCount;
getCount () {
return count;
}
hasBottles () {
return count > 0;
}
takeOne () {
count--;
return count;
}
return this;
} // end method newWall
newSinger (wallRef) {
wall = wallRef;
count = wall.getCount();
countString = formatBottleCount (count);
print ("");
singVerse () {
print (countString + "of beer on the wall,");
print (countString + "of beer;");
count = wall.takeOne();
print ("Take one down, pass it around,");
countString = formatBottleCount (count);
print (countString + "of beer on the wall.");
print ("");
} // end method singVerse
finish () {
print ("Gotta be another beer wall around here somewhere ...");
print ("");
}
return this;
} // end method newSinger
// Main program code:
wall = newWall (99);
singer = newSinger (wall);
while (wall.hasBottles()) {
singer.singVerse();
}
singer.finish();
// end of file "99bottles.bsh".
Download Source | Write Comment
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