diff --git a/.besouro/20161006162637726/actions.txt b/.besouro/20161006162637726/actions.txt new file mode 100644 index 0000000..8ec9827 --- /dev/null +++ b/.besouro/20161006162637726/actions.txt @@ -0,0 +1,90 @@ +FileOpenedAction 1475760397941 SpreadsheetTest.java 323 2 5 1 +EditAction 1475760420015 SpreadsheetTest.java 326 2 5 1 +CompilationAction 1475760420171 SpreadsheetTest.java +CompilationAction 1475760420171 SpreadsheetTest.java +UnitTestCaseAction 1475760425033 SpreadsheetTest.java FAIL +UnitTestSessionAction 1475760425034 SpreadsheetTest.java FAIL +RefactoringAction 1475760476452 TableCell.java ADD TableCell.java CLASS +FileOpenedAction 1475760476609 TableCell.java 33 0 0 0 +RefactoringAction 1475760491520 TableCell.java REMOVE TableCell.java CLASS +RefactoringAction 1475760512672 Tablecell.java ADD Tablecell.java CLASS +FileOpenedAction 1475760512792 Tablecell.java 33 0 0 0 +RefactoringAction 1475760549974 Tablecell.java REMOVE Tablecell.java CLASS +RefactoringAction 1475760566738 Cell.java ADD Cell.java CLASS +FileOpenedAction 1475760566858 Cell.java 28 0 0 0 +RefactoringAction 1475760581353 Spreadsheet.java ADD Cell cells FIELD +RefactoringAction 1475760630905 Cell.java ADD String id FIELD +RefactoringAction 1475760639419 Cell.java ADD String value FIELD +RefactoringAction 1475760652935 Cell.java ADD String getId() METHOD +RefactoringAction 1475760652936 Cell.java ADD void setId(String) METHOD +RefactoringAction 1475760652937 Cell.java ADD String getValue() METHOD +RefactoringAction 1475760652938 Cell.java ADD void setValue(String) METHOD +RefactoringAction 1475760792258 Cell.java REMOVE Cell.java CLASS +RefactoringAction 1475760797474 Spreadsheet.java REMOVE cells FIELD +RefactoringAction 1475761112631 Cell.java ADD Cell.java CLASS +FileOpenedAction 1475761112776 Cell.java 28 0 0 0 +RefactoringAction 1475761133807 Cell.java ADD String id FIELD +RefactoringAction 1475761133808 Cell.java ADD String value FIELD +RefactoringAction 1475761157828 Cell.java ADD String getId() METHOD +RefactoringAction 1475761157829 Cell.java ADD void setId(String) METHOD +RefactoringAction 1475761157829 Cell.java ADD String getValue() METHOD +RefactoringAction 1475761157830 Cell.java ADD void setValue(String) METHOD +EditAction 1475761180142 Cell.java 291 4 4 0 +RefactoringAction 1475761190050 Spreadsheet.java ADD Cell cells FIELD +RefactoringAction 1475761201096 Spreadsheet.java ADD void init FIELD +RefactoringAction 1475761204609 Spreadsheet.java RENAME init=>void init() METHOD +RefactoringAction 1475761239195 Spreadsheet.java REMOVE init() METHOD +RefactoringAction 1475761291168 Spreadsheet.java ADD import java.util.ArrayList IMPORT +CompilationAction 1475761340878 SpreadsheetTest.java +RefactoringAction 1475761372891 SpreadsheetTest.java ADD import org.junit.Before IMPORT +CompilationAction 1475761377331 SpreadsheetTest.java +RefactoringAction 1475761463921 Cell.java ADD Cell(String, String) METHOD +EditAction 1475761475432 Cell.java 396 5 6 0 +EditAction 1475761616333 Spreadsheet.java 364 3 3 0 +EditAction 1475761778311 Spreadsheet.java 423 3 4 0 +UnitTestCaseAction 1475761782519 SpreadsheetTest.java OK +UnitTestSessionAction 1475761782520 SpreadsheetTest.java OK +RefactoringAction 1475761841966 SpreadsheetTest.java ADD void testCreateCellA1()/2 METHOD +RefactoringAction 1475761846485 SpreadsheetTest.java RENAME testCreateCellA1()/2=>void testCreateCellA1_ FIELD +EditAction 1475761865867 SpreadsheetTest.java 471 3 7 2 +UnitTestCaseAction 1475761868322 SpreadsheetTest.java OK +UnitTestSessionAction 1475761868322 SpreadsheetTest.java OK +RefactoringAction 1475761914545 SpreadsheetTest.java ADD void testCreateCellA1_minus1()/2 METHOD +RefactoringAction 1475761919073 SpreadsheetTest.java RENAME testCreateCellA1_minus1()/2=>void testCreateCellA1_5() METHOD +RefactoringAction 1475761920086 SpreadsheetTest.java RENAME testCreateCellA1_5()=>void testCreateCellA1_5A() METHOD +EditAction 1475761995345 SpreadsheetTest.java 610 4 10 3 +EditAction 1475762016230 SpreadsheetTest.java 619 4 10 3 +UnitTestCaseAction 1475762018134 SpreadsheetTest.java FAIL +UnitTestSessionAction 1475762018135 SpreadsheetTest.java FAIL +EditAction 1475762935482 Spreadsheet.java 621 3 7 0 +UnitTestCaseAction 1475762937427 SpreadsheetTest.java FAIL +UnitTestSessionAction 1475762937428 SpreadsheetTest.java FAIL +EditAction 1475763226289 Spreadsheet.java 674 3 7 0 +UnitTestCaseAction 1475763229457 SpreadsheetTest.java FAIL +UnitTestSessionAction 1475763229457 SpreadsheetTest.java FAIL +EditAction 1475763312214 Spreadsheet.java 710 3 7 0 +UnitTestCaseAction 1475763314092 SpreadsheetTest.java OK +UnitTestSessionAction 1475763314092 SpreadsheetTest.java OK +RefactoringAction 1475763444823 SpreadsheetTest.java ADD void set() METHOD +RefactoringAction 1475763450339 SpreadsheetTest.java ADD void testCreateCellA1_5A()/2 METHOD +RefactoringAction 1475763505465 SpreadsheetTest.java RENAME testCreateCellA1_5A()/2=>void testCreateCellA1astring() METHOD +RefactoringAction 1475763513478 SpreadsheetTest.java REMOVE set() METHOD +EditAction 1475763515350 SpreadsheetTest.java 776 5 13 4 +UnitTestCaseAction 1475763517210 SpreadsheetTest.java FAIL +UnitTestSessionAction 1475763517210 SpreadsheetTest.java FAIL +EditAction 1475764116999 Spreadsheet.java 936 3 7 0 +UnitTestCaseAction 1475764119390 SpreadsheetTest.java OK +UnitTestSessionAction 1475764119390 SpreadsheetTest.java OK +RefactoringAction 1475764135540 SpreadsheetTest.java ADD void testCreateCellA1astring()/2 METHOD +RefactoringAction 1475764139052 SpreadsheetTest.java RENAME testCreateCellA1astring()/2=>void testCreateCellA1astringW() METHOD +RefactoringAction 1475764140064 SpreadsheetTest.java RENAME testCreateCellA1astringW()=>void testCreateCellA1astringWith() METHOD +RefactoringAction 1475764141075 SpreadsheetTest.java RENAME testCreateCellA1astringWith()=>void testCreateCellA1astringWithBAd() METHOD +RefactoringAction 1475764144589 SpreadsheetTest.java RENAME testCreateCellA1astringWithBAd()=>void testCreateCellA1astringWithBadFormat() METHOD +EditAction 1475764160091 SpreadsheetTest.java 943 6 16 5 +UnitTestCaseAction 1475764163364 SpreadsheetTest.java FAIL +UnitTestSessionAction 1475764163364 SpreadsheetTest.java FAIL +EditAction 1475764189020 Spreadsheet.java 965 3 7 0 +CompilationAction 1475764189298 Spreadsheet.java +CompilationAction 1475764204616 Spreadsheet.java +UnitTestCaseAction 1475764206957 SpreadsheetTest.java OK +UnitTestSessionAction 1475764206957 SpreadsheetTest.java OK diff --git a/.besouro/20161006162637726/besouroEpisodes.txt b/.besouro/20161006162637726/besouroEpisodes.txt new file mode 100644 index 0000000..f86867e --- /dev/null +++ b/.besouro/20161006162637726/besouroEpisodes.txt @@ -0,0 +1,52 @@ +1475761782520 refactoring 3 1384 true +1475761782521 refactoring 3 1384 true +1475761782522 refactoring 3 1384 true +1475761782523 refactoring 3 1384 true +1475761782524 refactoring 3 1384 true +1475761782525 refactoring 3 1384 true +1475761782526 production 1 1384 false +1475761782527 production 3 1384 false +1475761782528 refactoring 3 1384 true +1475761782529 refactoring 3 1384 true +1475761782530 refactoring 3 1384 true +1475761782531 refactoring 3 1384 true +1475761782532 refactoring 3 1384 true +1475761782533 refactoring 3 1384 true +1475761782534 refactoring 3 1384 true +1475761782535 refactoring 3 1384 true +1475761782536 refactoring 3 1384 true +1475761782537 refactoring 3 1384 true +1475761782538 refactoring 3 1384 true +1475761782539 refactoring 3 1384 true +1475761782540 refactoring 3 1384 true +1475761782541 refactoring 3 1384 true +1475761782542 refactoring 3 1384 true +1475761782543 refactoring 3 1384 true +1475761782544 refactoring 3 1384 true +1475761782545 refactoring 3 1384 true +1475761782546 refactoring 3 1384 true +1475761782547 refactoring 3 1384 true +1475761782548 refactoring 3 1384 true +1475761782549 refactoring 3 1384 true +1475761782550 refactoring 3 1384 true +1475761782551 refactoring 3 1384 true +1475761782552 refactoring 3 1384 true +1475761868322 test-addition 1 26 true +1475761868323 test-addition 1 26 true +1475763314092 test-first 3 1399 true +1475763314093 test-first 3 1399 true +1475763314094 test-first 3 1399 true +1475763314095 test-first 3 1399 true +1475763314096 test-first 3 1399 true +1475763314097 test-first 3 1399 true +1475763314098 test-first 3 1399 true +1475763314099 test-first 3 1399 true +1475763314100 test-first 3 1399 true +1475763314101 test-first 3 1399 true +1475763314102 test-first 3 1399 true +1475763314103 test-first 3 1399 true +1475764119390 test-first 3 674 true +1475764119391 test-first 3 674 true +1475764119392 test-first 3 674 true +1475764206957 test-first 3 71 true +1475764206958 test-first 3 71 true diff --git a/.besouro/20161006162637726/disagreements.txt b/.besouro/20161006162637726/disagreements.txt new file mode 100644 index 0000000..e69de29 diff --git a/.besouro/20161006162637726/randomHeuristicEpisodes.txt b/.besouro/20161006162637726/randomHeuristicEpisodes.txt new file mode 100644 index 0000000..6ad0aca --- /dev/null +++ b/.besouro/20161006162637726/randomHeuristicEpisodes.txt @@ -0,0 +1,5 @@ +1475761782520 refactoring 3 1384 true +1475761868322 test-addition 1 26 false +1475763314092 test-first 3 1399 true +1475764119390 test-first 3 674 true +1475764206957 test-first 3 71 true diff --git a/.besouro/20161006162637726/userComments.txt b/.besouro/20161006162637726/userComments.txt new file mode 100644 index 0000000..e69de29 diff --git a/.besouro/20161006162637726/zorroEpisodes.txt b/.besouro/20161006162637726/zorroEpisodes.txt new file mode 100644 index 0000000..1c5ddee --- /dev/null +++ b/.besouro/20161006162637726/zorroEpisodes.txt @@ -0,0 +1,5 @@ +1475761782520 refactoring 3 1384 true +1475761868322 test-addition 1 85 true +1475763314092 test-first 3 1445 true +1475764119390 test-first 3 805 true +1475764206957 test-first 3 87 true diff --git a/.besouro/20161013143012955/actions.txt b/.besouro/20161013143012955/actions.txt new file mode 100644 index 0000000..e69de29 diff --git a/.besouro/20161013143012955/zorroEpisodes.txt b/.besouro/20161013143012955/zorroEpisodes.txt new file mode 100644 index 0000000..e69de29 diff --git a/.besouro/20161013143038987/actions.txt b/.besouro/20161013143038987/actions.txt new file mode 100644 index 0000000..e69de29 diff --git a/.besouro/20161013143038987/zorroEpisodes.txt b/.besouro/20161013143038987/zorroEpisodes.txt new file mode 100644 index 0000000..e69de29 diff --git a/.besouro/20161013143105748/actions.txt b/.besouro/20161013143105748/actions.txt new file mode 100644 index 0000000..e69de29 diff --git a/.besouro/20161013143105748/zorroEpisodes.txt b/.besouro/20161013143105748/zorroEpisodes.txt new file mode 100644 index 0000000..e69de29 diff --git a/bin/Cell.class b/bin/Cell.class new file mode 100644 index 0000000..fa2e5b0 Binary files /dev/null and b/bin/Cell.class differ diff --git a/bin/Spreadsheet.class b/bin/Spreadsheet.class index 92b411f..b70e670 100644 Binary files a/bin/Spreadsheet.class and b/bin/Spreadsheet.class differ diff --git a/bin/SpreadsheetTest.class b/bin/SpreadsheetTest.class index e7086bd..fc6af4f 100644 Binary files a/bin/SpreadsheetTest.class and b/bin/SpreadsheetTest.class differ diff --git a/src/Cell.java b/src/Cell.java new file mode 100644 index 0000000..0452037 --- /dev/null +++ b/src/Cell.java @@ -0,0 +1,30 @@ + +public class Cell { + private String id; + private String value; + + + + public Cell(String id, String value) { + this.id = id; + this.value = value; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + +} diff --git a/src/Spreadsheet.java b/src/Spreadsheet.java index e4f120b..a0f2fdf 100644 --- a/src/Spreadsheet.java +++ b/src/Spreadsheet.java @@ -1,18 +1,152 @@ +import java.util.ArrayList; public class Spreadsheet { - + private ArrayList cells = new ArrayList(); + private ArrayList circularReferences = new ArrayList(); + + public String get(String cell) { - // to be implemented + for(Cell c: cells) { + if(cell.equals(c.getId())) + return c.getValue(); + } return null; } public void set(String cell, String value) { - // to be implemented + cells.add(new Cell(cell, value)); } public String evaluate(String cell) { - // to be implemented - return null; + String value = get(cell); + char c = value.charAt(0); + + if(Character.isDigit(c) || c =='-') { + return dealWithNumbers(value); + } + + else if(c== '=') { + StringBuilder sb = new StringBuilder(value); + sb.deleteCharAt(0); + if(isAValidString(sb.toString(), sb.charAt(0))) { + return dealWithStrings(sb.toString()); + } + + else if(Character.isDigit(sb.charAt(0)) || sb.charAt(0) =='-') { + return dealWithNumbers(sb.toString()); + } + + //definition of cell #ID? Uppercase A-Z, number + else if(Character.isUpperCase(sb.charAt(0)) && Character.isDigit(sb.charAt(1)) && 2 == sb.length()) { + String cellName = sb.toString(); + if(circularReferences.contains(cellName)){ + return "#Circular"; + } + else { + circularReferences.add(cellName); + return evaluate(cellName); + } + } + + else + return "#Error"; + } + + else { + if(isAValidString(value, c)) { + return dealWithStrings(value); + } + else + return "#Error"; + } + } + + + + + public String dealWithStrings(String input) { + StringBuilder sb = new StringBuilder(input); + sb.deleteCharAt(0); + sb.deleteCharAt(sb.length()-1); + return sb.toString(); + } + + + + + public String dealWithNumbers(String input) { + String value = input; + int counter = 0; + for(char ch: value.toCharArray()){ + if(ch == '-' && counter == 0) { + + } + else if(Character.isDigit(ch) || ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%') { + + } + else + return "#Error"; + counter++; + } + + ArrayList values = new ArrayList(); + ArrayList operations = new ArrayList(); + + int result = 0; + for (int i=0; i < value.length(); i++) { + char c = value.charAt(i); + if (Character.isDigit(c)) { + result = result*10 + Character.getNumericValue(c); + if(i == value.length()-1) + values.add(result); + } + else { + values.add(result); + operations.add(c); + result = 0; + } + + } + + int calculated = 0; + int operationCounter = -1; + for(int v : values) { + if(operationCounter == -1) { + calculated = v; + } + else { + char c = operations.get(operationCounter); + switch(c) { + case '+' : + calculated += v; + break; + case '-' : + calculated -= v; + break; + case '*' : + calculated = calculated * v; + break; + case '/' : + calculated = calculated / v; + break; + case '%' : + calculated = calculated % v; + break; + } + } + operationCounter++; + } + value = "" + calculated; + return value; + } + + + + public boolean isAValidString(String testable, char first) { + if(first == '\'' && testable.substring(testable.length() - 1).equals("'")) { + return true; + } + return false; } } diff --git a/tests/SpreadsheetTest.java b/tests/SpreadsheetTest.java index 9e0936a..4754e9b 100644 --- a/tests/SpreadsheetTest.java +++ b/tests/SpreadsheetTest.java @@ -1,15 +1,155 @@ import static org.junit.Assert.*; - - import org.junit.Test; +import org.junit.Before; public class SpreadsheetTest { + + Spreadsheet sheet; + + @Before + public void init() { + sheet = new Spreadsheet(); + } @Test - public void test() { - fail("Not yet implemented"); - + public void testCreateCellA1() { + sheet.set("A1", "1"); + String s = sheet.get("A1"); + assertEquals("1", s); + } + + @Test + public void testCreateCellA1_minus1() { + sheet.set("A1", "-1"); + String s = sheet.evaluate("A1"); + assertEquals("-1", s); + } + + @Test + public void testCreateCellA1_5A() { + sheet.set("A1", "5A"); + String s = sheet.evaluate("A1"); + assertEquals("#Error", s); + } + + @Test + public void testCreateCellA1astring() { + sheet.set("A1", "'a string'"); + String s = sheet.evaluate("A1"); + assertEquals("a string", s); + } + + @Test + public void testCreateCellA1astringWithNoEndQuote() { + sheet.set("A1", "'a string"); + String s = sheet.evaluate("A1"); + assertEquals("#Error", s); + } + + @Test + public void testCreateCellA1astringWithNoStartQuote() { + sheet.set("A1", "a string'"); + String s = sheet.evaluate("A1"); + assertEquals("#Error", s); + } + + @Test + public void testCreateCellA1WithSimpleEquationResultingInString() { + sheet.set("A1", "='a string'"); + String s = sheet.evaluate("A1"); + assertEquals("a string", s); + } + + @Test + public void testCreateCellA1WithSimpleEquationStringWithError() { + sheet.set("A1", "=a string'"); + String s = sheet.evaluate("A1"); + assertEquals("#Error", s); + } + + @Test + public void testCreateCellA1WithCellReferenceA5() { + sheet.set("A5", "3"); + sheet.set("A1", "=A5"); + String s = sheet.evaluate("A1"); + assertEquals("3", s); + } + + @Test + public void testCreateCellA1WithCellReferenceA5WithErrorInIt() { + sheet.set("A5", "5A"); + sheet.set("A1", "=A5"); + String s = sheet.evaluate("A1"); + assertEquals("#Error", s); + } + + @Test + public void testCreateCellA1WithCircularCellReference() { + sheet.set("A5", "=A1"); + sheet.set("A1", "=A5"); + String s = sheet.evaluate("A1"); + assertEquals("#Circular", s); + } + + @Test + public void testCreateCellA1WithCircularCellReferences() { + sheet.set("A5", "=A4"); + sheet.set("A4", "=A3"); + sheet.set("A3", "=A2"); + sheet.set("A2", "=A1"); + sheet.set("A1", "=A5"); + String s = sheet.evaluate("A1"); + assertEquals("#Circular", s); + } + + @Test + public void testCreateCellA1WithIntegerOperation5Plus1() { + sheet.set("A1", "=5+1"); + String s = sheet.evaluate("A1"); + assertEquals("6", s); + } + + @Test + public void testCreateCellA1WithIntegerOperation5Minus1() { + sheet.set("A1", "=5-1"); + String s = sheet.evaluate("A1"); + assertEquals("4", s); + } + + @Test + public void testCreateCellA1WithIntegerOperation5Divided1() { + sheet.set("A1", "=5/1"); + String s = sheet.evaluate("A1"); + assertEquals("5", s); + } + + @Test + public void testCreateCellA1WithIntegerOperation5Times2() { + sheet.set("A1", "=5*2"); + String s = sheet.evaluate("A1"); + assertEquals("10", s); + } + + @Test + public void testCreateCellA1WithIntegerOperation5Modus1() { + sheet.set("A1", "=5%1"); + String s = sheet.evaluate("A1"); + assertEquals("0", s); + } + + @Test + public void testCreateCellA1WithIntegerOperation555Plus1() { + sheet.set("A1", "=555+1"); + String s = sheet.evaluate("A1"); + assertEquals("556", s); + } + + @Test + public void testCreateCellA1Equals1() { + sheet.set("A1", "=1"); + String s = sheet.evaluate("A1"); + assertEquals("1", s); } }