From 540a63dbc2318a9c44ea627df97aa82b6546c657 Mon Sep 17 00:00:00 2001
From: Trishun <piotr_dec@msn.com>
Date: Sun, 23 Oct 2022 23:31:04 +0200
Subject: [PATCH] Provides custom, modern class for 1D Array task

---
 .../array_1d/CustomSolution.java              | 26 ++++++++++
 .../datastructures/array_1d/Solution.java     |  8 ++-
 .../datastructures/array_1d/SolutionTest.java | 50 +++++++++++++------
 3 files changed, 67 insertions(+), 17 deletions(-)
 create mode 100644 src/main/java/eu/ztsh/training/hackerrank/datastructures/array_1d/CustomSolution.java

diff --git a/src/main/java/eu/ztsh/training/hackerrank/datastructures/array_1d/CustomSolution.java b/src/main/java/eu/ztsh/training/hackerrank/datastructures/array_1d/CustomSolution.java
new file mode 100644
index 0000000..22ac17d
--- /dev/null
+++ b/src/main/java/eu/ztsh/training/hackerrank/datastructures/array_1d/CustomSolution.java
@@ -0,0 +1,26 @@
+package eu.ztsh.training.hackerrank.datastructures.array_1d;
+
+import java.util.Arrays;
+import java.util.Scanner;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.stream.Stream;
+
+/**
+ * Custom modern class for 1D Array task.
+ */
+public class CustomSolution {
+
+    public static void main(String[] args) {
+        var scan = new Scanner(System.in);
+        int n = scan.nextInt();
+        // 1. Create an array, a, capable of holding n integers.
+        int[] a = new int[n];
+        var index = new AtomicInteger(0);
+        // 2. Modify the code in the loop so that it saves each sequential value to its corresponding location in the array.
+        Stream.generate(scan::nextInt)
+                .limit(n) // takeWhile doesn't fill last element
+                .forEachOrdered(item -> a[index.getAndIncrement()] = item);
+        Arrays.stream(a).forEachOrdered(System.out::println);
+    }
+
+}
diff --git a/src/main/java/eu/ztsh/training/hackerrank/datastructures/array_1d/Solution.java b/src/main/java/eu/ztsh/training/hackerrank/datastructures/array_1d/Solution.java
index c1738e4..7db6a4b 100644
--- a/src/main/java/eu/ztsh/training/hackerrank/datastructures/array_1d/Solution.java
+++ b/src/main/java/eu/ztsh/training/hackerrank/datastructures/array_1d/Solution.java
@@ -2,18 +2,24 @@ package eu.ztsh.training.hackerrank.datastructures.array_1d;
 
 import java.util.Scanner;
 
-// https://www.hackerrank.com/challenges/java-1d-array-introduction/problem
+/**
+ * Java 1D Array task.
+ * Task spec: Java 8 + partially locked editor
+ * source: https://www.hackerrank.com/challenges/java-1d-array-introduction/problem
+ */
 public class Solution {
 
     public static void main(String[] args) {
         Scanner scan = new Scanner(System.in);
         int n = scan.nextInt();
+        // BEGIN: unlocked editor
         int[] a = new int[n];
         int index = 0;
         while (scan.hasNext()) {
             a[index] = scan.nextInt();
             index++;
         }
+        // END: unlocked editor
         scan.close();
         // Prints each sequential element in array a
         for (int i = 0; i < a.length; i++) {
diff --git a/src/test/java/eu/ztsh/training/hackerrank/datastructures/array_1d/SolutionTest.java b/src/test/java/eu/ztsh/training/hackerrank/datastructures/array_1d/SolutionTest.java
index 50193e2..7c0b1aa 100644
--- a/src/test/java/eu/ztsh/training/hackerrank/datastructures/array_1d/SolutionTest.java
+++ b/src/test/java/eu/ztsh/training/hackerrank/datastructures/array_1d/SolutionTest.java
@@ -4,30 +4,48 @@ import java.util.List;
 import eu.ztsh.training.hackerrank.HackerRankTest;
 import eu.ztsh.training.hackerrank.SolutionClassDescription;
 import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Nested;
 import org.junit.jupiter.api.Test;
 
 @DisplayName("HackerRank challenges: Java 1D Array")
-class SolutionTest extends HackerRankTest {
+class SolutionTest {
 
-    @Test
-    public void testCase0() {
-        simpleAssert(
-                List.of("5", "10", "20", "30", "40", "50"),
-                List.of("10", "20", "30", "40", "50")
-        );
+    @Nested
+    @DisplayName("Original Solution class")
+    class OriginalSolutionTest extends Array1DTest {
+        @Override
+        protected SolutionClassDescription getSolutionClassDescription() {
+            return new SolutionClassDescription(Solution.class);
+        }
     }
 
-    @Test
-    public void testCase1() {
-        simpleAssert(
-                List.of("3", "100", "200", "100"),
-                List.of("100", "200", "100")
-        );
+    @Nested
+    @DisplayName("Custom Solution class")
+    class CustomSolutionTest extends Array1DTest {
+        @Override
+        protected SolutionClassDescription getSolutionClassDescription() {
+            return new SolutionClassDescription(CustomSolution.class);
+        }
     }
 
-    @Override
-    protected SolutionClassDescription getSolutionClassDescription() {
-        return new SolutionClassDescription(Solution.class);
+    abstract static class Array1DTest extends HackerRankTest {
+
+        @Test
+        public void testCase0() {
+            simpleAssert(
+                    List.of("5", "10", "20", "30", "40", "50"),
+                    List.of("10", "20", "30", "40", "50")
+            );
+        }
+
+        @Test
+        public void testCase1() {
+            simpleAssert(
+                    List.of("3", "100", "200", "100"),
+                    List.of("100", "200", "100")
+            );
+        }
+
     }
 
 }
\ No newline at end of file