From 3fa371bcc362de7d9f6cdfa94fc4a20fa434cb12 Mon Sep 17 00:00:00 2001 From: asvarish <42956531+asvarish@users.noreply.github.com> Date: Fri, 31 May 2019 20:17:07 +0300 Subject: [PATCH 1/4] Update BottleSong.java --- .../ru/ifmo/cet/javabasics/BottleSong.java | 83 ++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) diff --git a/src/main/java/ru/ifmo/cet/javabasics/BottleSong.java b/src/main/java/ru/ifmo/cet/javabasics/BottleSong.java index 541b26e..e54dd8d 100644 --- a/src/main/java/ru/ifmo/cet/javabasics/BottleSong.java +++ b/src/main/java/ru/ifmo/cet/javabasics/BottleSong.java @@ -31,13 +31,94 @@ * Нужно ограничить возможность взятия бутылок натуральным число не более 99 бутылок за раз. */ public class BottleSong { + private final int drunk; + private final String[] under19 = { + "", + "one", + "two", + "three", + "four", + "five", + "six", + "seven", + "eight", + "nine", + "ten", + "eleven", + "twelve", + "thirteen", + "fourteen", + "fifteen", + "sixteen", + "seventeen", + "eighteen", + "nineteen" + }; + + private final String[] tenty = { + "", + "ten", + "twenty", + "thirty", + "forty", + "fifty", + "sixty", + "seventy", + "eighty", + "ninety" + }; + private final String[] tenty = { + "", + "ten", + "twenty", + "thirty", + "forty", + "fifty", + "sixty", + "seventy", + "eighty", + "ninety" + }; + + private String change(int number) { + if (number < 20) { + return under19[number]; + } + else if (number % 10 == 0) { + return tenty[number / 10]; + } + else { + return tenty[number / 10] + " " + under19[number % 10]; + } + } + + private String IfOne(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 Songg = new StringBuilder(); + for (counter = 99; counter > drunk; counter -= drunk) { + Songg.append(counter + " bottles of beer on the wall, " + counter + " bottles of beer.\n"); + Songg.append("Take " + change(drunk) + " down and pass around, " + (counter - drunk) + IfOne(counter - drunk) + " of beer on the wall.\n"); + } + + Songg.append(counter + IfOne(counter) + " of beer on the wall, " + counter + IfOne(counter) + " of beer.\n"); + Songg.append("Take " + change(counter) + " down and pass around, no more bottles of beer on the wall.\n"); + Songg.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 Songg.toString(); + } } -} From d9af589c22350d5e1db38b66ad21a15baaae47fa Mon Sep 17 00:00:00 2001 From: asvarish <42956531+asvarish@users.noreply.github.com> Date: Fri, 31 May 2019 20:27:48 +0300 Subject: [PATCH 2/4] Update BottleSong.java --- .../ru/ifmo/cet/javabasics/BottleSong.java | 45 +++++++------------ 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/src/main/java/ru/ifmo/cet/javabasics/BottleSong.java b/src/main/java/ru/ifmo/cet/javabasics/BottleSong.java index e54dd8d..9340fc7 100644 --- a/src/main/java/ru/ifmo/cet/javabasics/BottleSong.java +++ b/src/main/java/ru/ifmo/cet/javabasics/BottleSong.java @@ -30,10 +30,9 @@ * Значение передается в качестве параметра конструктора * Нужно ограничить возможность взятия бутылок натуральным число не более 99 бутылок за раз. */ -public class BottleSong { private final int drunk; - private final String[] under19 = { + private final String[] numbersUpTo19 = { "", "one", "two", @@ -56,19 +55,7 @@ public class BottleSong { "nineteen" }; - private final String[] tenty = { - "", - "ten", - "twenty", - "thirty", - "forty", - "fifty", - "sixty", - "seventy", - "eighty", - "ninety" - }; - private final String[] tenty = { + private final String[] tensNumbers = { "", "ten", "twenty", @@ -81,21 +68,22 @@ public class BottleSong { "ninety" }; - private String change(int number) { + private String transform(int number) { if (number < 20) { - return under19[number]; + return numbersUpTo19[number]; } else if (number % 10 == 0) { - return tenty[number / 10]; + return tensNumbers[number / 10]; } else { - return tenty[number / 10] + " " + under19[number % 10]; + return tensNumbers[number / 10] + " " + numbersUpTo19[number % 10]; } } - private String IfOne(int number){ + private String checkIfOne(int number){ return number == 1 ? " bottle": " bottles"; } + public BottleSong(int bottleTakenAtOnce) { //TODO drunk = bottleTakenAtOnce; @@ -109,16 +97,17 @@ public String getBottleSongLyrics() { } int counter; - StringBuilder Songg = new StringBuilder(); + StringBuilder res = new StringBuilder(); for (counter = 99; counter > drunk; counter -= drunk) { - Songg.append(counter + " bottles of beer on the wall, " + counter + " bottles of beer.\n"); - Songg.append("Take " + change(drunk) + " down and pass around, " + (counter - drunk) + IfOne(counter - drunk) + " of beer on the wall.\n"); + 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"); } - Songg.append(counter + IfOne(counter) + " of beer on the wall, " + counter + IfOne(counter) + " of beer.\n"); - Songg.append("Take " + change(counter) + " down and pass around, no more bottles of beer on the wall.\n"); - Songg.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"); + 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 Songg.toString(); - } + return res.toString(); } + +} From 803a27e3d853dcb1a622307810dbff2fec56d5ad Mon Sep 17 00:00:00 2001 From: asvarish <42956531+asvarish@users.noreply.github.com> Date: Fri, 31 May 2019 21:00:28 +0300 Subject: [PATCH 3/4] ASVA --- .../ru/ifmo/cet/javabasics/BottleSong.java | 182 ++++++++---------- 1 file changed, 77 insertions(+), 105 deletions(-) diff --git a/src/main/java/ru/ifmo/cet/javabasics/BottleSong.java b/src/main/java/ru/ifmo/cet/javabasics/BottleSong.java index 9340fc7..5a66efd 100644 --- a/src/main/java/ru/ifmo/cet/javabasics/BottleSong.java +++ b/src/main/java/ru/ifmo/cet/javabasics/BottleSong.java @@ -1,113 +1,85 @@ -package ru.ifmo.cet.javabasics; +public class BottleSong { -/** - * Нужно реализовать констурктор и метод, возвращающий слова песни про бутылки на стене. - *

- * Слова следующие: - *

- * 99 bottles of beer on the wall, 99 bottles of beer - * Take one down, pass it around, 98 bottles of beer - * 98 bottles of beer on the wall, 98 bottles of beer - * Take one down, pass it around, 97 bottles of beer - * 97 bottles of beer on the wall, 97 bottles of beer - * Take one down, pass it around, 96 bottles of beer - * 96 bottles of beer on the wall, 96 bottles of beer - * Take one down, pass it around, 95 bottles of beer - * 95 bottles of beer on the wall, 95 bottles of beer - * ... - *

- * 3 bottles of beer on the wall, 3 bottles of beer - * Take one down, pass it around, 2 bottles of beer - * 2 bottles of beer on the wall, 2 bottles of beer - * Take one down, pass it around, 1 bottles of beer - * 1 bottle of beer on the wall, 1 bottle of beer - * Take one down, pass it around, no more bottles of beer on the wall - * No more bottles of beer on the wall, no more bottles of beer - * Go to the store and buy some more, 99 bottles of beer on the wall - *

- * Дело усложняется тем, что текст песни переменный: - * За раз может быть взято несколько бутылок. - * Значение передается в качестве параметра конструктора - * Нужно ограничить возможность взятия бутылок натуральным число не более 99 бутылок за раз. - */ - 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]; + private int bottle = 99; + private int bottleTakenAtOnce; + private String bottleTaken; + public BottleSong(int bottleTakenAtOnce) { + //TODO + if ( bottleTakenAtOnce <= 0 || bottleTakenAtOnce > 99) { + throw new IllegalArgumentException(); } - else if (number % 10 == 0) { - return tensNumbers[number / 10]; + this.bottleTakenAtOnce = bottleTakenAtOnce; + bottleTaken = numToString(bottleTakenAtOnce); + } + public String getBottleSongLyrics(){ + StringBuilder sb = new StringBuilder(); + while (bottle >= bottleTakenAtOnce) { + sb.append(bottle + " bottles of beer on the wall, " + bottle + " bottles of beer.\n"); + sb.append("Take " + bottleTaken + " down and pass around, "); + bottle -= bottleTakenAtOnce; + if (bottle == 1) { + sb.append(bottle + " bottle of beer on the wall.\n"); + sb.append(bottle + " bottle of beer on the wall, " + bottle + " bottle of beer.\n"); + sb.append("Take " + numToString(bottle) + " down and pass around, no more bottles of beer on the wall.\n"); + bottle = 0; + } else if(bottle == 0) { + sb.append("no more bottles of beer on the wall.\n"); + } else { + sb.append(bottle + " bottles of beer on the wall.\n"); + } } - else { - return tensNumbers[number / 10] + " " + numbersUpTo19[number % 10]; + if (bottle < bottleTakenAtOnce && bottle > 0) { + sb.append(bottle + " bottles of beer on the wall, " + bottle + " bottles of beer.\n"); + sb.append("Take " + numToString(bottle) + " down and pass around, no more bottles of beer on the wall.\n"); + bottle = 0; } + sb.append("No more bottles of beer on the wall, no more bottles of beer.\n"); + sb.append("Go to the store and buy some more, 99 bottles of beer on the wall.\n"); + return sb.toString(); } - - 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(); + private String numToString(int num) { + String[] array = new String[100]; + array[0] = ""; + array[1] = "one"; + array[2] = "two"; + array[3] = "three"; + array[4] = "four"; + array[5] = "five"; + array[6] = "six"; + array[7] = "seven"; + array[8] = "eight"; + array[9] = "nine"; + array[10] = "ten"; + array[11] = "eleven"; + array[12] = "twelve"; + array[13] = "thirteen"; + array[14] = "fourteen"; + array[15] = "fifteen"; + array[16] = "sixteen"; + array[17] = "seventeen"; + array[18] = "eighteen"; + array[19] = "nineteen"; + array[20] = "twenty"; + array[30] = "thirty"; + array[40] = "forty"; + array[50] = "fifty"; + array[60] = "sixty"; + array[70] = "seventy"; + array[80] = "eighty"; + array[90] = "ninety"; + StringBuilder sb = new StringBuilder(); + if (num / 10 > 1 && num % 10 != 0) { + sb.append(array[(num / 10) * 10]).append(" ").append(array[num % 10]); } - - 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"); + if (num / 10 > 1 && num % 10 == 0) { + sb.append(array[(num / 10) * 10]); } - - 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(); + if (num / 10 == 1) { + sb.append(array[num]); + } + if (num / 10 == 0) { + sb.append(array[num]); + } + return sb.toString(); } - -} +} From cfa0a98cc7699fc5bfbfc9e599d391df79301489 Mon Sep 17 00:00:00 2001 From: asvarish <42956531+asvarish@users.noreply.github.com> Date: Fri, 31 May 2019 21:10:35 +0300 Subject: [PATCH 4/4] Update BottleSong.java --- .../ru/ifmo/cet/javabasics/BottleSong.java | 148 ++++++++---------- 1 file changed, 69 insertions(+), 79 deletions(-) diff --git a/src/main/java/ru/ifmo/cet/javabasics/BottleSong.java b/src/main/java/ru/ifmo/cet/javabasics/BottleSong.java index 5a66efd..eb368fb 100644 --- a/src/main/java/ru/ifmo/cet/javabasics/BottleSong.java +++ b/src/main/java/ru/ifmo/cet/javabasics/BottleSong.java @@ -1,85 +1,75 @@ -public class BottleSong { +package ru.ifmo.cet.javabasics; + +import java.util.HashMap; +import java.util.Map; - private int bottle = 99; - private int bottleTakenAtOnce; - private String bottleTaken; +public class BottleSong { + private int bottlesTakenAtOnce; public BottleSong(int bottleTakenAtOnce) { - //TODO - if ( bottleTakenAtOnce <= 0 || bottleTakenAtOnce > 99) { - throw new IllegalArgumentException(); - } - this.bottleTakenAtOnce = bottleTakenAtOnce; - bottleTaken = numToString(bottleTakenAtOnce); + this.bottlesTakenAtOnce = bottleTakenAtOnce; } - public String getBottleSongLyrics(){ - StringBuilder sb = new StringBuilder(); - while (bottle >= bottleTakenAtOnce) { - sb.append(bottle + " bottles of beer on the wall, " + bottle + " bottles of beer.\n"); - sb.append("Take " + bottleTaken + " down and pass around, "); - bottle -= bottleTakenAtOnce; - if (bottle == 1) { - sb.append(bottle + " bottle of beer on the wall.\n"); - sb.append(bottle + " bottle of beer on the wall, " + bottle + " bottle of beer.\n"); - sb.append("Take " + numToString(bottle) + " down and pass around, no more bottles of beer on the wall.\n"); - bottle = 0; - } else if(bottle == 0) { - sb.append("no more bottles of beer on the wall.\n"); - } else { - sb.append(bottle + " bottles of beer on the wall.\n"); + + public String getBottleSongLyrics() { + int bottles = 99; + String SUNum = ""; + String BottleSongLyrics = ""; + String BottlesBottle = "bottles"; + + Map bottleTaken = new HashMap(); + + bottleTaken.put(0, ""); + bottleTaken.put(1, "one"); + bottleTaken.put(2, "two"); + bottleTaken.put(3, "three"); + bottleTaken.put(4, "four"); + bottleTaken.put(5, "five"); + bottleTaken.put(6, "six"); + bottleTaken.put(7, "seven"); + bottleTaken.put(8, "eight"); + bottleTaken.put(9, "nine"); + bottleTaken.put(10, "ten"); + bottleTaken.put(11, "eleven"); + bottleTaken.put(12, "twelve"); + bottleTaken.put(13, "thirteen"); + bottleTaken.put(14, "fourteen"); + bottleTaken.put(15, "fifteen"); + bottleTaken.put(16, "sixteen"); + bottleTaken.put(17, "seventeen"); + bottleTaken.put(18, "eighteen"); + bottleTaken.put(19, "nineteen"); + bottleTaken.put(20, "twenty"); + bottleTaken.put(30, "thirty"); + bottleTaken.put(40, "forty"); + bottleTaken.put(50, "fifty"); + bottleTaken.put(60, "sixty"); + bottleTaken.put(70, "seventy"); + bottleTaken.put(80, "eighty"); + bottleTaken.put(90, "ninety"); + + if (bottlesTakenAtOnce < 20 || bottlesTakenAtOnce % 10 == 0) { + SUNum = bottleTaken.get(bottlesTakenAtOnce); + } else SUNum = bottleTaken.get(bottlesTakenAtOnce / 10 * 10) + " " + bottleTaken.get(bottlesTakenAtOnce % 10); + + if (bottlesTakenAtOnce < 100 && bottlesTakenAtOnce > 0) { + while (bottles > bottlesTakenAtOnce) { + BottleSongLyrics += bottles + " bottles of beer on the wall, " + bottles + " bottles of beer.\n"; + BottleSongLyrics += "Take "; + if ((bottles - bottlesTakenAtOnce > 0)) { + bottles -= bottlesTakenAtOnce; + if (bottles == 1) BottlesBottle = "bottle"; + BottleSongLyrics += SUNum + " down and pass around, " + bottles + " " + BottlesBottle + " of beer on the wall.\n"; + } } + if (bottles < 20) { + SUNum = bottleTaken.get(bottles); + } else SUNum = bottleTaken.get(bottles / 10 * 10) + " " + bottleTaken.get(bottles % 10); + BottleSongLyrics += bottles + " " + BottlesBottle + " of beer on the wall, " + bottles + " " + BottlesBottle + " of beer.\n"; + BottleSongLyrics += "Take " + SUNum + " down and pass around, no more bottles of beer on the wall.\n"; + BottleSongLyrics += "No more bottles of beer on the wall, no more bottles of beer.\n"; + BottleSongLyrics += "Go to the store and buy some more, 99 bottles of beer on the wall.\n"; + return BottleSongLyrics; } - if (bottle < bottleTakenAtOnce && bottle > 0) { - sb.append(bottle + " bottles of beer on the wall, " + bottle + " bottles of beer.\n"); - sb.append("Take " + numToString(bottle) + " down and pass around, no more bottles of beer on the wall.\n"); - bottle = 0; - } - sb.append("No more bottles of beer on the wall, no more bottles of beer.\n"); - sb.append("Go to the store and buy some more, 99 bottles of beer on the wall.\n"); - return sb.toString(); - } - private String numToString(int num) { - String[] array = new String[100]; - array[0] = ""; - array[1] = "one"; - array[2] = "two"; - array[3] = "three"; - array[4] = "four"; - array[5] = "five"; - array[6] = "six"; - array[7] = "seven"; - array[8] = "eight"; - array[9] = "nine"; - array[10] = "ten"; - array[11] = "eleven"; - array[12] = "twelve"; - array[13] = "thirteen"; - array[14] = "fourteen"; - array[15] = "fifteen"; - array[16] = "sixteen"; - array[17] = "seventeen"; - array[18] = "eighteen"; - array[19] = "nineteen"; - array[20] = "twenty"; - array[30] = "thirty"; - array[40] = "forty"; - array[50] = "fifty"; - array[60] = "sixty"; - array[70] = "seventy"; - array[80] = "eighty"; - array[90] = "ninety"; - StringBuilder sb = new StringBuilder(); - if (num / 10 > 1 && num % 10 != 0) { - sb.append(array[(num / 10) * 10]).append(" ").append(array[num % 10]); - } - if (num / 10 > 1 && num % 10 == 0) { - sb.append(array[(num / 10) * 10]); - } - if (num / 10 == 1) { - sb.append(array[num]); - } - if (num / 10 == 0) { - sb.append(array[num]); - } - return sb.toString(); + else throw new IllegalArgumentException(); } -} +} +