Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

P3121 Lider Alexandr task-1 #91

Open
wants to merge 1 commit into
base: task-1
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 71 additions & 3 deletions src/main/java/ru/ifmo/cet/javabasics/BottleSong.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}