From 593a7b0c6fa3efef1168299c625a2db16445788c Mon Sep 17 00:00:00 2001 From: Anirudh Jonnavithula Date: Thu, 7 Apr 2016 17:14:47 -0400 Subject: [PATCH] AJ168 BML27 lab code --- .classpath | 7 +++ src/debug/ContainerArray.java | 35 +++++++++---- src/debug/ContainerArrayTest.java | 14 +++++- src/tdd/Sheet.java | 53 ++++++++++++++++++++ src/tdd/TestSheet.java | 83 +++++++++++++++++++++++++++++++ 5 files changed, 180 insertions(+), 12 deletions(-) create mode 100644 .classpath create mode 100644 src/tdd/Sheet.java create mode 100644 src/tdd/TestSheet.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..3e0fb27 --- /dev/null +++ b/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/src/debug/ContainerArray.java b/src/debug/ContainerArray.java index 124ece6..679bd2e 100755 --- a/src/debug/ContainerArray.java +++ b/src/debug/ContainerArray.java @@ -1,33 +1,48 @@ package debug; +import java.util.ArrayList; +import java.util.List; + public class ContainerArray { - private int initialCapacity = 10; - private int currentSize = 0; - private Object[] internalArray; + private int limit; + private List internalArray; public ContainerArray () { this(10); } - public ContainerArray (int initialCapacity) { - internalArray = new Object[initialCapacity]; + public ContainerArray (int limit) { + internalArray = new ArrayList<>(); + this.limit = limit; } public void add (E element) { - internalArray[currentSize++] = element; + if (size() < limit){ + internalArray.add(element); + } } public int size () { - return currentSize; + return internalArray.size(); } public void remove (E objectToRemove) { - currentSize--; + if (internalArray.contains(objectToRemove)){ + internalArray.remove(objectToRemove); + } } - @SuppressWarnings("unchecked") public E get (int index) { - return (E)internalArray[index]; + return internalArray.get(index); } + + public boolean contains(E element) { + for (int i = 0; i < internalArray.size(); i++ ){ + if (internalArray.get(i).equals(element)){ + return true; + } + } + return false; + } } diff --git a/src/debug/ContainerArrayTest.java b/src/debug/ContainerArrayTest.java index c566d50..fd7f175 100755 --- a/src/debug/ContainerArrayTest.java +++ b/src/debug/ContainerArrayTest.java @@ -12,6 +12,15 @@ public class ContainerArrayTest { public void setUp () { myContainer = new ContainerArray<>(); } + + @Test + public void testAddPastLimit (){ + for(int i=0; i<10;i++) { + myContainer.add("Eagle"); + } + myContainer.add("Hawk"); + assertFalse(myContainer.contains("Hawk")); + } @Test public void testSizeChangeWithAdd () { @@ -25,7 +34,7 @@ public void testSizeChangeWithAdd () { public void testObjectIsStored () { String alligator = "Alligator"; myContainer.add(alligator); - assertEquals("Add should be same reference", alligator, myContainer.get(0)); + assertTrue(myContainer.contains(alligator)); } @Test @@ -42,6 +51,7 @@ public void testObjectIsRemoved () { myContainer.add("Alligator"); myContainer.add("Bear"); myContainer.remove("Bear"); - assertEquals("Remove should be same reference", alligator, myContainer.get(0)); + assertFalse(myContainer.contains("Bear")); } + } diff --git a/src/tdd/Sheet.java b/src/tdd/Sheet.java new file mode 100644 index 0000000..f6beef8 --- /dev/null +++ b/src/tdd/Sheet.java @@ -0,0 +1,53 @@ +package tdd; + +import java.util.HashMap; +import java.util.Map; + +public class Sheet { + + private Map myMap; + + public Sheet() { + myMap = new HashMap<>(); + } + + public String get(String string) { + if(myMap.containsKey(string)) { + if(isNumeric(myMap.get(string))) { + return myMap.get(string).trim(); + } + else { + return myMap.get(string); + } + } + else { + return ""; + } + } + + public void put(String theCell, String string) { + myMap.put(theCell, string); + } + + private static boolean isNumeric(String str) + { + try + { + double d = Double.parseDouble(str); + } + catch(NumberFormatException nfe) + { + return false; + } + return true; + } + + public String getLiteral(String theCell) { + if(myMap.containsKey(theCell)) { + return myMap.get(theCell); + } + else { + return ""; + } + } +} \ No newline at end of file diff --git a/src/tdd/TestSheet.java b/src/tdd/TestSheet.java new file mode 100644 index 0000000..cb41723 --- /dev/null +++ b/src/tdd/TestSheet.java @@ -0,0 +1,83 @@ +package tdd; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class TestSheet { + + @Test + public void testThatCellsAreEmptyByDefault() { + Sheet sheet = new Sheet(); + assertEquals("", sheet.get("A1")); + assertEquals("", sheet.get("ZX347")); + } + + @Test + public void testThatTextCellsAreStored() { + Sheet sheet = new Sheet(); + String theCell = "A21"; + + sheet.put(theCell, "A string"); + assertEquals("A string", sheet.get(theCell)); + + sheet.put(theCell, "A different string"); + assertEquals("A different string", sheet.get(theCell)); + + sheet.put(theCell, ""); + assertEquals("", sheet.get(theCell)); + } + + @Test + public void testThatManyCellsExist() { + Sheet sheet = new Sheet(); + sheet.put("A1", "First"); + sheet.put("X27", "Second"); + sheet.put("ZX901", "Third"); + + assertEquals("A1", "First", sheet.get("A1")); + assertEquals("X27", "Second", sheet.get("X27")); + assertEquals("ZX901", "Third", sheet.get("ZX901")); + + sheet.put("A1", "Fourth"); + assertEquals("A1 after", "Fourth", sheet.get("A1")); + assertEquals("X27 same", "Second", sheet.get("X27")); + assertEquals("ZX901 same", "Third", sheet.get("ZX901")); + } + + @Test + public void testThatNumericCellsAreIdentifiedAndStored() { + Sheet sheet = new Sheet(); + String theCell = "A21"; + + sheet.put(theCell, "X99"); // "Obvious" string + assertEquals("X99", sheet.get(theCell)); + + sheet.put(theCell, "14"); // "Obvious" number + assertEquals("14", sheet.get(theCell)); + + sheet.put(theCell, " 99 X"); // Whole string must be numeric + assertEquals(" 99 X", sheet.get(theCell)); + + sheet.put(theCell, " 1234 "); // Blanks ignored + assertEquals("1234", sheet.get(theCell)); + + sheet.put(theCell, " "); // Just a blank + assertEquals(" ", sheet.get(theCell)); + } + + @Test + public void testThatWeHaveAccessToCellLiteralValuesForEditing() { + Sheet sheet = new Sheet(); + String theCell = "A21"; + + sheet.put(theCell, "Some string"); + assertEquals("Some string", sheet.getLiteral(theCell)); + + sheet.put(theCell, " 1234 "); + assertEquals(" 1234 ", sheet.getLiteral(theCell)); + + sheet.put(theCell, "=7"); // Foreshadowing formulas:) + assertEquals("=7", sheet.getLiteral(theCell)); + } +}