From 4248061555c05b565fe381572ab7fc73d047cb0c Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Wed, 7 Jul 2021 19:18:01 +0300 Subject: [PATCH 01/56] init --- src/main/java/homework_1/Main.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/homework_1/Main.java b/src/main/java/homework_1/Main.java index 07c029a2..7fec21fa 100644 --- a/src/main/java/homework_1/Main.java +++ b/src/main/java/homework_1/Main.java @@ -3,6 +3,7 @@ public class Main { public static void main(String[] args) { + //Домашняя работа от 05.07.2021 System.out.println("Hello homework!"); } From e6d248c8a53e43bac8cb45b22efa0f740133be74 Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Thu, 8 Jul 2021 11:20:26 +0300 Subject: [PATCH 02/56] init --- src/main/java/homework_1/ConsoleColors.java | 76 +++++++++++++++++++++ src/main/java/homework_1/Main.java | 18 +++-- 2 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 src/main/java/homework_1/ConsoleColors.java diff --git a/src/main/java/homework_1/ConsoleColors.java b/src/main/java/homework_1/ConsoleColors.java new file mode 100644 index 00000000..b4181e52 --- /dev/null +++ b/src/main/java/homework_1/ConsoleColors.java @@ -0,0 +1,76 @@ +package homework_1; + +public class ConsoleColors { + // Reset + public static final String RESET = "\033[0m"; // Text Reset + + // Regular Colors + public static final String BLACK = "\033[0;30m"; // BLACK + public static final String RED = "\033[0;31m"; // RED + public static final String GREEN = "\033[0;32m"; // GREEN + public static final String YELLOW = "\033[0;33m"; // YELLOW + public static final String BLUE = "\033[0;34m"; // BLUE + public static final String PURPLE = "\033[0;35m"; // PURPLE + public static final String CYAN = "\033[0;36m"; // CYAN + public static final String WHITE = "\033[0;37m"; // WHITE + + // Bold + public static final String BLACK_BOLD = "\033[1;30m"; // BLACK + public static final String RED_BOLD = "\033[1;31m"; // RED + public static final String GREEN_BOLD = "\033[1;32m"; // GREEN + public static final String YELLOW_BOLD = "\033[1;33m"; // YELLOW + public static final String BLUE_BOLD = "\033[1;34m"; // BLUE + public static final String PURPLE_BOLD = "\033[1;35m"; // PURPLE + public static final String CYAN_BOLD = "\033[1;36m"; // CYAN + public static final String WHITE_BOLD = "\033[1;37m"; // WHITE + + // Underline + public static final String BLACK_UNDERLINED = "\033[4;30m"; // BLACK + public static final String RED_UNDERLINED = "\033[4;31m"; // RED + public static final String GREEN_UNDERLINED = "\033[4;32m"; // GREEN + public static final String YELLOW_UNDERLINED = "\033[4;33m"; // YELLOW + public static final String BLUE_UNDERLINED = "\033[4;34m"; // BLUE + public static final String PURPLE_UNDERLINED = "\033[4;35m"; // PURPLE + public static final String CYAN_UNDERLINED = "\033[4;36m"; // CYAN + public static final String WHITE_UNDERLINED = "\033[4;37m"; // WHITE + + // Background + public static final String BLACK_BACKGROUND = "\033[40m"; // BLACK + public static final String RED_BACKGROUND = "\033[41m"; // RED + public static final String GREEN_BACKGROUND = "\033[42m"; // GREEN + public static final String YELLOW_BACKGROUND = "\033[43m"; // YELLOW + public static final String BLUE_BACKGROUND = "\033[44m"; // BLUE + public static final String PURPLE_BACKGROUND = "\033[45m"; // PURPLE + public static final String CYAN_BACKGROUND = "\033[46m"; // CYAN + public static final String WHITE_BACKGROUND = "\033[47m"; // WHITE + + // High Intensity + public static final String BLACK_BRIGHT = "\033[0;90m"; // BLACK + public static final String RED_BRIGHT = "\033[0;91m"; // RED + public static final String GREEN_BRIGHT = "\033[0;92m"; // GREEN + public static final String YELLOW_BRIGHT = "\033[0;93m"; // YELLOW + public static final String BLUE_BRIGHT = "\033[0;94m"; // BLUE + public static final String PURPLE_BRIGHT = "\033[0;95m"; // PURPLE + public static final String CYAN_BRIGHT = "\033[0;96m"; // CYAN + public static final String WHITE_BRIGHT = "\033[0;97m"; // WHITE + + // Bold High Intensity + public static final String BLACK_BOLD_BRIGHT = "\033[1;90m"; // BLACK + public static final String RED_BOLD_BRIGHT = "\033[1;91m"; // RED + public static final String GREEN_BOLD_BRIGHT = "\033[1;92m"; // GREEN + public static final String YELLOW_BOLD_BRIGHT = "\033[1;93m"; // YELLOW + public static final String BLUE_BOLD_BRIGHT = "\033[1;94m"; // BLUE + public static final String PURPLE_BOLD_BRIGHT = "\033[1;95m"; // PURPLE + public static final String CYAN_BOLD_BRIGHT = "\033[1;96m"; // CYAN + public static final String WHITE_BOLD_BRIGHT = "\033[1;97m"; // WHITE + + // High Intensity backgrounds + public static final String BLACK_BACKGROUND_BRIGHT = "\033[0;100m"; // BLACK + public static final String RED_BACKGROUND_BRIGHT = "\033[0;101m"; // RED + public static final String GREEN_BACKGROUND_BRIGHT = "\033[0;102m"; // GREEN + public static final String YELLOW_BACKGROUND_BRIGHT = "\033[0;103m"; // YELLOW + public static final String BLUE_BACKGROUND_BRIGHT = "\033[0;104m"; // BLUE + public static final String PURPLE_BACKGROUND_BRIGHT = "\033[0;105m"; // PURPLE + public static final String CYAN_BACKGROUND_BRIGHT = "\033[0;106m"; // CYAN + public static final String WHITE_BACKGROUND_BRIGHT = "\033[0;107m"; // WHITE +} \ No newline at end of file diff --git a/src/main/java/homework_1/Main.java b/src/main/java/homework_1/Main.java index 7fec21fa..5a2dc2f5 100644 --- a/src/main/java/homework_1/Main.java +++ b/src/main/java/homework_1/Main.java @@ -2,9 +2,19 @@ public class Main { - public static void main(String[] args) { + private static void main(String[] args) { //Домашняя работа от 05.07.2021 - System.out.println("Hello homework!"); - } + if (args == null || args.length < 1) { + return; + } -} + for (String arg : args) { + if (arg.equals("error")) { + System.out.println(ConsoleColors.RED_BOLD + "Alarm!" + ConsoleColors.RESET); + break; + } else { + System.out.println(arg + ": " + arg.length() + " letters"); + } + } + } +} \ No newline at end of file From 4cf47c8a4a717147120ea6b29212e5e0e8754d54 Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Thu, 8 Jul 2021 11:28:38 +0300 Subject: [PATCH 03/56] init --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5d686e9f..98a2cffc 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # Java Core June 2021 -## *Nikolaev Artem* +## *Khlebnikov Evgeniy* | 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 | +| HW1 | [Console printer](https://github.com/KhlebnikovEvgeniy/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) From 0038fea87271c4635cf8419bbc9ce187a3f98c26 Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Thu, 8 Jul 2021 12:51:46 +0300 Subject: [PATCH 04/56] Homework 05.07.2021 --- src/main/java/homework_1/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/homework_1/Main.java b/src/main/java/homework_1/Main.java index 5a2dc2f5..afbde6a3 100644 --- a/src/main/java/homework_1/Main.java +++ b/src/main/java/homework_1/Main.java @@ -2,7 +2,7 @@ public class Main { - private static void main(String[] args) { + public static void main(String[] args) { //Домашняя работа от 05.07.2021 if (args == null || args.length < 1) { return; From 01d96d4e92fd0ace322741ece97982734fc21318 Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Thu, 8 Jul 2021 14:01:57 +0300 Subject: [PATCH 05/56] Homework 05.07.2021 (redone) --- src/main/java/homework_1/ConsoleColors.java | 76 --------------------- src/main/java/homework_1/Main.java | 76 +++++++++++++++++++++ 2 files changed, 76 insertions(+), 76 deletions(-) delete mode 100644 src/main/java/homework_1/ConsoleColors.java diff --git a/src/main/java/homework_1/ConsoleColors.java b/src/main/java/homework_1/ConsoleColors.java deleted file mode 100644 index b4181e52..00000000 --- a/src/main/java/homework_1/ConsoleColors.java +++ /dev/null @@ -1,76 +0,0 @@ -package homework_1; - -public class ConsoleColors { - // Reset - public static final String RESET = "\033[0m"; // Text Reset - - // Regular Colors - public static final String BLACK = "\033[0;30m"; // BLACK - public static final String RED = "\033[0;31m"; // RED - public static final String GREEN = "\033[0;32m"; // GREEN - public static final String YELLOW = "\033[0;33m"; // YELLOW - public static final String BLUE = "\033[0;34m"; // BLUE - public static final String PURPLE = "\033[0;35m"; // PURPLE - public static final String CYAN = "\033[0;36m"; // CYAN - public static final String WHITE = "\033[0;37m"; // WHITE - - // Bold - public static final String BLACK_BOLD = "\033[1;30m"; // BLACK - public static final String RED_BOLD = "\033[1;31m"; // RED - public static final String GREEN_BOLD = "\033[1;32m"; // GREEN - public static final String YELLOW_BOLD = "\033[1;33m"; // YELLOW - public static final String BLUE_BOLD = "\033[1;34m"; // BLUE - public static final String PURPLE_BOLD = "\033[1;35m"; // PURPLE - public static final String CYAN_BOLD = "\033[1;36m"; // CYAN - public static final String WHITE_BOLD = "\033[1;37m"; // WHITE - - // Underline - public static final String BLACK_UNDERLINED = "\033[4;30m"; // BLACK - public static final String RED_UNDERLINED = "\033[4;31m"; // RED - public static final String GREEN_UNDERLINED = "\033[4;32m"; // GREEN - public static final String YELLOW_UNDERLINED = "\033[4;33m"; // YELLOW - public static final String BLUE_UNDERLINED = "\033[4;34m"; // BLUE - public static final String PURPLE_UNDERLINED = "\033[4;35m"; // PURPLE - public static final String CYAN_UNDERLINED = "\033[4;36m"; // CYAN - public static final String WHITE_UNDERLINED = "\033[4;37m"; // WHITE - - // Background - public static final String BLACK_BACKGROUND = "\033[40m"; // BLACK - public static final String RED_BACKGROUND = "\033[41m"; // RED - public static final String GREEN_BACKGROUND = "\033[42m"; // GREEN - public static final String YELLOW_BACKGROUND = "\033[43m"; // YELLOW - public static final String BLUE_BACKGROUND = "\033[44m"; // BLUE - public static final String PURPLE_BACKGROUND = "\033[45m"; // PURPLE - public static final String CYAN_BACKGROUND = "\033[46m"; // CYAN - public static final String WHITE_BACKGROUND = "\033[47m"; // WHITE - - // High Intensity - public static final String BLACK_BRIGHT = "\033[0;90m"; // BLACK - public static final String RED_BRIGHT = "\033[0;91m"; // RED - public static final String GREEN_BRIGHT = "\033[0;92m"; // GREEN - public static final String YELLOW_BRIGHT = "\033[0;93m"; // YELLOW - public static final String BLUE_BRIGHT = "\033[0;94m"; // BLUE - public static final String PURPLE_BRIGHT = "\033[0;95m"; // PURPLE - public static final String CYAN_BRIGHT = "\033[0;96m"; // CYAN - public static final String WHITE_BRIGHT = "\033[0;97m"; // WHITE - - // Bold High Intensity - public static final String BLACK_BOLD_BRIGHT = "\033[1;90m"; // BLACK - public static final String RED_BOLD_BRIGHT = "\033[1;91m"; // RED - public static final String GREEN_BOLD_BRIGHT = "\033[1;92m"; // GREEN - public static final String YELLOW_BOLD_BRIGHT = "\033[1;93m"; // YELLOW - public static final String BLUE_BOLD_BRIGHT = "\033[1;94m"; // BLUE - public static final String PURPLE_BOLD_BRIGHT = "\033[1;95m"; // PURPLE - public static final String CYAN_BOLD_BRIGHT = "\033[1;96m"; // CYAN - public static final String WHITE_BOLD_BRIGHT = "\033[1;97m"; // WHITE - - // High Intensity backgrounds - public static final String BLACK_BACKGROUND_BRIGHT = "\033[0;100m"; // BLACK - public static final String RED_BACKGROUND_BRIGHT = "\033[0;101m"; // RED - public static final String GREEN_BACKGROUND_BRIGHT = "\033[0;102m"; // GREEN - public static final String YELLOW_BACKGROUND_BRIGHT = "\033[0;103m"; // YELLOW - public static final String BLUE_BACKGROUND_BRIGHT = "\033[0;104m"; // BLUE - public static final String PURPLE_BACKGROUND_BRIGHT = "\033[0;105m"; // PURPLE - public static final String CYAN_BACKGROUND_BRIGHT = "\033[0;106m"; // CYAN - public static final String WHITE_BACKGROUND_BRIGHT = "\033[0;107m"; // WHITE -} \ No newline at end of file diff --git a/src/main/java/homework_1/Main.java b/src/main/java/homework_1/Main.java index afbde6a3..655fd040 100644 --- a/src/main/java/homework_1/Main.java +++ b/src/main/java/homework_1/Main.java @@ -17,4 +17,80 @@ public static void main(String[] args) { } } } + + public class ConsoleColors { + // Reset + public static final String RESET = "\033[0m"; // Text Reset + + // Regular Colors + public static final String BLACK = "\033[0;30m"; // BLACK + public static final String RED = "\033[0;31m"; // RED + public static final String GREEN = "\033[0;32m"; // GREEN + public static final String YELLOW = "\033[0;33m"; // YELLOW + public static final String BLUE = "\033[0;34m"; // BLUE + public static final String PURPLE = "\033[0;35m"; // PURPLE + public static final String CYAN = "\033[0;36m"; // CYAN + public static final String WHITE = "\033[0;37m"; // WHITE + + // Bold + public static final String BLACK_BOLD = "\033[1;30m"; // BLACK + public static final String RED_BOLD = "\033[1;31m"; // RED + public static final String GREEN_BOLD = "\033[1;32m"; // GREEN + public static final String YELLOW_BOLD = "\033[1;33m"; // YELLOW + public static final String BLUE_BOLD = "\033[1;34m"; // BLUE + public static final String PURPLE_BOLD = "\033[1;35m"; // PURPLE + public static final String CYAN_BOLD = "\033[1;36m"; // CYAN + public static final String WHITE_BOLD = "\033[1;37m"; // WHITE + + // Underline + public static final String BLACK_UNDERLINED = "\033[4;30m"; // BLACK + public static final String RED_UNDERLINED = "\033[4;31m"; // RED + public static final String GREEN_UNDERLINED = "\033[4;32m"; // GREEN + public static final String YELLOW_UNDERLINED = "\033[4;33m"; // YELLOW + public static final String BLUE_UNDERLINED = "\033[4;34m"; // BLUE + public static final String PURPLE_UNDERLINED = "\033[4;35m"; // PURPLE + public static final String CYAN_UNDERLINED = "\033[4;36m"; // CYAN + public static final String WHITE_UNDERLINED = "\033[4;37m"; // WHITE + + // Background + public static final String BLACK_BACKGROUND = "\033[40m"; // BLACK + public static final String RED_BACKGROUND = "\033[41m"; // RED + public static final String GREEN_BACKGROUND = "\033[42m"; // GREEN + public static final String YELLOW_BACKGROUND = "\033[43m"; // YELLOW + public static final String BLUE_BACKGROUND = "\033[44m"; // BLUE + public static final String PURPLE_BACKGROUND = "\033[45m"; // PURPLE + public static final String CYAN_BACKGROUND = "\033[46m"; // CYAN + public static final String WHITE_BACKGROUND = "\033[47m"; // WHITE + + // High Intensity + public static final String BLACK_BRIGHT = "\033[0;90m"; // BLACK + public static final String RED_BRIGHT = "\033[0;91m"; // RED + public static final String GREEN_BRIGHT = "\033[0;92m"; // GREEN + public static final String YELLOW_BRIGHT = "\033[0;93m"; // YELLOW + public static final String BLUE_BRIGHT = "\033[0;94m"; // BLUE + public static final String PURPLE_BRIGHT = "\033[0;95m"; // PURPLE + public static final String CYAN_BRIGHT = "\033[0;96m"; // CYAN + public static final String WHITE_BRIGHT = "\033[0;97m"; // WHITE + + // Bold High Intensity + public static final String BLACK_BOLD_BRIGHT = "\033[1;90m"; // BLACK + public static final String RED_BOLD_BRIGHT = "\033[1;91m"; // RED + public static final String GREEN_BOLD_BRIGHT = "\033[1;92m"; // GREEN + public static final String YELLOW_BOLD_BRIGHT = "\033[1;93m"; // YELLOW + public static final String BLUE_BOLD_BRIGHT = "\033[1;94m"; // BLUE + public static final String PURPLE_BOLD_BRIGHT = "\033[1;95m"; // PURPLE + public static final String CYAN_BOLD_BRIGHT = "\033[1;96m"; // CYAN + public static final String WHITE_BOLD_BRIGHT = "\033[1;97m"; // WHITE + + // High Intensity backgrounds + public static final String BLACK_BACKGROUND_BRIGHT = "\033[0;100m"; // BLACK + public static final String RED_BACKGROUND_BRIGHT = "\033[0;101m"; // RED + public static final String GREEN_BACKGROUND_BRIGHT = "\033[0;102m"; // GREEN + public static final String YELLOW_BACKGROUND_BRIGHT = "\033[0;103m"; // YELLOW + public static final String BLUE_BACKGROUND_BRIGHT = "\033[0;104m"; // BLUE + public static final String PURPLE_BACKGROUND_BRIGHT = "\033[0;105m"; // PURPLE + public static final String CYAN_BACKGROUND_BRIGHT = "\033[0;106m"; // CYAN + public static final String WHITE_BACKGROUND_BRIGHT = "\033[0;107m"; // WHITE + } + } \ No newline at end of file From b35647f2cd26ec986cc65c52c13c7bc3eff2266b Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Thu, 8 Jul 2021 14:42:54 +0300 Subject: [PATCH 06/56] README (HW1 link is redone) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 98a2cffc..f0df943b 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,6 @@ | Number | Solution | Short description | --- | --- | --- | -| HW1 | [Console printer](https://github.com/KhlebnikovEvgeniy/Java_Core_June_2021/tree/master/src/main/java/homework_1) | The app that reads input arguments and prints them, until "error" argument | +| HW1 | [Console printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/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) From c58f7c816278860fe5eab158bec991bd3bec5f3e Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Fri, 9 Jul 2021 15:08:13 +0300 Subject: [PATCH 07/56] HW1 after review 09.07.2021 --- src/main/java/homework_1/Main.java | 86 ++---------------------------- 1 file changed, 4 insertions(+), 82 deletions(-) diff --git a/src/main/java/homework_1/Main.java b/src/main/java/homework_1/Main.java index 655fd040..d9b09c46 100644 --- a/src/main/java/homework_1/Main.java +++ b/src/main/java/homework_1/Main.java @@ -1,96 +1,18 @@ package homework_1; public class Main { + public static final String RESET = "\033[0m"; // Text Reset + public static final String RED_BOLD = "\033[1;31m"; // RED public static void main(String[] args) { - //Домашняя работа от 05.07.2021 - if (args == null || args.length < 1) { - return; - } - + //HW1 from 05.07.2021 (after review 09.07.2021) for (String arg : args) { if (arg.equals("error")) { - System.out.println(ConsoleColors.RED_BOLD + "Alarm!" + ConsoleColors.RESET); + System.out.println(Main.RED_BOLD + "Alarm!" + Main.RESET); break; } else { System.out.println(arg + ": " + arg.length() + " letters"); } } } - - public class ConsoleColors { - // Reset - public static final String RESET = "\033[0m"; // Text Reset - - // Regular Colors - public static final String BLACK = "\033[0;30m"; // BLACK - public static final String RED = "\033[0;31m"; // RED - public static final String GREEN = "\033[0;32m"; // GREEN - public static final String YELLOW = "\033[0;33m"; // YELLOW - public static final String BLUE = "\033[0;34m"; // BLUE - public static final String PURPLE = "\033[0;35m"; // PURPLE - public static final String CYAN = "\033[0;36m"; // CYAN - public static final String WHITE = "\033[0;37m"; // WHITE - - // Bold - public static final String BLACK_BOLD = "\033[1;30m"; // BLACK - public static final String RED_BOLD = "\033[1;31m"; // RED - public static final String GREEN_BOLD = "\033[1;32m"; // GREEN - public static final String YELLOW_BOLD = "\033[1;33m"; // YELLOW - public static final String BLUE_BOLD = "\033[1;34m"; // BLUE - public static final String PURPLE_BOLD = "\033[1;35m"; // PURPLE - public static final String CYAN_BOLD = "\033[1;36m"; // CYAN - public static final String WHITE_BOLD = "\033[1;37m"; // WHITE - - // Underline - public static final String BLACK_UNDERLINED = "\033[4;30m"; // BLACK - public static final String RED_UNDERLINED = "\033[4;31m"; // RED - public static final String GREEN_UNDERLINED = "\033[4;32m"; // GREEN - public static final String YELLOW_UNDERLINED = "\033[4;33m"; // YELLOW - public static final String BLUE_UNDERLINED = "\033[4;34m"; // BLUE - public static final String PURPLE_UNDERLINED = "\033[4;35m"; // PURPLE - public static final String CYAN_UNDERLINED = "\033[4;36m"; // CYAN - public static final String WHITE_UNDERLINED = "\033[4;37m"; // WHITE - - // Background - public static final String BLACK_BACKGROUND = "\033[40m"; // BLACK - public static final String RED_BACKGROUND = "\033[41m"; // RED - public static final String GREEN_BACKGROUND = "\033[42m"; // GREEN - public static final String YELLOW_BACKGROUND = "\033[43m"; // YELLOW - public static final String BLUE_BACKGROUND = "\033[44m"; // BLUE - public static final String PURPLE_BACKGROUND = "\033[45m"; // PURPLE - public static final String CYAN_BACKGROUND = "\033[46m"; // CYAN - public static final String WHITE_BACKGROUND = "\033[47m"; // WHITE - - // High Intensity - public static final String BLACK_BRIGHT = "\033[0;90m"; // BLACK - public static final String RED_BRIGHT = "\033[0;91m"; // RED - public static final String GREEN_BRIGHT = "\033[0;92m"; // GREEN - public static final String YELLOW_BRIGHT = "\033[0;93m"; // YELLOW - public static final String BLUE_BRIGHT = "\033[0;94m"; // BLUE - public static final String PURPLE_BRIGHT = "\033[0;95m"; // PURPLE - public static final String CYAN_BRIGHT = "\033[0;96m"; // CYAN - public static final String WHITE_BRIGHT = "\033[0;97m"; // WHITE - - // Bold High Intensity - public static final String BLACK_BOLD_BRIGHT = "\033[1;90m"; // BLACK - public static final String RED_BOLD_BRIGHT = "\033[1;91m"; // RED - public static final String GREEN_BOLD_BRIGHT = "\033[1;92m"; // GREEN - public static final String YELLOW_BOLD_BRIGHT = "\033[1;93m"; // YELLOW - public static final String BLUE_BOLD_BRIGHT = "\033[1;94m"; // BLUE - public static final String PURPLE_BOLD_BRIGHT = "\033[1;95m"; // PURPLE - public static final String CYAN_BOLD_BRIGHT = "\033[1;96m"; // CYAN - public static final String WHITE_BOLD_BRIGHT = "\033[1;97m"; // WHITE - - // High Intensity backgrounds - public static final String BLACK_BACKGROUND_BRIGHT = "\033[0;100m"; // BLACK - public static final String RED_BACKGROUND_BRIGHT = "\033[0;101m"; // RED - public static final String GREEN_BACKGROUND_BRIGHT = "\033[0;102m"; // GREEN - public static final String YELLOW_BACKGROUND_BRIGHT = "\033[0;103m"; // YELLOW - public static final String BLUE_BACKGROUND_BRIGHT = "\033[0;104m"; // BLUE - public static final String PURPLE_BACKGROUND_BRIGHT = "\033[0;105m"; // PURPLE - public static final String CYAN_BACKGROUND_BRIGHT = "\033[0;106m"; // CYAN - public static final String WHITE_BACKGROUND_BRIGHT = "\033[0;107m"; // WHITE - } - } \ No newline at end of file From 3ee9ea039640044c0a48e91de9299d82f920daa3 Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Thu, 22 Jul 2021 14:49:57 +0300 Subject: [PATCH 08/56] READMY edited 22.07.2021 --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index f0df943b..f6375685 100644 --- a/README.md +++ b/README.md @@ -5,5 +5,14 @@ | Number | Solution | Short description | --- | --- | --- | | HW1 | [Console printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_1) | The app that reads input arguments and prints them, until "error" argument | +| HW2 - 1 | [Traffic Light](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_2.traffic_light) | The app that reads the time from the console (seconds/hh:mm:ss) and displays the traffic light color (GREEN in green, YELLOW in yellow, RED in red) on the command line | +| HW2 - 2 | [Pyramid Printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_2.pyramid_printer) | The app that reads the number once from the command line, and prints the pyramid of "x" according to the positive integer entered. Quits after first printing | +| HW2 - 3 | [Random Chars Table](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_2.random_chars_table) | The app that reads length, width, strategy from the console generates the table and prints even/odd letters | +| HW3 | [ImmutableClass](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_3) | The Immutable Class An immutable class is created. Requirements for an immutable class have been added to a separate * .tht file in the homework_3 package.| + + +| Title | Link | Short description +| --- | --- | --- | +| CodingBat | [CodingBat Code Practice](https://codingbat.com/done?user=khebnikove@gmail.com&tag=2964520102) | CodingBat khebnikove@gmail.com Done Page | [Link to markdown giude](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet) From 6ebd569c6ff3ae40983514a91f3ca26e8dd6b82b Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Fri, 23 Jul 2021 14:19:46 +0300 Subject: [PATCH 09/56] READMY edited 22.07.2021 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f6375685..f368e944 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ | Number | Solution | Short description | --- | --- | --- | | HW1 | [Console printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_1) | The app that reads input arguments and prints them, until "error" argument | -| HW2 - 1 | [Traffic Light](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_2.traffic_light) | The app that reads the time from the console (seconds/hh:mm:ss) and displays the traffic light color (GREEN in green, YELLOW in yellow, RED in red) on the command line | +| HW2 - 1 | [Traffic Light](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_2.traffic_light) | The app that reads the time from the console (seconds) and displays the traffic light color (GREEN in green, YELLOW in yellow, RED in red) on the command line | | HW2 - 2 | [Pyramid Printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_2.pyramid_printer) | The app that reads the number once from the command line, and prints the pyramid of "x" according to the positive integer entered. Quits after first printing | | HW2 - 3 | [Random Chars Table](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_2.random_chars_table) | The app that reads length, width, strategy from the console generates the table and prints even/odd letters | | HW3 | [ImmutableClass](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_3) | The Immutable Class An immutable class is created. Requirements for an immutable class have been added to a separate * .tht file in the homework_3 package.| From 5b83f60eab20119f9c9404fb53e671995f652a2a Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Fri, 23 Jul 2021 14:36:18 +0300 Subject: [PATCH 10/56] HW2 edited 23.07.2021 --- .../java/homework_2/traffic_light/Main.java | 8 ++ .../traffic_light/TrafficLight.java | 82 +++++++++++++++++++ .../homework_3/ImmutableClassRequirements | 11 +++ 3 files changed, 101 insertions(+) create mode 100644 src/main/java/homework_2/traffic_light/Main.java create mode 100644 src/main/java/homework_2/traffic_light/TrafficLight.java create mode 100644 src/main/java/homework_3/ImmutableClassRequirements diff --git a/src/main/java/homework_2/traffic_light/Main.java b/src/main/java/homework_2/traffic_light/Main.java new file mode 100644 index 00000000..3ff7f428 --- /dev/null +++ b/src/main/java/homework_2/traffic_light/Main.java @@ -0,0 +1,8 @@ +package homework_2.traffic_light; + +public class Main { + + public static void main(String[] args) { + new TrafficLight().run(); + } +} diff --git a/src/main/java/homework_2/traffic_light/TrafficLight.java b/src/main/java/homework_2/traffic_light/TrafficLight.java new file mode 100644 index 00000000..ccbf173c --- /dev/null +++ b/src/main/java/homework_2/traffic_light/TrafficLight.java @@ -0,0 +1,82 @@ +package homework_2.traffic_light; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Scanner; +import java.util.regex.Pattern; + +public class TrafficLight implements Runnable { + + public static final String ERROR_MESSAGE; + public static final String RED_LIGHT_MESSAGE; + public static final String YELLOW_LIGHT_MESSAGE; + public static final String GREEN_LIGHT_MESSAGE; + public static final String DAY_IS_OVER_MESSAGE; + public static final String REGEX; + + static { + ERROR_MESSAGE = "Only 1 non-negative integer is allowed as passed parameter"; + DAY_IS_OVER_MESSAGE = "The day is over"; + RED_LIGHT_MESSAGE = "\u001b[31m RED \u001b[0m"; + YELLOW_LIGHT_MESSAGE = "\u001b[33m YELLOW \u001b[0m"; + GREEN_LIGHT_MESSAGE = "\u001b[32m GREEN \u001b[0m"; + REGEX = "\\d+"; + } + + Pattern pattern = Pattern.compile(REGEX); + + @Override + public void run() { + startGreeting(); + Scanner scanner = new Scanner(System.in); + String consoleString = scanner.nextLine(); + scanner.close(); + if (numberFormatIsValid(consoleString)) { + consoleString = consoleString.replaceAll("\\D+", ""); + System.out.println(getTrafficLightColor(Integer.parseInt(consoleString))); + } + } + + protected void startGreeting() { + System.out.println("Welcome to the Traffic Light Application"); + System.out.println("Enter the number of seconds since the start of the day"); + } + + protected String readLine() throws IOException { + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); + String consoleString = bufferedReader.readLine(); + return consoleString; + } + + protected boolean numberFormatIsValid(String consoleString) { + if (consoleString == null || consoleString.length() == 0) { + return false; + } + if (!pattern.matcher(consoleString).matches()) { + System.out.println(ERROR_MESSAGE); + return false; + } + if (Integer.parseInt(consoleString) > 86399) { + System.out.println(DAY_IS_OVER_MESSAGE); + return false; + } + return true; + } + + protected String getTrafficLightColor(int seconds) { + int secInMin = Math.floorMod(seconds, 60); + System.out.println("seconds= " + seconds); + System.out.println("secInMin= " + secInMin); + if (secInMin >= 0 && secInMin < 35) { + return GREEN_LIGHT_MESSAGE; + } + if ((secInMin >= 35 && secInMin < 40) || (secInMin >= 55 && secInMin < 60)) { + return YELLOW_LIGHT_MESSAGE; + } + if (secInMin >= 40 && secInMin < 55) { + return RED_LIGHT_MESSAGE; + } + return ERROR_MESSAGE; + } +} diff --git a/src/main/java/homework_3/ImmutableClassRequirements b/src/main/java/homework_3/ImmutableClassRequirements new file mode 100644 index 00000000..6ff4fe91 --- /dev/null +++ b/src/main/java/homework_3/ImmutableClassRequirements @@ -0,0 +1,11 @@ +Following are the requirements: + +1. The class must be declared as final (So that child classes can’t be created) +2. Data members in the class must be declared as private (So that direct access is not allowed) +3. Data members in the class must be declared as final + (So that we can’t change the value of it after object creation) +4. A parameterized constructor should initialize all the fields performing a deep copy + (So that data members can’t be modified with object reference) +5. Deep Copy of objects should be performed in the getter methods + (To return a copy rather than returning the actual object reference) +6. No setters (To not have the option to change the value of the instance variable) \ No newline at end of file From 3ddfb53ad99cc4b7ae021be64b67c3812339829a Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Fri, 23 Jul 2021 14:44:23 +0300 Subject: [PATCH 11/56] HW2 edited 23.07.2021 --- src/main/java/homework_2/pyramid_printer/Main.java | 8 ++++++++ .../java/homework_2/pyramid_printer/PyramidPrinter.java | 9 +++++++++ src/main/java/homework_2/random_chars_table/Main.java | 8 ++++++++ .../homework_2/random_chars_table/RandomCharsTable.java | 9 +++++++++ 4 files changed, 34 insertions(+) create mode 100644 src/main/java/homework_2/pyramid_printer/Main.java create mode 100644 src/main/java/homework_2/pyramid_printer/PyramidPrinter.java create mode 100644 src/main/java/homework_2/random_chars_table/Main.java create mode 100644 src/main/java/homework_2/random_chars_table/RandomCharsTable.java diff --git a/src/main/java/homework_2/pyramid_printer/Main.java b/src/main/java/homework_2/pyramid_printer/Main.java new file mode 100644 index 00000000..71e33035 --- /dev/null +++ b/src/main/java/homework_2/pyramid_printer/Main.java @@ -0,0 +1,8 @@ +package homework_2.pyramid_printer; + +public class Main { + + public static void main(String[] args) { + new PyramidPrinter().run(); + } +} diff --git a/src/main/java/homework_2/pyramid_printer/PyramidPrinter.java b/src/main/java/homework_2/pyramid_printer/PyramidPrinter.java new file mode 100644 index 00000000..11e2a4bd --- /dev/null +++ b/src/main/java/homework_2/pyramid_printer/PyramidPrinter.java @@ -0,0 +1,9 @@ +package homework_2.pyramid_printer; + +public class PyramidPrinter implements Runnable { + + @Override + public void run() { + + } +} diff --git a/src/main/java/homework_2/random_chars_table/Main.java b/src/main/java/homework_2/random_chars_table/Main.java new file mode 100644 index 00000000..be586c4b --- /dev/null +++ b/src/main/java/homework_2/random_chars_table/Main.java @@ -0,0 +1,8 @@ +package homework_2.random_chars_table; + +public class Main { + + public static void main(String[] args) { + new RandomCharsTable().run(); + } +} diff --git a/src/main/java/homework_2/random_chars_table/RandomCharsTable.java b/src/main/java/homework_2/random_chars_table/RandomCharsTable.java new file mode 100644 index 00000000..18810bed --- /dev/null +++ b/src/main/java/homework_2/random_chars_table/RandomCharsTable.java @@ -0,0 +1,9 @@ +package homework_2.random_chars_table; + +public class RandomCharsTable implements Runnable { + + @Override + public void run() { + + } +} From 1ecca8bc3220366397950c3b1ac83c7d883c635b Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Fri, 23 Jul 2021 15:41:27 +0300 Subject: [PATCH 12/56] HW2 edited 23.07.2021 --- .../pyramid_printer/PyramidPrinter.java | 45 +++++++++++++++++++ .../traffic_light/TrafficLight.java | 6 --- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/main/java/homework_2/pyramid_printer/PyramidPrinter.java b/src/main/java/homework_2/pyramid_printer/PyramidPrinter.java index 11e2a4bd..31869af8 100644 --- a/src/main/java/homework_2/pyramid_printer/PyramidPrinter.java +++ b/src/main/java/homework_2/pyramid_printer/PyramidPrinter.java @@ -1,9 +1,54 @@ package homework_2.pyramid_printer; +import java.util.Scanner; +import java.util.regex.Pattern; + public class PyramidPrinter implements Runnable { + public static final String ERROR_MESSAGE; + public static final String WELCOME_MESSAGE; + public static final String ENTER_HEIGHT_MESSAGE; + public static final String REGEX; + + static { + ERROR_MESSAGE = "Only 1 non-negative integer is allowed as passed parameter"; + WELCOME_MESSAGE = "Welcome to the Pyramid Printer Application"; + ENTER_HEIGHT_MESSAGE = "Enter the height parameter"; + REGEX = "\\d+"; + } + + Pattern pattern = Pattern.compile(REGEX); + @Override public void run() { + startGreeting(); + Scanner scanner = new Scanner(System.in); + String consoleString = scanner.nextLine(); + scanner.close(); + if (numberFormatIsValid(consoleString)) { + printPyramid(Integer.parseInt(consoleString)); + } + } + + protected void startGreeting() { + System.out.println(WELCOME_MESSAGE); + System.out.println(ENTER_HEIGHT_MESSAGE); + } + + protected boolean numberFormatIsValid(String consoleString) { + if (!pattern.matcher(consoleString).matches()) { + System.out.println(ERROR_MESSAGE); + return false; + } + return true; + } + protected void printPyramid(int height) { + for (int i = 1; i <= height; i++) { + for (int j = 0; j < i; j++) { + System.out.print("x"); + } + System.out.println(); + } } } diff --git a/src/main/java/homework_2/traffic_light/TrafficLight.java b/src/main/java/homework_2/traffic_light/TrafficLight.java index ccbf173c..445f1e7d 100644 --- a/src/main/java/homework_2/traffic_light/TrafficLight.java +++ b/src/main/java/homework_2/traffic_light/TrafficLight.java @@ -43,12 +43,6 @@ protected void startGreeting() { System.out.println("Enter the number of seconds since the start of the day"); } - protected String readLine() throws IOException { - BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); - String consoleString = bufferedReader.readLine(); - return consoleString; - } - protected boolean numberFormatIsValid(String consoleString) { if (consoleString == null || consoleString.length() == 0) { return false; From 8491b8038599798d3d20d398efc643425a5348f2 Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Fri, 23 Jul 2021 18:11:57 +0300 Subject: [PATCH 13/56] HW3 from 23.07.2021 --- .../homework_3/ImmutableClassEmployee.java | 47 +++++++++++++++++++ .../homework_3/ImmutableClassRequirements | 12 ++++- 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/main/java/homework_3/ImmutableClassEmployee.java diff --git a/src/main/java/homework_3/ImmutableClassEmployee.java b/src/main/java/homework_3/ImmutableClassEmployee.java new file mode 100644 index 00000000..93b178bd --- /dev/null +++ b/src/main/java/homework_3/ImmutableClassEmployee.java @@ -0,0 +1,47 @@ +package homework_3; + +import java.util.Date; + +//1. Declare your class as final, +// so other classes will not be able to extend it and break its immutability +final class ImmutableClassEmployee { + + //2. Mark all the fields private. So, they they can't be accessed outside the class + //3. Mark all fields final as well. So, no one can modify them anywhere else apart from the constructor. + private final int id; + private final String name; + private final Date dateOfBirth; + + //4. Provide an argument constructor with all initialization logic + public ImmutableClassEmployee(int id, String name, Date dateOfBirth) { + this.id = id; + this.name = name; + //5. Initialize all your fields by deeply copying them if they are not immutable by nature + this.dateOfBirth = new Date(dateOfBirth.getTime()); + } + + //6. Do not provide setters of your fields + public int getId() { + return id; + } + + public String getName() { + return name; + } + + //7. Instead of returning objects from the getters + // return a deep copy of them if this objects are not immutable by nature + public Date getDateOfBirth() { + return new Date(dateOfBirth.getTime()); + } + + @Override + public String toString() { + return "ImmutableClassEmployee{" + + "id = " + id + + ", name = '" + name + '\'' + + ", dateOfBirth = " + dateOfBirth + + ", dateOfBirth = " + dateOfBirth + + "}"; + } +} diff --git a/src/main/java/homework_3/ImmutableClassRequirements b/src/main/java/homework_3/ImmutableClassRequirements index 6ff4fe91..473ea792 100644 --- a/src/main/java/homework_3/ImmutableClassRequirements +++ b/src/main/java/homework_3/ImmutableClassRequirements @@ -1,4 +1,14 @@ -Following are the requirements: +Following are the requirements for immutable class: + +1. Mark your class final, +2. Mark all the fields private, +3. Mark all fields final as well, +4. Provide an argument constructor with all initialization logic, +5. Initialize all mutable fields by deep copying, +6. Do not provide setters for your fields, +7. Return a deep copy of mutable fields from the getters. + +OR 1. The class must be declared as final (So that child classes can’t be created) 2. Data members in the class must be declared as private (So that direct access is not allowed) From 7aef7eefe1c2ee0493a7ff96627cf1767de2bfd8 Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Fri, 23 Jul 2021 18:13:54 +0300 Subject: [PATCH 14/56] HW3 from 23.07.2021 --- src/main/java/homework_3/ImmutableClassRequirements | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/homework_3/ImmutableClassRequirements b/src/main/java/homework_3/ImmutableClassRequirements index 473ea792..f9c917f9 100644 --- a/src/main/java/homework_3/ImmutableClassRequirements +++ b/src/main/java/homework_3/ImmutableClassRequirements @@ -8,7 +8,7 @@ Following are the requirements for immutable class: 6. Do not provide setters for your fields, 7. Return a deep copy of mutable fields from the getters. -OR +OR IN OTHER WORDS 1. The class must be declared as final (So that child classes can’t be created) 2. Data members in the class must be declared as private (So that direct access is not allowed) From fb4e781c46fcc03cd2de97e015ec1a5f4313e5a5 Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Fri, 23 Jul 2021 18:28:27 +0300 Subject: [PATCH 15/56] HW3 from 23.07.2021 --- src/main/java/homework_3/ImmutableClassEmployee.java | 3 +-- src/main/java/homework_3/ImmutableClassRequirements | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/homework_3/ImmutableClassEmployee.java b/src/main/java/homework_3/ImmutableClassEmployee.java index 93b178bd..0d059807 100644 --- a/src/main/java/homework_3/ImmutableClassEmployee.java +++ b/src/main/java/homework_3/ImmutableClassEmployee.java @@ -41,7 +41,6 @@ public String toString() { "id = " + id + ", name = '" + name + '\'' + ", dateOfBirth = " + dateOfBirth + - ", dateOfBirth = " + dateOfBirth + - "}"; + ", dateOfBirth = " + dateOfBirth + "}"; } } diff --git a/src/main/java/homework_3/ImmutableClassRequirements b/src/main/java/homework_3/ImmutableClassRequirements index f9c917f9..c648a2bf 100644 --- a/src/main/java/homework_3/ImmutableClassRequirements +++ b/src/main/java/homework_3/ImmutableClassRequirements @@ -18,4 +18,4 @@ OR IN OTHER WORDS (So that data members can’t be modified with object reference) 5. Deep Copy of objects should be performed in the getter methods (To return a copy rather than returning the actual object reference) -6. No setters (To not have the option to change the value of the instance variable) \ No newline at end of file +6. No setters (To not have the option to change the value of the instance variable) From f49ed7b091b148b08797e7eb82a15589e6d50034 Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Sat, 24 Jul 2021 14:12:51 +0300 Subject: [PATCH 16/56] HW3 from 23.07.2021 --- .../random_chars_table/RandomCharsTable.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/main/java/homework_2/random_chars_table/RandomCharsTable.java b/src/main/java/homework_2/random_chars_table/RandomCharsTable.java index 18810bed..87401aea 100644 --- a/src/main/java/homework_2/random_chars_table/RandomCharsTable.java +++ b/src/main/java/homework_2/random_chars_table/RandomCharsTable.java @@ -1,9 +1,50 @@ package homework_2.random_chars_table; +import java.util.regex.Pattern; + public class RandomCharsTable implements Runnable { + + public static final String WELCOME_MESSAGE; + public static final String ENTER_PARAM_MESSAGE; + public static final String ERROR_MESSAGE; + public static final String REGEX; + + static { + WELCOME_MESSAGE = "Welcome to the Random Chars Table Application"; + ENTER_PARAM_MESSAGE = "Enter in one line 2 numbers and a line - " + + "the length and width of the table / two-dimensional array," + + " and the strategy (even or odd)"; + ERROR_MESSAGE = "Passed parameters should match the format " + + "[positive integer] [positive integer] [even|odd]"; + REGEX = "^((\\d+){2})(odd|even)"; + } + + Pattern pattern = Pattern.compile(REGEX); + @Override public void run() { + startGreeting(); + printOddCapitalLetters(); + printEvenCapitalLetters(); + } + + protected void startGreeting() { + System.out.println(WELCOME_MESSAGE); + System.out.println(ENTER_PARAM_MESSAGE); + } + + protected void printOddCapitalLetters() { + int[] aSCII = new int[128]; + for(int i = 65; i <= 89; i += 2) { + System.out.print((char)i); + } + } + protected void printEvenCapitalLetters() { + int[] aSCII = new int[128]; + for(int i = 66; i <= 90; i += 2) { + System.out.print((char)i); + } } } From 1ec7a312649eed1d329d98436d3afc8ae6c3b2c8 Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Sat, 24 Jul 2021 19:41:34 +0300 Subject: [PATCH 17/56] HW3 from 23.07.2021 --- .../random_chars_table/RandomCharsTable.java | 83 ++++++++++++++++--- .../RandomCharsTableCreator.java | 26 ++++++ 2 files changed, 96 insertions(+), 13 deletions(-) create mode 100644 src/main/java/homework_2/random_chars_table/RandomCharsTableCreator.java diff --git a/src/main/java/homework_2/random_chars_table/RandomCharsTable.java b/src/main/java/homework_2/random_chars_table/RandomCharsTable.java index 87401aea..71b8cac4 100644 --- a/src/main/java/homework_2/random_chars_table/RandomCharsTable.java +++ b/src/main/java/homework_2/random_chars_table/RandomCharsTable.java @@ -1,10 +1,12 @@ package homework_2.random_chars_table; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; import java.util.regex.Pattern; public class RandomCharsTable implements Runnable { - public static final String WELCOME_MESSAGE; public static final String ENTER_PARAM_MESSAGE; public static final String ERROR_MESSAGE; @@ -17,7 +19,7 @@ public class RandomCharsTable implements Runnable { " and the strategy (even or odd)"; ERROR_MESSAGE = "Passed parameters should match the format " + "[positive integer] [positive integer] [even|odd]"; - REGEX = "^((\\d+){2})(odd|even)"; + REGEX = "^([\\d]+[\\s]+[\\d])(odd|even)"; } Pattern pattern = Pattern.compile(REGEX); @@ -25,26 +27,81 @@ public class RandomCharsTable implements Runnable { @Override public void run() { startGreeting(); - printOddCapitalLetters(); - printEvenCapitalLetters(); + String consoleString = readConsoleString(); + solveTask(consoleString); } - protected void startGreeting() { + private void startGreeting() { System.out.println(WELCOME_MESSAGE); System.out.println(ENTER_PARAM_MESSAGE); } - protected void printOddCapitalLetters() { - int[] aSCII = new int[128]; - for(int i = 65; i <= 89; i += 2) { - System.out.print((char)i); + private String readConsoleString() { + Scanner scanner = new Scanner(System.in); + String consoleString = scanner.nextLine(); + scanner.close(); + return consoleString; + } + + private void printOddCapitalLetters(char[][] table) { + List oddChars = new ArrayList<>(); + for (char[] line : table) { + for (char column : line) { + if (column % 2 != 0) { + oddChars.add(column); + } + } + System.out.println("Even letters - " + + oddChars.toString().replaceAll("[\\[\\]]", "")); + } + } + + private void printEvenCapitalLetters(char[][] charsTable) { + List evenChars = new ArrayList<>(); + for (char[] line : charsTable) { + for (char column : line) { + if (column % 2 == 0) { + evenChars.add(column); + } + } + System.out.println("Even letters - " + + evenChars.toString().replaceAll("[\\[\\]]", "")); + } + } + + + private void printCapitalLetters(char[][] charsTable, String strategy) { + if (strategy.equalsIgnoreCase("odd")) { + printOddCapitalLetters(charsTable); + } else if (strategy.equalsIgnoreCase("even")) { + printEvenCapitalLetters(charsTable); + } else { + System.out.println(ERROR_MESSAGE); + throw new IllegalArgumentException(); + } + } + private void printTable(char[][] charsTable) { + for (char[] line : charsTable) { + System.out.print("| "); + for (char column : line) { + System.out.print(column + " | "); + } + System.out.println(); } } - protected void printEvenCapitalLetters() { - int[] aSCII = new int[128]; - for(int i = 66; i <= 90; i += 2) { - System.out.print((char)i); + private void solveTask(String consoleString) { + if (pattern.matcher(consoleString).matches()) { + String[] consoleStringArr = consoleString.split(" "); + int height = Integer.parseInt(consoleStringArr[0]); + int width = Integer.parseInt(consoleStringArr[1]); + String strategy = consoleStringArr[2]; + char[][] charsTable = new RandomCharsTableCreator().createRandomCharsTable(height, width); + printTable(charsTable); + printCapitalLetters(charsTable, strategy); + } else { + System.out.println("solveTask"); + System.out.println(ERROR_MESSAGE); } } } diff --git a/src/main/java/homework_2/random_chars_table/RandomCharsTableCreator.java b/src/main/java/homework_2/random_chars_table/RandomCharsTableCreator.java new file mode 100644 index 00000000..c1cc2f41 --- /dev/null +++ b/src/main/java/homework_2/random_chars_table/RandomCharsTableCreator.java @@ -0,0 +1,26 @@ +package homework_2.random_chars_table; + +import java.util.concurrent.ThreadLocalRandom; + +public class RandomCharsTableCreator { + + + char[][] createRandomCharsTable(int height, int width) { + if (height < 1 || width < 1) { + System.out.println(RandomCharsTable.ERROR_MESSAGE); + throw new IllegalArgumentException(); + } + char[][] randomCharsTable = new char[height][width]; + for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { + randomCharsTable[i][j] = getRandomChar(); + } + } + return randomCharsTable; + } + + private char getRandomChar() { + int randomNum = ThreadLocalRandom.current().nextInt(65, 90 + 1); + return (char) randomNum; + } +} From 29800badf0ddccfe423fd9a7a396cf1e96040532 Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Sat, 24 Jul 2021 21:51:19 +0300 Subject: [PATCH 18/56] HW2 from 24.07.2021 --- .../random_chars_table/RandomCharsTable.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main/java/homework_2/random_chars_table/RandomCharsTable.java b/src/main/java/homework_2/random_chars_table/RandomCharsTable.java index 71b8cac4..ca34b084 100644 --- a/src/main/java/homework_2/random_chars_table/RandomCharsTable.java +++ b/src/main/java/homework_2/random_chars_table/RandomCharsTable.java @@ -19,7 +19,7 @@ public class RandomCharsTable implements Runnable { " and the strategy (even or odd)"; ERROR_MESSAGE = "Passed parameters should match the format " + "[positive integer] [positive integer] [even|odd]"; - REGEX = "^([\\d]+[\\s]+[\\d])(odd|even)"; + REGEX = "\\s*[1-9]\\s+[1-9]\\s+(even|odd){1}$"; } Pattern pattern = Pattern.compile(REGEX); @@ -51,9 +51,9 @@ private void printOddCapitalLetters(char[][] table) { oddChars.add(column); } } - System.out.println("Even letters - " - + oddChars.toString().replaceAll("[\\[\\]]", "")); } + System.out.println("Odd letters - " + + oddChars.toString().replaceAll("[\\[\\]]", "")); } private void printEvenCapitalLetters(char[][] charsTable) { @@ -64,9 +64,9 @@ private void printEvenCapitalLetters(char[][] charsTable) { evenChars.add(column); } } - System.out.println("Even letters - " - + evenChars.toString().replaceAll("[\\[\\]]", "")); } + System.out.println("Even letters - " + + evenChars.toString().replaceAll("[\\[\\]]", "")); } @@ -100,7 +100,6 @@ private void solveTask(String consoleString) { printTable(charsTable); printCapitalLetters(charsTable, strategy); } else { - System.out.println("solveTask"); System.out.println(ERROR_MESSAGE); } } From c2278dd5709c0fa558cc5bd1461cd0d99dff7247 Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Sat, 24 Jul 2021 22:03:03 +0300 Subject: [PATCH 19/56] HW2 from 24.07.2021 --- .../java/homework_2/random_chars_table/RandomCharsTable.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/homework_2/random_chars_table/RandomCharsTable.java b/src/main/java/homework_2/random_chars_table/RandomCharsTable.java index ca34b084..760e90d6 100644 --- a/src/main/java/homework_2/random_chars_table/RandomCharsTable.java +++ b/src/main/java/homework_2/random_chars_table/RandomCharsTable.java @@ -19,7 +19,7 @@ public class RandomCharsTable implements Runnable { " and the strategy (even or odd)"; ERROR_MESSAGE = "Passed parameters should match the format " + "[positive integer] [positive integer] [even|odd]"; - REGEX = "\\s*[1-9]\\s+[1-9]\\s+(even|odd){1}$"; + REGEX = "\\s*[1-9]+\\s+[1-9]+\\s+(even|odd){1}$"; } Pattern pattern = Pattern.compile(REGEX); From b371ab2d5d5f84139a5674c8fcd1a42f4698116b Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Fri, 30 Jul 2021 14:42:15 +0300 Subject: [PATCH 20/56] HW4 Singleton (thread safe, double checked locking) --- .../CustomAnnotationTaskDescription | 4 ++++ .../java/homework_4/singleton/Singleton.java | 18 ++++++++++++++++++ .../singleton/SingletonTaskDescription | 7 +++++++ 3 files changed, 29 insertions(+) create mode 100644 src/main/java/homework_4/custom_annotation/CustomAnnotationTaskDescription create mode 100644 src/main/java/homework_4/singleton/Singleton.java create mode 100644 src/main/java/homework_4/singleton/SingletonTaskDescription diff --git a/src/main/java/homework_4/custom_annotation/CustomAnnotationTaskDescription b/src/main/java/homework_4/custom_annotation/CustomAnnotationTaskDescription new file mode 100644 index 00000000..9fad52fc --- /dev/null +++ b/src/main/java/homework_4/custom_annotation/CustomAnnotationTaskDescription @@ -0,0 +1,4 @@ +CustomAnnotation: +Реализовать кастомную аннотацию. +Название и реализация - на выбор, главное чтобы она где-то ставилась и что-то делала +(например, из нее мы бы инжектили дефолтные значения в конструктор) diff --git a/src/main/java/homework_4/singleton/Singleton.java b/src/main/java/homework_4/singleton/Singleton.java new file mode 100644 index 00000000..85fbcf94 --- /dev/null +++ b/src/main/java/homework_4/singleton/Singleton.java @@ -0,0 +1,18 @@ +package homework_4.singleton; + +class Singleton { + private static volatile Singleton instance = null; + private Singleton(){ + } + public static Singleton getInstance() { + //Double checked locking for correct operation in multithreaded applications + if (instance == null) { + synchronized (Singleton.class) { + if (instance == null) { + instance = new Singleton(); + } + } + } + return instance; + } +} diff --git a/src/main/java/homework_4/singleton/SingletonTaskDescription b/src/main/java/homework_4/singleton/SingletonTaskDescription new file mode 100644 index 00000000..80492592 --- /dev/null +++ b/src/main/java/homework_4/singleton/SingletonTaskDescription @@ -0,0 +1,7 @@ +Singleton: +Написать приложение-синглтон. +Максимум 1 обьект этого класса. +Приватный конструктор и публичный метод getInstance(); + + + From c6d5de302fcd4bfd3df7b306d0b4e3b96d976430 Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Fri, 30 Jul 2021 15:11:20 +0300 Subject: [PATCH 21/56] HW4 README updated 30.07.2021 Singleton (thread safe, double checked locking) --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f368e944..046c08e0 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ | HW2 - 2 | [Pyramid Printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_2.pyramid_printer) | The app that reads the number once from the command line, and prints the pyramid of "x" according to the positive integer entered. Quits after first printing | | HW2 - 3 | [Random Chars Table](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_2.random_chars_table) | The app that reads length, width, strategy from the console generates the table and prints even/odd letters | | HW3 | [ImmutableClass](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_3) | The Immutable Class An immutable class is created. Requirements for an immutable class have been added to a separate * .tht file in the homework_3 package.| +| HW4 - 2 | [Singleton](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_4.singleton) | Singleton Class. The Singleton class (thread-safe version) is created. Double checked locking was used in order to check whether the variable has already been initialized, if the another thread acquired the lock first, it may have already done the initialization.. | | Title | Link | Short description From 3aa487d478280b6633f2dda33a1cfd590cbc909f Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Fri, 30 Jul 2021 16:46:27 +0300 Subject: [PATCH 22/56] HW4 README updated 30.07.2021 --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 046c08e0..9bb4e230 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,12 @@ | Number | Solution | Short description | --- | --- | --- | | HW1 | [Console printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_1) | The app that reads input arguments and prints them, until "error" argument | -| HW2 - 1 | [Traffic Light](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_2.traffic_light) | The app that reads the time from the console (seconds) and displays the traffic light color (GREEN in green, YELLOW in yellow, RED in red) on the command line | -| HW2 - 2 | [Pyramid Printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_2.pyramid_printer) | The app that reads the number once from the command line, and prints the pyramid of "x" according to the positive integer entered. Quits after first printing | -| HW2 - 3 | [Random Chars Table](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_2.random_chars_table) | The app that reads length, width, strategy from the console generates the table and prints even/odd letters | +| HW2 - 1 | [Traffic Light](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_2/traffic_light) | The app that reads the time from the console (seconds) and displays the traffic light color (GREEN in green, YELLOW in yellow, RED in red) on the command line | +| HW2 - 2 | [Pyramid Printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_2/pyramid_printer) | The app that reads the number once from the command line, and prints the pyramid of "x" according to the positive integer entered. Quits after first printing | +| HW2 - 3 | [Random Chars Table](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_2/random_chars_table) | The app that reads length, width, strategy from the console generates the table and prints even/odd letters | | HW3 | [ImmutableClass](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_3) | The Immutable Class An immutable class is created. Requirements for an immutable class have been added to a separate * .tht file in the homework_3 package.| -| HW4 - 2 | [Singleton](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_4.singleton) | Singleton Class. The Singleton class (thread-safe version) is created. Double checked locking was used in order to check whether the variable has already been initialized, if the another thread acquired the lock first, it may have already done the initialization.. | +| HW4 - 2 | [Singleton](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_4/singleton) | Singleton Class. The Singleton class (thread-safe version) is created. Double checked locking was used in order to check whether the variable has already been initialized, if the another thread acquired the lock first, it may have already done the initialization. | +| HW4 - 3 | [Custom annotation](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_4/custom_annotation) | Custom annotation. | | Title | Link | Short description From 3866b9e7685392be52079935eb2874764aa0c2c4 Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Sat, 31 Jul 2021 00:29:27 +0300 Subject: [PATCH 23/56] HW4 SingletonTest from 31.07.2021 --- .../java/homework_4/singleton/SingletonTest.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/test/java/homework_4/singleton/SingletonTest.java diff --git a/src/test/java/homework_4/singleton/SingletonTest.java b/src/test/java/homework_4/singleton/SingletonTest.java new file mode 100644 index 00000000..37c72392 --- /dev/null +++ b/src/test/java/homework_4/singleton/SingletonTest.java @@ -0,0 +1,15 @@ +package homework_4.singleton; + +import base.UnitBase; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +class SingletonTest extends UnitBase { + + @Test + void getInstance() { + Singleton singletonInstance1 = Singleton.getInstance(); + Singleton singletonInstance2 = Singleton.getInstance(); + Assertions.assertSame(singletonInstance1, singletonInstance2); + } +} \ No newline at end of file From 3d49019c206f211f12ccc94cc442aad5dd99d8ee Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Sat, 31 Jul 2021 00:38:54 +0300 Subject: [PATCH 24/56] README (HW4 SingletonTest) added 31.07.2021 --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 9bb4e230..62dfa63d 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ | HW2 - 3 | [Random Chars Table](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_2/random_chars_table) | The app that reads length, width, strategy from the console generates the table and prints even/odd letters | | HW3 | [ImmutableClass](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_3) | The Immutable Class An immutable class is created. Requirements for an immutable class have been added to a separate * .tht file in the homework_3 package.| | HW4 - 2 | [Singleton](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_4/singleton) | Singleton Class. The Singleton class (thread-safe version) is created. Double checked locking was used in order to check whether the variable has already been initialized, if the another thread acquired the lock first, it may have already done the initialization. | +| HW4 - 2 Test | [Singleton Test](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/test/java/homework_4/singleton) | Singleton Class Test. | | HW4 - 3 | [Custom annotation](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_4/custom_annotation) | Custom annotation. | From 285ece4cc05253d9a3df7a33ea16ebfcbde65e62 Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Sat, 31 Jul 2021 16:58:34 +0300 Subject: [PATCH 25/56] Java_Core_June_2021 project restored 31.07.2021 --- gradlew | 2 +- src/main/java/homework_1/Main.java | 15 ++- .../java/homework_2/pyramid_printer/Main.java | 8 ++ .../pyramid_printer/PyramidPrinter.java | 54 +++++++++ .../homework_2/random_chars_table/Main.java | 8 ++ .../random_chars_table/RandomCharsTable.java | 106 ++++++++++++++++++ .../RandomCharsTableCreator.java | 26 +++++ .../java/homework_2/traffic_light/Main.java | 8 ++ .../traffic_light/TrafficLight.java | 76 +++++++++++++ .../homework_3/ImmutableClassEmployee.java | 46 ++++++++ .../homework_3/ImmutableClassRequirements | 21 ++++ .../CustomAnnotationTaskDescription | 4 + src/test/java/base/UnitBase.java | 2 +- .../pyramid_printer/PyramidPrinterTest.java | 24 ++++ .../RandomCharsTableCreatorTest.java | 16 +++ .../RandomCharsTableTest.java | 40 +++++++ .../traffic_light/TrafficLightTest.java | 24 ++++ .../ImmutableClassEmployeeTest.java | 20 ++++ 18 files changed, 495 insertions(+), 5 deletions(-) create mode 100644 src/main/java/homework_2/pyramid_printer/Main.java create mode 100644 src/main/java/homework_2/pyramid_printer/PyramidPrinter.java create mode 100644 src/main/java/homework_2/random_chars_table/Main.java create mode 100644 src/main/java/homework_2/random_chars_table/RandomCharsTable.java create mode 100644 src/main/java/homework_2/random_chars_table/RandomCharsTableCreator.java create mode 100644 src/main/java/homework_2/traffic_light/Main.java create mode 100644 src/main/java/homework_2/traffic_light/TrafficLight.java create mode 100644 src/main/java/homework_3/ImmutableClassEmployee.java create mode 100644 src/main/java/homework_3/ImmutableClassRequirements create mode 100644 src/main/java/homework_4/custom_annotation/CustomAnnotationTaskDescription create mode 100644 src/test/java/homework_2/pyramid_printer/PyramidPrinterTest.java create mode 100644 src/test/java/homework_2/random_chars_table/RandomCharsTableCreatorTest.java create mode 100644 src/test/java/homework_2/random_chars_table/RandomCharsTableTest.java create mode 100644 src/test/java/homework_2/traffic_light/TrafficLightTest.java create mode 100644 src/test/java/homework_3/ImmutableClassEmployeeTest.java diff --git a/gradlew b/gradlew index 4f906e0c..744e882e 100644 --- a/gradlew +++ b/gradlew @@ -72,7 +72,7 @@ case "`uname`" in Darwin* ) darwin=true ;; - MINGW* ) + MSYS* | MINGW* ) msys=true ;; NONSTOP* ) diff --git a/src/main/java/homework_1/Main.java b/src/main/java/homework_1/Main.java index 07c029a2..d9b09c46 100644 --- a/src/main/java/homework_1/Main.java +++ b/src/main/java/homework_1/Main.java @@ -1,9 +1,18 @@ package homework_1; public class Main { + public static final String RESET = "\033[0m"; // Text Reset + public static final String RED_BOLD = "\033[1;31m"; // RED public static void main(String[] args) { - System.out.println("Hello homework!"); + //HW1 from 05.07.2021 (after review 09.07.2021) + for (String arg : args) { + if (arg.equals("error")) { + System.out.println(Main.RED_BOLD + "Alarm!" + Main.RESET); + break; + } else { + System.out.println(arg + ": " + arg.length() + " letters"); + } + } } - -} +} \ No newline at end of file diff --git a/src/main/java/homework_2/pyramid_printer/Main.java b/src/main/java/homework_2/pyramid_printer/Main.java new file mode 100644 index 00000000..71e33035 --- /dev/null +++ b/src/main/java/homework_2/pyramid_printer/Main.java @@ -0,0 +1,8 @@ +package homework_2.pyramid_printer; + +public class Main { + + public static void main(String[] args) { + new PyramidPrinter().run(); + } +} diff --git a/src/main/java/homework_2/pyramid_printer/PyramidPrinter.java b/src/main/java/homework_2/pyramid_printer/PyramidPrinter.java new file mode 100644 index 00000000..31869af8 --- /dev/null +++ b/src/main/java/homework_2/pyramid_printer/PyramidPrinter.java @@ -0,0 +1,54 @@ +package homework_2.pyramid_printer; + +import java.util.Scanner; +import java.util.regex.Pattern; + +public class PyramidPrinter implements Runnable { + + public static final String ERROR_MESSAGE; + public static final String WELCOME_MESSAGE; + public static final String ENTER_HEIGHT_MESSAGE; + public static final String REGEX; + + static { + ERROR_MESSAGE = "Only 1 non-negative integer is allowed as passed parameter"; + WELCOME_MESSAGE = "Welcome to the Pyramid Printer Application"; + ENTER_HEIGHT_MESSAGE = "Enter the height parameter"; + REGEX = "\\d+"; + } + + Pattern pattern = Pattern.compile(REGEX); + + @Override + public void run() { + startGreeting(); + Scanner scanner = new Scanner(System.in); + String consoleString = scanner.nextLine(); + scanner.close(); + if (numberFormatIsValid(consoleString)) { + printPyramid(Integer.parseInt(consoleString)); + } + } + + protected void startGreeting() { + System.out.println(WELCOME_MESSAGE); + System.out.println(ENTER_HEIGHT_MESSAGE); + } + + protected boolean numberFormatIsValid(String consoleString) { + if (!pattern.matcher(consoleString).matches()) { + System.out.println(ERROR_MESSAGE); + return false; + } + return true; + } + + protected void printPyramid(int height) { + for (int i = 1; i <= height; i++) { + for (int j = 0; j < i; j++) { + System.out.print("x"); + } + System.out.println(); + } + } +} diff --git a/src/main/java/homework_2/random_chars_table/Main.java b/src/main/java/homework_2/random_chars_table/Main.java new file mode 100644 index 00000000..be586c4b --- /dev/null +++ b/src/main/java/homework_2/random_chars_table/Main.java @@ -0,0 +1,8 @@ +package homework_2.random_chars_table; + +public class Main { + + public static void main(String[] args) { + new RandomCharsTable().run(); + } +} diff --git a/src/main/java/homework_2/random_chars_table/RandomCharsTable.java b/src/main/java/homework_2/random_chars_table/RandomCharsTable.java new file mode 100644 index 00000000..a48b57ac --- /dev/null +++ b/src/main/java/homework_2/random_chars_table/RandomCharsTable.java @@ -0,0 +1,106 @@ +package homework_2.random_chars_table; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; +import java.util.regex.Pattern; + +public class RandomCharsTable implements Runnable { + + public static final String WELCOME_MESSAGE; + public static final String ENTER_PARAM_MESSAGE; + public static final String ERROR_MESSAGE; + public static final String REGEX; + + static { + WELCOME_MESSAGE = "Welcome to the Random Chars Table Application"; + ENTER_PARAM_MESSAGE = "Enter in one line 2 numbers and a line - " + + "the length and width of the table / two-dimensional array," + + " and the strategy (even or odd)"; + ERROR_MESSAGE = "Passed parameters should match the format " + + "[positive integer] [positive integer] [even|odd]"; + REGEX = "\\s*[1-9]+\\s+[1-9]+\\s+(even|odd){1}$"; + } + + Pattern pattern = Pattern.compile(REGEX); + + @Override + public void run() { + startGreeting(); + String consoleString = readConsoleString(); + solveTask(consoleString); + } + + void startGreeting() { + System.out.println(WELCOME_MESSAGE); + System.out.println(ENTER_PARAM_MESSAGE); + } + + String readConsoleString() { + Scanner scanner = new Scanner(System.in); + String consoleString = scanner.nextLine(); + scanner.close(); + return consoleString; + } + + void printOddCapitalLetters(char[][] table) { + List oddChars = new ArrayList<>(); + for (char[] line : table) { + for (char column : line) { + if (column % 2 != 0) { + oddChars.add(column); + } + } + } + System.out.println("Odd letters - " + + oddChars.toString().replaceAll("[\\[\\]]", "")); + } + + void printEvenCapitalLetters(char[][] charsTable) { + List evenChars = new ArrayList<>(); + for (char[] line : charsTable) { + for (char column : line) { + if (column % 2 == 0) { + evenChars.add(column); + } + } + } + System.out.println("Even letters - " + + evenChars.toString().replaceAll("[\\[\\]]", "")); + } + + + void printCapitalLetters(char[][] charsTable, String strategy) { + if (strategy.equalsIgnoreCase("odd")) { + printOddCapitalLetters(charsTable); + } else if (strategy.equalsIgnoreCase("even")) { + printEvenCapitalLetters(charsTable); + } else { + System.out.println(ERROR_MESSAGE); + throw new IllegalArgumentException(); + } + } + void printTable(char[][] charsTable) { + for (char[] line : charsTable) { + System.out.print("| "); + for (char column : line) { + System.out.print(column + " | "); + } + System.out.println(); + } + } + + void solveTask(String consoleString) { + if (pattern.matcher(consoleString).matches()) { + String[] consoleStringArr = consoleString.split(" "); + int height = Integer.parseInt(consoleStringArr[0]); + int width = Integer.parseInt(consoleStringArr[1]); + String strategy = consoleStringArr[2]; + char[][] charsTable = new RandomCharsTableCreator().createRandomCharsTable(height, width); + printTable(charsTable); + printCapitalLetters(charsTable, strategy); + } else { + System.out.println(ERROR_MESSAGE); + } + } +} diff --git a/src/main/java/homework_2/random_chars_table/RandomCharsTableCreator.java b/src/main/java/homework_2/random_chars_table/RandomCharsTableCreator.java new file mode 100644 index 00000000..26eb7798 --- /dev/null +++ b/src/main/java/homework_2/random_chars_table/RandomCharsTableCreator.java @@ -0,0 +1,26 @@ +package homework_2.random_chars_table; + +import java.util.concurrent.ThreadLocalRandom; + +public class RandomCharsTableCreator { + + + char[][] createRandomCharsTable(int height, int width) { + if (height < 1 || width < 1) { + System.out.println(RandomCharsTable.ERROR_MESSAGE); + throw new IllegalArgumentException(); + } + char[][] randomCharsTable = new char[height][width]; + for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { + randomCharsTable[i][j] = getRandomChar(); + } + } + return randomCharsTable; + } + + char getRandomChar() { + int randomNum = ThreadLocalRandom.current().nextInt(65, 90 + 1); + return (char) randomNum; + } +} diff --git a/src/main/java/homework_2/traffic_light/Main.java b/src/main/java/homework_2/traffic_light/Main.java new file mode 100644 index 00000000..3ff7f428 --- /dev/null +++ b/src/main/java/homework_2/traffic_light/Main.java @@ -0,0 +1,8 @@ +package homework_2.traffic_light; + +public class Main { + + public static void main(String[] args) { + new TrafficLight().run(); + } +} diff --git a/src/main/java/homework_2/traffic_light/TrafficLight.java b/src/main/java/homework_2/traffic_light/TrafficLight.java new file mode 100644 index 00000000..445f1e7d --- /dev/null +++ b/src/main/java/homework_2/traffic_light/TrafficLight.java @@ -0,0 +1,76 @@ +package homework_2.traffic_light; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Scanner; +import java.util.regex.Pattern; + +public class TrafficLight implements Runnable { + + public static final String ERROR_MESSAGE; + public static final String RED_LIGHT_MESSAGE; + public static final String YELLOW_LIGHT_MESSAGE; + public static final String GREEN_LIGHT_MESSAGE; + public static final String DAY_IS_OVER_MESSAGE; + public static final String REGEX; + + static { + ERROR_MESSAGE = "Only 1 non-negative integer is allowed as passed parameter"; + DAY_IS_OVER_MESSAGE = "The day is over"; + RED_LIGHT_MESSAGE = "\u001b[31m RED \u001b[0m"; + YELLOW_LIGHT_MESSAGE = "\u001b[33m YELLOW \u001b[0m"; + GREEN_LIGHT_MESSAGE = "\u001b[32m GREEN \u001b[0m"; + REGEX = "\\d+"; + } + + Pattern pattern = Pattern.compile(REGEX); + + @Override + public void run() { + startGreeting(); + Scanner scanner = new Scanner(System.in); + String consoleString = scanner.nextLine(); + scanner.close(); + if (numberFormatIsValid(consoleString)) { + consoleString = consoleString.replaceAll("\\D+", ""); + System.out.println(getTrafficLightColor(Integer.parseInt(consoleString))); + } + } + + protected void startGreeting() { + System.out.println("Welcome to the Traffic Light Application"); + System.out.println("Enter the number of seconds since the start of the day"); + } + + protected boolean numberFormatIsValid(String consoleString) { + if (consoleString == null || consoleString.length() == 0) { + return false; + } + if (!pattern.matcher(consoleString).matches()) { + System.out.println(ERROR_MESSAGE); + return false; + } + if (Integer.parseInt(consoleString) > 86399) { + System.out.println(DAY_IS_OVER_MESSAGE); + return false; + } + return true; + } + + protected String getTrafficLightColor(int seconds) { + int secInMin = Math.floorMod(seconds, 60); + System.out.println("seconds= " + seconds); + System.out.println("secInMin= " + secInMin); + if (secInMin >= 0 && secInMin < 35) { + return GREEN_LIGHT_MESSAGE; + } + if ((secInMin >= 35 && secInMin < 40) || (secInMin >= 55 && secInMin < 60)) { + return YELLOW_LIGHT_MESSAGE; + } + if (secInMin >= 40 && secInMin < 55) { + return RED_LIGHT_MESSAGE; + } + return ERROR_MESSAGE; + } +} diff --git a/src/main/java/homework_3/ImmutableClassEmployee.java b/src/main/java/homework_3/ImmutableClassEmployee.java new file mode 100644 index 00000000..0d059807 --- /dev/null +++ b/src/main/java/homework_3/ImmutableClassEmployee.java @@ -0,0 +1,46 @@ +package homework_3; + +import java.util.Date; + +//1. Declare your class as final, +// so other classes will not be able to extend it and break its immutability +final class ImmutableClassEmployee { + + //2. Mark all the fields private. So, they they can't be accessed outside the class + //3. Mark all fields final as well. So, no one can modify them anywhere else apart from the constructor. + private final int id; + private final String name; + private final Date dateOfBirth; + + //4. Provide an argument constructor with all initialization logic + public ImmutableClassEmployee(int id, String name, Date dateOfBirth) { + this.id = id; + this.name = name; + //5. Initialize all your fields by deeply copying them if they are not immutable by nature + this.dateOfBirth = new Date(dateOfBirth.getTime()); + } + + //6. Do not provide setters of your fields + public int getId() { + return id; + } + + public String getName() { + return name; + } + + //7. Instead of returning objects from the getters + // return a deep copy of them if this objects are not immutable by nature + public Date getDateOfBirth() { + return new Date(dateOfBirth.getTime()); + } + + @Override + public String toString() { + return "ImmutableClassEmployee{" + + "id = " + id + + ", name = '" + name + '\'' + + ", dateOfBirth = " + dateOfBirth + + ", dateOfBirth = " + dateOfBirth + "}"; + } +} diff --git a/src/main/java/homework_3/ImmutableClassRequirements b/src/main/java/homework_3/ImmutableClassRequirements new file mode 100644 index 00000000..c648a2bf --- /dev/null +++ b/src/main/java/homework_3/ImmutableClassRequirements @@ -0,0 +1,21 @@ +Following are the requirements for immutable class: + +1. Mark your class final, +2. Mark all the fields private, +3. Mark all fields final as well, +4. Provide an argument constructor with all initialization logic, +5. Initialize all mutable fields by deep copying, +6. Do not provide setters for your fields, +7. Return a deep copy of mutable fields from the getters. + +OR IN OTHER WORDS + +1. The class must be declared as final (So that child classes can’t be created) +2. Data members in the class must be declared as private (So that direct access is not allowed) +3. Data members in the class must be declared as final + (So that we can’t change the value of it after object creation) +4. A parameterized constructor should initialize all the fields performing a deep copy + (So that data members can’t be modified with object reference) +5. Deep Copy of objects should be performed in the getter methods + (To return a copy rather than returning the actual object reference) +6. No setters (To not have the option to change the value of the instance variable) diff --git a/src/main/java/homework_4/custom_annotation/CustomAnnotationTaskDescription b/src/main/java/homework_4/custom_annotation/CustomAnnotationTaskDescription new file mode 100644 index 00000000..9fad52fc --- /dev/null +++ b/src/main/java/homework_4/custom_annotation/CustomAnnotationTaskDescription @@ -0,0 +1,4 @@ +CustomAnnotation: +Реализовать кастомную аннотацию. +Название и реализация - на выбор, главное чтобы она где-то ставилась и что-то делала +(например, из нее мы бы инжектили дефолтные значения в конструктор) diff --git a/src/test/java/base/UnitBase.java b/src/test/java/base/UnitBase.java index 97e2685b..8df5dcf8 100644 --- a/src/test/java/base/UnitBase.java +++ b/src/test/java/base/UnitBase.java @@ -70,4 +70,4 @@ protected void printOut() { // assertEquals("xx", getOutputLines()[1]); // } -} +} \ No newline at end of file diff --git a/src/test/java/homework_2/pyramid_printer/PyramidPrinterTest.java b/src/test/java/homework_2/pyramid_printer/PyramidPrinterTest.java new file mode 100644 index 00000000..f434bbdd --- /dev/null +++ b/src/test/java/homework_2/pyramid_printer/PyramidPrinterTest.java @@ -0,0 +1,24 @@ +package homework_2.pyramid_printer; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class PyramidPrinterTest { + + @Test + void run() { + } + + @Test + void startGreeting() { + } + + @Test + void numberFormatIsValid() { + } + + @Test + void printPyramid() { + } +} \ No newline at end of file diff --git a/src/test/java/homework_2/random_chars_table/RandomCharsTableCreatorTest.java b/src/test/java/homework_2/random_chars_table/RandomCharsTableCreatorTest.java new file mode 100644 index 00000000..230fe095 --- /dev/null +++ b/src/test/java/homework_2/random_chars_table/RandomCharsTableCreatorTest.java @@ -0,0 +1,16 @@ +package homework_2.random_chars_table; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class RandomCharsTableCreatorTest { + + @Test + void createRandomCharsTable() { + } + + @Test + void getRandomChar() { + } +} \ No newline at end of file diff --git a/src/test/java/homework_2/random_chars_table/RandomCharsTableTest.java b/src/test/java/homework_2/random_chars_table/RandomCharsTableTest.java new file mode 100644 index 00000000..9fb9010c --- /dev/null +++ b/src/test/java/homework_2/random_chars_table/RandomCharsTableTest.java @@ -0,0 +1,40 @@ +package homework_2.random_chars_table; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class RandomCharsTableTest { + + @Test + void run() { + } + + @Test + void startGreeting() { + } + + @Test + void readConsoleString() { + } + + @Test + void printOddCapitalLetters() { + } + + @Test + void printEvenCapitalLetters() { + } + + @Test + void printCapitalLetters() { + } + + @Test + void printTable() { + } + + @Test + void solveTask() { + } +} \ No newline at end of file diff --git a/src/test/java/homework_2/traffic_light/TrafficLightTest.java b/src/test/java/homework_2/traffic_light/TrafficLightTest.java new file mode 100644 index 00000000..b3db9a3a --- /dev/null +++ b/src/test/java/homework_2/traffic_light/TrafficLightTest.java @@ -0,0 +1,24 @@ +package homework_2.traffic_light; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class TrafficLightTest { + + @Test + void run() { + } + + @Test + void startGreeting() { + } + + @Test + void numberFormatIsValid() { + } + + @Test + void getTrafficLightColor() { + } +} \ No newline at end of file diff --git a/src/test/java/homework_3/ImmutableClassEmployeeTest.java b/src/test/java/homework_3/ImmutableClassEmployeeTest.java new file mode 100644 index 00000000..00a51793 --- /dev/null +++ b/src/test/java/homework_3/ImmutableClassEmployeeTest.java @@ -0,0 +1,20 @@ +package homework_3; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class ImmutableClassEmployeeTest { + + @Test + void getId() { + } + + @Test + void getName() { + } + + @Test + void getDateOfBirth() { + } +} \ No newline at end of file From 37bf075032f2b8c4cc9240bec1f61de0dd6b7024 Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Sat, 31 Jul 2021 18:21:41 +0300 Subject: [PATCH 26/56] PyramidPrinterTest restored 31.07.2021 --- .../pyramid_printer/PyramidPrinterTest.java | 77 +++++++++++++++++-- 1 file changed, 71 insertions(+), 6 deletions(-) diff --git a/src/test/java/homework_2/pyramid_printer/PyramidPrinterTest.java b/src/test/java/homework_2/pyramid_printer/PyramidPrinterTest.java index f434bbdd..443781d9 100644 --- a/src/test/java/homework_2/pyramid_printer/PyramidPrinterTest.java +++ b/src/test/java/homework_2/pyramid_printer/PyramidPrinterTest.java @@ -1,24 +1,89 @@ package homework_2.pyramid_printer; +import base.UnitBase; import org.junit.jupiter.api.Test; - +import java.util.NoSuchElementException; import static org.junit.jupiter.api.Assertions.*; -class PyramidPrinterTest { +class PyramidPrinterTest extends UnitBase { + + @Test + void givenEmptyArgument_whenRun_thenNoSuchElementException() { + setInput(""); + assertThrows(NoSuchElementException.class, () -> new PyramidPrinter().run()); + } @Test - void run() { + void givenZeroAsArgument_whenRun_thenEmptyString() { + setInput("0"); + new PyramidPrinter().run(); + printOut(); + removeFromOutput("Welcome to the Pyramid Printer Application"); + removeFromOutput("Enter the height parameter"); + assertEquals("", getOutputLines()[0]); } + @Test - void startGreeting() { + void givenTenAsArgument_whenRun_thenTenRowsPyramid() { + setInput("10"); + new PyramidPrinter().run(); + printOut(); + removeFromOutput("Welcome to the Pyramid Printer Application"); + removeFromOutput("Enter the height parameter"); + assertEquals("x", getOutputLines()[0]); + assertEquals("xx", getOutputLines()[1]); + assertEquals("xxx", getOutputLines()[2]); + assertEquals("xxxx", getOutputLines()[3]); + assertEquals("xxxxx", getOutputLines()[4]); + assertEquals("xxxxxx", getOutputLines()[5]); + assertEquals("xxxxxxx", getOutputLines()[6]); + assertEquals("xxxxxxxx", getOutputLines()[7]); + assertEquals("xxxxxxxxx", getOutputLines()[8]); + assertEquals("xxxxxxxxxx", getOutputLines()[9]); } @Test - void numberFormatIsValid() { + void givenMinusOneAsArgument_whenRun_thenErrorMessage() { + setInput("-1"); + new PyramidPrinter().run(); + printOut(); + removeFromOutput("Welcome to the Pyramid Printer Application"); + removeFromOutput("Enter the height parameter"); + assertEquals("Only 1 non-negative integer is allowed as passed parameter", getOutputLines()[0]); } + @Test - void printPyramid() { + void givenStringValueAsArgument_whenRun_thenErrorMessage() { + setInput("StringAsValue"); + new PyramidPrinter().run(); + printOut(); + removeFromOutput("Welcome to the Pyramid Printer Application"); + removeFromOutput("Enter the height parameter"); + assertEquals("Only 1 non-negative integer is allowed as passed parameter", getOutputLines()[0]); } + + + @Test + void givenTwoArgumentsAsArgument_whenRun_thenErrorMessage() { + setInput("1 2"); + new PyramidPrinter().run(); + printOut(); + removeFromOutput("Welcome to the Pyramid Printer Application"); + removeFromOutput("Enter the height parameter"); + assertEquals("Only 1 non-negative integer is allowed as passed parameter", getOutputLines()[0]); + } + + + @Test + void givenDoubleAsArgument_whenRun_thenErrorMessage() { + setInput("0.12345"); + new PyramidPrinter().run(); + printOut(); + removeFromOutput("Welcome to the Pyramid Printer Application"); + removeFromOutput("Enter the height parameter"); + assertEquals("Only 1 non-negative integer is allowed as passed parameter", getOutputLines()[0]); + } + } \ No newline at end of file From 0c50abf9970bdecb390109552c24002a8532b30a Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Sat, 31 Jul 2021 18:27:40 +0300 Subject: [PATCH 27/56] README updated 31.07.2021 --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 62dfa63d..1f47713a 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ | HW1 | [Console printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_1) | The app that reads input arguments and prints them, until "error" argument | | HW2 - 1 | [Traffic Light](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_2/traffic_light) | The app that reads the time from the console (seconds) and displays the traffic light color (GREEN in green, YELLOW in yellow, RED in red) on the command line | | HW2 - 2 | [Pyramid Printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_2/pyramid_printer) | The app that reads the number once from the command line, and prints the pyramid of "x" according to the positive integer entered. Quits after first printing | +| HW2 - 2 Test| [Pyramid Printer Test](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/test/java/homework_2/pyramid_printer) | Pyramid Printer Test. | | HW2 - 3 | [Random Chars Table](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_2/random_chars_table) | The app that reads length, width, strategy from the console generates the table and prints even/odd letters | | HW3 | [ImmutableClass](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_3) | The Immutable Class An immutable class is created. Requirements for an immutable class have been added to a separate * .tht file in the homework_3 package.| | HW4 - 2 | [Singleton](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_4/singleton) | Singleton Class. The Singleton class (thread-safe version) is created. Double checked locking was used in order to check whether the variable has already been initialized, if the another thread acquired the lock first, it may have already done the initialization. | From cba91939679d07ccb776d807bc124e0935865794 Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Wed, 4 Aug 2021 12:19:23 +0300 Subject: [PATCH 28/56] HW4 Annotations from 04.08.2021 --- .../ClassInfoCustomAnnotation.java | 15 +++++++++++++++ .../custom_annotation/MaxFieldLength.java | 10 ++++++++++ .../MethodInfoCustomAnnotation.java | 16 ++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 src/main/java/homework_4/custom_annotation/ClassInfoCustomAnnotation.java create mode 100644 src/main/java/homework_4/custom_annotation/MaxFieldLength.java create mode 100644 src/main/java/homework_4/custom_annotation/MethodInfoCustomAnnotation.java diff --git a/src/main/java/homework_4/custom_annotation/ClassInfoCustomAnnotation.java b/src/main/java/homework_4/custom_annotation/ClassInfoCustomAnnotation.java new file mode 100644 index 00000000..0d2ed4cd --- /dev/null +++ b/src/main/java/homework_4/custom_annotation/ClassInfoCustomAnnotation.java @@ -0,0 +1,15 @@ +package homework_4.custom_annotation; + +import java.lang.annotation.*; + +@Documented +@Target(ElementType.TYPE) +@Inherited +@Retention(RetentionPolicy.RUNTIME) +public @interface ClassInfoCustomAnnotation { + String author() default "Khlebnikov Evgeniy"; + String date(); + int version() default 1; + @MaxFieldLength + String description(); +} diff --git a/src/main/java/homework_4/custom_annotation/MaxFieldLength.java b/src/main/java/homework_4/custom_annotation/MaxFieldLength.java new file mode 100644 index 00000000..46b97ccf --- /dev/null +++ b/src/main/java/homework_4/custom_annotation/MaxFieldLength.java @@ -0,0 +1,10 @@ +package homework_4.custom_annotation; + +import java.lang.annotation.*; + +@Target({ElementType.FIELD, ElementType.METHOD}) +@Inherited +@Retention(RetentionPolicy.RUNTIME) +public @interface MaxFieldLength { + int maxLength() default 100; +} diff --git a/src/main/java/homework_4/custom_annotation/MethodInfoCustomAnnotation.java b/src/main/java/homework_4/custom_annotation/MethodInfoCustomAnnotation.java new file mode 100644 index 00000000..5e54757e --- /dev/null +++ b/src/main/java/homework_4/custom_annotation/MethodInfoCustomAnnotation.java @@ -0,0 +1,16 @@ +package homework_4.custom_annotation; + +import java.lang.annotation.*; + +@Documented +@Target({ElementType.METHOD}) +@Inherited +@Retention(RetentionPolicy.RUNTIME) +public @interface MethodInfoCustomAnnotation { + String author() default "Khlebnikov Evgeniy"; + String date(); + int version() default 1; + boolean suppressExceptions() default false; + @MaxFieldLength + String comments(); +} \ No newline at end of file From 1cbfb61c709e288ef28ecf5c4253ad3613e8244f Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Wed, 11 Aug 2021 23:43:17 +0300 Subject: [PATCH 29/56] HW3 TrafficLightTest (restored) --- .../traffic_light/TrafficLightTest.java | 66 +++++++++++++++++-- 1 file changed, 61 insertions(+), 5 deletions(-) diff --git a/src/test/java/homework_2/traffic_light/TrafficLightTest.java b/src/test/java/homework_2/traffic_light/TrafficLightTest.java index b3db9a3a..9e51ca6f 100644 --- a/src/test/java/homework_2/traffic_light/TrafficLightTest.java +++ b/src/test/java/homework_2/traffic_light/TrafficLightTest.java @@ -1,24 +1,80 @@ package homework_2.traffic_light; +import base.UnitBase; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; -class TrafficLightTest { +class TrafficLightTest extends UnitBase { @Test - void run() { + void givenMinValue_whenRun_thenPrintColor() { + setInput("0"); + new TrafficLight().run(); + assertTrue(getOutput().contains("GREEN")); } @Test - void startGreeting() { + void givenNumberFrom0InclusiveTo35NotInclusive_whenRun_thenPrintColor() { + setInput("34"); + new TrafficLight().run(); + assertTrue(getOutput().contains("GREEN")); + } + + + @Test + void givenNumberFrom35InclusiveTo40NotInclusive_whenRun_thenPrintColor() { + setInput("39"); + new TrafficLight().run(); + assertTrue(getOutput().contains("YELLOW")); + } + + @Test + void givenNumberFrom40InclusiveTo55NotInclusive_whenRun_thenPrintColor() { + setInput("54"); + new TrafficLight().run(); + assertTrue(getOutput().contains("RED")); + } + + @Test + void givenNumberFrom55InclusiveTo60NotInclusive_whenRun_thenPrintColor() { + setInput("59"); + new TrafficLight().run(); + assertTrue(getOutput().contains("YELLOW")); + } + + @Test + void givenMaxValue86399_whenRun_thenPrintColor() { + setInput("86399"); + new TrafficLight().run(); + assertTrue(getOutput().contains("YELLOW")); + } + + @Test + void givenNumberBiggerThanItIsAllowed_whenRun_then_printErrorMessage() { + setInput("86400"); + new TrafficLight().run(); + assertTrue(getOutput().contains("The day is over")); + } + + @Test + void givenNegativeInteger_whenRun_thenPrintErrorMessage() { + setInput("-1"); + new TrafficLight().run(); + assertTrue(getOutput().contains("Only 1 non-negative integer is allowed as passed parameter")); } @Test - void numberFormatIsValid() { + void givenString_whenRun_thenPrintErrorMessage() { + setInput("abc"); + new TrafficLight().run(); + assertTrue(getOutput().contains("Only 1 non-negative integer is allowed as passed parameter")); } @Test - void getTrafficLightColor() { + void givenSeveralArguments_whenRun_thenPrintErrorMessage() { + setInput("2 2"); + new TrafficLight().run(); + assertTrue(getOutput().contains("Only 1 non-negative integer is allowed as passed parameter")); } } \ No newline at end of file From c7b542248d7f87df8edc476322fe3089ff77cb58 Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Wed, 11 Aug 2021 23:51:29 +0300 Subject: [PATCH 30/56] HW3 README --- README.md | 3 ++- .../random_chars_table/RandomCharsTableCreatorTest.java | 3 ++- .../homework_2/random_chars_table/RandomCharsTableTest.java | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1f47713a..75d20182 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,9 @@ | --- | --- | --- | | HW1 | [Console printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_1) | The app that reads input arguments and prints them, until "error" argument | | HW2 - 1 | [Traffic Light](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_2/traffic_light) | The app that reads the time from the console (seconds) and displays the traffic light color (GREEN in green, YELLOW in yellow, RED in red) on the command line | +| HW2 - 1 Test | [Traffic Light Test](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/test/java/homework_2/traffic_light) | Traffic Light Test | | HW2 - 2 | [Pyramid Printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_2/pyramid_printer) | The app that reads the number once from the command line, and prints the pyramid of "x" according to the positive integer entered. Quits after first printing | -| HW2 - 2 Test| [Pyramid Printer Test](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/test/java/homework_2/pyramid_printer) | Pyramid Printer Test. | +| HW2 - 2 Test | [Pyramid Printer Test](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/test/java/homework_2/pyramid_printer) | Pyramid Printer Test. | | HW2 - 3 | [Random Chars Table](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_2/random_chars_table) | The app that reads length, width, strategy from the console generates the table and prints even/odd letters | | HW3 | [ImmutableClass](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_3) | The Immutable Class An immutable class is created. Requirements for an immutable class have been added to a separate * .tht file in the homework_3 package.| | HW4 - 2 | [Singleton](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_4/singleton) | Singleton Class. The Singleton class (thread-safe version) is created. Double checked locking was used in order to check whether the variable has already been initialized, if the another thread acquired the lock first, it may have already done the initialization. | diff --git a/src/test/java/homework_2/random_chars_table/RandomCharsTableCreatorTest.java b/src/test/java/homework_2/random_chars_table/RandomCharsTableCreatorTest.java index 230fe095..9fb503c9 100644 --- a/src/test/java/homework_2/random_chars_table/RandomCharsTableCreatorTest.java +++ b/src/test/java/homework_2/random_chars_table/RandomCharsTableCreatorTest.java @@ -1,10 +1,11 @@ package homework_2.random_chars_table; +import base.UnitBase; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; -class RandomCharsTableCreatorTest { +class RandomCharsTableCreatorTest extends UnitBase { @Test void createRandomCharsTable() { diff --git a/src/test/java/homework_2/random_chars_table/RandomCharsTableTest.java b/src/test/java/homework_2/random_chars_table/RandomCharsTableTest.java index 9fb9010c..4f63524b 100644 --- a/src/test/java/homework_2/random_chars_table/RandomCharsTableTest.java +++ b/src/test/java/homework_2/random_chars_table/RandomCharsTableTest.java @@ -1,10 +1,11 @@ package homework_2.random_chars_table; +import base.UnitBase; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; -class RandomCharsTableTest { +class RandomCharsTableTest extends UnitBase { @Test void run() { From 149de9abf1aa7dc846af4f160d8e8bbfb3a29725 Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Sun, 15 Aug 2021 18:50:45 +0300 Subject: [PATCH 31/56] Java_Core_June_2021 project restored 31.07.2021 --- .../RandomCharsTableCreatorTest.java | 39 +++++++- .../RandomCharsTableTest.java | 94 +++++++++++++++++-- 2 files changed, 121 insertions(+), 12 deletions(-) diff --git a/src/test/java/homework_2/random_chars_table/RandomCharsTableCreatorTest.java b/src/test/java/homework_2/random_chars_table/RandomCharsTableCreatorTest.java index 9fb503c9..77ca7dff 100644 --- a/src/test/java/homework_2/random_chars_table/RandomCharsTableCreatorTest.java +++ b/src/test/java/homework_2/random_chars_table/RandomCharsTableCreatorTest.java @@ -1,17 +1,48 @@ package homework_2.random_chars_table; -import base.UnitBase; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.util.Arrays; +import java.util.List; import static org.junit.jupiter.api.Assertions.*; -class RandomCharsTableCreatorTest extends UnitBase { +class RandomCharsTableCreatorTest { + + private List evenLettersArrayList; + private List oddLettersArrayList; + + @BeforeEach + void setUp() { + evenLettersArrayList = Arrays.asList(new Character[]{60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90}); + oddLettersArrayList = Arrays.asList(new Character[]{61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89}); + } @Test - void createRandomCharsTable() { + void createRandomCharsTableTest1() { + RandomCharsTableCreator randomCharsTableCreator = new RandomCharsTableCreator(); + char[][] charsTable = randomCharsTableCreator.createRandomCharsTable(20, 40); + + assertEquals(20, charsTable.length); + for (char[] chars : charsTable) { + assertEquals(40, chars.length); + for (char ch : chars) { + assertTrue(evenLettersArrayList.contains(ch) || oddLettersArrayList.contains(ch)); + } + } } @Test - void getRandomChar() { + void createRandomCharsTableTest2() { + RandomCharsTableCreator randomCharsTableCreator = new RandomCharsTableCreator(); + char[][] charsTable = randomCharsTableCreator.createRandomCharsTable(12, 122); + + assertEquals(12, charsTable.length); + for (char[] chars : charsTable) { + assertEquals(122, chars.length); + for (char ch : chars) { + assertTrue(evenLettersArrayList.contains(ch) || oddLettersArrayList.contains(ch)); + } + } } } \ No newline at end of file diff --git a/src/test/java/homework_2/random_chars_table/RandomCharsTableTest.java b/src/test/java/homework_2/random_chars_table/RandomCharsTableTest.java index 4f63524b..515a4679 100644 --- a/src/test/java/homework_2/random_chars_table/RandomCharsTableTest.java +++ b/src/test/java/homework_2/random_chars_table/RandomCharsTableTest.java @@ -3,39 +3,117 @@ import base.UnitBase; import org.junit.jupiter.api.Test; +import java.util.NoSuchElementException; + import static org.junit.jupiter.api.Assertions.*; class RandomCharsTableTest extends UnitBase { + public final String WELCOME_MESSAGE_TEST = "Welcome to the Random Chars Table Application"; + public final String ENTER_PARAM_MESSAGE_TEST = "Enter in one line 2 numbers and a line - " + + "the length and width of the table / two-dimensional array," + + " and the strategy (even or odd)"; + public final String ERROR_MESSAGE_TEST = "Passed parameters should match the format " + + "[positive integer] [positive integer] [even|odd]"; + + @Test + void givenZeroAsArgument_whenRun_thenErrorMessage() { + setInput("0 0 odd"); + new RandomCharsTable().run(); + printOut(); + removeFromOutput(WELCOME_MESSAGE_TEST); + removeFromOutput(ENTER_PARAM_MESSAGE_TEST); + assertEquals(ERROR_MESSAGE_TEST, getOutputLines()[0]); + } + @Test - void run() { + void givenEmptyArgument_whenRun_thenNoSuchElementException() { + setInput(""); + assertThrows(NoSuchElementException.class, () -> new RandomCharsTable().run()); } @Test - void startGreeting() { + void givenNegativeIntegerAsFirstArgument_whenRun_thenPrintErrorMessage() { + setInput("-2 2 even"); + new RandomCharsTable().run(); + assertTrue(getOutput().contains(ERROR_MESSAGE_TEST)); } @Test - void readConsoleString() { + void givenNnegativeIntegerAsSecondArgument_whenRun_thenPrintErrorMessage() { + setInput("2 -2 even"); + new RandomCharsTable().run(); + assertTrue(getOutput().contains(ERROR_MESSAGE_TEST)); } @Test - void printOddCapitalLetters() { + void givenWrongStrategy_whenRun_thenPrintErrorMessage() { + setInput("2 2 wrongStrategy"); + new RandomCharsTable().run(); + assertTrue(getOutput().contains(ERROR_MESSAGE_TEST)); } @Test - void printEvenCapitalLetters() { + void givenLetterAsFirstArgument_whenRun_thenPrintErrorMessage() { + setInput("w 2 even"); + new RandomCharsTable().run(); + assertTrue(getOutput().contains(ERROR_MESSAGE_TEST)); } @Test - void printCapitalLetters() { + void givenLetterAsSecondrgument_whenRun_thenPrintErrorMessage() { + setInput("2 W even"); + new RandomCharsTable().run(); + assertTrue(getOutput().contains(ERROR_MESSAGE_TEST)); } @Test - void printTable() { + void givenDoubleAsFirstArgument_whenRun_thenPrintErrorMessage() { + setInput("0.12345 2 even"); + new RandomCharsTable().run(); + assertTrue(getOutput().contains(ERROR_MESSAGE_TEST)); } @Test - void solveTask() { + void givenDoubleAsSecondArgument_whenRun_thenPrintErrorMessage() { + setInput("2 0.12345 even"); + new RandomCharsTable().run(); + assertTrue(getOutput().contains(ERROR_MESSAGE_TEST)); } + + @Test + void givenMoreThanThreeArguments_whenRun_thenPrintErrorMessage() { + setInput("1 2 3 odd"); + new RandomCharsTable().run(); + assertTrue(getOutput().contains(ERROR_MESSAGE_TEST)); + } + + @Test + void givenLengthWidthEvenStrategy_whenRun_thenPrintEvenChars() { + setInput("8 8 even"); + new RandomCharsTable().run(); + removeFromOutput("Even letters - "); + removeFromOutput(","); + removeFromOutput(" "); + String str = getOutputLines()[getOutputLines().length - 1]; + for (int i = 0; i < str.length(); i++) { + assertEquals(0, str.charAt(i) % 2); + } + } + + + @Test + void givenLengthWidthOddStrategy_whenRun_thenPrintOddChars() { + setInput("8 8 odd"); + new RandomCharsTable().run(); + removeFromOutput("Odd letters - "); + removeFromOutput(","); + removeFromOutput(" "); + String str = getOutputLines()[getOutputLines().length - 1]; + for (int i = 0; i < str.length(); i++) { + assertEquals(1, str.charAt(i) % 2); + } + } + + } \ No newline at end of file From 06a661ca2e34e0c53ffaac13326d8213324e90f3 Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Sun, 15 Aug 2021 18:55:16 +0300 Subject: [PATCH 32/56] HW3 README update --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 75d20182..0182302f 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,9 @@ | HW2 - 1 | [Traffic Light](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_2/traffic_light) | The app that reads the time from the console (seconds) and displays the traffic light color (GREEN in green, YELLOW in yellow, RED in red) on the command line | | HW2 - 1 Test | [Traffic Light Test](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/test/java/homework_2/traffic_light) | Traffic Light Test | | HW2 - 2 | [Pyramid Printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_2/pyramid_printer) | The app that reads the number once from the command line, and prints the pyramid of "x" according to the positive integer entered. Quits after first printing | -| HW2 - 2 Test | [Pyramid Printer Test](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/test/java/homework_2/pyramid_printer) | Pyramid Printer Test. | +| HW2 - 2 Test | [Pyramid Printer Test](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/test/java/homework_2/pyramid_printer) | Pyramid Printer Test | | HW2 - 3 | [Random Chars Table](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_2/random_chars_table) | The app that reads length, width, strategy from the console generates the table and prints even/odd letters | +| HW2 - 3 Test | [Random Chars Table Test](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/test/java/homework_2/random_chars_table) | Random Chars Table Test | | HW3 | [ImmutableClass](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_3) | The Immutable Class An immutable class is created. Requirements for an immutable class have been added to a separate * .tht file in the homework_3 package.| | HW4 - 2 | [Singleton](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_4/singleton) | Singleton Class. The Singleton class (thread-safe version) is created. Double checked locking was used in order to check whether the variable has already been initialized, if the another thread acquired the lock first, it may have already done the initialization. | | HW4 - 2 Test | [Singleton Test](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/test/java/homework_4/singleton) | Singleton Class Test. | From d0c6e357359167ffe74022949c27013b612a669c Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Sun, 15 Aug 2021 20:45:00 +0300 Subject: [PATCH 33/56] HW4 update --- .../ClassInfoCustomAnnotation.java | 4 +++- .../CustomAnnotationTestClass.java | 9 +++++++ .../FilePathCustomAnnotation.java | 12 ++++++++++ .../MethodInfoCustomAnnotation.java | 1 - .../ClassInfoCustomAnnotationTest.java | 24 +++++++++++++++++++ .../FilePathCustomAnnotationTest.java | 18 ++++++++++++++ .../homework_4/singleton/SingletonTest.java | 1 + 7 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 src/main/java/homework_4/custom_annotation/CustomAnnotationTestClass.java create mode 100644 src/main/java/homework_4/custom_annotation/FilePathCustomAnnotation.java create mode 100644 src/test/java/homework_4/custom_annotation/ClassInfoCustomAnnotationTest.java create mode 100644 src/test/java/homework_4/custom_annotation/FilePathCustomAnnotationTest.java diff --git a/src/main/java/homework_4/custom_annotation/ClassInfoCustomAnnotation.java b/src/main/java/homework_4/custom_annotation/ClassInfoCustomAnnotation.java index 0d2ed4cd..25d2b3d2 100644 --- a/src/main/java/homework_4/custom_annotation/ClassInfoCustomAnnotation.java +++ b/src/main/java/homework_4/custom_annotation/ClassInfoCustomAnnotation.java @@ -1,6 +1,9 @@ package homework_4.custom_annotation; import java.lang.annotation.*; +import java.text.SimpleDateFormat; +import java.util.Calendar; + @Documented @Target(ElementType.TYPE) @@ -10,6 +13,5 @@ String author() default "Khlebnikov Evgeniy"; String date(); int version() default 1; - @MaxFieldLength String description(); } diff --git a/src/main/java/homework_4/custom_annotation/CustomAnnotationTestClass.java b/src/main/java/homework_4/custom_annotation/CustomAnnotationTestClass.java new file mode 100644 index 00000000..f196c64d --- /dev/null +++ b/src/main/java/homework_4/custom_annotation/CustomAnnotationTestClass.java @@ -0,0 +1,9 @@ +package homework_4.custom_annotation; + +@FilePathCustomAnnotation +@ClassInfoCustomAnnotation(date = "15/08/2021", description = "Class for testing annotations") +public class CustomAnnotationTestClass { + + protected static final String FILE_PATH = CustomAnnotationTestClass.class.getAnnotation(FilePathCustomAnnotation.class).path(); + +} diff --git a/src/main/java/homework_4/custom_annotation/FilePathCustomAnnotation.java b/src/main/java/homework_4/custom_annotation/FilePathCustomAnnotation.java new file mode 100644 index 00000000..59679bac --- /dev/null +++ b/src/main/java/homework_4/custom_annotation/FilePathCustomAnnotation.java @@ -0,0 +1,12 @@ +package homework_4.custom_annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +public @interface FilePathCustomAnnotation { + String path() default "./src/main/resources/custom_file_reader/testFile.txt"; +} \ No newline at end of file diff --git a/src/main/java/homework_4/custom_annotation/MethodInfoCustomAnnotation.java b/src/main/java/homework_4/custom_annotation/MethodInfoCustomAnnotation.java index 5e54757e..6d23140b 100644 --- a/src/main/java/homework_4/custom_annotation/MethodInfoCustomAnnotation.java +++ b/src/main/java/homework_4/custom_annotation/MethodInfoCustomAnnotation.java @@ -11,6 +11,5 @@ String date(); int version() default 1; boolean suppressExceptions() default false; - @MaxFieldLength String comments(); } \ No newline at end of file diff --git a/src/test/java/homework_4/custom_annotation/ClassInfoCustomAnnotationTest.java b/src/test/java/homework_4/custom_annotation/ClassInfoCustomAnnotationTest.java new file mode 100644 index 00000000..53a3fd9d --- /dev/null +++ b/src/test/java/homework_4/custom_annotation/ClassInfoCustomAnnotationTest.java @@ -0,0 +1,24 @@ +package homework_4.custom_annotation; + +import base.UnitBase; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ClassInfoCustomAnnotationTest extends UnitBase { + + private final String AUTHOR_TEST = "Khlebnikov Evgeniy"; + private final String DATE_STRING_TEST = "15/08/2021"; + private final int VERSION_TEST = 1; + private final String DESCRIPTION_TEST = "Class for testing annotations"; + + @Test + void testAnnotation() { + + assertEquals(AUTHOR_TEST, CustomAnnotationTestClass.class.getAnnotation(ClassInfoCustomAnnotation.class).author()); + assertEquals(DATE_STRING_TEST, CustomAnnotationTestClass.class.getAnnotation(ClassInfoCustomAnnotation.class).date()); + assertEquals(VERSION_TEST, CustomAnnotationTestClass.class.getAnnotation(ClassInfoCustomAnnotation.class).version()); + assertEquals(DESCRIPTION_TEST, CustomAnnotationTestClass.class.getAnnotation(ClassInfoCustomAnnotation.class).description()); + + } +} \ No newline at end of file diff --git a/src/test/java/homework_4/custom_annotation/FilePathCustomAnnotationTest.java b/src/test/java/homework_4/custom_annotation/FilePathCustomAnnotationTest.java new file mode 100644 index 00000000..77e7116c --- /dev/null +++ b/src/test/java/homework_4/custom_annotation/FilePathCustomAnnotationTest.java @@ -0,0 +1,18 @@ +package homework_4.custom_annotation; + +import base.UnitBase; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class FilePathCustomAnnotationTest extends UnitBase { + + public final String DEFAULT_FILE_PATH_TEST = "./src/main/resources/custom_file_reader/testFile.txt"; + + @Test + void testAnnotation() { + + assertEquals( DEFAULT_FILE_PATH_TEST, CustomAnnotationTestClass.class.getAnnotation(FilePathCustomAnnotation.class).path()); + + } +} \ No newline at end of file diff --git a/src/test/java/homework_4/singleton/SingletonTest.java b/src/test/java/homework_4/singleton/SingletonTest.java index 37c72392..730f2556 100644 --- a/src/test/java/homework_4/singleton/SingletonTest.java +++ b/src/test/java/homework_4/singleton/SingletonTest.java @@ -1,6 +1,7 @@ package homework_4.singleton; import base.UnitBase; +import homework_4.singleton.Singleton; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; From 982e7c0b4ed8a3e54e85cb638e691116764e92ae Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Sun, 15 Aug 2021 20:46:25 +0300 Subject: [PATCH 34/56] HW4 update --- .../custom_annotation/FilePathCustomAnnotationTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/homework_4/custom_annotation/FilePathCustomAnnotationTest.java b/src/test/java/homework_4/custom_annotation/FilePathCustomAnnotationTest.java index 77e7116c..442891e6 100644 --- a/src/test/java/homework_4/custom_annotation/FilePathCustomAnnotationTest.java +++ b/src/test/java/homework_4/custom_annotation/FilePathCustomAnnotationTest.java @@ -7,7 +7,7 @@ public class FilePathCustomAnnotationTest extends UnitBase { - public final String DEFAULT_FILE_PATH_TEST = "./src/main/resources/custom_file_reader/testFile.txt"; + private final String DEFAULT_FILE_PATH_TEST = "./src/main/resources/custom_file_reader/testFile.txt"; @Test void testAnnotation() { From 895e0ebc128a1fc77c05543e822800cc90590824 Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Mon, 16 Aug 2021 00:08:40 +0300 Subject: [PATCH 35/56] HW4 update (CustomFileReader) --- ...foCustomAnnotation.java => ClassInfo.java} | 4 +- .../CustomAnnotationTestClass.java | 2 +- ...oCustomAnnotation.java => MethodInfo.java} | 5 +- .../custom_file_reader/CustomFileReader.java | 83 +++++++++++++++++++ .../homework_4/custom_file_reader/Main.java | 13 +++ .../custom_file_reader/Messages.java | 7 ++ .../resources/custom_file_reader/testFile | 0 ...AnnotationTest.java => ClassInfoTest.java} | 10 +-- 8 files changed, 113 insertions(+), 11 deletions(-) rename src/main/java/homework_4/custom_annotation/{ClassInfoCustomAnnotation.java => ClassInfo.java} (81%) rename src/main/java/homework_4/custom_annotation/{MethodInfoCustomAnnotation.java => MethodInfo.java} (76%) create mode 100644 src/main/java/homework_4/custom_file_reader/CustomFileReader.java create mode 100644 src/main/java/homework_4/custom_file_reader/Main.java create mode 100644 src/main/java/homework_4/custom_file_reader/Messages.java create mode 100644 src/main/resources/custom_file_reader/testFile rename src/test/java/homework_4/custom_annotation/{ClassInfoCustomAnnotationTest.java => ClassInfoTest.java} (68%) diff --git a/src/main/java/homework_4/custom_annotation/ClassInfoCustomAnnotation.java b/src/main/java/homework_4/custom_annotation/ClassInfo.java similarity index 81% rename from src/main/java/homework_4/custom_annotation/ClassInfoCustomAnnotation.java rename to src/main/java/homework_4/custom_annotation/ClassInfo.java index 25d2b3d2..2650800f 100644 --- a/src/main/java/homework_4/custom_annotation/ClassInfoCustomAnnotation.java +++ b/src/main/java/homework_4/custom_annotation/ClassInfo.java @@ -9,9 +9,9 @@ @Target(ElementType.TYPE) @Inherited @Retention(RetentionPolicy.RUNTIME) -public @interface ClassInfoCustomAnnotation { +public @interface ClassInfo { String author() default "Khlebnikov Evgeniy"; String date(); int version() default 1; - String description(); + String specification(); } diff --git a/src/main/java/homework_4/custom_annotation/CustomAnnotationTestClass.java b/src/main/java/homework_4/custom_annotation/CustomAnnotationTestClass.java index f196c64d..5e0ddb5d 100644 --- a/src/main/java/homework_4/custom_annotation/CustomAnnotationTestClass.java +++ b/src/main/java/homework_4/custom_annotation/CustomAnnotationTestClass.java @@ -1,7 +1,7 @@ package homework_4.custom_annotation; @FilePathCustomAnnotation -@ClassInfoCustomAnnotation(date = "15/08/2021", description = "Class for testing annotations") +@ClassInfo(date = "15/08/2021", specification = "Class for testing annotations") public class CustomAnnotationTestClass { protected static final String FILE_PATH = CustomAnnotationTestClass.class.getAnnotation(FilePathCustomAnnotation.class).path(); diff --git a/src/main/java/homework_4/custom_annotation/MethodInfoCustomAnnotation.java b/src/main/java/homework_4/custom_annotation/MethodInfo.java similarity index 76% rename from src/main/java/homework_4/custom_annotation/MethodInfoCustomAnnotation.java rename to src/main/java/homework_4/custom_annotation/MethodInfo.java index 6d23140b..55f03162 100644 --- a/src/main/java/homework_4/custom_annotation/MethodInfoCustomAnnotation.java +++ b/src/main/java/homework_4/custom_annotation/MethodInfo.java @@ -6,10 +6,9 @@ @Target({ElementType.METHOD}) @Inherited @Retention(RetentionPolicy.RUNTIME) -public @interface MethodInfoCustomAnnotation { +public @interface MethodInfo { String author() default "Khlebnikov Evgeniy"; - String date(); int version() default 1; boolean suppressExceptions() default false; - String comments(); + String description(); } \ No newline at end of file diff --git a/src/main/java/homework_4/custom_file_reader/CustomFileReader.java b/src/main/java/homework_4/custom_file_reader/CustomFileReader.java new file mode 100644 index 00000000..eedf1ccd --- /dev/null +++ b/src/main/java/homework_4/custom_file_reader/CustomFileReader.java @@ -0,0 +1,83 @@ +package homework_4.custom_file_reader; + +import homework_4.custom_annotation.ClassInfo; +import homework_4.custom_annotation.FilePathCustomAnnotation; +import homework_4.custom_annotation.MethodInfo; + +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Scanner; + + +@ClassInfo(date = "15/08/2021", specification = "Customized FileReader Class") +@FilePathCustomAnnotation +public class CustomFileReader { + + private String filePath = CustomFileReader.class.getAnnotation(FilePathCustomAnnotation.class).path(); + + @MethodInfo(description = "NIO2") + public void run1() { + Path path = Paths.get(filePath); + try (BufferedReader bufferedReader = Files.newBufferedReader(path)) { + String result = bufferedReader.readLine(); + printResult(result); + } catch (IOException e) { + e.printStackTrace(); + printErrorMessage(); + } + } + + @MethodInfo(description = "BufferedReaderFileReader") + public void run2() { + try (BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath))) { + String result = bufferedReader.readLine(); + printResult(result); + } catch (IOException e) { + printErrorMessage(); + e.printStackTrace(); + } + } + + @MethodInfo(description = "InputStreamFileReader") + public void run3() { + try (InputStream inputStream = CustomFileReader.class.getResourceAsStream(filePath)) { + if (inputStream != null) { + try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { + String result = reader.readLine(); + printResult(result); + } catch (IOException e) { + e.printStackTrace(); + } + } + } catch (IOException e) { + printErrorMessage(); + e.printStackTrace(); + } + } + + @MethodInfo(description = "ScannerFileReader") + public void run4() { + try (Scanner scanner = new Scanner(new File(filePath))) { + scanner.useDelimiter("[.,]"); + StringBuilder stringBuilder = new StringBuilder(); + while (scanner.hasNext()) { + stringBuilder.append(scanner.next()); + } + System.out.println(stringBuilder); + } catch (FileNotFoundException e) { + printErrorMessage(); + e.printStackTrace(); + } + } + + public void printResult(String result) { + System.out.println(result.replaceAll("[.,]", "")); + } + + private void printErrorMessage() { + System.out.println(Messages.FILE_NOT_FOUND_MESSAGE); + } + +} diff --git a/src/main/java/homework_4/custom_file_reader/Main.java b/src/main/java/homework_4/custom_file_reader/Main.java new file mode 100644 index 00000000..d7659aab --- /dev/null +++ b/src/main/java/homework_4/custom_file_reader/Main.java @@ -0,0 +1,13 @@ +package homework_4.custom_file_reader; + +public class Main { + + public static void main(String[] args) { + + new CustomFileReader().run1(); + new CustomFileReader().run2(); + new CustomFileReader().run3(); + new CustomFileReader().run4(); + + } +} diff --git a/src/main/java/homework_4/custom_file_reader/Messages.java b/src/main/java/homework_4/custom_file_reader/Messages.java new file mode 100644 index 00000000..12e719df --- /dev/null +++ b/src/main/java/homework_4/custom_file_reader/Messages.java @@ -0,0 +1,7 @@ +package homework_4.custom_file_reader; + +public class Messages { + + public static final String FILE_NOT_FOUND_MESSAGE = "File not found. Incorrect path"; + +} diff --git a/src/main/resources/custom_file_reader/testFile b/src/main/resources/custom_file_reader/testFile new file mode 100644 index 00000000..e69de29b diff --git a/src/test/java/homework_4/custom_annotation/ClassInfoCustomAnnotationTest.java b/src/test/java/homework_4/custom_annotation/ClassInfoTest.java similarity index 68% rename from src/test/java/homework_4/custom_annotation/ClassInfoCustomAnnotationTest.java rename to src/test/java/homework_4/custom_annotation/ClassInfoTest.java index 53a3fd9d..454b8c3b 100644 --- a/src/test/java/homework_4/custom_annotation/ClassInfoCustomAnnotationTest.java +++ b/src/test/java/homework_4/custom_annotation/ClassInfoTest.java @@ -5,7 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; -public class ClassInfoCustomAnnotationTest extends UnitBase { +public class ClassInfoTest extends UnitBase { private final String AUTHOR_TEST = "Khlebnikov Evgeniy"; private final String DATE_STRING_TEST = "15/08/2021"; @@ -15,10 +15,10 @@ public class ClassInfoCustomAnnotationTest extends UnitBase { @Test void testAnnotation() { - assertEquals(AUTHOR_TEST, CustomAnnotationTestClass.class.getAnnotation(ClassInfoCustomAnnotation.class).author()); - assertEquals(DATE_STRING_TEST, CustomAnnotationTestClass.class.getAnnotation(ClassInfoCustomAnnotation.class).date()); - assertEquals(VERSION_TEST, CustomAnnotationTestClass.class.getAnnotation(ClassInfoCustomAnnotation.class).version()); - assertEquals(DESCRIPTION_TEST, CustomAnnotationTestClass.class.getAnnotation(ClassInfoCustomAnnotation.class).description()); + assertEquals(AUTHOR_TEST, CustomAnnotationTestClass.class.getAnnotation(ClassInfo.class).author()); + assertEquals(DATE_STRING_TEST, CustomAnnotationTestClass.class.getAnnotation(ClassInfo.class).date()); + assertEquals(VERSION_TEST, CustomAnnotationTestClass.class.getAnnotation(ClassInfo.class).version()); + assertEquals(DESCRIPTION_TEST, CustomAnnotationTestClass.class.getAnnotation(ClassInfo.class).specification()); } } \ No newline at end of file From 8e0250d8a9b740571762b59df2aeceb5661a686d Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Mon, 16 Aug 2021 00:38:35 +0300 Subject: [PATCH 36/56] HW4 update (CustomFileReader) --- .../FilePathCustomAnnotation.java | 2 +- .../custom_file_reader/Messages.java | 2 +- .../CustomFileReaderTest.java | 37 +++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 src/test/java/homework_4/custom_file_reader/CustomFileReaderTest.java diff --git a/src/main/java/homework_4/custom_annotation/FilePathCustomAnnotation.java b/src/main/java/homework_4/custom_annotation/FilePathCustomAnnotation.java index 59679bac..1343dc4d 100644 --- a/src/main/java/homework_4/custom_annotation/FilePathCustomAnnotation.java +++ b/src/main/java/homework_4/custom_annotation/FilePathCustomAnnotation.java @@ -8,5 +8,5 @@ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface FilePathCustomAnnotation { - String path() default "./src/main/resources/custom_file_reader/testFile.txt"; + String path() default "/src/main/resources/custom_file_reader/testFile.txt"; } \ No newline at end of file diff --git a/src/main/java/homework_4/custom_file_reader/Messages.java b/src/main/java/homework_4/custom_file_reader/Messages.java index 12e719df..5db40e30 100644 --- a/src/main/java/homework_4/custom_file_reader/Messages.java +++ b/src/main/java/homework_4/custom_file_reader/Messages.java @@ -2,6 +2,6 @@ public class Messages { - public static final String FILE_NOT_FOUND_MESSAGE = "File not found. Incorrect path"; + static final String FILE_NOT_FOUND_MESSAGE = "File not found. Incorrect path"; } diff --git a/src/test/java/homework_4/custom_file_reader/CustomFileReaderTest.java b/src/test/java/homework_4/custom_file_reader/CustomFileReaderTest.java new file mode 100644 index 00000000..a74db7a6 --- /dev/null +++ b/src/test/java/homework_4/custom_file_reader/CustomFileReaderTest.java @@ -0,0 +1,37 @@ +package homework_4.custom_file_reader; + +import base.UnitBase; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class CustomFileReaderTest extends UnitBase { + + CustomFileReader reader = new CustomFileReader(); + private final String stringTest = "CustomFileReader test passed"; + + @Test + void testNIO2() { + reader.run1(); + assertTrue(getOutput().contains(stringTest)); + } + + @Test + void testBufferedReaderFileReader() { + reader.run2(); + assertTrue(getOutput().contains(stringTest)); + } + + @Test + void testInputStreamFileReader() { + reader.run3(); + assertTrue(getOutput().contains(stringTest)); + } + + @Test + void testScannerFileReader() { + reader.run4(); + assertTrue(getOutput().contains(stringTest)); + } + +} \ No newline at end of file From 24c987b6815eb46b7f8e2b177f8ff8aac5f86346 Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Mon, 16 Aug 2021 00:52:19 +0300 Subject: [PATCH 37/56] HW4 README update --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0182302f..a83e0858 100644 --- a/README.md +++ b/README.md @@ -12,9 +12,12 @@ | HW2 - 3 | [Random Chars Table](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_2/random_chars_table) | The app that reads length, width, strategy from the console generates the table and prints even/odd letters | | HW2 - 3 Test | [Random Chars Table Test](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/test/java/homework_2/random_chars_table) | Random Chars Table Test | | HW3 | [ImmutableClass](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_3) | The Immutable Class An immutable class is created. Requirements for an immutable class have been added to a separate * .tht file in the homework_3 package.| +| HW4 - 1 | [ CustomFileReader](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_4/custom_file_reader) | CustomFileReader Class | +| HW4 - 1 Test | [ CustomFileReader Test](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/test/java/homework_4/custom_file_reader) | CustomFileReader Class Test | | HW4 - 2 | [Singleton](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_4/singleton) | Singleton Class. The Singleton class (thread-safe version) is created. Double checked locking was used in order to check whether the variable has already been initialized, if the another thread acquired the lock first, it may have already done the initialization. | | HW4 - 2 Test | [Singleton Test](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/test/java/homework_4/singleton) | Singleton Class Test. | -| HW4 - 3 | [Custom annotation](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_4/custom_annotation) | Custom annotation. | +| HW4 - 3 | [Custom Annotation](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_4/custom_annotation) | Custom annotation. | +| HW4 - 3 Test| [Custom Annotation Test](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/test/java/homework_4/custom_annotation) | Custom Annotation Test. | | Title | Link | Short description From ab5e1f2eb43eb75ea4f3f6a9513e746cf7049f16 Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Mon, 16 Aug 2021 15:01:31 +0300 Subject: [PATCH 38/56] HW4 README update --- .../custom_annotation/FilePathCustomAnnotation.java | 2 +- .../custom_file_reader/CustomFileReader.java | 11 ++++------- src/main/resources/custom_file_reader/testFile.txt | 1 + 3 files changed, 6 insertions(+), 8 deletions(-) create mode 100644 src/main/resources/custom_file_reader/testFile.txt diff --git a/src/main/java/homework_4/custom_annotation/FilePathCustomAnnotation.java b/src/main/java/homework_4/custom_annotation/FilePathCustomAnnotation.java index 1343dc4d..663d437a 100644 --- a/src/main/java/homework_4/custom_annotation/FilePathCustomAnnotation.java +++ b/src/main/java/homework_4/custom_annotation/FilePathCustomAnnotation.java @@ -8,5 +8,5 @@ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface FilePathCustomAnnotation { - String path() default "/src/main/resources/custom_file_reader/testFile.txt"; + String path() default "C:\\Users\\admin\\IdeaProjects\\Java_Core_June_2021\\src\\main\\resources\\custom_file_reader\\testFile.txt"; } \ No newline at end of file diff --git a/src/main/java/homework_4/custom_file_reader/CustomFileReader.java b/src/main/java/homework_4/custom_file_reader/CustomFileReader.java index eedf1ccd..39b4fed5 100644 --- a/src/main/java/homework_4/custom_file_reader/CustomFileReader.java +++ b/src/main/java/homework_4/custom_file_reader/CustomFileReader.java @@ -3,20 +3,17 @@ import homework_4.custom_annotation.ClassInfo; import homework_4.custom_annotation.FilePathCustomAnnotation; import homework_4.custom_annotation.MethodInfo; - import java.io.*; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Scanner; - @ClassInfo(date = "15/08/2021", specification = "Customized FileReader Class") @FilePathCustomAnnotation -public class CustomFileReader { +public class CustomFileReader { private String filePath = CustomFileReader.class.getAnnotation(FilePathCustomAnnotation.class).path(); - @MethodInfo(description = "NIO2") public void run1() { Path path = Paths.get(filePath); @@ -24,11 +21,11 @@ public void run1() { String result = bufferedReader.readLine(); printResult(result); } catch (IOException e) { - e.printStackTrace(); + System.out.println("NIO2"); printErrorMessage(); + e.printStackTrace(); } } - @MethodInfo(description = "BufferedReaderFileReader") public void run2() { try (BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath))) { @@ -39,7 +36,6 @@ public void run2() { e.printStackTrace(); } } - @MethodInfo(description = "InputStreamFileReader") public void run3() { try (InputStream inputStream = CustomFileReader.class.getResourceAsStream(filePath)) { @@ -72,6 +68,7 @@ public void run4() { } } + public void printResult(String result) { System.out.println(result.replaceAll("[.,]", "")); } diff --git a/src/main/resources/custom_file_reader/testFile.txt b/src/main/resources/custom_file_reader/testFile.txt new file mode 100644 index 00000000..4ea53c2b --- /dev/null +++ b/src/main/resources/custom_file_reader/testFile.txt @@ -0,0 +1 @@ +, Test , ,,file \ No newline at end of file From 0c184a89b6d0a287838ba8de04b3e2d20d6e178e Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Mon, 16 Aug 2021 15:03:12 +0300 Subject: [PATCH 39/56] HW4 README update --- testFile.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 testFile.txt diff --git a/testFile.txt b/testFile.txt new file mode 100644 index 00000000..79c09680 --- /dev/null +++ b/testFile.txt @@ -0,0 +1 @@ +, , Test ,,, file , \ No newline at end of file From db4fc4b38d91427b37185f157869e5473795b66f Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Wed, 18 Aug 2021 11:58:59 +0300 Subject: [PATCH 40/56] HW4 README update --- .../homework_4/custom_file_reader/CustomFileReaderTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/homework_4/custom_file_reader/CustomFileReaderTest.java b/src/test/java/homework_4/custom_file_reader/CustomFileReaderTest.java index a74db7a6..d1b38d37 100644 --- a/src/test/java/homework_4/custom_file_reader/CustomFileReaderTest.java +++ b/src/test/java/homework_4/custom_file_reader/CustomFileReaderTest.java @@ -8,7 +8,7 @@ class CustomFileReaderTest extends UnitBase { CustomFileReader reader = new CustomFileReader(); - private final String stringTest = "CustomFileReader test passed"; + private final String stringTest = "Test file"; @Test void testNIO2() { From 3f05b67e437cbd4470c27753fc4eecabbe55856b Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Wed, 18 Aug 2021 12:19:42 +0300 Subject: [PATCH 41/56] HW4 README update --- .../custom_file_reader/CustomFileReader.java | 18 +----------------- .../homework_4/custom_file_reader/Main.java | 1 - .../CustomFileReaderTest.java | 8 +------- 3 files changed, 2 insertions(+), 25 deletions(-) diff --git a/src/main/java/homework_4/custom_file_reader/CustomFileReader.java b/src/main/java/homework_4/custom_file_reader/CustomFileReader.java index 39b4fed5..748f705e 100644 --- a/src/main/java/homework_4/custom_file_reader/CustomFileReader.java +++ b/src/main/java/homework_4/custom_file_reader/CustomFileReader.java @@ -36,25 +36,9 @@ public void run2() { e.printStackTrace(); } } - @MethodInfo(description = "InputStreamFileReader") - public void run3() { - try (InputStream inputStream = CustomFileReader.class.getResourceAsStream(filePath)) { - if (inputStream != null) { - try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { - String result = reader.readLine(); - printResult(result); - } catch (IOException e) { - e.printStackTrace(); - } - } - } catch (IOException e) { - printErrorMessage(); - e.printStackTrace(); - } - } @MethodInfo(description = "ScannerFileReader") - public void run4() { + public void run3() { try (Scanner scanner = new Scanner(new File(filePath))) { scanner.useDelimiter("[.,]"); StringBuilder stringBuilder = new StringBuilder(); diff --git a/src/main/java/homework_4/custom_file_reader/Main.java b/src/main/java/homework_4/custom_file_reader/Main.java index d7659aab..8f4b2b91 100644 --- a/src/main/java/homework_4/custom_file_reader/Main.java +++ b/src/main/java/homework_4/custom_file_reader/Main.java @@ -7,7 +7,6 @@ public static void main(String[] args) { new CustomFileReader().run1(); new CustomFileReader().run2(); new CustomFileReader().run3(); - new CustomFileReader().run4(); } } diff --git a/src/test/java/homework_4/custom_file_reader/CustomFileReaderTest.java b/src/test/java/homework_4/custom_file_reader/CustomFileReaderTest.java index d1b38d37..87ad7db1 100644 --- a/src/test/java/homework_4/custom_file_reader/CustomFileReaderTest.java +++ b/src/test/java/homework_4/custom_file_reader/CustomFileReaderTest.java @@ -22,15 +22,9 @@ void testBufferedReaderFileReader() { assertTrue(getOutput().contains(stringTest)); } - @Test - void testInputStreamFileReader() { - reader.run3(); - assertTrue(getOutput().contains(stringTest)); - } - @Test void testScannerFileReader() { - reader.run4(); + reader.run3(); assertTrue(getOutput().contains(stringTest)); } From a62d0144c060fe010866577a4d5030bf526cef0b Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Tue, 21 Sep 2021 22:06:51 +0300 Subject: [PATCH 42/56] HW7 --- .../java/homework_7/KittenToCatFunction.java | 11 +++++++++++ src/main/java/homework_7/Main.java | 19 +++++++++++++++++++ src/main/java/homework_7/pets/Cat.java | 13 +++++++++++++ src/main/java/homework_7/pets/Kitten.java | 13 +++++++++++++ 4 files changed, 56 insertions(+) create mode 100644 src/main/java/homework_7/KittenToCatFunction.java create mode 100644 src/main/java/homework_7/Main.java create mode 100644 src/main/java/homework_7/pets/Cat.java create mode 100644 src/main/java/homework_7/pets/Kitten.java diff --git a/src/main/java/homework_7/KittenToCatFunction.java b/src/main/java/homework_7/KittenToCatFunction.java new file mode 100644 index 00000000..65e5277b --- /dev/null +++ b/src/main/java/homework_7/KittenToCatFunction.java @@ -0,0 +1,11 @@ +package homework_7; + +import homework_7.pets.Cat; +import homework_7.pets.Kitten; + +@FunctionalInterface +public interface KittenToCatFunction { + + R grow(T t); + +} \ No newline at end of file diff --git a/src/main/java/homework_7/Main.java b/src/main/java/homework_7/Main.java new file mode 100644 index 00000000..cf48d293 --- /dev/null +++ b/src/main/java/homework_7/Main.java @@ -0,0 +1,19 @@ +package homework_7; + +import homework_7.pets.Cat; +import homework_7.pets.Kitten; + +public class Main { + public static void main(String[] args) { + + Kitten kitten = new Kitten("Lilac", 1, 1.5); + System.out.println(kitten); + + KittenToCatFunction function = (k -> new Cat(k.getColor(), + k.getAgeInMonth() + 11, k.getWeight() * 2.5)); + + Cat cat = function.grow(kitten); + System.out.println(cat); + + } +} \ No newline at end of file diff --git a/src/main/java/homework_7/pets/Cat.java b/src/main/java/homework_7/pets/Cat.java new file mode 100644 index 00000000..2a08ef7a --- /dev/null +++ b/src/main/java/homework_7/pets/Cat.java @@ -0,0 +1,13 @@ +package homework_7.pets; + +import lombok.*; + +@Data +@AllArgsConstructor +public class Cat { + + private String color; + private int ageInMonth; + private double weight; + +} \ No newline at end of file diff --git a/src/main/java/homework_7/pets/Kitten.java b/src/main/java/homework_7/pets/Kitten.java new file mode 100644 index 00000000..6ff5aa7e --- /dev/null +++ b/src/main/java/homework_7/pets/Kitten.java @@ -0,0 +1,13 @@ +package homework_7.pets; + +import lombok.*; + +@Data +@AllArgsConstructor +public class Kitten { + + private String color; + private int ageInMonth; + private double weight; + +} \ No newline at end of file From e2de6c5b60b3546c2a489520046e5e14703adc8b Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Tue, 21 Sep 2021 22:10:01 +0300 Subject: [PATCH 43/56] HW7 README Update --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index a83e0858..f8f45ab8 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ | HW4 - 2 Test | [Singleton Test](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/test/java/homework_4/singleton) | Singleton Class Test. | | HW4 - 3 | [Custom Annotation](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_4/custom_annotation) | Custom annotation. | | HW4 - 3 Test| [Custom Annotation Test](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/test/java/homework_4/custom_annotation) | Custom Annotation Test. | +| HW7| [KittenToCatFunction](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_7) | KittenToCatFunction. | | Title | Link | Short description From 8e02ed926d8fac37d12a769f1deda75411174962 Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Tue, 21 Sep 2021 22:30:13 +0300 Subject: [PATCH 44/56] HW6 --- .../MapProblemsGenerator.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/main/java/homework_6/map_problems_generator/MapProblemsGenerator.java diff --git a/src/main/java/homework_6/map_problems_generator/MapProblemsGenerator.java b/src/main/java/homework_6/map_problems_generator/MapProblemsGenerator.java new file mode 100644 index 00000000..0aa49a17 --- /dev/null +++ b/src/main/java/homework_6/map_problems_generator/MapProblemsGenerator.java @@ -0,0 +1,34 @@ +package homework_6.map_problems_generator; + +import lombok.*; +import java.util.Objects; + +public class MapProblemsGenerator { + + @Data + @AllArgsConstructor + public static class MapProblemsCollisionGenerator { + private int number; + private String name; + + @Override + public boolean equals(Object object) { + if (this == object) return true; + if (object == null || getClass() != object.getClass()) return false; + MapProblemsCollisionGenerator mpcg = (MapProblemsCollisionGenerator) object; + return number == mpcg.number && Objects.equals(name, mpcg.name); + } + + @Override + public int hashCode() { + return 1; + } + } + + @Data + @AllArgsConstructor + public static class MapProblemsMutableGenerator { + private int number; + private String name; + } +} \ No newline at end of file From 4770f7ee34b2c5ed4fe63e8d37aeffc6ad8f63d5 Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Tue, 21 Sep 2021 22:34:40 +0300 Subject: [PATCH 45/56] HW6 README Update --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f8f45ab8..ddab0743 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ | HW4 - 2 Test | [Singleton Test](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/test/java/homework_4/singleton) | Singleton Class Test. | | HW4 - 3 | [Custom Annotation](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_4/custom_annotation) | Custom annotation. | | HW4 - 3 Test| [Custom Annotation Test](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/test/java/homework_4/custom_annotation) | Custom Annotation Test. | +| HW6| [MapProblemsGenerator](https://github.com/NikolaevArtem/Java_Core_June_2021/blob/feature/KhlebnikovEvgeniy/src/main/java/homework_6/map_problems_generator) | MapProblemsGenerator. | | HW7| [KittenToCatFunction](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_7) | KittenToCatFunction. | From bea502910e6db86bc92ee6a535b7178cb7f6f512 Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Wed, 22 Sep 2021 00:07:19 +0300 Subject: [PATCH 46/56] HW5 --- .../CustomRegexMatcher.java | 32 +++++++++ .../homework_5/custom_regex_matcher/Main.java | 10 +++ .../java/homework_5/power_of_number/Main.java | 9 +++ .../power_of_number/PowerOfNumber.java | 67 +++++++++++++++++++ 4 files changed, 118 insertions(+) create mode 100644 src/main/java/homework_5/custom_regex_matcher/CustomRegexMatcher.java create mode 100644 src/main/java/homework_5/custom_regex_matcher/Main.java create mode 100644 src/main/java/homework_5/power_of_number/Main.java create mode 100644 src/main/java/homework_5/power_of_number/PowerOfNumber.java diff --git a/src/main/java/homework_5/custom_regex_matcher/CustomRegexMatcher.java b/src/main/java/homework_5/custom_regex_matcher/CustomRegexMatcher.java new file mode 100644 index 00000000..15b54f45 --- /dev/null +++ b/src/main/java/homework_5/custom_regex_matcher/CustomRegexMatcher.java @@ -0,0 +1,32 @@ +package homework_5.custom_regex_matcher; + +import java.util.regex.Pattern; +import static java.lang.System.lineSeparator; + +public class CustomRegexMatcher { + + private static final String CUSTOM_REGEX_FOR_URL = "\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]"; + + public void run(String[] args) { + + if (args.length < 1) { + + printMessage("No match"); + + } + + for (String s : args) { + printMessage(isMatch(s) + lineSeparator()); + } + + } + + private boolean isMatch(String string) { + return Pattern.matches(CUSTOM_REGEX_FOR_URL, string); + } + + private void printMessage(String message) { + System.out.print(message); + } + +} \ No newline at end of file diff --git a/src/main/java/homework_5/custom_regex_matcher/Main.java b/src/main/java/homework_5/custom_regex_matcher/Main.java new file mode 100644 index 00000000..703ea1b2 --- /dev/null +++ b/src/main/java/homework_5/custom_regex_matcher/Main.java @@ -0,0 +1,10 @@ +package homework_5.custom_regex_matcher; + +public class Main { + + public static void main(String[] args) { + + new CustomRegexMatcher().run(args); + + } +} \ No newline at end of file diff --git a/src/main/java/homework_5/power_of_number/Main.java b/src/main/java/homework_5/power_of_number/Main.java new file mode 100644 index 00000000..64417702 --- /dev/null +++ b/src/main/java/homework_5/power_of_number/Main.java @@ -0,0 +1,9 @@ +package homework_5.power_of_number; + +public class Main { + + public static void main(String[] args) { + new PowerOfNumber().run(); + } + +} \ No newline at end of file diff --git a/src/main/java/homework_5/power_of_number/PowerOfNumber.java b/src/main/java/homework_5/power_of_number/PowerOfNumber.java new file mode 100644 index 00000000..50494b94 --- /dev/null +++ b/src/main/java/homework_5/power_of_number/PowerOfNumber.java @@ -0,0 +1,67 @@ +package homework_5.power_of_number; + +import java.math.BigInteger; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + + +public class PowerOfNumber { + + private int number; + private int power; + private BigInteger powerOfNumber; + + public static final String ERROR_MESSAGE; + public static final String ENTER_MESSAGE; + + static { + ERROR_MESSAGE = "Only 2 non-negative integers are allowed"; + ENTER_MESSAGE = "Enter 2 positive integer numbers: "; + } + + protected void run() { + try { + readConsole(); + if (power == 0) { + powerOfNumber = BigInteger.ONE; + } else if (number == 0) { + powerOfNumber = BigInteger.ZERO; + } else { + powerOfNumber = BigInteger.valueOf(number); + riseNumberToPower(power); + } + System.out.printf("The number %s raised to the power %s equals: %s", number, power, powerOfNumber); + } catch (IOException | NullPointerException e) { + System.out.println(ERROR_MESSAGE); + } + + } + + private void riseNumberToPower(int power) { + + if (power != 1) { + powerOfNumber = powerOfNumber.multiply(BigInteger.valueOf(number)); + power--; + riseNumberToPower(power); + } + + } + + private void readConsole() throws IOException, NullPointerException { + + System.out.print(ENTER_MESSAGE); + try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in))) { + String inputString = bufferedReader.readLine(); + + if (inputString.matches("^\\d+\\s\\d+$")) { + String[] argsArray = inputString.split("\\s+"); + number = Integer.parseInt(argsArray[0]); + power = Integer.parseInt(argsArray[1]); + } else { + System.out.println(ERROR_MESSAGE); + } + } + } + +} \ No newline at end of file From 9e01891c7623ab45ef070e4155e39e2ce7822af1 Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Wed, 22 Sep 2021 00:14:04 +0300 Subject: [PATCH 47/56] HW5 README Update --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index ddab0743..2a1e43e1 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,8 @@ | HW4 - 2 Test | [Singleton Test](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/test/java/homework_4/singleton) | Singleton Class Test. | | HW4 - 3 | [Custom Annotation](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_4/custom_annotation) | Custom annotation. | | HW4 - 3 Test| [Custom Annotation Test](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/test/java/homework_4/custom_annotation) | Custom Annotation Test. | +| HW5 - 1| [CustomRegexMatcher](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_5/custom_regex_matcher) | CustomRegexMatcher. | +| HW5 - 2| [PowerOfNumber](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_5/power_of_number) | PowerOfNumber. | | HW6| [MapProblemsGenerator](https://github.com/NikolaevArtem/Java_Core_June_2021/blob/feature/KhlebnikovEvgeniy/src/main/java/homework_6/map_problems_generator) | MapProblemsGenerator. | | HW7| [KittenToCatFunction](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_7) | KittenToCatFunction. | From 5be87e9ecfb5c2c32e9d515702c6cb34c23f95ba Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Wed, 22 Sep 2021 20:07:31 +0300 Subject: [PATCH 48/56] HW5 README Update --- build.gradle | 1 + 1 file changed, 1 insertion(+) diff --git a/build.gradle b/build.gradle index b91dc843..522c2e14 100644 --- a/build.gradle +++ b/build.gradle @@ -12,6 +12,7 @@ repositories { dependencies { testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0' + testCompile 'org.junit.jupiter:junit-jupiter-params:5.7.0' } test { From 8f4e0a7d6d596a342d11ecc5fef4fd414ed4bf37 Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Wed, 22 Sep 2021 20:23:41 +0300 Subject: [PATCH 49/56] HW5 README Update --- build.gradle | 1 - 1 file changed, 1 deletion(-) diff --git a/build.gradle b/build.gradle index 522c2e14..b91dc843 100644 --- a/build.gradle +++ b/build.gradle @@ -12,7 +12,6 @@ repositories { dependencies { testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0' - testCompile 'org.junit.jupiter:junit-jupiter-params:5.7.0' } test { From 104c358f6fcab1680be78de57f2519caa6c62ed8 Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Wed, 22 Sep 2021 20:46:21 +0300 Subject: [PATCH 50/56] HW5 PowerOfNumberTest --- .../power_of_number/PowerOfNumberTest.java | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 src/test/java/homework_5/power_of_number/PowerOfNumberTest.java diff --git a/src/test/java/homework_5/power_of_number/PowerOfNumberTest.java b/src/test/java/homework_5/power_of_number/PowerOfNumberTest.java new file mode 100644 index 00000000..4fed07c1 --- /dev/null +++ b/src/test/java/homework_5/power_of_number/PowerOfNumberTest.java @@ -0,0 +1,114 @@ +package homework_5.power_of_number; + +import base.UnitBase; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class PowerOfNumberTest extends UnitBase { + + PowerOfNumber powerOfNumber = new PowerOfNumber(); + + + @Test + void given2and3_whenRun_then8() { + setInput("2 3"); + powerOfNumber.run(); + removeFromOutput("Enter 2 positive integer numbers: "); + removeFromOutput("The number 2 raised to the power 3 equals: "); + assertEquals("8", getOutputLines()[0]); + } + + @Test + void give3and2_whenRun_then9() { + setInput("3 2"); + powerOfNumber.run(); + removeFromOutput("Enter 2 positive integer numbers: "); + removeFromOutput("The number 3 raised to the power 2 equals: "); + assertEquals("9", getOutputLines()[0]); + } + + @Test + void given0and0_whenRun_thenReturn() { + setInput("0 0"); + powerOfNumber.run(); + removeFromOutput("Enter 2 positive integer numbers: "); + removeFromOutput("The number 0 raised to the power 0 equals: "); + assertEquals("1", getOutputLines()[0]); + } + + @Test + void given0and3_whenRun_thenReturn() { + setInput("0 3"); + powerOfNumber.run(); + removeFromOutput("Enter 2 positive integer numbers: "); + removeFromOutput("The number 0 raised to the power 3 equals: "); + assertEquals("0", getOutputLines()[0]); + } + + @Test + void given3and0_whenRun_thenReturn() { + setInput("3 0"); + powerOfNumber.run(); + removeFromOutput("Enter 2 positive integer numbers: "); + removeFromOutput("The number 3 raised to the power 0 equals: "); + assertEquals("1", getOutputLines()[0]); + } + + @Test + void givenStringAndNumber_whenRun_thenError() { + setInput("two 2"); + powerOfNumber.run(); + removeFromOutput("Enter 2 positive integer numbers: "); + assertEquals("Only 2 non-negative integers are allowed", getOutputLines()[0]); + } + + @Test + void giveNegativeAndNumber_whenRun_thenError() { + setInput("-7 3"); + powerOfNumber.run(); + removeFromOutput("Enter 2 positive integer numbers: "); + assertEquals("Only 2 non-negative integers are allowed", getOutputLines()[0]); + } + + @Test + void givenNumberAndNegative_whenRun_thenError() { + setInput("4 -2"); + powerOfNumber.run(); + removeFromOutput("Enter 2 positive integer numbers: "); + assertEquals("Only 2 non-negative integers are allowed", getOutputLines()[0]); + } + + @Test + void givenNullString_whenRun_thenError() { + setInput(""); + powerOfNumber.run(); + removeFromOutput("Enter 2 positive integer numbers: "); + assertEquals("Only 2 non-negative integers are allowed", getOutputLines()[0]); + } + + @Test + void givenEmptyStting_whenRun_thenError() { + setInput(" "); + powerOfNumber.run(); + removeFromOutput("Enter 2 positive integer numbers: "); + assertEquals("Only 2 non-negative integers are allowed", getOutputLines()[0]); + } + + @Test + void givenLessParams_whenRun_thenError() { + setInput("5"); + powerOfNumber.run(); + removeFromOutput("Enter 2 positive integer numbers: "); + assertEquals("Only 2 non-negative integers are allowed", getOutputLines()[0]); + } + + @Test + void giveMoreParams_whenRun_thenError() { + setInput("5 4 5"); + powerOfNumber.run(); + removeFromOutput("Enter 2 positive integer numbers: "); + assertEquals("Only 2 non-negative integers are allowed", getOutputLines()[0]); + } + +} \ No newline at end of file From 899289f13362d64ebd0b8e3ed8c339c20223f58c Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Wed, 22 Sep 2021 20:50:43 +0300 Subject: [PATCH 51/56] HW5 PowerOfNumberTest --- .../java/homework_5/power_of_number/PowerOfNumberTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/homework_5/power_of_number/PowerOfNumberTest.java b/src/test/java/homework_5/power_of_number/PowerOfNumberTest.java index 4fed07c1..63b4f794 100644 --- a/src/test/java/homework_5/power_of_number/PowerOfNumberTest.java +++ b/src/test/java/homework_5/power_of_number/PowerOfNumberTest.java @@ -73,7 +73,7 @@ void giveNegativeAndNumber_whenRun_thenError() { @Test void givenNumberAndNegative_whenRun_thenError() { - setInput("4 -2"); + setInput("5 -2"); powerOfNumber.run(); removeFromOutput("Enter 2 positive integer numbers: "); assertEquals("Only 2 non-negative integers are allowed", getOutputLines()[0]); @@ -97,7 +97,7 @@ void givenEmptyStting_whenRun_thenError() { @Test void givenLessParams_whenRun_thenError() { - setInput("5"); + setInput("1"); powerOfNumber.run(); removeFromOutput("Enter 2 positive integer numbers: "); assertEquals("Only 2 non-negative integers are allowed", getOutputLines()[0]); @@ -105,7 +105,7 @@ void givenLessParams_whenRun_thenError() { @Test void giveMoreParams_whenRun_thenError() { - setInput("5 4 5"); + setInput("1 2 3"); powerOfNumber.run(); removeFromOutput("Enter 2 positive integer numbers: "); assertEquals("Only 2 non-negative integers are allowed", getOutputLines()[0]); From bf0d934ed88dc52ca29d0c074ba6c666c4e1faa4 Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Wed, 22 Sep 2021 23:03:15 +0300 Subject: [PATCH 52/56] HW5 CustomRegexMatcherTest --- .../CustomRegexMatcher.java | 39 +++++++++------ .../homework_5/custom_regex_matcher/Main.java | 2 +- .../CustomRegexMatcherTest.java | 47 +++++++++++++++++++ 3 files changed, 73 insertions(+), 15 deletions(-) create mode 100644 src/test/java/homework_5/custom_regex_matcher/CustomRegexMatcherTest.java diff --git a/src/main/java/homework_5/custom_regex_matcher/CustomRegexMatcher.java b/src/main/java/homework_5/custom_regex_matcher/CustomRegexMatcher.java index 15b54f45..00e54bda 100644 --- a/src/main/java/homework_5/custom_regex_matcher/CustomRegexMatcher.java +++ b/src/main/java/homework_5/custom_regex_matcher/CustomRegexMatcher.java @@ -1,32 +1,43 @@ package homework_5.custom_regex_matcher; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; import java.util.regex.Pattern; -import static java.lang.System.lineSeparator; -public class CustomRegexMatcher { +public class CustomRegexMatcher implements Runnable { - private static final String CUSTOM_REGEX_FOR_URL = "\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]"; + @Override + public void run() { + System.out.println("Enter an URL address"); + checkURL(); + } - public void run(String[] args) { + public static String CustomRegexForURL; - if (args.length < 1) { + static { + CustomRegexForURL = "\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]"; + } - printMessage("No match"); + private void checkURL() { - } + try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in))) { + String string = bufferedReader.readLine(); + if (checkURLMatch(string)) { + System.out.println("URL is valid"); + } else { + System.out.println("URL is not valid"); + } - for (String s : args) { - printMessage(isMatch(s) + lineSeparator()); + } catch (IOException e) { + e.printStackTrace(); } } - private boolean isMatch(String string) { - return Pattern.matches(CUSTOM_REGEX_FOR_URL, string); + private boolean checkURLMatch(String string) { + return Pattern.matches(CustomRegexForURL, string); } - private void printMessage(String message) { - System.out.print(message); - } } \ No newline at end of file diff --git a/src/main/java/homework_5/custom_regex_matcher/Main.java b/src/main/java/homework_5/custom_regex_matcher/Main.java index 703ea1b2..a4aeb0ca 100644 --- a/src/main/java/homework_5/custom_regex_matcher/Main.java +++ b/src/main/java/homework_5/custom_regex_matcher/Main.java @@ -4,7 +4,7 @@ public class Main { public static void main(String[] args) { - new CustomRegexMatcher().run(args); + new CustomRegexMatcher().run(); } } \ No newline at end of file diff --git a/src/test/java/homework_5/custom_regex_matcher/CustomRegexMatcherTest.java b/src/test/java/homework_5/custom_regex_matcher/CustomRegexMatcherTest.java new file mode 100644 index 00000000..9827544c --- /dev/null +++ b/src/test/java/homework_5/custom_regex_matcher/CustomRegexMatcherTest.java @@ -0,0 +1,47 @@ +package homework_5.custom_regex_matcher; + +import base.UnitBase; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class CustomRegexMatcherTest extends UnitBase { + + CustomRegexMatcher customRegexMatcher = new CustomRegexMatcher(); + + @Test + void givenRegexAndGoogle_whenRun_returnURLIsValid() { + setInput("https://www.google.com/"); + customRegexMatcher.run(); + assertEquals("URL is valid", getOutputLines()[1]); + } + + @Test + void givenRegexAndEpam_whenRun_returnURLIsValid() { + setInput("https://www.epam.com/careers"); + customRegexMatcher.run(); + assertEquals("URL is valid", getOutputLines()[1]); + } + + @Test + void givenRegexAndFPT_whenRun_returnURLIsValid() { + setInput("ftp://websiteaddress.com"); + customRegexMatcher.run(); + assertEquals("URL is valid", getOutputLines()[1]); + } + + @Test + void givenRegexAndNumber_whenRun_returnURLIsNotValid() { + setInput("123456789"); + customRegexMatcher.run(); + assertEquals("URL is not valid", getOutputLines()[1]); + } + + @Test + void givenRegexAndString_whenRun_returnURLIsNotValid() { + setInput("String"); + customRegexMatcher.run(); + assertEquals("URL is not valid", getOutputLines()[1]); + } + +} \ No newline at end of file From b34b2483cf28e86dcc48d0719218ff8f68172d1c Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Wed, 22 Sep 2021 23:07:29 +0300 Subject: [PATCH 53/56] HW5 CustomRegexMatcherTest --- .../java/homework_5/custom_regex_matcher/CustomRegexMatcher.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/homework_5/custom_regex_matcher/CustomRegexMatcher.java b/src/main/java/homework_5/custom_regex_matcher/CustomRegexMatcher.java index 00e54bda..56d4cf18 100644 --- a/src/main/java/homework_5/custom_regex_matcher/CustomRegexMatcher.java +++ b/src/main/java/homework_5/custom_regex_matcher/CustomRegexMatcher.java @@ -39,5 +39,4 @@ private boolean checkURLMatch(String string) { return Pattern.matches(CustomRegexForURL, string); } - } \ No newline at end of file From 30251198128aa2d1eb267b3604f56a74947da53d Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Wed, 22 Sep 2021 23:19:42 +0300 Subject: [PATCH 54/56] HW5 README Update --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 2a1e43e1..03483c77 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,9 @@ | HW4 - 3 | [Custom Annotation](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_4/custom_annotation) | Custom annotation. | | HW4 - 3 Test| [Custom Annotation Test](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/test/java/homework_4/custom_annotation) | Custom Annotation Test. | | HW5 - 1| [CustomRegexMatcher](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_5/custom_regex_matcher) | CustomRegexMatcher. | +| HW5 - 1 Test| [CustomRegexMatcherTest](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/test/java/homework_5/custom_regex_matcher) | CustomRegexMatcherTest. | | HW5 - 2| [PowerOfNumber](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_5/power_of_number) | PowerOfNumber. | +| HW5 - 2| [PowerOfNumberTest](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/test/java/homework_5/power_of_number) | PowerOfNumber. | | HW6| [MapProblemsGenerator](https://github.com/NikolaevArtem/Java_Core_June_2021/blob/feature/KhlebnikovEvgeniy/src/main/java/homework_6/map_problems_generator) | MapProblemsGenerator. | | HW7| [KittenToCatFunction](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KhlebnikovEvgeniy/src/main/java/homework_7) | KittenToCatFunction. | From 732ce4952c32e1b74070bd3c403f89b1c95f9494 Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Wed, 22 Sep 2021 23:22:06 +0300 Subject: [PATCH 55/56] Course Project --- src/main/java/course_project/Main.java | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/main/java/course_project/Main.java diff --git a/src/main/java/course_project/Main.java b/src/main/java/course_project/Main.java new file mode 100644 index 00000000..3c882990 --- /dev/null +++ b/src/main/java/course_project/Main.java @@ -0,0 +1,4 @@ +package course_project; + +public class Main { +} From 1219d139bcb2aed7f3c9f68b37a2c9b9d041d1f4 Mon Sep 17 00:00:00 2001 From: KhlebnikovEvgeniy Date: Fri, 24 Sep 2021 04:31:36 +0300 Subject: [PATCH 56/56] Course Project SEA BATTLE CONSOLE GAME --- src/main/java/course_project/Main.java | 10 +- .../course_project/engine/BattleShipGame.java | 10 + .../java/course_project/engine/GameMap.java | 434 ++++++++++++++++++ src/main/java/course_project/engine/Menu.java | 105 +++++ .../java/course_project/engine/Position.java | 19 + .../course_project/engine/SquareState.java | 20 + .../players/ComputerPlayer.java | 194 ++++++++ .../course_project/players/HumanPlayer.java | 247 ++++++++++ .../java/course_project/players/Player.java | 22 + .../java/course_project/ships/BattleShip.java | 8 + .../java/course_project/ships/Carrier.java | 8 + .../java/course_project/ships/Cruiser.java | 8 + .../java/course_project/ships/Destroyer.java | 9 + src/main/java/course_project/ships/Ship.java | 44 ++ .../java/course_project/ships/ShipSize.java | 19 + .../java/course_project/ships/ShipType.java | 20 + .../java/course_project/ships/Submarine.java | 8 + .../java/course_project/utils/FontColors.java | 34 ++ 18 files changed, 1218 insertions(+), 1 deletion(-) create mode 100644 src/main/java/course_project/engine/BattleShipGame.java create mode 100644 src/main/java/course_project/engine/GameMap.java create mode 100644 src/main/java/course_project/engine/Menu.java create mode 100644 src/main/java/course_project/engine/Position.java create mode 100644 src/main/java/course_project/engine/SquareState.java create mode 100644 src/main/java/course_project/players/ComputerPlayer.java create mode 100644 src/main/java/course_project/players/HumanPlayer.java create mode 100644 src/main/java/course_project/players/Player.java create mode 100644 src/main/java/course_project/ships/BattleShip.java create mode 100644 src/main/java/course_project/ships/Carrier.java create mode 100644 src/main/java/course_project/ships/Cruiser.java create mode 100644 src/main/java/course_project/ships/Destroyer.java create mode 100644 src/main/java/course_project/ships/Ship.java create mode 100644 src/main/java/course_project/ships/ShipSize.java create mode 100644 src/main/java/course_project/ships/ShipType.java create mode 100644 src/main/java/course_project/ships/Submarine.java create mode 100644 src/main/java/course_project/utils/FontColors.java diff --git a/src/main/java/course_project/Main.java b/src/main/java/course_project/Main.java index 3c882990..1543a875 100644 --- a/src/main/java/course_project/Main.java +++ b/src/main/java/course_project/Main.java @@ -1,4 +1,12 @@ package course_project; +import course_project.engine.BattleShipGame; + public class Main { -} + + public static void main(String[] args) { + + new BattleShipGame(); + + } +} \ No newline at end of file diff --git a/src/main/java/course_project/engine/BattleShipGame.java b/src/main/java/course_project/engine/BattleShipGame.java new file mode 100644 index 00000000..a0409290 --- /dev/null +++ b/src/main/java/course_project/engine/BattleShipGame.java @@ -0,0 +1,10 @@ +package course_project.engine; + +public class BattleShipGame { + + public BattleShipGame() { + + Menu menu = new Menu(); + menu.chooseGameOptions(); + } +} \ No newline at end of file diff --git a/src/main/java/course_project/engine/GameMap.java b/src/main/java/course_project/engine/GameMap.java new file mode 100644 index 00000000..93a233b2 --- /dev/null +++ b/src/main/java/course_project/engine/GameMap.java @@ -0,0 +1,434 @@ +package course_project.engine; + +import course_project.ships.*; + +import java.util.ArrayList; +import java.util.InputMismatchException; +import java.util.Random; +import java.util.Scanner; + +public class GameMap { + + private Scanner input = new Scanner(System.in); + private ArrayList ships = new ArrayList<>(); + + // Colours for background + public static final String ANSI_BLUE_BACKGROUND = "\u001B[44m"; + public static final String YELLOW_BACKGROUND_BRIGHT = "\033[0;103m"; + public static final String BLACK_BACKGROUND_BRIGHT = "\033[0;100m"; + public static final String GREEN_BACKGROUND_BRIGHT = "\033[0;102m"; + public static final String PURPLE_BACKGROUND_BRIGHT = "\033[0;105m"; + public static final String CYAN_BACKGROUND_BRIGHT = "\033[0;106m"; + public static final String RED_BACKGROUND_BRIGHT = "\033[0;101m"; + + //Colours for text + public static final String ANSI_BLACK = "\u001B[30m"; + public static final String ANSI_RED = "\u001B[31m"; + public static final String ANSI_BLUE = "\u001B[34m"; + public static final String WHITE_BOLD = "\033[1;37m"; + public static final String BLACK_BOLD = "\033[1;30m"; + + // Reset colour + public static final String ANSI_RESET = "\u001B[0m"; + + public GameMap() { + } + + public void printEmptyMap(String[][] map) { + + System.out.println(ANSI_BLUE_BACKGROUND + ANSI_BLACK + + " OCEAN MAP " + ANSI_RESET); + + for (int line = 1; line < map.length; line++) { + System.out.print(ANSI_BLUE + "_____" + ANSI_RESET); + } + + for (int y = 1; y < map.length; y++) { + for (int x = 1; x < map.length; x++) { + map[x][y] = " "; + } + } + + System.out.println(); + for (int column = 1; column < map.length; column++) { + String square = ""; + for (int row = 1; row < map.length; row++) { + square += " [" + map[row][column] + "] "; + } + System.out.print(ANSI_BLUE + square + ANSI_RESET); + System.out.println(ANSI_RED + "|" + column + "Y" + "|" + ANSI_RESET); + } + + for (int line = 1; line < map.length; line++) { + System.out.print(ANSI_BLUE + "_____" + ANSI_RESET); + } + + System.out.println(); + System.out.print(" "); + + for (int k = 1; k < map.length; k++) { + System.out.print(ANSI_RED + "" + k + "X" + " | " + ANSI_RESET); + } + + System.out.println("\n" + "\n" + + YELLOW_BACKGROUND_BRIGHT + WHITE_BOLD + "S: SUBMARINE (1) " + ANSI_RESET + " " + + BLACK_BACKGROUND_BRIGHT + WHITE_BOLD + "D: DESTROYER (1) " + ANSI_RESET + " " + + GREEN_BACKGROUND_BRIGHT + BLACK_BOLD + "C: CRUISER (1) " + ANSI_RESET + " " + + PURPLE_BACKGROUND_BRIGHT + WHITE_BOLD + "B: BATTLESHIP (1) " + ANSI_RESET + " " + + CYAN_BACKGROUND_BRIGHT + WHITE_BOLD + "c: CARRIER (2) " + ANSI_RESET + ); + System.out.println("\n"); + } + + private void AIRandomCoordinatesToMap(String map[][], int size, ShipType shipType) { + + Random random = new Random(); + try { + int randomX = random.nextInt(10 - 1) + 1; + int randomY = random.nextInt(10 - 1) + 1; + + while (randomY < 1 || randomY > 8 && shipType.equals(ShipType.SUBMARINE)) { + randomY = random.nextInt(10 - 1) + 1; + } + + while (randomY < 1 || randomY > 9 && shipType.equals(ShipType.DESTROYER)) { + randomY = random.nextInt(10 - 1) + 1; + } + + while (randomY < 1 || randomY > 7 && shipType.equals(ShipType.CRUISER)) { + randomY = random.nextInt(10 - 1) + 1; + } + + while (randomY < 1 || randomY > 6 && shipType.equals(ShipType.BATTLESHIP)) { + randomY = random.nextInt(10 - 1) + 1; + } + + while (randomY < 1 || randomY > 8 && shipType.equals(ShipType.CARRIER1)) { + randomY = random.nextInt(10 - 1) + 1; + } + + while (randomY < 1 || randomY > 8 && shipType.equals(ShipType.CARRIER2)) { + randomY = random.nextInt(10 - 1) + 1; + } + + while (!map[randomX][randomY].equals(" ")) { + for (int i = 0; i < size; i++) { + for (int y = randomY; y <= randomY; y++) { + y += i; + for (int x = randomX; x <= randomX; x++) { + if (!map[randomX][randomY].equals(" ")) { + randomX = random.nextInt(10 - 1) + 1; + randomY = random.nextInt(10 - 1) + 1; + } else { + break; + } + } + } + } + } + + for (int i = 0; i < size; i++) { + for (int y = randomY; y <= randomY; y++) { + y += i; + for (int x = randomX; x <= randomX; x++) { + + switch (shipType) { + case SUBMARINE: + map[x][y] = YELLOW_BACKGROUND_BRIGHT + WHITE_BOLD + ShipType.SUBMARINE.getShipType() + ANSI_RESET + ANSI_BLUE; + Submarine submarine = new Submarine(x, y); + ships.add(submarine); + break; + case DESTROYER: + map[x][y] = BLACK_BACKGROUND_BRIGHT + WHITE_BOLD + ShipType.DESTROYER.getShipType() + ANSI_RESET + ANSI_BLUE; + Destroyer destroyer = new Destroyer(x, y); + ships.add(destroyer); + break; + case CRUISER: + map[x][y] = GREEN_BACKGROUND_BRIGHT + BLACK_BOLD + ShipType.CRUISER.getShipType() + ANSI_RESET + ANSI_BLUE; + Cruiser cruiser = new Cruiser(x, y); + ships.add(cruiser); + break; + case BATTLESHIP: + map[x][y] = PURPLE_BACKGROUND_BRIGHT + WHITE_BOLD + ShipType.BATTLESHIP.getShipType() + ANSI_RESET + ANSI_BLUE; + BattleShip battleShip = new BattleShip(x, y); + ships.add(battleShip); + break; + case CARRIER1: + map[x][y] = CYAN_BACKGROUND_BRIGHT + WHITE_BOLD + ShipType.CARRIER1.getShipType() + ANSI_RESET + ANSI_BLUE; + Carrier carrier1 = new Carrier(x, y); + ships.add(carrier1); + break; + case CARRIER2: + map[x][y] = CYAN_BACKGROUND_BRIGHT + WHITE_BOLD + ShipType.CARRIER2.getShipType() + ANSI_RESET + ANSI_BLUE; + Carrier carrier2 = new Carrier(x, y); + ships.add(carrier2); + break; + default: + System.out.println("Incorrect"); + break; + } + } + } + } + + } + catch(Exception e){ + System.out.println("Something went wrong. "); + } + } + + private void addCoordinateToMap(String map[][], int size, ShipType shipType) { + + try { + System.out.println("Add X-coordinate for your " + shipType + " with size " + size); + int xInput = input.nextInt(); + + System.out.println("Add Y-coordinate for your " + shipType + " with size " + size); + int yInput = input.nextInt(); + + while (xInput < 1 || xInput > 10) { + System.out.println("Please Enter Valid X- Coordinate"); + xInput = input.nextInt(); + } + + while (yInput < 1 || yInput > 8 && shipType.equals(ShipType.SUBMARINE)) { + System.out.println("Please Enter Valid Y-Coordinate"); + yInput = input.nextInt(); + } + + while (yInput < 1 || yInput > 9 && shipType.equals(ShipType.DESTROYER)) { + System.out.println("Please Enter Valid Y-Coordinate"); + yInput = input.nextInt(); + } + + while (yInput < 1 || yInput > 7 && shipType.equals(ShipType.CRUISER)) { + System.out.println("Please Enter Valid Y-Coordinate"); + yInput = input.nextInt(); + } + + while (yInput < 1 || yInput > 6 && shipType.equals(ShipType.BATTLESHIP)) { + System.out.println("Please Enter Valid Y-Coordinate"); + yInput = input.nextInt(); + } + + while (yInput < 1 || yInput > 8 && shipType.equals(ShipType.CARRIER1)) { + System.out.println("Please Enter Valid Y-Coordinate"); + yInput = input.nextInt(); + } + + while (yInput < 1 || yInput > 8 && shipType.equals(ShipType.CARRIER2)) { + System.out.println("Please Enter Valid Y-Coordinate"); + yInput = input.nextInt(); + } + + while (!map[xInput][yInput].equals(" ")) { + for (int i = 0; i < size; i++){ + for (int y = yInput; y <= yInput; y++) { + y += i; + for (int x = xInput; x <= xInput; x++) { + if (map[x][y] != " ") { + System.out.println("You can't place a boat on another boat!"); + System.out.println("Add X-coordinate for your " + shipType + " with size " + size); + xInput = input.nextInt(); + + System.out.println("Add Y-coordinate for your " + shipType + " with size " + size); + yInput = input.nextInt(); + } else { + break; + } + } + } + } + } + + for (int i = 0; i < size; i++) { + for (int y = yInput; y <= yInput; y++) { + y += i; + for (int x = xInput; x <= xInput; x++) { + + switch (shipType) { + case SUBMARINE: + map[x][y] = YELLOW_BACKGROUND_BRIGHT + WHITE_BOLD + ShipType.SUBMARINE.getShipType() + ANSI_RESET + ANSI_BLUE; + Submarine submarinePlayer = new Submarine(x, y); + ships.add(submarinePlayer); + if(i==1){ + System.out.println(submarinePlayer.toString()); + } + break; + case DESTROYER: + map[x][y] = BLACK_BACKGROUND_BRIGHT + WHITE_BOLD + ShipType.DESTROYER.getShipType() + ANSI_RESET + ANSI_BLUE; + Destroyer destroyerPlayer = new Destroyer(x, y); + ships.add(destroyerPlayer); + if(i==1){ + System.out.println(destroyerPlayer.toString()); + } + break; + case CRUISER: + map[x][y] = GREEN_BACKGROUND_BRIGHT + BLACK_BOLD + ShipType.CRUISER.getShipType() + ANSI_RESET + ANSI_BLUE; + Cruiser cruiserPlayer = new Cruiser(x, y); + ships.add(cruiserPlayer); + if(i==1){ + System.out.println(cruiserPlayer.toString()); + } + break; + case BATTLESHIP: + map[x][y] = PURPLE_BACKGROUND_BRIGHT + WHITE_BOLD + ShipType.BATTLESHIP.getShipType() + ANSI_RESET + ANSI_BLUE; + BattleShip battleShipPlayer = new BattleShip(x, y); + ships.add(battleShipPlayer); + if(i==1){ + System.out.println(battleShipPlayer.toString()); + } + break; + case CARRIER1: + map[x][y] = CYAN_BACKGROUND_BRIGHT + WHITE_BOLD + ShipType.CARRIER1.getShipType() + ANSI_RESET + ANSI_BLUE; + Carrier carrier1Player = new Carrier(x, y); + ships.add(carrier1Player); + if(i==1){ + System.out.println(carrier1Player.toString()); + } + break; + case CARRIER2: + map[x][y] = CYAN_BACKGROUND_BRIGHT + WHITE_BOLD + ShipType.CARRIER2.getShipType() + ANSI_RESET + ANSI_BLUE; + Carrier carrier2Player = new Carrier(x, y); + ships.add(carrier2Player); + if(i==1){ + System.out.println(carrier2Player.toString()); + } + break; + default: + System.out.println("Incorrect"); + break; + } + } + } + } + } catch (InputMismatchException e) { + e.getMessage(); + } + } + + + public void printPlayerMapShips (String[][] map, int size, ShipType shipType){ + + addCoordinateToMap(map, size, shipType); + System.out.println(ANSI_BLUE_BACKGROUND + ANSI_BLACK + + " OCEAN MAP " + ANSI_RESET); + + for (int line = 1; line <= map.length; line++) { + System.out.print(ANSI_BLUE + "_____" + ANSI_RESET); + } + + System.out.println(); + + for (int column = 1; column < map.length; column++) { + String square = ""; + for (int row = 1; row < map.length; row++) { + square += " [" + map[row][column] + "] "; + } + + System.out.print(ANSI_BLUE + square + ANSI_RESET); + System.out.println(ANSI_RED + "|" + column + "Y" + "|" + ANSI_RESET); + } + + for (int line = 1; line <= map.length; line++) { + System.out.print(ANSI_BLUE + "_____" + ANSI_RESET); + } + + System.out.println(); + System.out.print(" "); + + for (int k = 1; k < map.length; k++) { + System.out.print(ANSI_RED + "" + k + "X" + " | " + ANSI_RESET); + } + + System.out.println("\n" + "\n" + + YELLOW_BACKGROUND_BRIGHT + WHITE_BOLD + "S: SUBMARINE (1) " + ANSI_RESET + " " + + BLACK_BACKGROUND_BRIGHT + WHITE_BOLD + "D: DESTROYER (1) " + ANSI_RESET + " " + + GREEN_BACKGROUND_BRIGHT + BLACK_BOLD + "C: CRUISER (1) " + ANSI_RESET + " " + + PURPLE_BACKGROUND_BRIGHT + WHITE_BOLD + "B: BATTLESHIP (1) " + ANSI_RESET + " " + + CYAN_BACKGROUND_BRIGHT + WHITE_BOLD + "c: CARRIER (2) " + ANSI_RESET + ); + + System.out.println("\n"); + } + + public void printAIMapShips (String map[][],int size, ShipType shipType){ + + AIRandomCoordinatesToMap(map, size, shipType); + + System.out.println(ANSI_BLUE_BACKGROUND + ANSI_BLACK + + " OCEAN MAP " + ANSI_RESET); + + for (int line = 1; line <= map.length; line++) { + System.out.print(ANSI_BLUE + "_____" + ANSI_RESET); + } + + System.out.println(); + for (int column = 1; column < map.length; column++) { + String square = ""; + for (int row = 1; row < map.length; row++) { + square += " [" + map[row][column] + "] "; + } + + System.out.print(ANSI_BLUE + square + ANSI_RESET); + System.out.println(ANSI_RED + "|" + column + "Y" + "|" + ANSI_RESET); + } + + for (int line = 1; line <= map.length; line++) { + System.out.print(ANSI_BLUE + "_____" + ANSI_RESET); + } + + System.out.println(); + System.out.print(" "); + + for (int k = 1; k < map.length; k++) { + System.out.print(ANSI_RED + "" + k + "X" + " | " + ANSI_RESET); + } + + System.out.println("\n" + "\n" + + YELLOW_BACKGROUND_BRIGHT + WHITE_BOLD + "S: SUBMARINE (1) " + ANSI_RESET + " " + + BLACK_BACKGROUND_BRIGHT + WHITE_BOLD + "D: DESTROYER (1) " + ANSI_RESET + " " + + GREEN_BACKGROUND_BRIGHT + BLACK_BOLD + "C: CRUISER (1) " + ANSI_RESET + " " + + PURPLE_BACKGROUND_BRIGHT + WHITE_BOLD + "B: BATTLESHIP (1) " + ANSI_RESET + " " + + CYAN_BACKGROUND_BRIGHT + WHITE_BOLD + "c: CARRIER (2) " + ANSI_RESET + ); + System.out.println("\n"); + } + + public void printBattle (String[][] map, int x, int y, String battle){ + + System.out.println(ANSI_BLUE_BACKGROUND + ANSI_BLACK + + " OCEAN MAP " + ANSI_RESET); + + for (int line = 1; line <= map.length; line++) { + System.out.print(ANSI_BLUE + "_____" + ANSI_RESET); + } + System.out.println(); + + for (int column = 1; column < map.length; column++) { + String square = ""; + for (int row = 1; row < map.length; row++) { + square += " [" + map[row][column] + "] "; + } + System.out.print(ANSI_BLUE + square + ANSI_RESET); + System.out.println(ANSI_RED + "|" + column + "Y" + "|" + ANSI_RESET); + } + + for (int line = 1; line <= map.length; line++) { + System.out.print(ANSI_BLUE + "_____" + ANSI_RESET); + } + System.out.println(); + System.out.print(" "); + + for (int k = 1; k < map.length; k++) { + System.out.print(ANSI_RED + "" + k + "X" + " | " + ANSI_RESET); + } + + System.out.println("\n" + "\n" + + RED_BACKGROUND_BRIGHT + WHITE_BOLD + "HIT (*) " + ANSI_RESET + " " + + BLACK_BACKGROUND_BRIGHT + WHITE_BOLD + "MISS (X) " + ANSI_RESET + " " + ); + System.out.println("\n"); + } + +} \ No newline at end of file diff --git a/src/main/java/course_project/engine/Menu.java b/src/main/java/course_project/engine/Menu.java new file mode 100644 index 00000000..4b15bf2e --- /dev/null +++ b/src/main/java/course_project/engine/Menu.java @@ -0,0 +1,105 @@ +package course_project.engine; + +import course_project.engine.BattleShipGame; +import course_project.players.ComputerPlayer; +import course_project.players.HumanPlayer; +import course_project.utils.FontColors; +import lombok.Data; + +import java.util.InputMismatchException; +import java.util.Scanner; + +@Data +public class Menu { + + private String[][] playerOneMap = new String[11][11]; + private String[][] playerTwoMap = new String[11][11]; + private String[][] computerMap = new String[11][11]; + private Scanner input = new Scanner(System.in); + private boolean gameStillRunning; + + public boolean isGameStillRunning() { + return gameStillRunning; + } + + public void chooseGameOptions() { + + try { + do { + FontColors logo = new FontColors(); + logo.menuLogo(); + + System.out.println("Welcome to Sea Battle! Please select mode using key 1, 2 or 3. Press "); + System.out.println("1. Player VS Player (PvP)"); + System.out.println("2. Player VS Computer (PvE)"); + System.out.println("3. Quit"); + int userChoice = input.nextInt(); + + + Scanner inputFromUser = new Scanner(System.in); + + switch (userChoice) { + case 1: + System.out.println("You have chosen 'Player VS Player' (PvP)" + "\n"); + + //Welcoming players and let them enter their name + System.out.println("Player 1, please type in your name"); + String playerOneName = inputFromUser.nextLine(); + HumanPlayer player1 = new HumanPlayer(playerOneName); // playerOneName is the input from user. + System.out.println("Welcome," + " " + player1.getName() + "!" + "\n"); + + System.out.println("Player 2, please type in your name"); + String playerTwoName = inputFromUser.nextLine(); + HumanPlayer player2 = new HumanPlayer(playerTwoName); + System.out.println("Welcome," + " " + player2.getName() + "!" + "\n"); + + + // Let player 1 and player 2 place their ships + System.out.println(player1.getName() + "," + " " + "please place your ships (6) as shown below"); + player1.placeShips(playerOneMap, player1.getName()); + + System.out.println(player2.getName() + "," + " " + "please place your ships (6) as shown below"); + player2.placeShips(playerTwoMap, player2.getName()); + + + // Let player ONE shoot first, then let each player take turn on shooting. + player1.shoot(playerOneMap, playerTwoMap, player1.getName(), player2.getName()); + + break; + + case 2: + System.out.println("You have chosen 'Player VS Computer' (PvE)" + "\n \n" + "Please enter your name:"); + + String humanName = inputFromUser.nextLine(); + HumanPlayer humanPlayer = new HumanPlayer(humanName); + System.out.println("Welcome," + " " + humanPlayer.getName() + "!"); + + // Name of Computer will include automatically via Computer-Class + ComputerPlayer computerPlayer = new ComputerPlayer(); + System.out.println("You'll be playing against the" + " " + computerPlayer.getName() + "\n"); + + System.out.println(humanPlayer.getName() + "," + " " + "please place your ships (6) as shown below"); + humanPlayer.placeShips(playerOneMap, humanPlayer.getName()); + + computerPlayer.AIDeployShips(computerMap); + + computerPlayer.shootAI(computerMap, playerOneMap, humanPlayer.getName(), computerPlayer.getName()); + + break; + + + case 3: + System.out.println("You will now Quit Sea Wars"); + System.exit(0); // Shuts down program + default: + throw new IllegalStateException("Unexpected value: " + userChoice); + } + + } while (gameStillRunning = true); + } catch (InputMismatchException e) { + e.getMessage(); + System.out.println("Invalid choice. Please select 1, 2 or 3."); + BattleShipGame bA = new BattleShipGame(); + } + } +} \ No newline at end of file diff --git a/src/main/java/course_project/engine/Position.java b/src/main/java/course_project/engine/Position.java new file mode 100644 index 00000000..9557b6c5 --- /dev/null +++ b/src/main/java/course_project/engine/Position.java @@ -0,0 +1,19 @@ +package course_project.engine; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class Position { + + private int x; + private int y; + + public String toString() { + return "Position{" + + "x=" + x + + ", y=" + y + + '}'; + } +} diff --git a/src/main/java/course_project/engine/SquareState.java b/src/main/java/course_project/engine/SquareState.java new file mode 100644 index 00000000..d470f060 --- /dev/null +++ b/src/main/java/course_project/engine/SquareState.java @@ -0,0 +1,20 @@ +package course_project.engine; + +import lombok.Getter; + +import static course_project_1.utils.ConsoleColors.*; + +@Getter +public enum SquareState { + + NONE(CYAN_BACKGROUND + " " + RESET), + MISS(YELLOW + "º" + RESET), + HIT(RED_BOLD + "•" + RESET); + + private String squareSymbol; + + SquareState (String squareSymbol) { + this.squareSymbol = squareSymbol; + } + +} \ No newline at end of file diff --git a/src/main/java/course_project/players/ComputerPlayer.java b/src/main/java/course_project/players/ComputerPlayer.java new file mode 100644 index 00000000..636a116c --- /dev/null +++ b/src/main/java/course_project/players/ComputerPlayer.java @@ -0,0 +1,194 @@ +package course_project.players; + +import course_project.engine.GameMap; +import course_project.ships.ShipSize; +import course_project.ships.ShipType; +import course_project.engine.SquareState; + +import java.util.Random; +import java.util.Scanner; + +public class ComputerPlayer extends Player { + + private static final String RED_BACKGROUND_BRIGHT = "\033[0;101m"; + private static final String ANSI_BLUE = "\u001B[34m"; + private static final String BLACK_BACKGROUND_BRIGHT = "\033[0;100m"; + private static final String ANSI_WHITE_BACKGROUND = "\u001B[47m"; + + private static final String ANSI_RESET = "\u001B[0m"; + + private GameMap gameMap = new GameMap(); + + private String[][] battleMap1 = new String[11][11]; + private String[][] battleMapAI = new String[11][11]; + private String[][] emptyMap = new String[11][11]; + + private Scanner input = new Scanner(System.in); + + public ComputerPlayer() { + super("Computer"); + } + + public void shootAI(String[][] playerOneMap, String[][] computerMap, String player1, String AI) { + + System.out.println(player1 + " , let's get started! "); + gameMap.printEmptyMap(emptyMap); + + String hitBarPlayer1 = ""; + String hitBarAI = ""; + String winBar = "********************"; + + int hitsPlayer1 = 0; + int hitsAI = 0; + + Random random = new Random(); + + int countPlayer = 0; + int countAI = 0; + + for (int y = 1; y < battleMap1.length; y++) { + for (int x = 1; x < battleMap1.length; x++) { + battleMap1[x][y] = SquareState.NONE.getSquareSymbol(); + } + } + + for (int y = 1; y < battleMapAI.length; y++) { + for (int x = 1; x < battleMapAI.length; x++) { + battleMapAI[x][y] = SquareState.NONE.getSquareSymbol(); + } + } + + do { + String ships[] = {"S", "D", "C", "B", "c"}; + + System.out.println("Your turn to shoot, " + player1 + "!"); + System.out.println("Enter X-coordinate: "); + int xShoot = input.nextInt(); + System.out.println("Enter Y-coordinate: "); + int yShoot = input.nextInt(); + + while (xShoot < 1 || xShoot > 10 || yShoot < 1 || yShoot > 10) { + System.out.println("Incorrect choice! "); + System.out.println("Enter X-coordinate: "); + xShoot = input.nextInt(); + System.out.println("Enter Y-coordinate: "); + yShoot = input.nextInt(); + } + + for (int i = 0; i < ships.length; i++) { + + if (computerMap[xShoot][yShoot].contains(ships[i])) { + + if (battleMapAI[xShoot][yShoot].contains(SquareState.HIT.getSquareSymbol())) { + System.out.println("You've already hit this cell. " + "\n"); + } else { + System.out.println("HIT! " + "\n"); + + battleMapAI[xShoot][yShoot] = RED_BACKGROUND_BRIGHT + SquareState.HIT.getSquareSymbol() + ANSI_RESET + ANSI_BLUE; + hitBarPlayer1 += SquareState.HIT.getSquareSymbol(); + hitsPlayer1++; + } + } + } + + if (computerMap[xShoot][yShoot].contains(SquareState.NONE.getSquareSymbol())) { + System.out.println("MISS!"); + battleMapAI[xShoot][yShoot] = BLACK_BACKGROUND_BRIGHT + SquareState.MISS.getSquareSymbol() + ANSI_RESET + ANSI_BLUE; + } + + System.out.println("MAP OF PLAYER " + AI); + gameMap.printBattle(battleMapAI, xShoot, yShoot, "battle"); + if (hitsPlayer1 == 20) { + System.out.println(player1 + " WINS! GAME OVER FOR: " + AI); + break; + } + + int xShootAI = random.nextInt(10 - 1) + 1; + int yShootAI = random.nextInt(10 - 1) + 1; + + for (int i = 0; i < ships.length; i++) { + if (playerOneMap[xShootAI][yShootAI].contains(ships[i])) { + if (battleMap1[xShoot][yShoot].contains(SquareState.HIT.getSquareSymbol())) { + System.out.println("You've already hit this cell. " + "\n"); + } else { + System.out.println("HIT! " + "\n"); + battleMap1[xShoot][yShoot] = RED_BACKGROUND_BRIGHT + SquareState.HIT.getSquareSymbol() + ANSI_RESET + ANSI_BLUE; + hitBarPlayer1 += SquareState.HIT.getSquareSymbol(); + hitsAI++; + } + } + } + + if (playerOneMap[xShootAI][yShootAI].contains(SquareState.NONE.getSquareSymbol())) { + System.out.println("MISS!"); + battleMap1[xShootAI][yShootAI] = BLACK_BACKGROUND_BRIGHT + SquareState.MISS.getSquareSymbol() + ANSI_RESET + ANSI_BLUE; + } + + System.out.println("MAP OF PLAYER " + player1); + gameMap.printBattle(battleMap1, xShootAI, yShootAI, "battle"); + + if (hitsAI == 20) { + System.out.println(AI + " WINS! GAME OVER FOR: " + player1); + break; + } + + System.out.println("Number of hits for " + player1 + " is: " + hitsPlayer1); + System.out.println(ANSI_WHITE_BACKGROUND + winBar + ANSI_RESET); + System.out.println(RED_BACKGROUND_BRIGHT + hitBarPlayer1 + ANSI_RESET); + System.out.println("Number of hits for " + AI + " is: " + hitsAI); + System.out.println(ANSI_WHITE_BACKGROUND + winBar + ANSI_RESET); + System.out.println(RED_BACKGROUND_BRIGHT + hitBarAI + ANSI_RESET); + + } + while (true); + } + + public void AIDeployShips(String playerMap[][]) { + + int submarineSize = ShipSize.SUBMARINE.getSize(); + int destroyerSize = ShipSize.DESTROYER.getSize(); + int cruiserSize = ShipSize.CRUISER.getSize(); + int battleshipSize = ShipSize.BATTLESHIP.getSize(); + int carrier1Size = ShipSize.CARRIER1.getSize(); + int carrier2Size = ShipSize.CARRIER2.getSize(); + + ShipType submarine = ShipType.SUBMARINE; + ShipType destroyer = ShipType.DESTROYER; + ShipType cruiser = ShipType.CRUISER; + ShipType battleship = ShipType.BATTLESHIP; + ShipType carrier1 = ShipType.CARRIER1; + ShipType carrier2 = ShipType.CARRIER2; + + ShipType[] shipNames = {submarine, destroyer, cruiser, battleship, carrier1, carrier2}; + gameMap.printEmptyMap(playerMap); + + for (int i = 0; i < shipNames.length; i++) { + int[] coordinates; + + switch (shipNames[i]) { + case SUBMARINE: + gameMap.printAIMapShips(playerMap, submarineSize, submarine); + break; + case DESTROYER: + gameMap.printAIMapShips(playerMap, destroyerSize, destroyer); + break; + case CRUISER: + gameMap.printAIMapShips(playerMap, cruiserSize, cruiser); + break; + case BATTLESHIP: + gameMap.printAIMapShips(playerMap, battleshipSize, battleship); + break; + case CARRIER1: + gameMap.printAIMapShips(playerMap, carrier1Size, carrier1); + break; + case CARRIER2: + gameMap.printAIMapShips(playerMap, carrier2Size, carrier2); + break; + default: + System.out.println("Incorrect"); + break; + } + } + } + +} \ No newline at end of file diff --git a/src/main/java/course_project/players/HumanPlayer.java b/src/main/java/course_project/players/HumanPlayer.java new file mode 100644 index 00000000..5fdee482 --- /dev/null +++ b/src/main/java/course_project/players/HumanPlayer.java @@ -0,0 +1,247 @@ +package course_project.players; + +import course_project.engine.GameMap; +import course_project.engine.SquareState; +import course_project.ships.ShipSize; +import course_project.ships.ShipType; + +import java.util.Scanner; + +public class HumanPlayer extends Player { + + private static final String RED_BACKGROUND_BRIGHT = "\033[0;101m"; + private static final String ANSI_WHITE_BACKGROUND = "\u001B[47m"; + private static final String BLACK_BACKGROUND_BRIGHT = "\033[0;100m"; + private static final String ANSI_BLUE = "\u001B[34m"; + private static final String ANSI_RESET = "\u001B[0m"; + private Scanner input = new Scanner(System.in); + private GameMap gameMap = new GameMap(); + private String[][] battleMap1 = new String[11][11]; + private String[][] battleMap2 = new String[11][11]; + private String[][] emptyMap = new String[11][11]; + + public HumanPlayer(String name) { + super(name); + } + + public void shoot(String[][] playerOneMap, String[][] playerTwoMap, String player1, String player2){ + + System.out.println(player1 + " get ready to battle! "); + gameMap.printEmptyMap(emptyMap); + String hitBarPlayer1 = ""; + String hitBarPlayer2 = ""; + String winBar = "********************"; + + int hitsPlayer1 = 0; + int hitsPlayer2 = 0; + + int countPlayer1 = 0; + int countPlayer2 = 0; + + for (int y = 1; y < battleMap2.length; y++) { + for (int x = 1; x < battleMap2.length; x++) { + battleMap2[x][y] = SquareState.NONE.getSquareSymbol(); + } + } + + for (int y = 1; y < battleMap1.length; y++) { + for (int x = 1; x < battleMap1.length; x++) { + battleMap1[x][y] = SquareState.NONE.getSquareSymbol(); + } + } + + do { + String[] ships = {"S", "D", "C", "B", "c"}; + System.out.println("Your turn to shoot, " + player1 + "!"); + System.out.println("Shoot! Enter X-coordinate: "); + int xShoot = input.nextInt(); + System.out.println("Shoot! Enter Y-coordinate: "); + int yShoot = input.nextInt(); + + while(xShoot <1 || xShoot>10 || yShoot<1 || yShoot>10){ + System.out.println("Invalid choice! "); + System.out.println("Shoot! Enter X-coordinate: "); + xShoot = input.nextInt(); + System.out.println("Shoot! Enter Y-coordinate: "); + yShoot = input.nextInt(); + } + + for (int i = 0; i < ships.length; i++) { + if (playerTwoMap[xShoot][yShoot].contains(ships[i])) { + if(battleMap2[xShoot][yShoot].contains(SquareState.HIT.getSquareSymbol())){ + System.out.println("You have hit this cell. " + "\n"); + } + else{ + System.out.println("HIT! " + "\n"); + countPlayer1++; + if(countPlayer1 == ShipSize.SUBMARINE.getSize() && playerOneMap[xShoot][yShoot].contains(ShipType.SUBMARINE.getShipType())){ + System.out.println("YOU SUNK THE " + ShipType.SUBMARINE + "!"); + countPlayer1 = 0; + } + if(countPlayer1 == ShipSize.DESTROYER.getSize() && playerOneMap[xShoot][yShoot].contains(ShipType.DESTROYER.getShipType())){ + System.out.println("YOU SUNK THE " + ShipType.DESTROYER + "!"); + countPlayer1 = 0; + } + if(countPlayer1 == ShipSize.CRUISER.getSize() && playerOneMap[xShoot][yShoot].contains(ShipType.CRUISER.getShipType())){ + System.out.println("YOU SUNK THE " + ShipType.CRUISER + "!"); + countPlayer1 = 0; + } + if(countPlayer1 == ShipSize.BATTLESHIP.getSize() && playerOneMap[xShoot][yShoot].contains(ShipType.BATTLESHIP.getShipType())){ + System.out.println("YOU SUNK THE " + ShipType.BATTLESHIP + "!"); + countPlayer1 = 0; + } + if(countPlayer1 == ShipSize.CARRIER1.getSize() && playerOneMap[xShoot][yShoot].contains(ShipType.CARRIER1.getShipType())){ + System.out.println("YOU SUNK THE " + ShipType.CARRIER1 + "!"); + countPlayer1 = 0; + } + if(countPlayer1 == ShipSize.CARRIER2.getSize() && playerOneMap[xShoot][yShoot].contains(ShipType.CARRIER2.getShipType())){ + System.out.println("YOU SUNK THE " + ShipType.CARRIER2 + "!"); + countPlayer1 = 0; + } + + battleMap2[xShoot][yShoot] = RED_BACKGROUND_BRIGHT + SquareState.HIT.getSquareSymbol() + ANSI_RESET + ANSI_BLUE; + hitBarPlayer1 += SquareState.HIT.getSquareSymbol(); + hitsPlayer1++; + } + } + } + + if(playerTwoMap[xShoot][yShoot].contains(SquareState.NONE.getSquareSymbol())) { + System.out.println("MISS! " + "\n"); + battleMap2[xShoot][yShoot] = BLACK_BACKGROUND_BRIGHT + SquareState.MISS.getSquareSymbol() + ANSI_RESET + ANSI_BLUE; + } + + System.out.println("MAP OF PLAYER " + player2); + gameMap.printBattle(battleMap2, xShoot, yShoot, "battle"); + if (hitsPlayer1 == 20) { + System.out.println(player1 + " WINS! GAME OVER FOR: " + player2); + break; + } + + System.out.println("Your turn to shoot, " + player2 + "!"); + System.out.println("Shoot! Enter X-coordinate: "); + xShoot = input.nextInt(); + System.out.println("Shoot! Enter Y-coordinate: "); + yShoot = input.nextInt(); + + while(xShoot <1 || xShoot>10 || yShoot<1 || yShoot>10){ + System.out.println("Incorrect choice"); + System.out.println("Shoot! Enter X-coordinate: "); + xShoot = input.nextInt(); + System.out.println("Shoot! Enter Y-coordinate: "); + yShoot = input.nextInt(); + } + + for (int i = 0; i < ships.length; i++) { + if (playerOneMap[xShoot][yShoot].contains(ships[i])) { + + if(battleMap1[xShoot][yShoot].contains(SquareState.HIT.getSquareSymbol())){ + System.out.println("You have hit this cell. " + "\n"); + } + else{ + System.out.println("HIT! " + "\n"); + countPlayer2++; + if(countPlayer2 == ShipSize.SUBMARINE.getSize() && playerOneMap[xShoot][yShoot].contains(ShipType.SUBMARINE.getShipType())){ + System.out.println("YOU SUNK THE " + ShipType.SUBMARINE + "!"); + countPlayer2 = 0; + } + if(countPlayer2 == ShipSize.DESTROYER.getSize() && playerOneMap[xShoot][yShoot].contains(ShipType.DESTROYER.getShipType())){ + System.out.println("YOU SUNK THE " + ShipType.DESTROYER + "!"); + countPlayer2 = 0; + } + if(countPlayer2 == ShipSize.CRUISER.getSize() && playerOneMap[xShoot][yShoot].contains(ShipType.CRUISER.getShipType())){ + System.out.println("YOU SUNK THE " + ShipType.CRUISER + "!"); + countPlayer2 = 0; + } + if(countPlayer2 == ShipSize.BATTLESHIP.getSize() && playerOneMap[xShoot][yShoot].contains(ShipType.BATTLESHIP.getShipType())){ + System.out.println("YOU SUNK THE " + ShipType.BATTLESHIP + "!"); + countPlayer2 = 0; + } + if(countPlayer2 == ShipSize.CARRIER1.getSize() && playerOneMap[xShoot][yShoot].contains(ShipType.CARRIER1.getShipType())){ + System.out.println("YOU SUNK THE " + ShipType.CARRIER1 + "!"); + countPlayer2 = 0; + } + if(countPlayer2 == ShipSize.CARRIER2.getSize() && playerOneMap[xShoot][yShoot].contains(ShipType.CARRIER2.getShipType())){ + System.out.println("YOU SUNK THE " + ShipType.CARRIER2 + "!"); + countPlayer2 = 0; + } + + battleMap1[xShoot][yShoot] = RED_BACKGROUND_BRIGHT + SquareState.HIT.getSquareSymbol() + ANSI_RESET + ANSI_BLUE; + hitBarPlayer2 += SquareState.HIT.getSquareSymbol(); + hitsPlayer2++; + } + } + } + + if(playerOneMap[xShoot][yShoot].contains(SquareState.NONE.getSquareSymbol())) { + System.out.println("MISS! " + "\n"); + battleMap1[xShoot][yShoot] = BLACK_BACKGROUND_BRIGHT + SquareState.MISS.getSquareSymbol() + ANSI_RESET + ANSI_BLUE; + } + + System.out.println("MAP OF PLAYER " + player1); + gameMap.printBattle(battleMap1, xShoot, yShoot, "battle"); + + if (hitsPlayer2 == 20) { + System.out.println(player2 + " WINS! GAME OVER FOR: " + player1); + break; + } + + System.out.println("Number of hits for " + player1 + " is: " + hitsPlayer1); + System.out.println(ANSI_WHITE_BACKGROUND + winBar + ANSI_RESET); + System.out.println(RED_BACKGROUND_BRIGHT + hitBarPlayer1 + ANSI_RESET); + System.out.println("Number of hits for " + player2 + " is: " + hitsPlayer2); + System.out.println(ANSI_WHITE_BACKGROUND + winBar + ANSI_RESET); + System.out.println(RED_BACKGROUND_BRIGHT + hitBarPlayer2 + ANSI_RESET); + + }while(true); + } + + + public void placeShips(String[][] playerMap, String playerName) { + int submarineSize = 3; + int destroyerSize = 2; + int cruiserSize = 4; + int battleshipSize = 5; + int carrier1Size = 3; + int carrier2Size = 3; + + ShipType submarine = ShipType.SUBMARINE; + ShipType destroyer = ShipType.DESTROYER; + ShipType cruiser = ShipType.CRUISER; + ShipType battleship = ShipType.BATTLESHIP; + ShipType carrier1 = ShipType.CARRIER1; + ShipType carrier2 = ShipType.CARRIER2; + + ShipType[] shipNames = {submarine, destroyer, cruiser, battleship, carrier1, carrier2}; + + gameMap.printEmptyMap(playerMap); + + for (int i = 0; i < shipNames.length; i++) { + int[] coordinates; + + switch (shipNames[i]) { + case SUBMARINE: + gameMap.printPlayerMapShips(playerMap, submarineSize, submarine); + break; + case DESTROYER: + gameMap.printPlayerMapShips(playerMap, destroyerSize, destroyer); + break; + case CRUISER: + gameMap.printPlayerMapShips(playerMap, cruiserSize, cruiser); + break; + case BATTLESHIP: + gameMap.printPlayerMapShips(playerMap, battleshipSize, battleship); + break; + case CARRIER1: + gameMap.printPlayerMapShips(playerMap, carrier1Size, carrier1); + break; + case CARRIER2: + gameMap.printPlayerMapShips(playerMap, carrier2Size, carrier2); + break; + default: + System.out.println("Incorrect"); + break; + } + } + } +} diff --git a/src/main/java/course_project/players/Player.java b/src/main/java/course_project/players/Player.java new file mode 100644 index 00000000..0f18d816 --- /dev/null +++ b/src/main/java/course_project/players/Player.java @@ -0,0 +1,22 @@ +package course_project.players; + +import course_project.engine.GameMap; + +public abstract class Player { + + private String name; + public GameMap playerGameMap; + + + public Player(String name){ + this.name = name; + playerGameMap = new GameMap(); + } + public void shoot(String[][] playerOneMap, String[][] computerMap, String name){ + + } + public String getName(){ + return this.name; + } + +} \ No newline at end of file diff --git a/src/main/java/course_project/ships/BattleShip.java b/src/main/java/course_project/ships/BattleShip.java new file mode 100644 index 00000000..63b73cc6 --- /dev/null +++ b/src/main/java/course_project/ships/BattleShip.java @@ -0,0 +1,8 @@ +package course_project.ships; + +public class BattleShip extends Ship { + + public BattleShip(int posX, int posY) { + super("Battleship", 5, posX, posY); + } +} \ No newline at end of file diff --git a/src/main/java/course_project/ships/Carrier.java b/src/main/java/course_project/ships/Carrier.java new file mode 100644 index 00000000..eef60b76 --- /dev/null +++ b/src/main/java/course_project/ships/Carrier.java @@ -0,0 +1,8 @@ +package course_project.ships; + +public class Carrier extends Ship { + + public Carrier(int posX, int posY){ + super("Carrier", 3, posX, posY); + } +} \ No newline at end of file diff --git a/src/main/java/course_project/ships/Cruiser.java b/src/main/java/course_project/ships/Cruiser.java new file mode 100644 index 00000000..5758c0c0 --- /dev/null +++ b/src/main/java/course_project/ships/Cruiser.java @@ -0,0 +1,8 @@ +package course_project.ships; + +public class Cruiser extends Ship { + public Cruiser(int posX, int posY){ + super("Cruiser", 4, posX, posY); + } + +} \ No newline at end of file diff --git a/src/main/java/course_project/ships/Destroyer.java b/src/main/java/course_project/ships/Destroyer.java new file mode 100644 index 00000000..cbbabc9b --- /dev/null +++ b/src/main/java/course_project/ships/Destroyer.java @@ -0,0 +1,9 @@ +package course_project.ships; + +public class Destroyer extends Ship { + public Destroyer(int posX, int posY) { + super("Destroyer", 2, posX, posY); + + } + +} \ No newline at end of file diff --git a/src/main/java/course_project/ships/Ship.java b/src/main/java/course_project/ships/Ship.java new file mode 100644 index 00000000..13b67a1b --- /dev/null +++ b/src/main/java/course_project/ships/Ship.java @@ -0,0 +1,44 @@ +package course_project.ships; + +import course_project.engine.Position; + +import java.util.ArrayList; + +public class Ship { + private int size; + private String typeOfShip; + private int posX; + private int posY; + private ArrayList positionsArrayList = new ArrayList<>(); + + public Ship(String type, int size, int posX, int posY) { + this.size = size; + this.typeOfShip = type; + this.posX = posX; + this.posY = posY; + + Position position = new Position(this.posX, this.posY); + positionsArrayList.add(position); + } + + private int getSize() { + return this.size; + } + + private int getPosX() { + return this.posX; + } + + private int getPosY() { + return this.posY - 1; + } + + public String toString() { + return "You have added the following ship: " + "\n" + + "Type of ship: " + typeOfShip + "\n" + + "Size: " + getSize() + "\n" + + "X-position: " + getPosX() + "\n" + + "Y-position: " + getPosY() + "\n"; + } + +} \ No newline at end of file diff --git a/src/main/java/course_project/ships/ShipSize.java b/src/main/java/course_project/ships/ShipSize.java new file mode 100644 index 00000000..2a04c067 --- /dev/null +++ b/src/main/java/course_project/ships/ShipSize.java @@ -0,0 +1,19 @@ +package course_project.ships; + +import lombok.Getter; + +@Getter +public enum ShipSize { + SUBMARINE(3), + DESTROYER(2), + CRUISER(4), + BATTLESHIP(5), + CARRIER1(3), + CARRIER2(3); + + private int size; + + ShipSize(int size){ + this.size = size; + } +} \ No newline at end of file diff --git a/src/main/java/course_project/ships/ShipType.java b/src/main/java/course_project/ships/ShipType.java new file mode 100644 index 00000000..b51ffc8d --- /dev/null +++ b/src/main/java/course_project/ships/ShipType.java @@ -0,0 +1,20 @@ +package course_project.ships; + +import lombok.Getter; + +@Getter +public enum ShipType { + SUBMARINE("S"), + DESTROYER("D"), + CRUISER("C"), + BATTLESHIP("B"), + CARRIER1("c"), + CARRIER2("c"); + + private String shipType; + + ShipType(String shipType) { + this.shipType = shipType; + } + +} \ No newline at end of file diff --git a/src/main/java/course_project/ships/Submarine.java b/src/main/java/course_project/ships/Submarine.java new file mode 100644 index 00000000..3428ccd9 --- /dev/null +++ b/src/main/java/course_project/ships/Submarine.java @@ -0,0 +1,8 @@ +package course_project.ships; + +public class Submarine extends Ship { + + public Submarine(int posX, int posY) { + super("Submarine", 3, posX, posY); + } +} \ No newline at end of file diff --git a/src/main/java/course_project/utils/FontColors.java b/src/main/java/course_project/utils/FontColors.java new file mode 100644 index 00000000..74cd8a4f --- /dev/null +++ b/src/main/java/course_project/utils/FontColors.java @@ -0,0 +1,34 @@ +package course_project.utils; + +public class FontColors { + + + // Colours for background + public static final String ANSI_BLUE_BACKGROUND = "\u001B[44m"; + public static final String RED_BACKGROUND = "\033[41m"; + + // Reset colour + public static final String ANSI_RESET = "\u001B[0m"; + + // Colours for String + public static final String BLACK_BOLD = "\033[1;30m"; // BLACK + + + public FontColors() { + //menuLogo(); + } + + + // Method prints a colorful logo + + public void menuLogo() { + + System.out.println( + RED_BACKGROUND + BLACK_BOLD + " WELCOME TO SEA BATTLE! " + ANSI_RESET + + "\n" + "\n" + + + + " " + ANSI_BLUE_BACKGROUND + " " + ANSI_RESET + "\n" + ); + } +} \ No newline at end of file