class BottlesOfBeerSong { private number bottles; private string output; private string format; constructor(number bottles) { self(bottles, "html"); } constructor(number bottles, string format) { .bottles = bottles; .format = format; } function play() { while (.bottles > 0) { string bottles = "bottle"; .start(); if (.bottles > 1) { bottles += "s"; } .line("${.bottles} ${bottles} of beer on the wall,"); .line("${.bottles} ${bottles} of beer,"); .line("Take one down, pass it around,"); .bottles--; if (.bottles > 0) { .line("${.bottles} ${bottles} of beer on the wall."); } else { .line("No bottles of beer on the wall!"); } .stop(); } return .output; } private function start() { if (.format == "html") { .output += "\n
";
}
}
private function line(string line) {
if (.format == "html") {
.output += "\n\t${line}
";
} else {
.output += "\n${line}";
}
}
private function stop() {
if (.format == "html") {
.output = .output[..-7] + "\n