diff --git a/src/debug/ContainerArray.java b/src/debug/ContainerArray.java index 124ece6..a1c4d3e 100755 --- a/src/debug/ContainerArray.java +++ b/src/debug/ContainerArray.java @@ -1,33 +1,39 @@ package debug; +import java.util.ArrayList; public class ContainerArray { - private int initialCapacity = 10; - private int currentSize = 0; - private Object[] internalArray; - - public ContainerArray () { - this(10); - } - - public ContainerArray (int initialCapacity) { - internalArray = new Object[initialCapacity]; - } - - public void add (E element) { - internalArray[currentSize++] = element; - } - - public int size () { - return currentSize; - } - - public void remove (E objectToRemove) { - currentSize--; - } - - @SuppressWarnings("unchecked") - public E get (int index) { - return (E)internalArray[index]; - } + private int initialCapacity = 10; + private int currentSize = 0; + private ArrayList internalArray; + + public ContainerArray() { + this(10); + } + + public ContainerArray(int initialCapacity) { + internalArray = new ArrayList<>(); + } + + public void add(E element) { + internalArray.add(element); + } + + public int size() { + return internalArray.size(); + } + + public void remove(E objectToRemove) { + internalArray.remove(objectToRemove); + } + + @SuppressWarnings("unchecked") + public E get(int index) { + + try { + return (E) internalArray.get(index); + } catch (Exception e) { + return null; + } + } } diff --git a/src/debug/ContainerArrayTest.java b/src/debug/ContainerArrayTest.java index c566d50..a825965 100755 --- a/src/debug/ContainerArrayTest.java +++ b/src/debug/ContainerArrayTest.java @@ -12,7 +12,52 @@ public class ContainerArrayTest { public void setUp () { myContainer = new ContainerArray<>(); } - + + @Test + public void testGetNegative() + { + assertEquals("Get negative", null, myContainer.get(-10)); + } + + @Test + public void testRemoveMany() + { + for(int i = 0; i < 100; i++) + myContainer.remove(Integer.toString(i)); + + assertEquals("Removes many", 0, myContainer.size()); + } + + @Test + public void testAddMany() + { + for(int i = 0; i < 100; i++) + myContainer.add(Integer.toString(i)); + + assertEquals("Add many", 100, myContainer.size()); + } + + @Test + public void testGetUnset() + { + myContainer.add("Alligator"); + myContainer.add("Bear"); + myContainer.add("Camel"); + + assertEquals("Get unset item", null, myContainer.get(10)); + } + + @Test + public void testRemove() + { + myContainer.add("Alligator"); + myContainer.add("Bear"); + myContainer.add("Camel"); + myContainer.remove("Alligator"); + + assertEquals("Remove object", "Bear", myContainer.get(0)); + } + @Test public void testSizeChangeWithAdd () { myContainer.add("Alligator"); diff --git a/src/tdd/ContainerArray.java b/src/tdd/ContainerArray.java new file mode 100644 index 0000000..5880acb --- /dev/null +++ b/src/tdd/ContainerArray.java @@ -0,0 +1,33 @@ +package debug; + + +public class ContainerArray { + private int initialCapacity = 10; + private int currentSize = 0; + private Object[] internalArray; + + public ContainerArray () { + this(10); + } + + public ContainerArray (int initialCapacity) { + internalArray = new Object[initialCapacity]; + } + + public void add (E element) { //more than 10 items means error, need to copy to new array + internalArray[currentSize++] = element; + } + + public int size () { + return currentSize; + } + + public void remove (E objectToRemove) { //removing item which isn't in container, also item still stays there, + currentSize--; //only works as stack right now + } //may result in negative size + + @SuppressWarnings("unchecked") + public E get (int index) { //negative index, index out of bounds + return (E)internalArray[index]; + } +} diff --git a/src/tdd/ContainerArrayTest.java b/src/tdd/ContainerArrayTest.java new file mode 100644 index 0000000..c566d50 --- /dev/null +++ b/src/tdd/ContainerArrayTest.java @@ -0,0 +1,47 @@ +package debug; + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; + + +public class ContainerArrayTest { + private ContainerArray myContainer = null; + + @Before + public void setUp () { + myContainer = new ContainerArray<>(); + } + + @Test + public void testSizeChangeWithAdd () { + myContainer.add("Alligator"); + myContainer.add("Bear"); + myContainer.add("Camel"); + assertEquals("Add size", 3, myContainer.size()); + } + + @Test + public void testObjectIsStored () { + String alligator = "Alligator"; + myContainer.add(alligator); + assertEquals("Add should be same reference", alligator, myContainer.get(0)); + } + + @Test + public void testSizeChangeWithRemove () { + myContainer.add("Alligator"); + myContainer.add("Bear"); + myContainer.remove("Alligator"); + assertEquals("Remove size", 1, myContainer.size()); + } + + @Test + public void testObjectIsRemoved () { + String alligator = "Alligator"; + myContainer.add("Alligator"); + myContainer.add("Bear"); + myContainer.remove("Bear"); + assertEquals("Remove should be same reference", alligator, myContainer.get(0)); + } +} diff --git a/src/tdd/Sheet.java b/src/tdd/Sheet.java new file mode 100644 index 0000000..b3f61c8 --- /dev/null +++ b/src/tdd/Sheet.java @@ -0,0 +1,41 @@ +package debug; + +import java.util.HashMap; +import java.util.Map; + +import com.sun.xml.internal.ws.util.StringUtils; + +public class Sheet { + + Map data; + + public Sheet() { + data = new HashMap(); + } + + public void put(String cell, String value) { + data.put(cell, value); + } + + public String get(String cell) { + if (data.containsKey(cell)) { + String newData = data.get(cell).trim(); + if (newData.length() != 0 && newData.matches("[A-Za-z0-9]+")) { + return newData; + } else { + return data.get(cell); + } + } else { + return ""; + } + } + + public String getLiteral(String cell) { + if (data.containsKey(cell)) { + return data.get(cell); + } else { + return ""; + } + } + +} diff --git a/src/tdd/SheetTest.java b/src/tdd/SheetTest.java new file mode 100644 index 0000000..ff9884e --- /dev/null +++ b/src/tdd/SheetTest.java @@ -0,0 +1,98 @@ +package debug; + +import static org.junit.Assert.*; +import org.junit.Test; + + +public class SheetTest { + + + + @Test + public void testThatCellsAreEmptyByDefault() { + Sheet sheet = new Sheet(); + assertEquals("", sheet.get("A1")); + assertEquals("", sheet.get("ZX347")); + } + + // Implement each test before going to the next one. + + @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)); + } + + // Implement each test before going to the next one; then refactor. + + @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")); + } + + + // Implement each test before going to the next one. + // You can split this test case if it helps. + + @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)); + } + + // Refactor before going to each succeeding test. + + @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)); + } + + + +} diff --git a/src/voogasalad/AuthSerializer.java b/src/voogasalad/AuthSerializer.java new file mode 100644 index 0000000..3297eab --- /dev/null +++ b/src/voogasalad/AuthSerializer.java @@ -0,0 +1,82 @@ +package game_data; + +import com.thoughtworks.xstream.XStream; +import javax.swing.JFileChooser; +import javax.swing.filechooser.FileNameExtensionFilter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.* + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; + +public class AuthSerializer { + public static void SerializeData(Object o) { + + File f = pickFile(true); + XStream xstream = new XStream(); + String xml = xstream.toXML(o); + + try { + FileWriter writer = new FileWriter(f); + writer.write(xml); + writer.flush(); + writer.close(); + } catch (IOException e) { + System.out.println("Error saving to file " + f.getAbsolutePath()); + } + } + + private static File pickFile(boolean amSaving) { + JFileChooser fileChooser = new JFileChooser(); + fileChooser.setCurrentDirectory(new File(System.getProperty("user.home"))); + FileNameExtensionFilter xmlFilter = new FileNameExtensionFilter("xml files (*.xml)", "xml"); + fileChooser.addChoosableFileFilter(xmlFilter); + fileChooser.setFileFilter(xmlFilter); + + int result = 0; + if (amSaving) + result = fileChooser.showSaveDialog(null); + else + result = fileChooser.showOpenDialog(null); + + if (result != JFileChooser.APPROVE_OPTION) + return null; + + return fileChooser.getSelectedFile(); + } + + public static Object Deserialize() { + File f = pickFile(false); + XStream xstream = new XStream(); + + return xstream.fromXML(f); + } + + @Test + public void testArrayList() + { + ArrayList list = new ArrayList(); + for(int i = 0; i < 100; i++) + list.add(i); + + SerializeData(list); + Object o = Deserialize(); + assertEquals("Test ArrayList", o, list); + } + + + @Test + public void testArrayListString() + { + ArrayList list = new ArrayList(); + for(int i = 0; i < 100; i++) + list.add(Integer.toString(i)); + + SerializeData(list); + Object o = Deserialize(); + assertEquals("Test ArrayList with String", o, list); + } +} diff --git a/src/voogasalad/HUDTest.java b/src/voogasalad/HUDTest.java new file mode 100644 index 0000000..e05ab90 --- /dev/null +++ b/src/voogasalad/HUDTest.java @@ -0,0 +1,38 @@ +package voogasalad; + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; + + +public class HUDTest { + + public class ContainerArrayTest { + private HUDScreen; + + @Before + public void setUp () { + myHUD = new HUDScreen(); + } + + @Test + public void testAdd() { + myHUD.add("Health", 100); + assertequals(myHUD.get("Health"), 100); + } + + @Test + public void testRemove() { + myHUD.remove("Health", 100); + assertnull(myHUD.get("Health")); + } + + @Test + public void testMiss() { + assertnull(myHUD.get("example")); + } + + + + +} \ No newline at end of file