diff --git a/README.md b/README.md index 5d686e9f..2c5d7828 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,10 @@ -# Java Core June 2021 +# Java_Core_June_2021 -## *Nikolaev Artem* +## *Anton Miazin* | Number | Solution | Short description | --- | --- | --- | -| HW1 | [Console printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/master/src/main/java/homework_1) | The app that reads input arguments and prints them, until "error" argument | - -[Link to markdown giude](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet) +| HW1 | [Console printer]( https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/AntonMiazin/src/main/java/Homework_1 ) | The app that reads input arguments and prints them, until "error" argument | +| HW2 | [Traffic Light]( https://github.com/NikolaevArtem/Java_Core_June_2021/blob/feature/AntonMiazin/src/main/java/Homework_2/TrafficLight.java ) | The looped app that reads console input and return a color of a traffic light | +| HW2 | [Pyramid Printer]( https://github.com/NikolaevArtem/Java_Core_June_2021/blob/feature/AntonMiazin/src/main/java/Homework_2/PyramidPrinter.java ) | The app that build a triangle pyramid with a custom high | +| HW2 | [Random Chars Table]( https://github.com/NikolaevArtem/Java_Core_June_2021/blob/feature/AntonMiazin/src/main/java/Homework_2/RandomCharsTable.java ) | The looped app that create & print a custom array of the capital letters | \ No newline at end of file diff --git a/src/main/java/Homework_1/ConsolePrinter.java b/src/main/java/Homework_1/ConsolePrinter.java new file mode 100644 index 00000000..936d5708 --- /dev/null +++ b/src/main/java/Homework_1/ConsolePrinter.java @@ -0,0 +1,16 @@ +package Homework_1; + +public class ConsolePrinter { + public static void main(String[] args) { + final String ANSI_RED = "\u001B[31m"; + final String ANSI_RESET = "\u001B[0m\n"; + for (String arg : args) { + if (arg.equals("error")) { + System.out.println(ANSI_RED + "Alarm!" + ANSI_RESET); + break; + } else { + System.out.println(arg + " : " + arg.length() + " letters"); + } + } + } +} \ No newline at end of file diff --git a/src/main/java/Homework_2/PyramidPrinter/Main.java b/src/main/java/Homework_2/PyramidPrinter/Main.java new file mode 100644 index 00000000..c04150e4 --- /dev/null +++ b/src/main/java/Homework_2/PyramidPrinter/Main.java @@ -0,0 +1,8 @@ +package Homework_2.PyramidPrinter; + +public class Main { + + public static void main(String[] args) { + new PyramidPrinter().run(); + } +} diff --git a/src/main/java/Homework_2/PyramidPrinter/PyramidPrinter.java b/src/main/java/Homework_2/PyramidPrinter/PyramidPrinter.java new file mode 100644 index 00000000..cd637f29 --- /dev/null +++ b/src/main/java/Homework_2/PyramidPrinter/PyramidPrinter.java @@ -0,0 +1,58 @@ +package Homework_2.PyramidPrinter; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +public class PyramidPrinter { + + public void run () { + String inputData = consoleReading(); + String result = processing(inputData); + output(result); + } + + + private String consoleReading() { + System.out.print("Enter the pyramid height as a single integer: "); + try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) { + String input = reader.readLine(); + return input; + } + catch (IOException e) { + return "This should never happen..."; + } + } + + private String processing(String input) { + try { + int blocks = Integer.parseInt(input); + if (blocks < 0) { + throw new NumberFormatException(); + } + return pyramid(blocks); + } catch (NumberFormatException e) { + return "Only 1 non-negative integer is allowed as passed parameter"; + } + } + + private String pyramid (int blocks) { + StringBuilder build = new StringBuilder(); + try{ + for (int i = 0; i < blocks; i++) { + for (int j = 0; j <= i; j++) { + build.append('x'); + } + build.append('\n'); + } + return build.toString(); + } + catch (OutOfMemoryError e) { + return "Heap space is out of memory, please input a smaller integer"; + } + } + + private void output (String result){ + System.out.print(result); + } +} diff --git a/src/main/java/Homework_2/RandomCharsTable/Main.java b/src/main/java/Homework_2/RandomCharsTable/Main.java new file mode 100644 index 00000000..c95d0bd6 --- /dev/null +++ b/src/main/java/Homework_2/RandomCharsTable/Main.java @@ -0,0 +1,8 @@ +package Homework_2.RandomCharsTable; + +public class Main { + + public static void main(String[] args) { + new RandomCharsTable().run(); + } +} diff --git a/src/main/java/Homework_2/RandomCharsTable/RandomCharsTable.java b/src/main/java/Homework_2/RandomCharsTable/RandomCharsTable.java new file mode 100644 index 00000000..fad3ef98 --- /dev/null +++ b/src/main/java/Homework_2/RandomCharsTable/RandomCharsTable.java @@ -0,0 +1,77 @@ +package Homework_2.RandomCharsTable; + + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Random; + +public class RandomCharsTable { + + public void run () { + String inputData = consoleReading(); + String result = processing(inputData); + output(result); + } + + private static String consoleReading() { + System.out.print("Enter the sizes of a table and a strategy: "); + try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) { + String input = reader.readLine(); + return input; + } + catch (IOException e) { + return "This should never happen..."; + } + } + + private String processing(String input) { + try { + String[] input_parts = input.split(" "); + int length = Integer.parseInt(input_parts[0]); + int width = Integer.parseInt(input_parts[1]); + String strategy = input_parts[2]; + if (length < 0 || width < 0) { + throw new NumberFormatException(); + } + if (!strategy.equalsIgnoreCase("even") && !strategy.equalsIgnoreCase("odd")) { + throw new NumberFormatException(); + } + return elementsOf(length, width, strategy); + } + catch (NumberFormatException e) { + return "Passed parameters should match the format [positive integer] [positive integer] [even|odd]"; + } + } + + private String elementsOf(int length, int width, String strategy) { + StringBuilder table = new StringBuilder(); + StringBuilder evens = new StringBuilder(); + StringBuilder odds = new StringBuilder(); + evens.append("Even letters - "); + odds.append("Odd letters - "); + for (int i = 0; i < length; i++){ + table.append('|'); + for(int j = 0; j < width; j++) { + Random ran = new Random(); + int number = 65 + ran.nextInt(26); + table.append((char) number) + .append('|'); + if (number % 2 == 0){ + evens = evens.length() == 15 ? evens.append((char) number) : evens.append(", " + (char) number); + } + else { + odds = odds.length() == 14 ? odds.append((char) number) : odds.append(", " + (char) number); + } + } + table.append("\n"); + } + table = strategy.equalsIgnoreCase("even") ? table.append(evens) : table.append(odds); + return table.toString(); + } + + private void output (String result){ + System.out.print(result); + } +} + diff --git a/src/main/java/Homework_2/TrafficLight/Main.java b/src/main/java/Homework_2/TrafficLight/Main.java new file mode 100644 index 00000000..3a5d00e6 --- /dev/null +++ b/src/main/java/Homework_2/TrafficLight/Main.java @@ -0,0 +1,8 @@ +package Homework_2.TrafficLight; + + +public class Main { + public static void main(String[] args) { + new TrafficLight().run(); + } +} diff --git a/src/main/java/Homework_2/TrafficLight/TrafficLight.java b/src/main/java/Homework_2/TrafficLight/TrafficLight.java new file mode 100644 index 00000000..a658549a --- /dev/null +++ b/src/main/java/Homework_2/TrafficLight/TrafficLight.java @@ -0,0 +1,60 @@ +package Homework_2.TrafficLight; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +public class TrafficLight { + + public void run () { + String inputData = consoleReading(); + String result = processing(inputData); + output(result); + } + + + private static String consoleReading() { + System.out.print("Please enter the amount of seconds as integer within 0 - 86399 range: "); + try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) { + String input = reader.readLine(); + return input; + } + catch (IOException e) { + return "This should never happen..."; + } + } + + private String processing(String input) { + try { + int time = Integer.parseInt(input); + if (time < 0){ + throw new NumberFormatException(); + } + else if (time > 86399){ + throw new NumberFormatException(); + } + return color(time); + } + catch (NumberFormatException e) { + return "Only 1 non-negative integer is allowed as passed parameter"; + } + } + + private String color(int time) { + StringBuilder light = new StringBuilder(); + int timeSec = time % 60; + if (timeSec >= 0 && timeSec < 35) { + light.append("GREEN"); + } else if ((timeSec >= 35 && timeSec < 40) || (timeSec >= 55 && timeSec < 60)) { + light.append("YELLOW"); + } else if (timeSec >= 40 && timeSec < 54) { + light.append("RED"); + } + return light.toString(); + } + + private void output (String result){ + System.out.print(result); + } +} + diff --git a/src/main/java/homework_1/Main.java b/src/main/java/Main.java similarity index 52% rename from src/main/java/homework_1/Main.java rename to src/main/java/Main.java index 07c029a2..3ab203b5 100644 --- a/src/main/java/homework_1/Main.java +++ b/src/main/java/Main.java @@ -1,9 +1,7 @@ -package homework_1; - public class Main { public static void main(String[] args) { - System.out.println("Hello homework!"); + System.out.println("Hello, username!"); } } diff --git a/src/test/java/Homework_2/PyramidPrinterTest/PyramidPrinterTest.java b/src/test/java/Homework_2/PyramidPrinterTest/PyramidPrinterTest.java new file mode 100644 index 00000000..ffd35b70 --- /dev/null +++ b/src/test/java/Homework_2/PyramidPrinterTest/PyramidPrinterTest.java @@ -0,0 +1,85 @@ +package Homework_2.PyramidPrinterTest; + +import Homework_2.PyramidPrinter.PyramidPrinter; +import base.UnitBase; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class PyramidPrinterTest extends UnitBase { + + @Test + void CaseSymbol_error() { + setInput("Hello!"); + + new PyramidPrinter().run(); + + removeFromOutput("Enter the pyramid height as a single integer: "); + assertEquals("Only 1 non-negative integer is allowed as passed parameter", getOutput()); + + } + + @Test + void CaseDouble_error() { + setInput("0.1"); + + new PyramidPrinter().run(); + + removeFromOutput("Enter the pyramid height as a single integer: "); + assertEquals("Only 1 non-negative integer is allowed as passed parameter", getOutput()); + } + + @Test + void CaseStandard_pass() { + setInput("4"); + + new PyramidPrinter().run(); + + printOut(); + removeFromOutput("Enter the pyramid height as a single integer: "); + assertEquals("x", getOutputLines()[0]); + assertEquals("xx", getOutputLines()[1]); + assertEquals("xxx", getOutputLines()[2]); + assertEquals("xxxx", getOutputLines()[3]); + } + + @Test + void CaseZero_pass() { + setInput("0"); + + new PyramidPrinter().run(); + + removeFromOutput("Enter the pyramid height as a single integer: "); + assertEquals("", getOutput()); + } + + @Test + void CaseNegative_error() { + setInput("-5"); + + new PyramidPrinter().run(); + + removeFromOutput("Enter the pyramid height as a single integer: "); + assertEquals("Only 1 non-negative integer is allowed as passed parameter", getOutput()); + } + + @Test + void CaseLong_error() { + setInput("2147483648"); + + new PyramidPrinter().run(); + + removeFromOutput("Enter the pyramid height as a single integer: "); + assertEquals("Only 1 non-negative integer is allowed as passed parameter", getOutput()); + } + + @Test + void CaseOutOfMemory_error() { + setInput("100500"); + + new PyramidPrinter().run(); + + removeFromOutput("Enter the pyramid height as a single integer: "); + assertEquals("Heap space is out of memory, please input a smaller integer", getOutput()); + } +} diff --git a/src/test/java/Homework_2/RandomCharsTest/RandomCharsTest.java b/src/test/java/Homework_2/RandomCharsTest/RandomCharsTest.java new file mode 100644 index 00000000..ca0450e8 --- /dev/null +++ b/src/test/java/Homework_2/RandomCharsTest/RandomCharsTest.java @@ -0,0 +1,29 @@ +package Homework_2.RandomCharsTest; + +import Homework_2.RandomCharsTable.RandomCharsTable; +import base.UnitBase; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class RandomCharsTest extends UnitBase { + + @Test + void CaseStandard1_pass() { + setInput("2 2 even"); + + new RandomCharsTable().run(); + + printOut(); + removeFromOutput("Enter the sizes of a table and a strategy: "); + assertEquals("x", getOutputLines()[0]); + assertEquals("xx", getOutputLines()[1]); + assertEquals("Even Letters -", getOutputLines()[2]); + + } + +// |L|O| +// |Y|Z| + + +} diff --git a/src/test/java/Homework_2/TrafficLightTest/TrafficLightTest.java b/src/test/java/Homework_2/TrafficLightTest/TrafficLightTest.java new file mode 100644 index 00000000..4cd8b026 --- /dev/null +++ b/src/test/java/Homework_2/TrafficLightTest/TrafficLightTest.java @@ -0,0 +1,121 @@ +package Homework_2.TrafficLightTest; + +import Homework_2.TrafficLight.TrafficLight; +import base.UnitBase; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class TrafficLightTest extends UnitBase { + + @Test + void Case30_Green() { + setInput("30"); + + new TrafficLight().run(); + + printOut(); + removeFromOutput("Please enter the amount of seconds as integer within 0 - 86399 range: "); + assertEquals("GREEN", getOutput()); + } + + @Test + void Case36_Yellow() { + setInput("36"); + + new TrafficLight().run(); + + printOut(); + removeFromOutput("Please enter the amount of seconds as integer within 0 - 86399 range: "); + assertEquals("YELLOW", getOutput()); + } + + @Test + void Case55_Yellow() { + setInput("55"); + + new TrafficLight().run(); + + printOut(); + removeFromOutput("Please enter the amount of seconds as integer within 0 - 86399 range: "); + assertEquals("YELLOW", getOutput()); + } + + @Test + void Case48_Red() { + setInput("48"); + + new TrafficLight().run(); + + printOut(); + removeFromOutput("Please enter the amount of seconds as integer within 0 - 86399 range: "); + assertEquals("RED", getOutput()); + } + + @Test + void CaseNegative_Error() { + setInput("-2"); + + new TrafficLight().run(); + + printOut(); + removeFromOutput("Please enter the amount of seconds as integer within 0 - 86399 range: "); + assertEquals("Only 1 non-negative integer is allowed as passed parameter", getOutput()); + } + + @Test + void CaseOutOfRange_Error() { + setInput("86400"); + + new TrafficLight().run(); + + printOut(); + removeFromOutput("Please enter the amount of seconds as integer within 0 - 86399 range: "); + assertEquals("Only 1 non-negative integer is allowed as passed parameter", getOutput()); + } + + @Test + void CaseLong_Error() { + setInput("2147483648"); + + new TrafficLight().run(); + + printOut(); + removeFromOutput("Please enter the amount of seconds as integer within 0 - 86399 range: "); + assertEquals("Only 1 non-negative integer is allowed as passed parameter", getOutput()); + } + + @Test + void CaseSymbol_Error() { + setInput("test"); + + new TrafficLight().run(); + + printOut(); + removeFromOutput("Please enter the amount of seconds as integer within 0 - 86399 range: "); + assertEquals("Only 1 non-negative integer is allowed as passed parameter", getOutput()); + } + + @Test + void CaseTwoInt_Error() { + setInput("5 65"); + + new TrafficLight().run(); + + printOut(); + removeFromOutput("Please enter the amount of seconds as integer within 0 - 86399 range: "); + assertEquals("Only 1 non-negative integer is allowed as passed parameter", getOutput()); + } + + @Test + void CaseDouble_Error() { + setInput("5.65"); + + new TrafficLight().run(); + + printOut(); + removeFromOutput("Please enter the amount of seconds as integer within 0 - 86399 range: "); + assertEquals("Only 1 non-negative integer is allowed as passed parameter", getOutput()); + } + +}