Repository: TheAlgorithms/Java
Pull request #5427 (2024-08-28 20:13:47)

refactor: `QueueUsingTwoStacks`
Class cleanup.
Readability improvements.
Added tests.

<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5427?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 50.15%. Comparing base [(`203d544`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/203d54466897201b9d832afec5f4120f6d6dcbcd?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`def0979`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/def0979ba43fdf0d6304bb16c4c14543183cda12?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5427      +/-   ##
============================================
+ Coverage     49.97%   50.15%   +0.18%     
- Complexity     3108     3120      +12     
============================================
  Files           524      524              
  Lines         15166    15143      -23     
  Branches       2884     2878       -6     
============================================
+ Hits           7579     7595      +16     
+ Misses         7271     7230      -41     
- Partials        316      318       +2     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5427?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/src/main/java/com/thealgorithms/others/QueueUsingTwoStacks.java b/src/main/java/com/thealgorithms/others/QueueUsingTwoStacks.java
index 259e108a..b43110d4 100644
--- a/src/main/java/com/thealgorithms/others/QueueUsingTwoStacks.java
+++ b/src/main/java/com/thealgorithms/others/QueueUsingTwoStacks.java
@@ -15,17 +15,14 @@ import java.util.Stack;
  *
  * @author sahilb2 (https://www.github.com/sahilb2)
  */
-class QueueWithStack {
-
-    // Stack to keep track of elements inserted into the queue
-    private Stack<Object> inStack;
-    // Stack to keep track of elements to be removed next in queue
-    private Stack<Object> outStack;
+public class QueueUsingTwoStacks {
+    private final Stack<Object> inStack;
+    private final Stack<Object> outStack;
 
     /**
      * Constructor
      */
-    QueueWithStack() {
+    public QueueUsingTwoStacks() {
         this.inStack = new Stack<>();
         this.outStack = new Stack<>();
     }
@@ -94,7 +91,7 @@ class QueueWithStack {
      * @return true if the inStack is empty.
      */
     public boolean isInStackEmpty() {
-        return (inStack.size() == 0);
+        return (inStack.isEmpty());
     }
 
     /**
@@ -103,73 +100,6 @@ class QueueWithStack {
      * @return true if the outStack is empty.
      */
     public boolean isOutStackEmpty() {
-        return (outStack.size() == 0);
-    }
-}
-
-/**
- * This class is the example for the Queue class
- *
- * @author sahilb2 (https://www.github.com/sahilb2)
- */
-public final class QueueUsingTwoStacks {
-    private QueueUsingTwoStacks() {
-    }
-
-    /**
-     * Main method
-     *
-     * @param args Command line arguments
-     */
-    public static void main(String[] args) {
-        QueueWithStack myQueue = new QueueWithStack();
-        myQueue.insert(1);
-        System.out.println(myQueue.peekBack()); // Will print 1
-        // instack: [(top) 1]
-        // outStack: []
-        myQueue.insert(2);
-        System.out.println(myQueue.peekBack()); // Will print 2
-        // instack: [(top) 2, 1]
-        // outStack: []
-        myQueue.insert(3);
-        System.out.println(myQueue.peekBack()); // Will print 3
-        // instack: [(top) 3, 2, 1]
-        // outStack: []
-        myQueue.insert(4);
-        System.out.println(myQueue.peekBack()); // Will print 4
-        // instack: [(top) 4, 3, 2, 1]
-        // outStack: []
-
-        System.out.println(myQueue.isEmpty()); // Will print false
-
-        System.out.println(myQueue.remove()); // Will print 1
-        System.out.println((myQueue.isInStackEmpty()) ? "null" : myQueue.peekBack()); // Will print NULL
-        // instack: []
-        // outStack: [(top) 2, 3, 4]
-
-        myQueue.insert(5);
-        System.out.println(myQueue.peekFront()); // Will print 2
-        // instack: [(top) 5]
-        // outStack: [(top) 2, 3, 4]
-
-        myQueue.remove();
-        System.out.println(myQueue.peekFront()); // Will print 3
-        // instack: [(top) 5]
-        // outStack: [(top) 3, 4]
-        myQueue.remove();
-        System.out.println(myQueue.peekFront()); // Will print 4
-        // instack: [(top) 5]
-        // outStack: [(top) 4]
-        myQueue.remove();
-        // instack: [(top) 5]
-        // outStack: []
-        System.out.println(myQueue.peekFront()); // Will print 5
-        // instack: []
-        // outStack: [(top) 5]
-        myQueue.remove();
-        // instack: []
-        // outStack: []
-
-        System.out.println(myQueue.isEmpty()); // Will print true
+        return (outStack.isEmpty());
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/others/QueueUsingTwoStacksTest.java b/src/test/java/com/thealgorithms/others/QueueUsingTwoStacksTest.java
new file mode 100644
index 00000000..4901b9c0
--- /dev/null
+++ b/src/test/java/com/thealgorithms/others/QueueUsingTwoStacksTest.java
@@ -0,0 +1,145 @@
+package com.thealgorithms.others;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.EmptyStackException;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class QueueUsingTwoStacksTest {
+
+    private QueueUsingTwoStacks queue;
+
+    @BeforeEach
+    void setUp() {
+        queue = new QueueUsingTwoStacks();
+    }
+
+    @Test
+    void testIsEmptyInitially() {
+        assertTrue(queue.isEmpty(), "Queue should be empty initially");
+    }
+
+    @Test
+    void testInsertSingleElement() {
+        queue.insert(1);
+        assertFalse(queue.isEmpty(), "Queue should not be empty after inserting an element");
+        assertEquals(1, queue.peekFront(), "The front element should be the inserted element");
+    }
+
+    @Test
+    void testRemoveSingleElement() {
+        queue.insert(1);
+        assertEquals(1, queue.remove(), "Removing should return the first inserted element");
+        assertTrue(queue.isEmpty(), "Queue should be empty after removing the only element");
+    }
+
+    @Test
+    void testRemoveMultipleElements() {
+        queue.insert(1);
+        queue.insert(2);
+        queue.insert(3);
+        assertEquals(1, queue.remove(), "First removed element should be the first inserted element");
+        assertEquals(2, queue.remove(), "Second removed element should be the second inserted element");
+        assertEquals(3, queue.remove(), "Third removed element should be the third inserted element");
+        assertTrue(queue.isEmpty(), "Queue should be empty after removing all elements");
+    }
+
+    @Test
+    void testPeekFrontWithMultipleElements() {
+        queue.insert(1);
+        queue.insert(2);
+        queue.insert(3);
+        assertEquals(1, queue.peekFront(), "The front element should be the first inserted element");
+    }
+
+    @Test
+    void testPeekBackWithMultipleElements() {
+        queue.insert(1);
+        queue.insert(2);
+        queue.insert(3);
+        assertEquals(3, queue.peekBack(), "The back element should be the last inserted element");
+    }
+
+    @Test
+    void testPeekFrontAfterRemovals() {
+        queue.insert(1);
+        queue.insert(2);
+        queue.insert(3);
+        queue.remove();
+        assertEquals(2, queue.peekFront(), "After removing one element, the front should be the second element");
+    }
+
+    @Test
+    void testIsEmptyAfterRemovals() {
+        queue.insert(1);
+        queue.insert(2);
+        queue.remove();
+        queue.remove();
+        assertTrue(queue.isEmpty(), "Queue should be empty after removing all elements");
+    }
+
+    @Test
+    void testRemoveFromEmptyQueue() {
+        org.junit.jupiter.api.Assertions.assertThrows(EmptyStackException.class, queue::remove, "Removing from an empty queue should throw an exception");
+    }
+
+    @Test
+    void testPeekFrontFromEmptyQueue() {
+        org.junit.jupiter.api.Assertions.assertThrows(EmptyStackException.class, queue::peekFront, "Peeking front from an empty queue should throw an exception");
+    }
+
+    @Test
+    void testPeekBackFromEmptyQueue() {
+        org.junit.jupiter.api.Assertions.assertThrows(EmptyStackException.class, queue::peekBack, "Peeking back from an empty queue should throw an exception");
+    }
+
+    @Test
+    void testIsInStackEmptyInitially() {
+        assertTrue(queue.isInStackEmpty(), "inStack should be empty initially");
+    }
+
+    @Test
+    void testIsOutStackEmptyInitially() {
+        assertTrue(queue.isOutStackEmpty(), "outStack should be empty initially");
+    }
+
+    @Test
+    void testIsInStackEmptyAfterInsertion() {
+        queue.insert(1);
+        assertFalse(queue.isInStackEmpty(), "inStack should not be empty after an insertion");
+    }
+
+    @Test
+    void testIsOutStackEmptyAfterInsertion() {
+        queue.insert(1);
+        assertTrue(queue.isOutStackEmpty(), "outStack should still be empty after an insertion");
+    }
+
+    @Test
+    void testIsOutStackEmptyAfterRemoval() {
+        queue.insert(1);
+        queue.remove();
+        assertTrue(queue.isOutStackEmpty(), "outStack should be empty after removing the only element");
+    }
+
+    @Test
+    void testIsInStackEmptyAfterMultipleRemovals() {
+        queue.insert(1);
+        queue.insert(2);
+        queue.remove();
+        queue.remove();
+        assertTrue(queue.isInStackEmpty(), "inStack should be empty after removing all elements");
+    }
+
+    @Test
+    void testIsOutStackEmptyAfterMultipleRemovals() {
+        queue.insert(1);
+        queue.insert(2);
+        queue.remove();
+        queue.remove();
+        assertTrue(queue.isOutStackEmpty(), "outStack should be empty after removing all elements");
+    }
+}