Skip to content
This repository was archived by the owner on Oct 25, 2021. It is now read-only.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 |
16 changes: 16 additions & 0 deletions src/main/java/Homework_1/ConsolePrinter.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
}
8 changes: 8 additions & 0 deletions src/main/java/Homework_2/PyramidPrinter/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package Homework_2.PyramidPrinter;

public class Main {

public static void main(String[] args) {
new PyramidPrinter().run();
}
}
58 changes: 58 additions & 0 deletions src/main/java/Homework_2/PyramidPrinter/PyramidPrinter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package Homework_2.PyramidPrinter;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix package names


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);
}
}
8 changes: 8 additions & 0 deletions src/main/java/Homework_2/RandomCharsTable/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package Homework_2.RandomCharsTable;

public class Main {

public static void main(String[] args) {
new RandomCharsTable().run();
}
}
77 changes: 77 additions & 0 deletions src/main/java/Homework_2/RandomCharsTable/RandomCharsTable.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

8 changes: 8 additions & 0 deletions src/main/java/Homework_2/TrafficLight/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package Homework_2.TrafficLight;


public class Main {
public static void main(String[] args) {
new TrafficLight().run();
}
}
60 changes: 60 additions & 0 deletions src/main/java/Homework_2/TrafficLight/TrafficLight.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

Original file line number Diff line number Diff line change
@@ -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!");
}

}
Original file line number Diff line number Diff line change
@@ -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());
}
}
29 changes: 29 additions & 0 deletions src/test/java/Homework_2/RandomCharsTest/RandomCharsTest.java
Original file line number Diff line number Diff line change
@@ -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|


}
Loading