public class BottleSong {
/**
* The number of bottles on which stanzas to be written. The max and min limit to set this is 0 and 99 respectively.
*/
private int numberOfBottles = 0;
/**
* Strings that will be displayed in ones positions
*/
private String[] onesStrings = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
/**
* Strings that will be displayed in tens positions
*/
private String[] tensStrings = {"zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
/**
* The special numbers which don't come in a pattern. They are the numbers between 10 and 20
*/
private String[] tenToTwentyStrings = {"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
/**
* Constructor
* @param numberOfBottles The number of bottles on which stangas to be written
* @see #numberOfBottles
*/
public BottleSong(int numberOfBottles){
setNumberOfBottles(numberOfBottles);
}
/**
* Set appropriate number of bottles according to the conditions
* @param numberOfBottles
*/
private void setNumberOfBottles(int numberOfBottles){
if (numberOfBottles < 0){ // Min should be zero
this.numberOfBottles = 0;
}else if (numberOfBottles > 99){ // Max should be ninety nine
this.numberOfBottles = 99;
}else{
this.numberOfBottles = numberOfBottles;
}
}
/**
* Print the whole poem
*/
public void printStanza(){
while (numberOfBottles >= 0){
//
printStanza(numberOfBottles);
numberOfBottles --;
}
}
/**
* Print stanza for the current number of bottles
* @param numberOfBottles Current number of bottle count present.
*/
private void printStanza(int numberOfBottles){
String numberOfBottlesInText = getTextForNumber(numberOfBottles) + " bottle" + (numberOfBottles != 1 ? "s" : "");
System.out.println(numberOfBottlesInText + " on the wall\n" + numberOfBottlesInText);
if (numberOfBottles > 0){// For last bottle, no need to print this line
System.out.println("\nTake one down, pass it around\n");
}
}
/**
* Provides the text string for the given number. For example, one for 1.
* @param numberToBeConverted The number to be converted
* @return The number in text format
*/
private String getTextForNumber(int numberToBeConverted){
String textForNumber = "";
int tensPart = numberToBeConverted / 10;
int onesPart = numberToBeConverted % 10;
if (onesPart == 0){
textForNumber += tensStrings[tensPart];
}else{
if (tensPart > 1){
textForNumber += (tensStrings[tensPart] + " " + onesStrings[onesPart - 1]);
}else if (tensPart == 1){
textForNumber += tenToTwentyStrings[onesPart - 1];
}else{
textForNumber += onesStrings[onesPart - 1];
}
}
return textForNumber;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) { here
new BottleSong(Integer.parseInt(args[0])).
pr… here
}
} here