From 5350eb1e2c7c8e46d6dba6939c8c77f883ec641c Mon Sep 17 00:00:00 2001 From: belonel Date: Wed, 29 May 2019 17:40:59 +0300 Subject: [PATCH] Just in time --- .../ru/ifmo/cet/javabasics/BottleSong.java | 74 ++++++++++++++++++- 1 file changed, 71 insertions(+), 3 deletions(-) diff --git a/src/main/java/ru/ifmo/cet/javabasics/BottleSong.java b/src/main/java/ru/ifmo/cet/javabasics/BottleSong.java index 541b26e..2673f30 100644 --- a/src/main/java/ru/ifmo/cet/javabasics/BottleSong.java +++ b/src/main/java/ru/ifmo/cet/javabasics/BottleSong.java @@ -31,13 +31,81 @@ * Нужно ограничить возможность взятия бутылок натуральным число не более 99 бутылок за раз. */ public class BottleSong { + private final int drunk; + + private final String[] numbersUpTo19 = { + "", + "one", + "two", + "three", + "four", + "five", + "six", + "seven", + "eight", + "nine", + "ten", + "eleven", + "twelve", + "thirteen", + "fourteen", + "fifteen", + "sixteen", + "seventeen", + "eighteen", + "nineteen" + }; + + private final String[] tensNumbers = { + "", + "ten", + "twenty", + "thirty", + "forty", + "fifty", + "sixty", + "seventy", + "eighty", + "ninety" + }; + + private String transform(int number) { + if (number < 20) { + return numbersUpTo19[number]; + } + else if (number % 10 == 0) { + return tensNumbers[number / 10]; + } + else { + return tensNumbers[number / 10] + " " + numbersUpTo19[number % 10]; + } + } + + private String checkIfOne(int number){ + return number == 1 ? " bottle": " bottles"; + } public BottleSong(int bottleTakenAtOnce) { - //TODO + drunk = bottleTakenAtOnce; } public String getBottleSongLyrics() { - //TODO - throw new UnsupportedOperationException(); + if (drunk > 99 || drunk <= 0) { + throw new IllegalArgumentException(); + } + + int counter; + StringBuilder res = new StringBuilder(); + for (counter = 99; counter > drunk; counter -= drunk) { + res.append(counter + " bottles of beer on the wall, " + counter + " bottles of beer.\n"); + res.append("Take " + transform(drunk) + " down and pass around, " + (counter - drunk) + checkIfOne(counter - drunk) + " of beer on the wall.\n"); + } + + res.append(counter + checkIfOne(counter) + " of beer on the wall, " + counter + checkIfOne(counter) + " of beer.\n"); + res.append("Take " + transform(counter) + " down and pass around, no more bottles of beer on the wall.\n"); + res.append("No more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.\n"); + + return res.toString(); } + }