diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..cd250993 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +# Ignorowanie folderów i plików generowanych przez narzędzia i IDE +/bin/ +/target/ +/.idea/ +*.iml + +# Ignorowanie plików wygenerowanych przez systemy operacyjne +Thumbs.db +ehthumbs.db \ No newline at end of file diff --git a/pom.xml b/pom.xml index df6458d1..65a309bc 100644 --- a/pom.xml +++ b/pom.xml @@ -29,5 +29,11 @@ 4.12 test - + + org.junit.jupiter + junit-jupiter + RELEASE + compile + + \ No newline at end of file diff --git a/src/main/java/edu/kis/vh/nursery/DefaultCountingOutRhymer.java b/src/main/java/edu/kis/vh/nursery/DefaultCountingOutRhymer.java new file mode 100644 index 00000000..b02cdad2 --- /dev/null +++ b/src/main/java/edu/kis/vh/nursery/DefaultCountingOutRhymer.java @@ -0,0 +1,41 @@ +package edu.kis.vh.nursery; + +public class DefaultCountingOutRhymer { + + private static final int NUMBERS_ARRAY_SIZE = 12; + private static final int TOTAL_INIT_SIZE = -1; + private static final int RETURN_VALUE = -1; + private int[] numbers = new int[NUMBERS_ARRAY_SIZE]; + // TODO: Move getTotal() method below the total variable declaration for better readability + public int getTotal() { + return total; + } + + private int total = TOTAL_INIT_SIZE; + + public void countIn(int in) { + if (!isFull()) + numbers[++total] = in; + } + + public boolean callCheck() { + return total == TOTAL_INIT_SIZE; + } + + public boolean isFull() { + return total == NUMBERS_ARRAY_SIZE + TOTAL_INIT_SIZE; + } + + protected int peekaboo() { + if (callCheck()) + return RETURN_VALUE; + return numbers[total]; + } + + public int countOut() { + if (callCheck()) + return RETURN_VALUE; + return numbers[total--]; + } + +} \ No newline at end of file diff --git a/src/main/java/edu/kis/vh/nursery/FIFORhymer.java b/src/main/java/edu/kis/vh/nursery/FIFORhymer.java index 28591ada..1edb6d5a 100644 --- a/src/main/java/edu/kis/vh/nursery/FIFORhymer.java +++ b/src/main/java/edu/kis/vh/nursery/FIFORhymer.java @@ -1,21 +1,21 @@ package edu.kis.vh.nursery; -public class FIFORhymer extends defaultCountingOutRhymer { +public class FIFORhymer extends DefaultCountingOutRhymer { + + private DefaultCountingOutRhymer temp = new DefaultCountingOutRhymer(); - public defaultCountingOutRhymer temp = new defaultCountingOutRhymer(); - @Override public int countOut() { while (!callCheck()) - - temp.countIn(super.countOut()); - + + temp.countIn(super.countOut()); + int ret = temp.countOut(); - + while (!temp.callCheck()) - - countIn(temp.countOut()); - + + countIn(temp.countOut()); + return ret; } -} +} \ No newline at end of file diff --git a/src/main/java/edu/kis/vh/nursery/HanoiRhymer.java b/src/main/java/edu/kis/vh/nursery/HanoiRhymer.java index 755b2d58..d78d3fbb 100644 --- a/src/main/java/edu/kis/vh/nursery/HanoiRhymer.java +++ b/src/main/java/edu/kis/vh/nursery/HanoiRhymer.java @@ -1,17 +1,18 @@ package edu.kis.vh.nursery; -public class HanoiRhymer extends defaultCountingOutRhymer { +public class HanoiRhymer extends DefaultCountingOutRhymer { -int totalRejected = 0; + private int totalRejected = 0; - public int reportRejected() { + int reportRejected() { return totalRejected; } + @Override public void countIn(int in) { - if (!callCheck() && in > peekaboo()) + if (!callCheck() && in > peekaboo()) totalRejected++; - else - super.countIn(in); + else + super.countIn(in); } } diff --git a/src/main/java/edu/kis/vh/nursery/defaultCountingOutRhymer.java b/src/main/java/edu/kis/vh/nursery/defaultCountingOutRhymer.java deleted file mode 100644 index 199abf6d..00000000 --- a/src/main/java/edu/kis/vh/nursery/defaultCountingOutRhymer.java +++ /dev/null @@ -1,34 +0,0 @@ -package edu.kis.vh.nursery; - -public class defaultCountingOutRhymer { - - private int[] NUMBERS = new int[12]; - - public int total = -1; - - public void countIn(int in) { - if (!isFull()) - NUMBERS[++total] = in; - } - - public boolean callCheck() { - return total == -1; - } - - public boolean isFull() { - return total == 11; - } - - protected int peekaboo() { - if (callCheck()) - return -1; - return NUMBERS[total]; - } - - public int countOut() { - if (callCheck()) - return -1; - return NUMBERS[total--]; - } - -} diff --git a/src/main/java/edu/kis/vh/nursery/factory/DefaultRhymersFactory.java b/src/main/java/edu/kis/vh/nursery/factory/DefaultRhymersFactory.java index 60ba2a16..1387fa70 100644 --- a/src/main/java/edu/kis/vh/nursery/factory/DefaultRhymersFactory.java +++ b/src/main/java/edu/kis/vh/nursery/factory/DefaultRhymersFactory.java @@ -1,29 +1,28 @@ package edu.kis.vh.nursery.factory; -import edu.kis.vh.nursery.defaultCountingOutRhymer; +import edu.kis.vh.nursery.DefaultCountingOutRhymer; import edu.kis.vh.nursery.FIFORhymer; import edu.kis.vh.nursery.HanoiRhymer; -import edu.kis.vh.nursery.factory.Rhymersfactory; public class DefaultRhymersFactory implements Rhymersfactory { @Override - public defaultCountingOutRhymer GetStandardRhymer() { - return new defaultCountingOutRhymer(); + public DefaultCountingOutRhymer getStandardRhymer() { + return new DefaultCountingOutRhymer(); } @Override - public defaultCountingOutRhymer GetFalseRhymer() { - return new defaultCountingOutRhymer(); + public DefaultCountingOutRhymer getFalseRhymer() { + return new DefaultCountingOutRhymer(); } @Override - public defaultCountingOutRhymer GetFIFORhymer() { + public DefaultCountingOutRhymer getFIFORhymer() { return new FIFORhymer(); } @Override - public defaultCountingOutRhymer GetHanoiRhymer() { + public DefaultCountingOutRhymer getHanoiRhymer() { return new HanoiRhymer(); } diff --git a/src/main/java/edu/kis/vh/nursery/factory/Rhymersfactory.java b/src/main/java/edu/kis/vh/nursery/factory/Rhymersfactory.java index 033755d0..da726cf0 100644 --- a/src/main/java/edu/kis/vh/nursery/factory/Rhymersfactory.java +++ b/src/main/java/edu/kis/vh/nursery/factory/Rhymersfactory.java @@ -1,15 +1,15 @@ package edu.kis.vh.nursery.factory; -import edu.kis.vh.nursery.defaultCountingOutRhymer; - - public interface Rhymersfactory { - - public defaultCountingOutRhymer GetStandardRhymer(); - - public defaultCountingOutRhymer GetFalseRhymer(); - - public defaultCountingOutRhymer GetFIFORhymer(); - - public defaultCountingOutRhymer GetHanoiRhymer(); - - } +import edu.kis.vh.nursery.DefaultCountingOutRhymer; + +public interface Rhymersfactory { + + public DefaultCountingOutRhymer getStandardRhymer(); + + public DefaultCountingOutRhymer getFalseRhymer(); + + public DefaultCountingOutRhymer getFIFORhymer(); + + public DefaultCountingOutRhymer getHanoiRhymer(); + +} diff --git a/src/main/java/edu/kis/vh/nursery/list/IntLinkedList.java b/src/main/java/edu/kis/vh/nursery/list/IntLinkedList.java index 07efc0a2..a2115add 100644 --- a/src/main/java/edu/kis/vh/nursery/list/IntLinkedList.java +++ b/src/main/java/edu/kis/vh/nursery/list/IntLinkedList.java @@ -1,10 +1,35 @@ package edu.kis.vh.nursery.list; +/** + * The IntLinkedList class represents a doubly linked list of integers. + * It supports basic operations such as push, pop, and checking if the list is empty or full. + */ public class IntLinkedList { - Node last; - int i; + /** + * The constant value returned when an operation fails. + */ + public static final int returnValue = -1; + /** + * The last node in the linked list. + */ + private Node last; + + /** + * An integer used for various purposes (currently unused). + */ + + // TODO: Remove unused variable 'i' if it's not needed in the future + private int i; + + /** + * Pushes a new integer onto the top of the linked list. + * If the list is empty, creates a new node as the last element. + * Otherwise, adds the new node to the end of the list. + * + * @param i the integer to be added to the list + */ public void push(int i) { if (last == null) last = new Node(i); @@ -15,26 +40,78 @@ public void push(int i) { } } + /** + * Checks if the linked list is empty. + * + * @return true if the list is empty, false otherwise + */ public boolean isEmpty() { return last == null; } + /** + * Checks if the linked list is full. + * This implementation always returns false since the list is dynamically sized. + * + * @return false always, as the list cannot be full + */ public boolean isFull() { return false; } + /** + * Returns the value of the last element in the list without removing it. + * If the list is empty, returns returnValue. + * + * @return the value of the last element, or returnValue if the list is empty + */ public int top() { if (isEmpty()) - return -1; + return returnValue; return last.value; } + /** + * Removes and returns the value of the last element in the list. + * If the list is empty, returns returnValue. + * + * @return the value of the last element, or returnValue if the list is empty + */ public int pop() { if (isEmpty()) - return -1; + return returnValue; int ret = last.value; last = last.prev; return ret; } + /** + * The Node class represents a node in a doubly linked list. + */ + private class Node { + /** + * The value stored in the node. + */ + private int value; + + /** + * A reference to the previous node in the list. + */ + private Node prev; + + /** + * A reference to the next node in the list. + */ + private Node next; + + /** + * Creates a new node with the specified value. + * + * @param i the value to be stored in the node + */ + private Node(int i) { + value = i; + } + } + } diff --git a/src/main/java/edu/kis/vh/nursery/list/IntLinkedListTest.java b/src/main/java/edu/kis/vh/nursery/list/IntLinkedListTest.java new file mode 100644 index 00000000..5d6b463c --- /dev/null +++ b/src/main/java/edu/kis/vh/nursery/list/IntLinkedListTest.java @@ -0,0 +1,56 @@ +package edu.kis.vh.nursery.list; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +public class IntLinkedListTest { + + @Test + public void testPush() { + IntLinkedList list = new IntLinkedList(); + assertTrue(list.isEmpty(), "List should be empty initially"); + + list.push(10); + assertFalse(list.isEmpty(), "List should not be empty after pushing an element"); + assertEquals(10, list.top(), "Top element should be 10"); + } + + @Test + public void testIsEmpty() { + IntLinkedList list = new IntLinkedList(); + assertTrue(list.isEmpty(), "List should be empty initially"); + + list.push(10); + assertFalse(list.isEmpty(), "List should not be empty after pushing an element"); + } + + @Test + public void testIsFull() { + IntLinkedList list = new IntLinkedList(); + assertFalse(list.isFull(), "List should never be full"); + } + + @Test + public void testTop() { + IntLinkedList list = new IntLinkedList(); + assertEquals(IntLinkedList.returnValue, list.top(), "Top of an empty list should return returnValue"); + + list.push(10); + assertEquals(10, list.top(), "Top element should be 10"); + + list.push(20); + assertEquals(20, list.top(), "Top element should be 20"); + } + + @Test + public void testPop() { + IntLinkedList list = new IntLinkedList(); + assertEquals(IntLinkedList.returnValue, list.pop(), "Pop from an empty list should return returnValue"); + + list.push(10); + list.push(20); + assertEquals(20, list.pop(), "Popped element should be 20"); + assertEquals(10, list.pop(), "Popped element should be 10"); + assertEquals(IntLinkedList.returnValue, list.pop(), "Pop from an empty list should return returnValue"); + } +} diff --git a/src/main/java/edu/kis/vh/nursery/list/Node.java b/src/main/java/edu/kis/vh/nursery/list/Node.java deleted file mode 100644 index 0a2cd75e..00000000 --- a/src/main/java/edu/kis/vh/nursery/list/Node.java +++ /dev/null @@ -1,12 +0,0 @@ -package edu.kis.vh.nursery.list; - -public class Node { - - public int value; - public Node prev, next; - - public Node(int i) { - value = i; - } - -} diff --git a/src/test/java/edu/kis/vh/nursery/RhymersDemo.java b/src/test/java/edu/kis/vh/nursery/RhymersDemo.java index 05aba9fa..63852077 100644 --- a/src/test/java/edu/kis/vh/nursery/RhymersDemo.java +++ b/src/test/java/edu/kis/vh/nursery/RhymersDemo.java @@ -1,7 +1,5 @@ package edu.kis.vh.nursery; -import edu.kis.vh.nursery.defaultCountingOutRhymer; -import edu.kis.vh.nursery.HanoiRhymer; import edu.kis.vh.nursery.factory.DefaultRhymersFactory; import edu.kis.vh.nursery.factory.Rhymersfactory; @@ -9,27 +7,28 @@ class RhymersDemo { public static void main(String[] args) { Rhymersfactory factory = new DefaultRhymersFactory(); - - defaultCountingOutRhymer[] rhymers = { factory.GetStandardRhymer(), factory.GetFalseRhymer(), - factory.GetFIFORhymer(), factory.GetHanoiRhymer()}; - + testRhymers(factory); + } + + private static void testRhymers(Rhymersfactory factory) { + DefaultCountingOutRhymer[] rhymers = { factory.getStandardRhymer(), factory.getFalseRhymer(), + factory.getFIFORhymer(), factory.getHanoiRhymer()}; + for (int i = 1; i < 15; i++) for (int j = 0; j < 3; j++) rhymers[j].countIn(i); - + java.util.Random rn = new java.util.Random(); for (int i = 1; i < 15; i++) rhymers[3].countIn(rn.nextInt(20)); - + for (int i = 0; i < rhymers.length; i++) { while (!rhymers[i].callCheck()) System.out.print(rhymers[i].countOut() + " "); System.out.println(); } - + System.out.println("total rejected is " + ((HanoiRhymer) rhymers[3]).reportRejected()); - } - -} \ No newline at end of file +} diff --git a/src/test/java/edu/kis/vh/nursery/RhymersJUnitTest.java b/src/test/java/edu/kis/vh/nursery/RhymersJUnitTest.java index 144906eb..330ee112 100644 --- a/src/test/java/edu/kis/vh/nursery/RhymersJUnitTest.java +++ b/src/test/java/edu/kis/vh/nursery/RhymersJUnitTest.java @@ -7,7 +7,8 @@ public class RhymersJUnitTest { @Test public void testCountIn() { - defaultCountingOutRhymer rhymer = new defaultCountingOutRhymer(); + DefaultCountingOutRhymer rhymer = new DefaultCountingOutRhymer(); + // TODO: Change the constant name to uppercase int testValue = 4; rhymer.countIn(testValue); @@ -17,7 +18,7 @@ public void testCountIn() { @Test public void testCallCheck() { - defaultCountingOutRhymer rhymer = new defaultCountingOutRhymer(); + DefaultCountingOutRhymer rhymer = new DefaultCountingOutRhymer(); boolean result = rhymer.callCheck(); Assert.assertEquals(true, result); @@ -29,7 +30,7 @@ public void testCallCheck() { @Test public void testIsFull() { - defaultCountingOutRhymer rhymer = new defaultCountingOutRhymer(); + DefaultCountingOutRhymer rhymer = new DefaultCountingOutRhymer(); final int STACK_CAPACITY = 12; for (int i = 0; i < STACK_CAPACITY; i++) { boolean result = rhymer.isFull(); @@ -43,12 +44,12 @@ public void testIsFull() { @Test public void testPeekaboo() { - defaultCountingOutRhymer rhymer = new defaultCountingOutRhymer(); + DefaultCountingOutRhymer rhymer = new DefaultCountingOutRhymer(); final int EMPTY_STACK_VALUE = -1; int result = rhymer.peekaboo(); Assert.assertEquals(EMPTY_STACK_VALUE, result); - + // TODO: Change the constant name to uppercase and change the name to make it easier to analyze the code int testValue = 4; rhymer.countIn(testValue); @@ -60,12 +61,12 @@ public void testPeekaboo() { @Test public void testCountOut() { - defaultCountingOutRhymer rhymer = new defaultCountingOutRhymer(); + DefaultCountingOutRhymer rhymer = new DefaultCountingOutRhymer(); final int EMPTY_STACK_VALUE = -1; int result = rhymer.countOut(); Assert.assertEquals(EMPTY_STACK_VALUE, result); - + // TODO: Change the constant name to uppercase and change the name to make it easier to analyze the code int testValue = 4; rhymer.countIn(testValue);