Advertisement
JeffGrigg

NumberOfBottlesOfBeerOnTheWall

Feb 17th, 2020
1,027
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.67 KB | None | 0 0
  1. public abstract class NumberOfBottlesOfBeerOnTheWall {
  2.  
  3.     private NumberOfBottlesOfBeerOnTheWall() {
  4.         throw new IllegalArgumentException("Not expecting instances of this class.");
  5.     }
  6.  
  7.     public static void singNumberOfBottlesOfBeerOnTheWall(final int numberOfBottles) {
  8.  
  9.         if (numberOfBottles >= 0) {
  10.             printVerseForThisManyBottles(numberOfBottles);
  11.             singNumberOfBottlesOfBeerOnTheWall(numberOfBottles - 1);
  12.         }
  13.     }
  14.  
  15.     private static void printVerseForThisManyBottles(final int numberOfBottles) {
  16.  
  17.         printNBottlesOfBeer(numberOfBottles, " on the wall. ");
  18.         printNBottlesOfBeer(numberOfBottles, ". ");
  19.  
  20.         if (numberOfBottles > 0) {
  21.             printTakeOneDownLeaving(numberOfBottles);
  22.         } else {
  23.             printNoMoreBottlesToPassAround();
  24.         }
  25.  
  26.         System.out.println();
  27.     }
  28.  
  29.     private static void printTakeOneDownLeaving(final int numberOfBottles) {
  30.         System.out.print("Take "
  31.                 + ((numberOfBottles == 1) ? "it" : "one")
  32.                 + " down and pass it around. ");
  33.  
  34.         printNBottlesOfBeer(numberOfBottles - 1, " on the wall.");
  35.     }
  36.  
  37.     private static void printNoMoreBottlesToPassAround() {
  38.         System.out.print("There are no more to pass around. ");
  39.  
  40.         printNBottlesOfBeer(0, " on the wall.");
  41.     }
  42.  
  43.     private static void printNBottlesOfBeer(final int numberOfBottles, final String postfix) {
  44.         System.out.print(((numberOfBottles == 0) ? "No" : numberOfBottles)
  45.                 + " "
  46.                 + ((numberOfBottles == 1) ? "bottle" : "bottles")
  47.                 + " of beer"
  48.                 + postfix);
  49.     }
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement