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..bce3abf 100755 --- a/src/debug/ContainerArray.java +++ b/src/debug/ContainerArray.java @@ -2,7 +2,7 @@ public class ContainerArray { - private int initialCapacity = 10; + private int limit; private int currentSize = 0; private Object[] internalArray; @@ -10,12 +10,25 @@ public ContainerArray () { this(10); } - public ContainerArray (int initialCapacity) { - internalArray = new Object[initialCapacity]; + public ContainerArray (int limit) { + this.limit = limit; + internalArray = new Object[limit]; } public void add (E element) { - internalArray[currentSize++] = element; + int currentIndex = -1; + for(int i = 0; i < limit; i++) { + if(internalArray[i] == null) { + currentIndex = i; + break; + } + } + try { + internalArray[currentIndex] = element; + currentSize++; + } catch(Exception e) { + throw new ArrayIndexOutOfBoundsException(); + } } public int size () { @@ -23,7 +36,12 @@ public int size () { } public void remove (E objectToRemove) { - currentSize--; + for(int i = 0; i < currentSize; i++) { + if(internalArray[i] == objectToRemove) { + internalArray[i] = null; + currentSize--; + } + } } @SuppressWarnings("unchecked") diff --git a/src/debug/ContainerArrayTest.java b/src/debug/ContainerArrayTest.java index c566d50..e6b28bd 100755 --- a/src/debug/ContainerArrayTest.java +++ b/src/debug/ContainerArrayTest.java @@ -35,7 +35,24 @@ public void testSizeChangeWithRemove () { myContainer.remove("Alligator"); assertEquals("Remove size", 1, myContainer.size()); } + + @Test + public void testObjectGoneWithRemove () { + myContainer.add("Alligator"); + myContainer.add("Bear"); + myContainer.remove("Alligator"); + assertEquals("Object should be removed", null, myContainer.get(0)); + } + @Test + public void testObjectToRemoveExists () { + myContainer.add("Alligator"); + myContainer.add("Bear"); + myContainer.remove("Cat"); + assertEquals("Size should not decrease if object does not exist", 2, myContainer.size()); + } + + @Test public void testObjectIsRemoved () { String alligator = "Alligator"; @@ -44,4 +61,32 @@ public void testObjectIsRemoved () { myContainer.remove("Bear"); assertEquals("Remove should be same reference", alligator, myContainer.get(0)); } + + @Test + public void testRemoveThenAdd () { + myContainer.add("Alligator"); + myContainer.add("Bear"); + myContainer.add("Cat"); + myContainer.add("Duck"); + myContainer.remove("Bear"); + myContainer.add("Banana"); + assertEquals("Adding shouldn't overwrite", "Duck", myContainer.get(3)); + } + + @Test(expected = ArrayIndexOutOfBoundsException.class) + public void testOverflowCapacity () { + for(int i = 0; i < 11; i++) { + myContainer.add("Alligator"); + } + assertEquals("Adding shouldn't overwrite", "Alligator", myContainer.get(0)); + } + + @Test + public void testLimit () { + myContainer = new ContainerArray<>(20); + for(int i = 0; i < 15; i++) { + myContainer.add("Alligator"); + } + assertEquals("Custom constructor should update limit", "Alligator", myContainer.get(0)); + } } diff --git a/src/tdd/Sheet.java b/src/tdd/Sheet.java new file mode 100644 index 0000000..cd97e7c --- /dev/null +++ b/src/tdd/Sheet.java @@ -0,0 +1,35 @@ +package tdd; + +import java.util.HashMap; +import java.util.Map; + +public class Sheet { + + private Map sheetMap; + + public Sheet() { + this.sheetMap = new HashMap(); + } + + public String get(String theCell) { + if(this.sheetMap.containsKey(theCell)) { + String testIfNumber = this.sheetMap.get(theCell).replace(" ", ""); + try { + Integer.parseInt(testIfNumber); + return testIfNumber; + } catch(NumberFormatException e) { + return this.sheetMap.get(theCell); + } + } + return ""; + } + + public void put(String theCell, String string) { + this.sheetMap.put(theCell, string); + } + + public String getLiteral(String theCell) { + return this.sheetMap.get(theCell); + } + +} diff --git a/src/tdd/TestSheet.java b/src/tdd/TestSheet.java new file mode 100644 index 0000000..42fac5b --- /dev/null +++ b/src/tdd/TestSheet.java @@ -0,0 +1,93 @@ +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")); + } + + // 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/ResourceDecipherer.java b/src/voogasalad/ResourceDecipherer.java new file mode 100644 index 0000000..479cc74 --- /dev/null +++ b/src/voogasalad/ResourceDecipherer.java @@ -0,0 +1,69 @@ +package voogasalad; +import java.util.ResourceBundle; + +/** + * A decrypter of file paths, in order to determine what kind of file a user + * is trying to import. + * @author DoovalSalad + * + */ +public class ResourceDecipherer { + + private static ResourceBundle fileExtensions; + private static final String IMAGE = "IMAGE"; + private static final String AUDIO = "AUDIO"; + + /** + * Returns a VoogaFileType enum depending on the path and extension of the file + * @param path: path of the image + * @return a VoogaFileType (IMAGE or AUDIO) + */ + public static VoogaFileType decipherName(String path) { + fileExtensions = VoogaBundles.extensionProperties; + if(fileExtensions.containsKey(getExtension(path, '.'))) { + return VoogaFileType.valueOf(fileExtensions.getString(getExtension(path, '.'))); + } + return null; + + } + + /** + * Determines if a file being imported is an image + * @param path: the path of the item + * @return: true if it is valid + */ + public static boolean isImage(String path) { + if(decipherName(path) == null) return false; + return decipherName(path).name().equals(IMAGE); + } + + /** + * Determines if a file being imported is an audio file + * @param path: the path of the item + * @return: true if it is valid + */ + public static boolean isAudio(String path) { + if(decipherName(path) == null) return false; + return decipherName(path).name().equals(AUDIO); + } + + /** + * Returns the extension given the path and delimiter to use. For example, + * would return "png" if a PNG file was passed as a parameter, along with the + * '.' delimiter to separate extension from the name. + * @param path: path of the item + * @param delimiter: character with which to separate + * @return + */ + public static String getExtension(String path, char delimiter) { + String extension = path; + + int i = path.lastIndexOf(delimiter); + if (i > 0) { + extension = path.substring(i+1).toLowerCase(); + } + + return extension; + } + +} diff --git a/src/voogasalad/ResourceDeciphererTest.java b/src/voogasalad/ResourceDeciphererTest.java new file mode 100644 index 0000000..37dc918 --- /dev/null +++ b/src/voogasalad/ResourceDeciphererTest.java @@ -0,0 +1,27 @@ +package voogasalad; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class ResourceDeciphererTest { + + @Test + public void testIsImage() { + assertEquals("Should be an image", true, ResourceDecipherer.isImage("hello.png")); + assertEquals("Should be an image", true, ResourceDecipherer.isImage("C:\\User\\Programs\\hello.gif")); + assertEquals("Should be an image", true, ResourceDecipherer.isImage("file:/../hello.bmp")); + assertEquals("Should be an image", false, ResourceDecipherer.isImage("hello.wav")); + assertEquals("Should be an image", false, ResourceDecipherer.isImage("hello.jpgg")); + } + + @Test + public void testIsAudio() { + assertEquals("Should be an audio", true, ResourceDecipherer.isAudio("hello.wav")); + assertEquals("Should be an audio", true, ResourceDecipherer.isAudio("C:\\User\\Programs\\hello.mp3")); + assertEquals("Should be an audio", true, ResourceDecipherer.isAudio("file:/../hello.wav")); + assertEquals("Should be an audio", false, ResourceDecipherer.isAudio("hello.flv")); + assertEquals("Should be an audio", false, ResourceDecipherer.isAudio("hello..png")); + } + +} diff --git a/src/voogasalad/VoogaBundles.java b/src/voogasalad/VoogaBundles.java new file mode 100644 index 0000000..ba8f8d5 --- /dev/null +++ b/src/voogasalad/VoogaBundles.java @@ -0,0 +1,16 @@ +package voogasalad; + +import java.util.ResourceBundle; + +/** + * A class to contain public static references to resource bundles, + * so that multiple classes requiring access to these properties do + * not need to independently and repeatedly instantiate them. + * + */ +public class VoogaBundles { + + public final static ResourceBundle extensionProperties = ResourceBundle.getBundle("resources/extensions"); + public final static ResourceBundle toolbarProperties = ResourceBundle.getBundle("resources/toolbarbuttons"); + +} diff --git a/src/voogasalad/VoogaFileType.java b/src/voogasalad/VoogaFileType.java new file mode 100644 index 0000000..8c08160 --- /dev/null +++ b/src/voogasalad/VoogaFileType.java @@ -0,0 +1,12 @@ +package voogasalad; + +/** + * The enum specifying what kind of item a particular VoogaFile is, + * currently supporting folders, images, and audio files. + * @author DoovalSalad + * + */ +public enum VoogaFileType { + FOLDER, IMAGE, AUDIO; + +} \ No newline at end of file