- Generified Kata class
- Added extensive testing
- GetPrivateAttribute
- Remove
- Add
- MergeLinkedList
- MergeKata
import java.util.LinkedList; public class Kata<T> { private LinkedList<T> linkedList; public Kata() { this.linkedList = new LinkedList<>(); } public Kata(LinkedList<T> list) { this.linkedList = list; } public Kata(Kata<T> kata) { this.linkedList = kata.linkedList; } public LinkedList<T> getLinkedList(){ return this.linkedList; } public void AddItem(T itemToAdd, T whereToAddIt) { this.linkedList.add(this.linkedList.indexOf(whereToAddIt) + 1, itemToAdd); } public void RemoveItem(T whereToRemoveIt) { this.linkedList.remove(whereToRemoveIt); } public void MergeLists(LinkedList<T> listToMerge) { this.linkedList.addAll(listToMerge); } public void MergeKatas(Kata<T> kataToMerge) { this.linkedList.addAll(kataToMerge.linkedList); } }
import java.util.*;- import java.util.LinkedList;
public class Kata {private LinkedList<String> linkedList;public static void AddItem(String itemToAdd, String whereToAddIt) {// When given a string, whereToAddIt, find that string// in the LinkedList and add itemToAdd directly after whereToAddIt// in the LinkedList}public static void RemoveItem(String whereToRemoveIt) {// When given a string, whereToRemoveIt, find that string// in the LinkedList and remove it from the linked list- public class Kata<T> {
- private LinkedList<T> linkedList;
- public Kata() {
- this.linkedList = new LinkedList<>();
- }
public static void MergeLists(LinkedList<String> listToMerge) {// Merge this linked list with the previous- public Kata(LinkedList<T> list) {
- this.linkedList = list;
- }
- public Kata(Kata<T> kata) {
- this.linkedList = kata.linkedList;
- }
- public LinkedList<T> getLinkedList(){
- return this.linkedList;
- }
- public void AddItem(T itemToAdd, T whereToAddIt) {
- this.linkedList.add(this.linkedList.indexOf(whereToAddIt) + 1, itemToAdd);
- }
- public void RemoveItem(T whereToRemoveIt) {
- this.linkedList.remove(whereToRemoveIt);
- }
- public void MergeLists(LinkedList<T> listToMerge) {
- this.linkedList.addAll(listToMerge);
- }
- public void MergeKatas(Kata<T> kataToMerge) {
- this.linkedList.addAll(kataToMerge.linkedList);
- }
- }
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.util.Arrays; import java.util.LinkedList; class SolutionTest { private Kata<String> kata; @BeforeEach void initList(){ this.kata = new Kata<>(new LinkedList<>(Arrays.asList("Orange", "Apple", "Banana", "Pear"))); } //region GetLinkedList @Test void getLinkedList() { try { this.kata.getLinkedList(); } catch (Exception e) { Assertions.fail("Unable to retrieve linked list"); } } //endregion //region AddItem @Test void addItemFirstIndex() { this.kata.AddItem("Peach", "Orange"); Assertions.assertEquals(Arrays.asList("Orange", "Peach", "Apple", "Banana", "Pear"), this.kata.getLinkedList()); } @Test void addItemLastIndex() { this.kata.AddItem("Dragonfruit", "Pear"); Assertions.assertEquals(Arrays.asList("Orange", "Apple", "Banana", "Pear", "Dragonfruit"), this.kata.getLinkedList()); } @Test void addItemMiddleIndex() { this.kata.AddItem("Watermelon", "Apple"); Assertions.assertEquals(Arrays.asList("Orange", "Apple", "Watermelon", "Banana", "Pear"), this.kata.getLinkedList()); } //endregion //region RemoveItem @Test void removeValidElement() { this.kata.RemoveItem("Apple"); Assertions.assertEquals(Arrays.asList("Orange", "Banana", "Pear"), this.kata.getLinkedList()); } @Test void removeInvalidElement() { this.kata.RemoveItem("Dragonfruit"); Assertions.assertEquals(Arrays.asList("Orange", "Apple", "Banana", "Pear"), this.kata.getLinkedList()); } //endregion //region MergeLists @Test void mergeList() { LinkedList<String> mergingList = new LinkedList<>(Arrays.asList("Dragonfruit", "Papaya")); this.kata.MergeLists(mergingList); Assertions.assertEquals(Arrays.asList("Orange", "Apple", "Banana", "Pear", "Dragonfruit", "Papaya"), this.kata.getLinkedList()); } //endregion //region MergeKatas @Test void mergeKatas() { Kata<String> mergingKata = new Kata<String>(new LinkedList<>(Arrays.asList("Dragonfruit", "Kiwi"))); this.kata.MergeKatas(mergingKata); Assertions.assertEquals(Arrays.asList("Orange", "Apple", "Banana", "Pear", "Dragonfruit", "Kiwi"), this.kata.getLinkedList()); } //endregion }
- import org.junit.jupiter.api.Assertions;
- import org.junit.jupiter.api.BeforeEach;
- import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;// TODO: Write valid tests to prove your code- import java.util.Arrays;
- import java.util.LinkedList;
- class SolutionTest {
- private Kata<String> kata;
- @BeforeEach
- void initList(){
- this.kata = new Kata<>(new LinkedList<>(Arrays.asList("Orange", "Apple", "Banana", "Pear")));
- }
- //region GetLinkedList
- @Test
void testSomething() {// assertEquals("expected", "actual");- void getLinkedList() {
- try {
- this.kata.getLinkedList();
- } catch (Exception e) {
- Assertions.fail("Unable to retrieve linked list");
- }
- }
- //endregion
- //region AddItem
- @Test
- void addItemFirstIndex() {
- this.kata.AddItem("Peach", "Orange");
- Assertions.assertEquals(Arrays.asList("Orange", "Peach", "Apple", "Banana", "Pear"), this.kata.getLinkedList());
- }
- @Test
- void addItemLastIndex() {
- this.kata.AddItem("Dragonfruit", "Pear");
- Assertions.assertEquals(Arrays.asList("Orange", "Apple", "Banana", "Pear", "Dragonfruit"), this.kata.getLinkedList());
- }
- @Test
- void addItemMiddleIndex() {
- this.kata.AddItem("Watermelon", "Apple");
- Assertions.assertEquals(Arrays.asList("Orange", "Apple", "Watermelon", "Banana", "Pear"), this.kata.getLinkedList());
- }
- //endregion
- //region RemoveItem
- @Test
- void removeValidElement() {
- this.kata.RemoveItem("Apple");
- Assertions.assertEquals(Arrays.asList("Orange", "Banana", "Pear"), this.kata.getLinkedList());
- }
- @Test
- void removeInvalidElement() {
- this.kata.RemoveItem("Dragonfruit");
- Assertions.assertEquals(Arrays.asList("Orange", "Apple", "Banana", "Pear"), this.kata.getLinkedList());
- }
- //endregion
- //region MergeLists
- @Test
- void mergeList() {
- LinkedList<String> mergingList = new LinkedList<>(Arrays.asList("Dragonfruit", "Papaya"));
- this.kata.MergeLists(mergingList);
- Assertions.assertEquals(Arrays.asList("Orange", "Apple", "Banana", "Pear", "Dragonfruit", "Papaya"), this.kata.getLinkedList());
- }
- //endregion
- //region MergeKatas
- @Test
- void mergeKatas() {
- Kata<String> mergingKata = new Kata<String>(new LinkedList<>(Arrays.asList("Dragonfruit", "Kiwi")));
- this.kata.MergeKatas(mergingKata);
- Assertions.assertEquals(Arrays.asList("Orange", "Apple", "Banana", "Pear", "Dragonfruit", "Kiwi"), this.kata.getLinkedList());
- }
- //endregion
- }