diff --git a/Paquet.java b/Paquet.java deleted file mode 100644 index 53c0364..0000000 --- a/Paquet.java +++ /dev/null @@ -1,110 +0,0 @@ -import java.util.*; - -public class Paquet implements Iterable{ - private int size; - private T[] tab; - public Iterator iterator(){ - return new Iterator() { - int currentIndex=0; - @Override - public boolean hasNext() { - return currentIndex index; i--) { - tab[i] = tab[i - 1]; - } - tab[index] = item; - size++; - - - } - public void remove(int index) { - if(index<0 || index>=size) { - System.out.println("\nwe can not remove element maybe because index "+index+" is out of Bounds."); - } - else { - for(int i=index;i=size) { - throw new ArrayIndexOutOfBoundsException(); - } - return tab[index]; - } - public String toString() { - String s="Les elements de notre Collection sont :"; - Iterator it = this.iterator(); - while(it.hasNext()) { - s=s+" "+it.next(); - } - return s; - } - public boolean isEmpty() { - return size == 0; - } - public T set(int index, T newVal) { - if (index < 0 || index >= size) throw new ArrayIndexOutOfBoundsException(); - T old = tab[index]; - tab[index] = newVal; - return old; - } - public static void main(String[] args) { - Paquet p =new Paquet(); - try { - p.add(10); - p.add(20); - p.add(30); - p.add(40); - p.add(50); - System.out.println(p.get(0)); - Iterator it2 = p.iterator(); - while(it2.hasNext()) { - System.out.print(it2.next()+" "); - } - p.remove(4); - System.out.println(); - Iterator it = p.iterator(); - while(it.hasNext()) { - System.out.print(it.next()+" "); - } - System.out.println(); - System.out.println(p.toString()); - }catch(Exception e) { - e.getMessage(); - } - } - -} diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..8640a65 --- /dev/null +++ b/pom.xml @@ -0,0 +1,54 @@ + + + 4.0.0 + io.github.kaoutharelbakouri + kaoutharelbakouri-implement-arraylist-from-scratch + 1.0.0-SNAPSHOT + jar + ${project.groupId}:${project.artifactId} + Java Code to Implement an ArrayList From Scratch + + + UTF-8 + 1.8 + 1.8 + + + + + + maven-compiler-plugin + 3.8.0 + + ${maven.compiler.source} + ${maven.compiler.target} + + + + maven-surefire-plugin + 2.22.2 + + + maven-failsafe-plugin + 2.22.2 + + + + + + + org.junit.jupiter + junit-jupiter + 5.8.2 + test + + + org.junit.vintage + junit-vintage-engine + 5.8.2 + test + + + \ No newline at end of file diff --git a/src/main/java/io/github/kaoutharelbakouri/Paquet.java b/src/main/java/io/github/kaoutharelbakouri/Paquet.java new file mode 100644 index 0000000..3d0e343 --- /dev/null +++ b/src/main/java/io/github/kaoutharelbakouri/Paquet.java @@ -0,0 +1,97 @@ +package io.github.kaoutharelbakouri; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class Paquet implements Iterable { + private int size; + private T[] tab; + + public Iterator iterator() { + return new Iterator() { + int currentIndex = 0; + + @Override + public boolean hasNext() { + return currentIndex < size; + } + + @Override + public T next() { + if (!hasNext()) throw new NoSuchElementException(); + return tab[currentIndex++]; + } + + @Override + public void remove() { + Paquet.this.remove(--currentIndex); + } + }; + } + + @SuppressWarnings("unchecked") + public Paquet() { + size = 0; + tab = (T[]) new Object[10]; + } + + public void add(T e) { + if (size == tab.length) { + tab = Arrays.copyOf(tab, size * 2 + 1); + } + tab[size++] = e; + } + + public void add(int index, T item) { + checkArrayIndexOutOfBounds(index); + if (size == tab.length) { + tab = Arrays.copyOf(tab, size * 2 + 1); + } + for (int i = size; i > index; i--) { + tab[i] = tab[i - 1]; + } + tab[index] = item; + size++; + } + + public void remove(int index) { + checkArrayIndexOutOfBounds(index); + for (int i = index; i < size - 1; i++) { + tab[i] = tab[i + 1]; + } + size--; + } + + public int getSize() { + return size; + } + + public T get(int index) { + checkArrayIndexOutOfBounds(index); + return tab[index]; + } + + public String toString() { + StringBuilder s = new StringBuilder("Les elements de notre Collection sont :"); + for (T t : this) { + s.append(" ").append(t); + } + return s.toString(); + } + + public boolean isEmpty() { + return size == 0; + } + + public void set(int index, T newVal) { + checkArrayIndexOutOfBounds(index); + tab[index] = newVal; + } + + private void checkArrayIndexOutOfBounds(int index) throws ArrayIndexOutOfBoundsException { + if (index < 0 || index >= size) { + throw new ArrayIndexOutOfBoundsException(index + " is out of Bounds"); + } + } +} diff --git a/src/test/java/io/github/kaoutharelbakouri/PaquetTest.java b/src/test/java/io/github/kaoutharelbakouri/PaquetTest.java new file mode 100644 index 0000000..d5ef61b --- /dev/null +++ b/src/test/java/io/github/kaoutharelbakouri/PaquetTest.java @@ -0,0 +1,164 @@ +package io.github.kaoutharelbakouri; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.function.Executable; + +import java.util.Iterator; + +import static org.junit.jupiter.api.Assertions.*; + +@TestInstance(TestInstance.Lifecycle.PER_METHOD) +public class PaquetTest { + + private static final Integer FIRST_ELEMENT = 12; + private static final Integer SECOND_ELEMENT = 45; + private Paquet paquet; + + @BeforeEach + public void init() { + paquet = new Paquet<>(); + + // Add some arbitrary values + paquet.add(FIRST_ELEMENT); + paquet.add(SECOND_ELEMENT); + } + + @DisplayName("iterator method should return Iterator") + @Test + public void should_return_iterator() { + Iterator iterator = paquet.iterator(); + assertNotNull(iterator); + } + + @DisplayName("add method with one argument should add element to the last position of array") + @Test + public void should_add_with_one_arg_success() { + paquet.add(4); + paquet.add(18); + int lastIndex = paquet.getSize() - 1; + try { + // check that last added value is on the last position (which its index equals 2) of the array + assertEquals(paquet.get(lastIndex), 18); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @DisplayName("add method with two arguments should add element to the given index of array") + @Test + public void should_add_with_two_arg_success() { + int index = 1; + Integer valueToAdd = 845; + + // Add `valueToAdd` at `index` + paquet.add(index, valueToAdd); + try { + assertEquals(paquet.get(index), valueToAdd); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @DisplayName("add method with two arguments should throw exception if given index is out of bounds") + @Test + public void should_add_with_two_arg_fail() { + int index = 34; + Integer valueToAdd = 13; + assertThrowsArrayIndexOuOfBounds(index, () -> paquet.add(index, valueToAdd)); + } + + @DisplayName("remove method should remove element if exists in the array") + @Test + public void should_remove_success() { + try { + // The first element of the array was added in the method `init()` + assertEquals(paquet.get(0), FIRST_ELEMENT); + + // remove the first element + paquet.remove(0); + + // The first element is removed successfully and the second one got its position + assertNotEquals(FIRST_ELEMENT, paquet.get(0)); + assertEquals(SECOND_ELEMENT, paquet.get(0)); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @DisplayName("remove method should throw exception if given index is out of bounds") + @Test + public void should_remove_fail() { + int index = 34; + assertThrowsArrayIndexOuOfBounds(index, () -> paquet.remove(index)); + } + + @DisplayName("size method should return length of array") + @Test + public void should_return_size() { + // We added two values in `init()` method. So, the `expectedSize` would be equal to 2 + int expectedSize = 2; + assertEquals(expectedSize, paquet.getSize()); + } + + @DisplayName("get method should return element of given index") + @Test + public void should_get_element_of_index() { + try { + assertEquals(FIRST_ELEMENT, paquet.get(0)); + assertEquals(SECOND_ELEMENT, paquet.get(1)); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @DisplayName("get method should throw exception if given index is out of bounds") + @Test + public void should_get_fail() { + int index = 21; + assertThrowsArrayIndexOuOfBounds(index, () -> paquet.get(index)); + } + + @DisplayName("toString method should return a string containing formatted data of the object") + @Test + public void should_return_data_of_object() { + String expected = "Les elements de notre Collection sont : 12 45"; + assertEquals(expected, paquet.toString()); + } + + @DisplayName("isEmpty method should return a boolean true if array is empty") + @Test + public void should_return_true_if_empty() { + assertFalse(paquet.isEmpty()); + + paquet = new Paquet<>(); + assertTrue(paquet.isEmpty()); + } + + @DisplayName("set method should edit an element of a given index") + @Test + public void should_set_element_of_index() { + Integer newFirstValue = 152; + paquet.set(0, newFirstValue); + try { + assertEquals(newFirstValue, paquet.get(0)); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @DisplayName("set method should throw exception if given index is out of bounds") + @Test + public void should_set_fail() { + int index = 121; + assertThrowsArrayIndexOuOfBounds(index, () -> paquet.set(index, 45)); + } + + private void assertThrowsArrayIndexOuOfBounds(int index, Executable executable) { + String expectedMessage = index + " is out of Bounds"; + Exception exception = assertThrows(ArrayIndexOutOfBoundsException.class, executable); + assertEquals(expectedMessage, exception.getMessage()); + } +}