From 5612c37408f97d0c3d51e121774d34d99e4ce902 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Thu, 24 Oct 2019 14:01:00 -0400 Subject: [PATCH 01/82] DB core functionality mostly in - much too long since committing --- .DS_Store | Bin 0 -> 6148 bytes .gitignore | 1 + pom.xml | 27 ++++++ src/.DS_Store | Bin 0 -> 6148 bytes src/main/.DS_Store | Bin 0 -> 6148 bytes src/main/java/DB.java | 145 ++++++++++++++++++++++++++++++++ src/test/java/DBTest.java | 169 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 342 insertions(+) create mode 100644 .DS_Store create mode 100644 src/.DS_Store create mode 100644 src/main/.DS_Store create mode 100644 src/main/java/DB.java create mode 100644 src/test/java/DBTest.java diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..d92ddf0dfea967a4df3ab90415b58ee3abddd721 GIT binary patch literal 6148 zcmeHK%}T>S5Z<*_w-vDmp&s|*t%sJD77s$G_25m2=s~4TY_Wlulq5B1t>iKEjeG-N z$C=%YSnR=*h@FAmZ)Rs^H}gUEhcU*x(_w?L3S&$_L*%F|5gcCXS~J0j9OsB9t{=K! zy}1+P`|E(;USKhc*o~Wh{eItzlY0G~uN8{x8=FFuMR~h?ZI8p$PMl;Kcb(`Gty3o% zyGB0?L;q8E+;eY~_A9$bVURdpFp5;NHylC8)rA)f!?YVtgJG=Zv9&{pk|^~n)!D3l zP?OD8V_uW9PN!azt!8^ZFNx}Y!{8Q1 z^5{H@kQg8ahyh|?wHPqxfY@5CvZ-2PfEf4z1Gqm(Xo!}MGO!F|BL}%?Ag686lKoVZ{^`xE1?abpproject-2-atm 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.7.0 + + 1.8 + 1.8 + + + + + + + + pl.pragmatists + JUnitParams + 1.1.1 + test + + + com.opencsv + opencsv + 4.5 + + \ No newline at end of file diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..74331cd049228d6a0f1fa9a8b010dfdebe511ac2 GIT binary patch literal 6148 zcmeHK%}xR_5N-jrV2m70@Ystt4iE^$gUJT*;LT)>9@M~YAhOA_C9p^oG5Z+$M!tcs z<4oHn5;=G>MrM-so9WE7J72Q>VT|$Cv{z-U$QTpQ5IHJq1c%qUGBy~I;~f5MGV(*^ zgZ*xOe;x4Kb8N^w7O?2+_xqzTn8cOJ2hU`4TiZKA6hvXKaOV!x%#FQxI%#?18?-LG zc;H+8QmJsMd;V3>pLL1{rz(oQAnK2Ga?tBT$nA9y_0+7TrcrOA=W&fgh`h*milzCy zaa@+QdUa8j^JcSBmi1a=vB-5S`@VII43`gq}bbr_dAoN>n>^~3-% zu*yKzm_4PSvsOauLPNp01Qig_S1tiy i;68GwoyIRwhd9S#Vi0G+ah(oG7XeKOb;Q6gFz^ZbGEis$ literal 0 HcmV?d00001 diff --git a/src/main/.DS_Store b/src/main/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..d54a045baaa09961660cc2ad3546a6bd91dd09fb GIT binary patch literal 6148 zcmeHK%}&BV5Z;BV3&zO71dn_1#sLC>cra-Y58g~_^q>aXlt>M>1d5GfO&>$w$T#qH zoY`H7L=K*ek(p%oo7tJ!?R-i1hcU+6Q@_Soi7_UiA#zk!2oA4wjzDmn1`5ORGPgnpE?qiN_*)I5%M2vHD)ZmB$* zH4iJY-l(M&Icv4571^ja)3hMU2eqTq-u>g#>^XgT)dX|kU&*e)IlO_gsPDlUhr=km zg|T`3JdThUAO?s5Vqkq3FlT_+UZ0YwQeuD@_#p$hKS*eZw!uuJ+B%@a>ofW-L=@2R zErBQv+6FU?-~r(}6;P*g^Tgmf9mb`Jvkhh%bvol}WthjTJb%1!wK|MT70$S=k$Pf) z7+7W?ugwyk|0nRvtbF7zr;tSq5Ci{=0d94iP8*6cXY04}@T`^4deBfXE<*(b^rcGx j7`Tt@E2s8L)FI9`m}$gWa9pJW(nUZKLLD*i3k-Y$b5u}b literal 0 HcmV?d00001 diff --git a/src/main/java/DB.java b/src/main/java/DB.java new file mode 100644 index 0000000..f9694c7 --- /dev/null +++ b/src/main/java/DB.java @@ -0,0 +1,145 @@ +import com.opencsv.CSVReader; +import com.opencsv.CSVWriter; + +import java.io.*; +import java.nio.file.Files; +import java.nio.file.NoSuchFileException; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; + +public class DB { + + private String fileName; + private String path; + private Integer rowLength; + + public DB(String fileName, Integer rowLength) throws IOException { + this.fileName = fileName; + this.path = fileNameToPath(this.fileName, System.getProperty("user.dir")); + this.rowLength = rowLength; + + try { // look for an existing file with that name + Reader reader = Files.newBufferedReader(Paths.get(this.path)); + CSVReader csvReader = new CSVReader(reader); + + // get all records at once + List records = csvReader.readAll(); + // loop through records + + } + catch(NoSuchFileException e) { // make a new file, if one doesn't exist in place + File file = new File(this.path); + FileWriter outputfile = new FileWriter(file); + + // create CSVWriter object filewriter object as parameter + CSVWriter writer = new CSVWriter(outputfile); + + writer.close(); + + } catch (IOException e) { + e.printStackTrace(); + } + } + + public String getFileName() { + return this.fileName; + } + + public String getPath() { + return this.path; + } + + public static String pathToFileName (String FN) { + + String[] splitter = FN.split("/"); + return splitter[splitter.length-1]; + } + + public static String fileNameToPath (String FN, String PWD) { + return PWD + "/data/" + FN; + } + + public Integer length() { + return readAllRows().size(); + } + + public Integer getRowLength() { + return this.rowLength; + } + + public void setPath(String path) { //fix this shit + this.fileName = path; + } + + public Boolean checkIntegrity() { + ArrayList records = readAllRows(); + + for (String[] row : records) { + if (row.length != this.rowLength) { + return false; + } + } + return true; + } + + public void addRow(String[] row) { + try { + File file = new File(this.path); + FileWriter outputfile = new FileWriter(file, true); + + // create CSVWriter object filewriter object as parameter + CSVWriter writer = new CSVWriter(outputfile); + if (row.length == this.rowLength) { // only write if this row is the correct length + writer.writeNext(row); + } + + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public ArrayList readAllRows() { + try { // look for an existing file with that name + Reader reader = Files.newBufferedReader(Paths.get(this.path)); + CSVReader csvReader = new CSVReader(reader); + + // get all records at once + List records = csvReader.readAll(); + // loop through records + return new ArrayList (records); + + } catch (IOException e) { + e.printStackTrace(); + return null; + } + + } + + public void printDB() { + for (String[] row : readAllRows()) { + System.out.println(""); + + for (String field : row) { + System.out.print(field + "/"); + } + } + } + + public void clear() { + try { + File file = new File(this.path); + FileWriter outputfile = new FileWriter(file); + + // create CSVWriter object filewriter object as parameter + CSVWriter writer = new CSVWriter(outputfile); + + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + +} diff --git a/src/test/java/DBTest.java b/src/test/java/DBTest.java new file mode 100644 index 0000000..fd1e59a --- /dev/null +++ b/src/test/java/DBTest.java @@ -0,0 +1,169 @@ +import junitparams.JUnitParamsRunner; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Random; + +@RunWith(JUnitParamsRunner.class) +public class DBTest { + + @Test + public void constructorTest() { + + Random random = new Random(); + String fileName = Integer.toString(Math.abs(random.nextInt())) + ".csv"; + + DB db1 = null; + try { + db1 = new DB(fileName, 4); + } catch (IOException e) { + e.printStackTrace(); + } + + String expected = fileName; + Assert.assertEquals(db1.getFileName(), expected); + } + + @Test + public void testDBFileName() { + String fileName = "test.csv"; + DB testDB = null; + try { + testDB = new DB(fileName, 4); + } catch (IOException e) { + e.printStackTrace(); + } + + String actual = testDB.getFileName(); + Assert.assertEquals(testDB.getFileName(), fileName); + } + + @Test + public void testFileNametoPath() { + String expected = "/Users/josh/Desktop/Projects/CR-MacroLabs-OOP-ATM/data/610393892.csv"; + String input = "610393892.csv"; + Assert.assertEquals(expected, DB.pathToFileName(input)); + } + + @Test + public void testPathtoFileName() { + String input = "610393892.csv"; + String expected = "/Users/josh/Desktop/Projects/CR-MacroLabs-OOP-ATM/data/610393892.csv"; + Assert.assertEquals(expected, DB.fileNameToPath(input, System.getProperty("user.dir"))); + } + + @Test + public void tempStuff() { + String fileName = "test.csv"; + DB testDB = null; + try { + testDB = new DB(fileName, 4); + } catch (IOException e) { + e.printStackTrace(); + } + + testDB.addRow(new String[] {"Item 1", "Item 2", "Item 3", "Item 4"}); + testDB.addRow(new String[] {"Item 1b", "Item 2b", "Item 3b", "Item 4b"}); + testDB.addRow(new String[] {"Item 1c", "Item 2c", "Item 3c", "Item 4c"}); + testDB.addRow(new String[] {"Item 1d", "Item 2d", "Item 3d", "Item 4d"}); + + } + + @Test + public void readTestDBTest() { + String fileName = "test.csv"; + + ArrayList expected = new ArrayList<>(); + expected.add(new String[] {"Item 1", "Item 2", "Item 3", "Item 4"}); + expected.add(new String[] {"Item 1b", "Item 2b", "Item 3b", "Item 4b"}); + expected.add(new String[] {"Item 1c", "Item 2c", "Item 3c", "Item 4c"}); + expected.add(new String[] {"Item 1d", "Item 2d", "Item 3d", "Item 4d"}); + + DB testDB = null; + try { + testDB = new DB(fileName, 4); + + ArrayList records = testDB.readAllRows(); + + for (int i = 0; i < 4; i++) { + Assert.assertEquals(expected.get(i), records.get(i)); + } + + + + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void printDBTest() { + String fileName = "test.csv"; + DB testDB = null; + try { + testDB = new DB(fileName, 4); + } catch (IOException e) { + e.printStackTrace(); + } + testDB.printDB(); + } + + @Test + public void clearDBTest() { + Random random = new Random(); + String fileName = Integer.toString(Math.abs(random.nextInt())) + ".csv"; + + DB db1 = null; + try { + db1 = new DB(fileName, 4); + } catch (IOException e) { + e.printStackTrace(); + } + + db1.addRow(new String[]{"Sticky", "Icky", "Wicky", "Quicky"}); + db1.clear(); + + ArrayList records = db1.readAllRows(); + + Assert.assertEquals(0,records.size()); + } + + @Test + public void addRowTest() { + Random random = new Random(); + String fileName = Integer.toString(Math.abs(random.nextInt())) + ".csv"; + + DB db1 = null; + try { + db1 = new DB(fileName, 4); + } catch (IOException e) { + e.printStackTrace(); + } + ArrayList data = new ArrayList<>(); + data.add(new String[] {"Item 1", "Item 2", "Item 3", "Item 4"}); + data.add(new String[] {"Item 1b", "Item 2b", "Item 3b", "Item 4b"}); + data.add(new String[] {"Item 1c", "Item 2c", "Item 3c", "Item 4c"}); + data.add(new String[] {"Item 1d", "Item 2d", "Item 3d", "Item 4d"}); + + ArrayList records; + for (int i = 0; i < 4; i++) { + db1.addRow(data.get(i)); + Assert.assertTrue(i + 1 == db1.length()); + + records = db1.readAllRows(); + + for (int j = 0; j <= i; j++) { + //System.out.println(String.format("Rows: %d Testing row: %d", i, j)); + Assert.assertEquals(records.get(i), data.get(i)); + } + } + } + + @Test + public void getRowLengthTest() { + + } +} \ No newline at end of file From 87554cc8e5a96869a195c8c371be681ff9541d52 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Thu, 24 Oct 2019 14:02:12 -0400 Subject: [PATCH 02/82] gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 6fe5a0b..c1960de 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ target/ .project .classpath .settings -data/* \ No newline at end of file +data/* +*.DS_Store \ No newline at end of file From 04d3b7ffeffd5c79282a4d45478d3b7db7253fda Mon Sep 17 00:00:00 2001 From: deltagphys Date: Thu, 24 Oct 2019 14:08:47 -0400 Subject: [PATCH 03/82] fix the bad branching --- src/.DS_Store | Bin 6148 -> 6148 bytes src/main/.DS_Store | Bin 6148 -> 6148 bytes src/main/java/DB.java | 145 -------------------------------- src/test/java/DBTest.java | 169 -------------------------------------- 4 files changed, 314 deletions(-) delete mode 100644 src/main/java/DB.java delete mode 100644 src/test/java/DBTest.java diff --git a/src/.DS_Store b/src/.DS_Store index 74331cd049228d6a0f1fa9a8b010dfdebe511ac2..566bda17d4472aac2873d28c8a772c48b465c7b3 100644 GIT binary patch delta 99 zcmZoMXfc=|#>B)qu~2NHo+2a5#DLw46_^+qStj!^zTr$MPR>cn&(C3;{Dx6lh=rkq tA(f$+p#(0xS&-=_%Vu^Ceh#1!K+bpO$^0Ug9E=PMK*YeXIY49!GXPWa89V?0 delta 72 zcmZoMXfc=|#>B`mu~2NHo+2aD#DLwC4MbQb^E17i%*)Kb*@XEr%f<#>#?9;;{2V}a an+=)2Gf(ChvE*O?0!9V~rp*B&TbKdB)Dd_9 diff --git a/src/main/.DS_Store b/src/main/.DS_Store index d54a045baaa09961660cc2ad3546a6bd91dd09fb..cce1d29597286e157f66fea013cd443fd90b484a 100644 GIT binary patch delta 76 zcmZoMXfc=|#>B)qu~2NHo+2aj#DLw46_^+qnJ4owzTr$MPR>cn&(C4p{DzT@bu&8$ eKL=3lWB`mu~2NHo+2aD#DLwC4MbQb^E18N%**_XWn+UV<7Rdaeh#3T&5F$5 WnJ4p$SaL7`0V4wg)8+t?EzAIYQV^a1 diff --git a/src/main/java/DB.java b/src/main/java/DB.java deleted file mode 100644 index f9694c7..0000000 --- a/src/main/java/DB.java +++ /dev/null @@ -1,145 +0,0 @@ -import com.opencsv.CSVReader; -import com.opencsv.CSVWriter; - -import java.io.*; -import java.nio.file.Files; -import java.nio.file.NoSuchFileException; -import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.List; - -public class DB { - - private String fileName; - private String path; - private Integer rowLength; - - public DB(String fileName, Integer rowLength) throws IOException { - this.fileName = fileName; - this.path = fileNameToPath(this.fileName, System.getProperty("user.dir")); - this.rowLength = rowLength; - - try { // look for an existing file with that name - Reader reader = Files.newBufferedReader(Paths.get(this.path)); - CSVReader csvReader = new CSVReader(reader); - - // get all records at once - List records = csvReader.readAll(); - // loop through records - - } - catch(NoSuchFileException e) { // make a new file, if one doesn't exist in place - File file = new File(this.path); - FileWriter outputfile = new FileWriter(file); - - // create CSVWriter object filewriter object as parameter - CSVWriter writer = new CSVWriter(outputfile); - - writer.close(); - - } catch (IOException e) { - e.printStackTrace(); - } - } - - public String getFileName() { - return this.fileName; - } - - public String getPath() { - return this.path; - } - - public static String pathToFileName (String FN) { - - String[] splitter = FN.split("/"); - return splitter[splitter.length-1]; - } - - public static String fileNameToPath (String FN, String PWD) { - return PWD + "/data/" + FN; - } - - public Integer length() { - return readAllRows().size(); - } - - public Integer getRowLength() { - return this.rowLength; - } - - public void setPath(String path) { //fix this shit - this.fileName = path; - } - - public Boolean checkIntegrity() { - ArrayList records = readAllRows(); - - for (String[] row : records) { - if (row.length != this.rowLength) { - return false; - } - } - return true; - } - - public void addRow(String[] row) { - try { - File file = new File(this.path); - FileWriter outputfile = new FileWriter(file, true); - - // create CSVWriter object filewriter object as parameter - CSVWriter writer = new CSVWriter(outputfile); - if (row.length == this.rowLength) { // only write if this row is the correct length - writer.writeNext(row); - } - - writer.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - public ArrayList readAllRows() { - try { // look for an existing file with that name - Reader reader = Files.newBufferedReader(Paths.get(this.path)); - CSVReader csvReader = new CSVReader(reader); - - // get all records at once - List records = csvReader.readAll(); - // loop through records - return new ArrayList (records); - - } catch (IOException e) { - e.printStackTrace(); - return null; - } - - } - - public void printDB() { - for (String[] row : readAllRows()) { - System.out.println(""); - - for (String field : row) { - System.out.print(field + "/"); - } - } - } - - public void clear() { - try { - File file = new File(this.path); - FileWriter outputfile = new FileWriter(file); - - // create CSVWriter object filewriter object as parameter - CSVWriter writer = new CSVWriter(outputfile); - - writer.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - -} diff --git a/src/test/java/DBTest.java b/src/test/java/DBTest.java deleted file mode 100644 index fd1e59a..0000000 --- a/src/test/java/DBTest.java +++ /dev/null @@ -1,169 +0,0 @@ -import junitparams.JUnitParamsRunner; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Random; - -@RunWith(JUnitParamsRunner.class) -public class DBTest { - - @Test - public void constructorTest() { - - Random random = new Random(); - String fileName = Integer.toString(Math.abs(random.nextInt())) + ".csv"; - - DB db1 = null; - try { - db1 = new DB(fileName, 4); - } catch (IOException e) { - e.printStackTrace(); - } - - String expected = fileName; - Assert.assertEquals(db1.getFileName(), expected); - } - - @Test - public void testDBFileName() { - String fileName = "test.csv"; - DB testDB = null; - try { - testDB = new DB(fileName, 4); - } catch (IOException e) { - e.printStackTrace(); - } - - String actual = testDB.getFileName(); - Assert.assertEquals(testDB.getFileName(), fileName); - } - - @Test - public void testFileNametoPath() { - String expected = "/Users/josh/Desktop/Projects/CR-MacroLabs-OOP-ATM/data/610393892.csv"; - String input = "610393892.csv"; - Assert.assertEquals(expected, DB.pathToFileName(input)); - } - - @Test - public void testPathtoFileName() { - String input = "610393892.csv"; - String expected = "/Users/josh/Desktop/Projects/CR-MacroLabs-OOP-ATM/data/610393892.csv"; - Assert.assertEquals(expected, DB.fileNameToPath(input, System.getProperty("user.dir"))); - } - - @Test - public void tempStuff() { - String fileName = "test.csv"; - DB testDB = null; - try { - testDB = new DB(fileName, 4); - } catch (IOException e) { - e.printStackTrace(); - } - - testDB.addRow(new String[] {"Item 1", "Item 2", "Item 3", "Item 4"}); - testDB.addRow(new String[] {"Item 1b", "Item 2b", "Item 3b", "Item 4b"}); - testDB.addRow(new String[] {"Item 1c", "Item 2c", "Item 3c", "Item 4c"}); - testDB.addRow(new String[] {"Item 1d", "Item 2d", "Item 3d", "Item 4d"}); - - } - - @Test - public void readTestDBTest() { - String fileName = "test.csv"; - - ArrayList expected = new ArrayList<>(); - expected.add(new String[] {"Item 1", "Item 2", "Item 3", "Item 4"}); - expected.add(new String[] {"Item 1b", "Item 2b", "Item 3b", "Item 4b"}); - expected.add(new String[] {"Item 1c", "Item 2c", "Item 3c", "Item 4c"}); - expected.add(new String[] {"Item 1d", "Item 2d", "Item 3d", "Item 4d"}); - - DB testDB = null; - try { - testDB = new DB(fileName, 4); - - ArrayList records = testDB.readAllRows(); - - for (int i = 0; i < 4; i++) { - Assert.assertEquals(expected.get(i), records.get(i)); - } - - - - } catch (IOException e) { - e.printStackTrace(); - } - } - - @Test - public void printDBTest() { - String fileName = "test.csv"; - DB testDB = null; - try { - testDB = new DB(fileName, 4); - } catch (IOException e) { - e.printStackTrace(); - } - testDB.printDB(); - } - - @Test - public void clearDBTest() { - Random random = new Random(); - String fileName = Integer.toString(Math.abs(random.nextInt())) + ".csv"; - - DB db1 = null; - try { - db1 = new DB(fileName, 4); - } catch (IOException e) { - e.printStackTrace(); - } - - db1.addRow(new String[]{"Sticky", "Icky", "Wicky", "Quicky"}); - db1.clear(); - - ArrayList records = db1.readAllRows(); - - Assert.assertEquals(0,records.size()); - } - - @Test - public void addRowTest() { - Random random = new Random(); - String fileName = Integer.toString(Math.abs(random.nextInt())) + ".csv"; - - DB db1 = null; - try { - db1 = new DB(fileName, 4); - } catch (IOException e) { - e.printStackTrace(); - } - ArrayList data = new ArrayList<>(); - data.add(new String[] {"Item 1", "Item 2", "Item 3", "Item 4"}); - data.add(new String[] {"Item 1b", "Item 2b", "Item 3b", "Item 4b"}); - data.add(new String[] {"Item 1c", "Item 2c", "Item 3c", "Item 4c"}); - data.add(new String[] {"Item 1d", "Item 2d", "Item 3d", "Item 4d"}); - - ArrayList records; - for (int i = 0; i < 4; i++) { - db1.addRow(data.get(i)); - Assert.assertTrue(i + 1 == db1.length()); - - records = db1.readAllRows(); - - for (int j = 0; j <= i; j++) { - //System.out.println(String.format("Rows: %d Testing row: %d", i, j)); - Assert.assertEquals(records.get(i), data.get(i)); - } - } - } - - @Test - public void getRowLengthTest() { - - } -} \ No newline at end of file From 26cea5b7a7259b7dc1a67d5bbb43bcc820c4d231 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Thu, 24 Oct 2019 14:10:54 -0400 Subject: [PATCH 04/82] . --- .gitignore | 2 + src/main/java/DB.java | 145 ++++++++++++++++++++++++++++++++ src/test/java/DBTest.java | 169 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 316 insertions(+) create mode 100644 src/main/java/DB.java create mode 100644 src/test/java/DBTest.java diff --git a/.gitignore b/.gitignore index b99dc45..8054da6 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ target/ .project .classpath .settings +data/ +*.DS_Store \ No newline at end of file diff --git a/src/main/java/DB.java b/src/main/java/DB.java new file mode 100644 index 0000000..f9694c7 --- /dev/null +++ b/src/main/java/DB.java @@ -0,0 +1,145 @@ +import com.opencsv.CSVReader; +import com.opencsv.CSVWriter; + +import java.io.*; +import java.nio.file.Files; +import java.nio.file.NoSuchFileException; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; + +public class DB { + + private String fileName; + private String path; + private Integer rowLength; + + public DB(String fileName, Integer rowLength) throws IOException { + this.fileName = fileName; + this.path = fileNameToPath(this.fileName, System.getProperty("user.dir")); + this.rowLength = rowLength; + + try { // look for an existing file with that name + Reader reader = Files.newBufferedReader(Paths.get(this.path)); + CSVReader csvReader = new CSVReader(reader); + + // get all records at once + List records = csvReader.readAll(); + // loop through records + + } + catch(NoSuchFileException e) { // make a new file, if one doesn't exist in place + File file = new File(this.path); + FileWriter outputfile = new FileWriter(file); + + // create CSVWriter object filewriter object as parameter + CSVWriter writer = new CSVWriter(outputfile); + + writer.close(); + + } catch (IOException e) { + e.printStackTrace(); + } + } + + public String getFileName() { + return this.fileName; + } + + public String getPath() { + return this.path; + } + + public static String pathToFileName (String FN) { + + String[] splitter = FN.split("/"); + return splitter[splitter.length-1]; + } + + public static String fileNameToPath (String FN, String PWD) { + return PWD + "/data/" + FN; + } + + public Integer length() { + return readAllRows().size(); + } + + public Integer getRowLength() { + return this.rowLength; + } + + public void setPath(String path) { //fix this shit + this.fileName = path; + } + + public Boolean checkIntegrity() { + ArrayList records = readAllRows(); + + for (String[] row : records) { + if (row.length != this.rowLength) { + return false; + } + } + return true; + } + + public void addRow(String[] row) { + try { + File file = new File(this.path); + FileWriter outputfile = new FileWriter(file, true); + + // create CSVWriter object filewriter object as parameter + CSVWriter writer = new CSVWriter(outputfile); + if (row.length == this.rowLength) { // only write if this row is the correct length + writer.writeNext(row); + } + + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public ArrayList readAllRows() { + try { // look for an existing file with that name + Reader reader = Files.newBufferedReader(Paths.get(this.path)); + CSVReader csvReader = new CSVReader(reader); + + // get all records at once + List records = csvReader.readAll(); + // loop through records + return new ArrayList (records); + + } catch (IOException e) { + e.printStackTrace(); + return null; + } + + } + + public void printDB() { + for (String[] row : readAllRows()) { + System.out.println(""); + + for (String field : row) { + System.out.print(field + "/"); + } + } + } + + public void clear() { + try { + File file = new File(this.path); + FileWriter outputfile = new FileWriter(file); + + // create CSVWriter object filewriter object as parameter + CSVWriter writer = new CSVWriter(outputfile); + + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + +} diff --git a/src/test/java/DBTest.java b/src/test/java/DBTest.java new file mode 100644 index 0000000..fd1e59a --- /dev/null +++ b/src/test/java/DBTest.java @@ -0,0 +1,169 @@ +import junitparams.JUnitParamsRunner; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Random; + +@RunWith(JUnitParamsRunner.class) +public class DBTest { + + @Test + public void constructorTest() { + + Random random = new Random(); + String fileName = Integer.toString(Math.abs(random.nextInt())) + ".csv"; + + DB db1 = null; + try { + db1 = new DB(fileName, 4); + } catch (IOException e) { + e.printStackTrace(); + } + + String expected = fileName; + Assert.assertEquals(db1.getFileName(), expected); + } + + @Test + public void testDBFileName() { + String fileName = "test.csv"; + DB testDB = null; + try { + testDB = new DB(fileName, 4); + } catch (IOException e) { + e.printStackTrace(); + } + + String actual = testDB.getFileName(); + Assert.assertEquals(testDB.getFileName(), fileName); + } + + @Test + public void testFileNametoPath() { + String expected = "/Users/josh/Desktop/Projects/CR-MacroLabs-OOP-ATM/data/610393892.csv"; + String input = "610393892.csv"; + Assert.assertEquals(expected, DB.pathToFileName(input)); + } + + @Test + public void testPathtoFileName() { + String input = "610393892.csv"; + String expected = "/Users/josh/Desktop/Projects/CR-MacroLabs-OOP-ATM/data/610393892.csv"; + Assert.assertEquals(expected, DB.fileNameToPath(input, System.getProperty("user.dir"))); + } + + @Test + public void tempStuff() { + String fileName = "test.csv"; + DB testDB = null; + try { + testDB = new DB(fileName, 4); + } catch (IOException e) { + e.printStackTrace(); + } + + testDB.addRow(new String[] {"Item 1", "Item 2", "Item 3", "Item 4"}); + testDB.addRow(new String[] {"Item 1b", "Item 2b", "Item 3b", "Item 4b"}); + testDB.addRow(new String[] {"Item 1c", "Item 2c", "Item 3c", "Item 4c"}); + testDB.addRow(new String[] {"Item 1d", "Item 2d", "Item 3d", "Item 4d"}); + + } + + @Test + public void readTestDBTest() { + String fileName = "test.csv"; + + ArrayList expected = new ArrayList<>(); + expected.add(new String[] {"Item 1", "Item 2", "Item 3", "Item 4"}); + expected.add(new String[] {"Item 1b", "Item 2b", "Item 3b", "Item 4b"}); + expected.add(new String[] {"Item 1c", "Item 2c", "Item 3c", "Item 4c"}); + expected.add(new String[] {"Item 1d", "Item 2d", "Item 3d", "Item 4d"}); + + DB testDB = null; + try { + testDB = new DB(fileName, 4); + + ArrayList records = testDB.readAllRows(); + + for (int i = 0; i < 4; i++) { + Assert.assertEquals(expected.get(i), records.get(i)); + } + + + + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void printDBTest() { + String fileName = "test.csv"; + DB testDB = null; + try { + testDB = new DB(fileName, 4); + } catch (IOException e) { + e.printStackTrace(); + } + testDB.printDB(); + } + + @Test + public void clearDBTest() { + Random random = new Random(); + String fileName = Integer.toString(Math.abs(random.nextInt())) + ".csv"; + + DB db1 = null; + try { + db1 = new DB(fileName, 4); + } catch (IOException e) { + e.printStackTrace(); + } + + db1.addRow(new String[]{"Sticky", "Icky", "Wicky", "Quicky"}); + db1.clear(); + + ArrayList records = db1.readAllRows(); + + Assert.assertEquals(0,records.size()); + } + + @Test + public void addRowTest() { + Random random = new Random(); + String fileName = Integer.toString(Math.abs(random.nextInt())) + ".csv"; + + DB db1 = null; + try { + db1 = new DB(fileName, 4); + } catch (IOException e) { + e.printStackTrace(); + } + ArrayList data = new ArrayList<>(); + data.add(new String[] {"Item 1", "Item 2", "Item 3", "Item 4"}); + data.add(new String[] {"Item 1b", "Item 2b", "Item 3b", "Item 4b"}); + data.add(new String[] {"Item 1c", "Item 2c", "Item 3c", "Item 4c"}); + data.add(new String[] {"Item 1d", "Item 2d", "Item 3d", "Item 4d"}); + + ArrayList records; + for (int i = 0; i < 4; i++) { + db1.addRow(data.get(i)); + Assert.assertTrue(i + 1 == db1.length()); + + records = db1.readAllRows(); + + for (int j = 0; j <= i; j++) { + //System.out.println(String.format("Rows: %d Testing row: %d", i, j)); + Assert.assertEquals(records.get(i), data.get(i)); + } + } + } + + @Test + public void getRowLengthTest() { + + } +} \ No newline at end of file From cbebe653d1a0cfaf7b99da2e02df43391ebf4126 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Thu, 24 Oct 2019 14:13:22 -0400 Subject: [PATCH 05/82] comment out testing thing --- src/test/java/DBTest.java | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/test/java/DBTest.java b/src/test/java/DBTest.java index fd1e59a..3d47852 100644 --- a/src/test/java/DBTest.java +++ b/src/test/java/DBTest.java @@ -57,18 +57,18 @@ public void testPathtoFileName() { @Test public void tempStuff() { - String fileName = "test.csv"; - DB testDB = null; - try { - testDB = new DB(fileName, 4); - } catch (IOException e) { - e.printStackTrace(); - } - - testDB.addRow(new String[] {"Item 1", "Item 2", "Item 3", "Item 4"}); - testDB.addRow(new String[] {"Item 1b", "Item 2b", "Item 3b", "Item 4b"}); - testDB.addRow(new String[] {"Item 1c", "Item 2c", "Item 3c", "Item 4c"}); - testDB.addRow(new String[] {"Item 1d", "Item 2d", "Item 3d", "Item 4d"}); +// String fileName = "test.csv"; +// DB testDB = null; +// try { +// testDB = new DB(fileName, 4); +// } catch (IOException e) { +// e.printStackTrace(); +// } +// +// testDB.addRow(new String[] {"Item 1", "Item 2", "Item 3", "Item 4"}); +// testDB.addRow(new String[] {"Item 1b", "Item 2b", "Item 3b", "Item 4b"}); +// testDB.addRow(new String[] {"Item 1c", "Item 2c", "Item 3c", "Item 4c"}); +// testDB.addRow(new String[] {"Item 1d", "Item 2d", "Item 3d", "Item 4d"}); } From e4aad6fcc5ec59120bced614b02b58f19deaacdf Mon Sep 17 00:00:00 2001 From: deltagphys Date: Thu, 24 Oct 2019 14:26:19 -0400 Subject: [PATCH 06/82] testing for row length, getting file name --- pom.xml | 27 ++++++++++++++++++++++++++ src/main/java/DB.java | 8 ++++---- src/test/java/DBTest.java | 41 +++++++++++++++++++++++++++++++++++---- 3 files changed, 68 insertions(+), 8 deletions(-) diff --git a/pom.xml b/pom.xml index 9901415..5ad041c 100644 --- a/pom.xml +++ b/pom.xml @@ -8,5 +8,32 @@ project-2-atm 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.7.0 + + 1.8 + 1.8 + + + + + + + + pl.pragmatists + JUnitParams + 1.1.1 + test + + + com.opencsv + opencsv + 4.5 + + \ No newline at end of file diff --git a/src/main/java/DB.java b/src/main/java/DB.java index f9694c7..0ef73c1 100644 --- a/src/main/java/DB.java +++ b/src/main/java/DB.java @@ -50,9 +50,9 @@ public String getPath() { return this.path; } - public static String pathToFileName (String FN) { + public static String pathToFileName (String Path) { - String[] splitter = FN.split("/"); + String[] splitter = Path.split("/"); return splitter[splitter.length-1]; } @@ -68,8 +68,8 @@ public Integer getRowLength() { return this.rowLength; } - public void setPath(String path) { //fix this shit - this.fileName = path; + public void setPath(String fName) { + this.fileName = fName; } public Boolean checkIntegrity() { diff --git a/src/test/java/DBTest.java b/src/test/java/DBTest.java index 3d47852..ed310da 100644 --- a/src/test/java/DBTest.java +++ b/src/test/java/DBTest.java @@ -42,14 +42,14 @@ public void testDBFileName() { } @Test - public void testFileNametoPath() { - String expected = "/Users/josh/Desktop/Projects/CR-MacroLabs-OOP-ATM/data/610393892.csv"; - String input = "610393892.csv"; + public void testPathToFileName() { + String input = "/Users/josh/Desktop/Projects/CR-MacroLabs-OOP-ATM/data/610393892.csv"; + String expected = "610393892.csv"; Assert.assertEquals(expected, DB.pathToFileName(input)); } @Test - public void testPathtoFileName() { + public void testFileNametoPath() { String input = "610393892.csv"; String expected = "/Users/josh/Desktop/Projects/CR-MacroLabs-OOP-ATM/data/610393892.csv"; Assert.assertEquals(expected, DB.fileNameToPath(input, System.getProperty("user.dir"))); @@ -164,6 +164,39 @@ public void addRowTest() { @Test public void getRowLengthTest() { + Random random = new Random(); + String fileName = Integer.toString(Math.abs(random.nextInt())) + ".csv"; + + DB db1 = null; + Integer rowL = random.nextInt(10); + try { + db1 = new DB(fileName, rowL); + } catch (IOException e) { + e.printStackTrace(); + } + + String[] row = new String[rowL]; + db1.addRow(row); + Assert.assertTrue(1 == db1.length()); + + Assert.assertTrue(rowL == db1.getRowLength()); + } + + @Test + public void getFileNameTest() { + Random random = new Random(); + String fileName = Integer.toString(Math.abs(random.nextInt())) + ".csv"; + + DB db1 = null; + Integer rowL = random.nextInt(10); + try { + db1 = new DB(fileName, rowL); + } catch (IOException e) { + e.printStackTrace(); + } + + Assert.assertTrue(fileName == db1.getFileName()); + } } \ No newline at end of file From 19b163e7819dccb2259907fb402e17d05c2293f2 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Thu, 24 Oct 2019 14:47:26 -0400 Subject: [PATCH 07/82] DB deletion --- src/main/java/DB.java | 139 +++++++++++++++++++++++--------------- src/test/java/DBTest.java | 46 +++++++++++-- 2 files changed, 128 insertions(+), 57 deletions(-) diff --git a/src/main/java/DB.java b/src/main/java/DB.java index 0ef73c1..d045c11 100644 --- a/src/main/java/DB.java +++ b/src/main/java/DB.java @@ -13,19 +13,23 @@ public class DB { private String fileName; private String path; private Integer rowLength; + private Boolean deleted; public DB(String fileName, Integer rowLength) throws IOException { this.fileName = fileName; this.path = fileNameToPath(this.fileName, System.getProperty("user.dir")); this.rowLength = rowLength; + this.deleted = false; try { // look for an existing file with that name Reader reader = Files.newBufferedReader(Paths.get(this.path)); CSVReader csvReader = new CSVReader(reader); - // get all records at once + // get all records at once, use that rowLength to override given one, if nec. List records = csvReader.readAll(); - // loop through records + if (records.size() > 0) { + this.rowLength = records.get(0).length; + } } catch(NoSuchFileException e) { // make a new file, if one doesn't exist in place @@ -43,17 +47,20 @@ public DB(String fileName, Integer rowLength) throws IOException { } public String getFileName() { - return this.fileName; + if (!this.deleted) { + return this.fileName; + } else { + return null; + } } - public String getPath() { - return this.path; + public Boolean isDeleted() { + return deleted; } public static String pathToFileName (String Path) { - String[] splitter = Path.split("/"); - return splitter[splitter.length-1]; + return splitter[splitter.length - 1]; } public static String fileNameToPath (String FN, String PWD) { @@ -61,85 +68,111 @@ public static String fileNameToPath (String FN, String PWD) { } public Integer length() { - return readAllRows().size(); + if (!this.deleted) { + return readAllRows().size(); + } else { + return null; + } } public Integer getRowLength() { - return this.rowLength; - } - - public void setPath(String fName) { - this.fileName = fName; + if (!this.deleted) { + return this.rowLength; + } else { + return null; + } } public Boolean checkIntegrity() { - ArrayList records = readAllRows(); + if (!this.deleted) { + ArrayList records = readAllRows(); - for (String[] row : records) { - if (row.length != this.rowLength) { - return false; + for (String[] row : records) { + if (row.length != this.rowLength) { + return false; + } } + return true; + } else { + return false; } - return true; } public void addRow(String[] row) { - try { - File file = new File(this.path); - FileWriter outputfile = new FileWriter(file, true); - - // create CSVWriter object filewriter object as parameter - CSVWriter writer = new CSVWriter(outputfile); - if (row.length == this.rowLength) { // only write if this row is the correct length - writer.writeNext(row); + if (!this.deleted) { + try { + File file = new File(this.path); + FileWriter outputfile = new FileWriter(file, true); + + // create CSVWriter object filewriter object as parameter + CSVWriter writer = new CSVWriter(outputfile); + if (row.length == this.rowLength) { // only write if this row is the correct length + writer.writeNext(row); + } + + writer.close(); + } catch (IOException e) { + e.printStackTrace(); } - - writer.close(); - } catch (IOException e) { - e.printStackTrace(); } } public ArrayList readAllRows() { - try { // look for an existing file with that name - Reader reader = Files.newBufferedReader(Paths.get(this.path)); - CSVReader csvReader = new CSVReader(reader); - - // get all records at once - List records = csvReader.readAll(); - // loop through records - return new ArrayList (records); - - } catch (IOException e) { - e.printStackTrace(); + if (!this.deleted) { + try { // look for an existing file with that name + Reader reader = Files.newBufferedReader(Paths.get(this.path)); + CSVReader csvReader = new CSVReader(reader); + + // get all records at once + List records = csvReader.readAll(); + // loop through records + return new ArrayList(records); + + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } else { return null; } } public void printDB() { - for (String[] row : readAllRows()) { - System.out.println(""); + if (!this.deleted) { + for (String[] row : readAllRows()) { + System.out.println(""); - for (String field : row) { - System.out.print(field + "/"); + for (String field : row) { + System.out.print(field + "/"); + } } } } public void clear() { - try { - File file = new File(this.path); - FileWriter outputfile = new FileWriter(file); + if (!this.deleted) { + try { + File file = new File(this.path); + FileWriter outputfile = new FileWriter(file); - // create CSVWriter object filewriter object as parameter - CSVWriter writer = new CSVWriter(outputfile); + // create CSVWriter object filewriter object as parameter + CSVWriter writer = new CSVWriter(outputfile); - writer.close(); - } catch (IOException e) { - e.printStackTrace(); + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + } } } + public void delete() { + + File file = new File(this.path); + file.delete(); + this.deleted = true; + + } + } diff --git a/src/test/java/DBTest.java b/src/test/java/DBTest.java index ed310da..0eabb88 100644 --- a/src/test/java/DBTest.java +++ b/src/test/java/DBTest.java @@ -25,10 +25,26 @@ public void constructorTest() { String expected = fileName; Assert.assertEquals(db1.getFileName(), expected); + db1.delete(); } @Test - public void testDBFileName() { + public void constructorBadRowLengthTest() { + + String fileName = "test.csv"; + DB testDB = null; + try { + testDB = new DB(fileName, 7); + } catch (IOException e) { + e.printStackTrace(); + } + + Integer actual = testDB.getRowLength(); + Assert.assertTrue(4 == actual); + } + + @Test + public void DBFileNameTest() { String fileName = "test.csv"; DB testDB = null; try { @@ -42,14 +58,14 @@ public void testDBFileName() { } @Test - public void testPathToFileName() { + public void pathToFileNameTest() { String input = "/Users/josh/Desktop/Projects/CR-MacroLabs-OOP-ATM/data/610393892.csv"; String expected = "610393892.csv"; Assert.assertEquals(expected, DB.pathToFileName(input)); } @Test - public void testFileNametoPath() { + public void fileNametoPathTest() { String input = "610393892.csv"; String expected = "/Users/josh/Desktop/Projects/CR-MacroLabs-OOP-ATM/data/610393892.csv"; Assert.assertEquals(expected, DB.fileNameToPath(input, System.getProperty("user.dir"))); @@ -129,6 +145,7 @@ public void clearDBTest() { ArrayList records = db1.readAllRows(); Assert.assertEquals(0,records.size()); + db1.delete(); } @Test @@ -160,6 +177,7 @@ public void addRowTest() { Assert.assertEquals(records.get(i), data.get(i)); } } + db1.delete(); } @Test @@ -180,6 +198,7 @@ public void getRowLengthTest() { Assert.assertTrue(1 == db1.length()); Assert.assertTrue(rowL == db1.getRowLength()); + db1.delete(); } @Test @@ -196,7 +215,26 @@ public void getFileNameTest() { } Assert.assertTrue(fileName == db1.getFileName()); + db1.delete(); + } + + @Test + public void deleteFileTest() { + Random random = new Random(); + String fileName = Integer.toString(Math.abs(random.nextInt())) + ".csv"; - + DB db1 = null; + Integer rowL = random.nextInt(10); + try { + db1 = new DB(fileName, rowL); + } catch (IOException e) { + e.printStackTrace(); + } + + Assert.assertTrue(false == db1.isDeleted()); + db1.delete(); + Assert.assertTrue(true == db1.isDeleted()); } + + } \ No newline at end of file From 7de21bb58a66f0a1a3301b7464751ae1a9f8bcee Mon Sep 17 00:00:00 2001 From: deltagphys Date: Thu, 24 Oct 2019 14:58:12 -0400 Subject: [PATCH 08/82] DB integrity checks --- src/test/java/DBTest.java | 66 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/src/test/java/DBTest.java b/src/test/java/DBTest.java index 0eabb88..8b6872e 100644 --- a/src/test/java/DBTest.java +++ b/src/test/java/DBTest.java @@ -236,5 +236,71 @@ public void deleteFileTest() { Assert.assertTrue(true == db1.isDeleted()); } + @Test + public void integrityGoodTest() { + Random random = new Random(); + String fileName = Integer.toString(Math.abs(random.nextInt())) + ".csv"; + + DB db1 = null; + try { + db1 = new DB(fileName, 4); + } catch (IOException e) { + e.printStackTrace(); + } + ArrayList data = new ArrayList<>(); + data.add(new String[] {"Item 1", "Item 2", "Item 3", "Item 4"}); + data.add(new String[] {"Item 1b", "Item 2b", "Item 3b", "Item 4b"}); + data.add(new String[] {"Item 1c", "Item 2c", "Item 3c", "Item 4c"}); + data.add(new String[] {"Item 1d", "Item 2d", "Item 3d", "Item 4d"}); + ArrayList records; + for (int i = 0; i < 4; i++) { + db1.addRow(data.get(i)); + } + + Assert.assertEquals(true, db1.checkIntegrity()); + db1.delete(); + } + + @Test + public void integrityBadRowAddTest() { // check to make sure it won't add an improper length of row + Random random = new Random(); + String fileName = Integer.toString(Math.abs(random.nextInt())) + ".csv"; + + DB db1 = null; + try { + db1 = new DB(fileName, 4); + } catch (IOException e) { + e.printStackTrace(); + } + ArrayList data = new ArrayList<>(); + data.add(new String[] {"Item 1", "Item 2", "Item 3", "Item 4"}); + data.add(new String[] {"Item 1b", "Item 2b", "Item 3b", "Item 4b"}); + data.add(new String[] {"Item 1c", "Item 2c", "Item 3c"}); + data.add(new String[] {"Item 1d", "Item 2d", "Item 3d", "Item 4d"}); + + ArrayList records; + for (int i = 0; i < 4; i++) { + db1.addRow(data.get(i)); + } + + Assert.assertTrue(3 == db1.readAllRows().size()); + Assert.assertEquals(true, db1.checkIntegrity()); + db1.delete(); + } + + @Test + public void integrityBadTest() { // check to make sure it won't add an improper length of row + + String fileName = "testBad.csv"; + + DB db1 = null; + try { + db1 = new DB(fileName, 4); + } catch (IOException e) { + e.printStackTrace(); + } + + Assert.assertEquals(false, db1.checkIntegrity()); + } } \ No newline at end of file From 5c74f2da03d296e0bc9b7949bbcaf8faf3e96045 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Thu, 24 Oct 2019 15:12:42 -0400 Subject: [PATCH 09/82] row replacement --- src/main/java/DB.java | 12 ++++++++++++ src/test/java/DBTest.java | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/main/java/DB.java b/src/main/java/DB.java index d045c11..e3d0d30 100644 --- a/src/main/java/DB.java +++ b/src/main/java/DB.java @@ -117,6 +117,18 @@ public void addRow(String[] row) { } } + public void replaceRow(Integer rowNum, String[] replacement) { + ArrayList records = null; + if (!this.deleted && rowNum < length() && replacement.length == this.rowLength) { + records = readAllRows(); + records.set(rowNum, replacement); + } + clear(); + for (String[] row : records) { + addRow(row); + } + } + public ArrayList readAllRows() { if (!this.deleted) { try { // look for an existing file with that name diff --git a/src/test/java/DBTest.java b/src/test/java/DBTest.java index 8b6872e..a5740cc 100644 --- a/src/test/java/DBTest.java +++ b/src/test/java/DBTest.java @@ -303,4 +303,41 @@ public void integrityBadTest() { // check to make sure it won't add an improper Assert.assertEquals(false, db1.checkIntegrity()); } + + @Test + public void replaceRowTest() { + Random random = new Random(); + String fileName = Integer.toString(Math.abs(random.nextInt())) + ".csv"; + + DB db1 = null; + try { + db1 = new DB(fileName, 4); + } catch (IOException e) { + e.printStackTrace(); + } + ArrayList data = new ArrayList<>(); + data.add(new String[] {"Item 1", "Item 2", "Item 3", "Item 4"}); + data.add(new String[] {"Item 1b", "Item 2b", "Item 3b", "Item 4b"}); + data.add(new String[] {"Item 1c", "Item 2c", "Item 3c", "Item 4c"}); + data.add(new String[] {"Item 1d", "Item 2d", "Item 3d", "Item 4d"}); + + for (int i = 0; i < 4; i++) { + db1.addRow(data.get(i)); + } + + String[] replacementRow = new String[] {"Changed Item 1c", "Changed Item 2c", "Changed Item 3c", "Changed Item 4c"}; + + db1.replaceRow(2, replacementRow); + + ArrayList records = db1.readAllRows(); + + for (int i = 0; i < 4; i++) { + if (i == 2) { + Assert.assertEquals(replacementRow, records.get(i)); + } else { + Assert.assertEquals(data.get(i), records.get(i)); + } + } + db1.delete(); + } } \ No newline at end of file From 83e5e50d063e101562efaa45ffdc859a75c3c549 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Thu, 24 Oct 2019 15:25:22 -0400 Subject: [PATCH 10/82] row deletion by index --- src/main/java/DB.java | 12 ++++++++++++ src/test/java/DBTest.java | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/src/main/java/DB.java b/src/main/java/DB.java index e3d0d30..78ef8eb 100644 --- a/src/main/java/DB.java +++ b/src/main/java/DB.java @@ -129,6 +129,18 @@ public void replaceRow(Integer rowNum, String[] replacement) { } } + public void deleteRow(int rowNum) { + ArrayList records = null; + if (!this.deleted && rowNum < length()) { + records = readAllRows(); + records.remove(rowNum); + } + clear(); + for (String[] row : records) { + addRow(row); + } + } + public ArrayList readAllRows() { if (!this.deleted) { try { // look for an existing file with that name diff --git a/src/test/java/DBTest.java b/src/test/java/DBTest.java index a5740cc..e7654a8 100644 --- a/src/test/java/DBTest.java +++ b/src/test/java/DBTest.java @@ -340,4 +340,40 @@ public void replaceRowTest() { } db1.delete(); } + + @Test + public void deleteRowTest() { + Random random = new Random(); + String fileName = Integer.toString(Math.abs(random.nextInt())) + ".csv"; + + DB db1 = null; + try { + db1 = new DB(fileName, 4); + } catch (IOException e) { + e.printStackTrace(); + } + ArrayList data = new ArrayList<>(); + data.add(new String[] {"Item 1", "Item 2", "Item 3", "Item 4"}); + data.add(new String[] {"Item 1b", "Item 2b", "Item 3b", "Item 4b"}); + data.add(new String[] {"Item 1c", "Item 2c", "Item 3c", "Item 4c"}); + data.add(new String[] {"Item 1d", "Item 2d", "Item 3d", "Item 4d"}); + data.add(new String[] {"Item 1e", "Item 2e", "Item 3e", "Item 4e"}); + + for (int i = 0; i < data.size(); i++) { + db1.addRow(data.get(i)); + } + + db1.deleteRow(2); + + ArrayList records = db1.readAllRows(); + + for (int i = 0; i < 4; i++) { + if (i >= 2) { + Assert.assertEquals(data.get(i+1), records.get(i)); + } else { + Assert.assertEquals(data.get(i), records.get(i)); + } + } + db1.delete(); + } } \ No newline at end of file From 4a92b4fb6012d3022f35dc29282ac23901242aef Mon Sep 17 00:00:00 2001 From: deltagphys Date: Thu, 24 Oct 2019 15:41:35 -0400 Subject: [PATCH 11/82] row reading and row serialization - static and object-referenced --- src/main/java/DB.java | 17 +++++++++++ src/test/java/DBTest.java | 62 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/src/main/java/DB.java b/src/main/java/DB.java index 78ef8eb..29bfd09 100644 --- a/src/main/java/DB.java +++ b/src/main/java/DB.java @@ -141,6 +141,23 @@ public void deleteRow(int rowNum) { } } + public String[] readRow(int rowNum) { + ArrayList records = null; + if (!this.deleted && rowNum < length()) { + records = readAllRows(); + return records.get(rowNum); + } + return new String[this.rowLength]; + } + + public String serialize(int rowNum) { + return DB.serialize(readRow(rowNum)); + } + + public static String serialize(String[] row) { + return String.join("/", row); + } + public ArrayList readAllRows() { if (!this.deleted) { try { // look for an existing file with that name diff --git a/src/test/java/DBTest.java b/src/test/java/DBTest.java index e7654a8..472f87f 100644 --- a/src/test/java/DBTest.java +++ b/src/test/java/DBTest.java @@ -376,4 +376,66 @@ public void deleteRowTest() { } db1.delete(); } + + @Test + public void readRowTest() { + Random random = new Random(); + String fileName = Integer.toString(Math.abs(random.nextInt())) + ".csv"; + + DB db1 = null; + try { + db1 = new DB(fileName, 4); + } catch (IOException e) { + e.printStackTrace(); + } + ArrayList data = new ArrayList<>(); + data.add(new String[] {"Item 1", "Item 2", "Item 3", "Item 4"}); + data.add(new String[] {"Item 1b", "Item 2b", "Item 3b", "Item 4b"}); + data.add(new String[] {"Item 1c", "Item 2c", "Item 3c", "Item 4c"}); + data.add(new String[] {"Item 1d", "Item 2d", "Item 3d", "Item 4d"}); + data.add(new String[] {"Item 1e", "Item 2e", "Item 3e", "Item 4e"}); + + for (int i = 0; i < data.size(); i++) { + db1.addRow(data.get(i)); + } + + for (int i = 0; i < 4; i++) { + Assert.assertEquals(data.get(i), db1.readRow(i)); + } + db1.delete(); + } + + @Test + public void serializeTest() { + Random random = new Random(); + String fileName = Integer.toString(Math.abs(random.nextInt())) + ".csv"; + + DB db1 = null; + try { + db1 = new DB(fileName, 4); + } catch (IOException e) { + e.printStackTrace(); + } + ArrayList data = new ArrayList<>(); + data.add(new String[] {"Item 1", "Item 2", "Item 3", "Item 4"}); + data.add(new String[] {"Item 1b", "Item 2b", "Item 3b", "Item 4b"}); + data.add(new String[] {"Item 1c", "Item 2c", "Item 3c", "Item 4c"}); + data.add(new String[] {"Item 1d", "Item 2d", "Item 3d", "Item 4d"}); + data.add(new String[] {"Item 1e", "Item 2e", "Item 3e", "Item 4e"}); + + for (int i = 0; i < data.size(); i++) { + db1.addRow(data.get(i)); + } + + for (int i = 0; i < 4; i++) { + Assert.assertEquals(String.join("/",data.get(i)), db1.serialize(i)); + } + db1.delete(); + } + + @Test + public void staticSerializeTest() { + String[] row = new String[] {"Item 1b", "Item 2b", "Item 3b", "Item 4b"}; + Assert.assertEquals(String.join("/",row), DB.serialize(row)); + } } \ No newline at end of file From 7c0ab19070babfea0981ba85a6d73cf9c4c88552 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Fri, 25 Oct 2019 09:46:23 -0400 Subject: [PATCH 12/82] partial serialization - static and object-focused --- src/main/java/DB.java | 17 +++++++++++++++++ src/test/java/DBTest.java | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/main/java/DB.java b/src/main/java/DB.java index 29bfd09..6950171 100644 --- a/src/main/java/DB.java +++ b/src/main/java/DB.java @@ -7,6 +7,7 @@ import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; +import java.util.regex.Pattern; public class DB { @@ -154,10 +155,26 @@ public String serialize(int rowNum) { return DB.serialize(readRow(rowNum)); } + public String partialSerialize(int rowNum, int[] fields) { + return DB.partialSerialize(readRow(rowNum),fields); + } + public static String serialize(String[] row) { return String.join("/", row); } + public static String partialSerialize(String[] row, int[] fields) { + String[] partialArray = new String[fields.length]; + for (int i = 0; i < fields.length; i++) { + partialArray[i] = row[fields[i]]; + } + return String.join("/", partialArray); + } + + public int findRow(Pattern pattern, int[] fields){ + return 0; + } + public ArrayList readAllRows() { if (!this.deleted) { try { // look for an existing file with that name diff --git a/src/test/java/DBTest.java b/src/test/java/DBTest.java index 472f87f..b1b3a70 100644 --- a/src/test/java/DBTest.java +++ b/src/test/java/DBTest.java @@ -438,4 +438,41 @@ public void staticSerializeTest() { String[] row = new String[] {"Item 1b", "Item 2b", "Item 3b", "Item 4b"}; Assert.assertEquals(String.join("/",row), DB.serialize(row)); } + + @Test + public void partialSerializeTest() { + Random random = new Random(); + String fileName = Integer.toString(Math.abs(random.nextInt())) + ".csv"; + + DB db1 = null; + try { + db1 = new DB(fileName, 4); + } catch (IOException e) { + e.printStackTrace(); + } + ArrayList data = new ArrayList<>(); + data.add(new String[] {"Item 1", "Item 2", "Item 3", "Item 4"}); + data.add(new String[] {"Item 1b", "Item 2b", "Item 3b", "Item 4b"}); + data.add(new String[] {"Item 1c", "Item 2c", "Item 3c", "Item 4c"}); + data.add(new String[] {"Item 1d", "Item 2d", "Item 3d", "Item 4d"}); + data.add(new String[] {"Item 1e", "Item 2e", "Item 3e", "Item 4e"}); + + for (int i = 0; i < data.size(); i++) { + db1.addRow(data.get(i)); + } + + + Assert.assertEquals("Item 1d/Item 2d/Item 4d", db1.partialSerialize(3,new int[] {0,1,3})); + Assert.assertEquals("Item 3e", db1.partialSerialize(4,new int[] {2})); + Assert.assertEquals("Item 2e/Item 3e", db1.partialSerialize(4,new int[] {1,2})); + + db1.delete(); + } + + @Test + public void partialsStaticSerializeTest() { + String[] row = new String[] {"Item 1b", "Item 2b", "Item 3b", "Item 4b"}; + int[] fields = {1,3}; + Assert.assertEquals("Item 2b/Item 4b", DB.partialSerialize(row, fields)); + } } \ No newline at end of file From 732dff6d79a56ed222264089f5e2a463c7805309 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Fri, 25 Oct 2019 11:00:13 -0400 Subject: [PATCH 13/82] find row and find partial row --- src/main/java/DB.java | 44 ++++++++++++++++++++++++---- src/test/java/DBTest.java | 60 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 96 insertions(+), 8 deletions(-) diff --git a/src/main/java/DB.java b/src/main/java/DB.java index 6950171..f2a6077 100644 --- a/src/main/java/DB.java +++ b/src/main/java/DB.java @@ -151,12 +151,48 @@ public String[] readRow(int rowNum) { return new String[this.rowLength]; } + public int findWholeRow (String[] row) { + if (!this.deleted) { + String[] expected; + for (int i = 0; i < this.length(); i++) { + if (this.serialize(row).equals(this.serialize(i))) { + return i; + } + } + return -1; + } else { + return -1; + } + } + + public int findPartialRow (String[] fragment, int[] fields) { + if (!this.deleted) { + String signature = serialize(fragment); + for (int i = 0; i < this.length(); i++) { + if (signature.equals(this.partialSerialize(i,fields))) { + return i; + } + } + return -1; + } else { + return -1; + } + } + public String serialize(int rowNum) { - return DB.serialize(readRow(rowNum)); + if (!this.deleted) { + return DB.serialize(this.readRow(rowNum)); + } else { + return null; + } } public String partialSerialize(int rowNum, int[] fields) { - return DB.partialSerialize(readRow(rowNum),fields); + if (!this.deleted) { + return DB.partialSerialize(this.readRow(rowNum),fields); + } else { + return null; + } } public static String serialize(String[] row) { @@ -171,10 +207,6 @@ public static String partialSerialize(String[] row, int[] fields) { return String.join("/", partialArray); } - public int findRow(Pattern pattern, int[] fields){ - return 0; - } - public ArrayList readAllRows() { if (!this.deleted) { try { // look for an existing file with that name diff --git a/src/test/java/DBTest.java b/src/test/java/DBTest.java index b1b3a70..b33b1ec 100644 --- a/src/test/java/DBTest.java +++ b/src/test/java/DBTest.java @@ -461,7 +461,6 @@ public void partialSerializeTest() { db1.addRow(data.get(i)); } - Assert.assertEquals("Item 1d/Item 2d/Item 4d", db1.partialSerialize(3,new int[] {0,1,3})); Assert.assertEquals("Item 3e", db1.partialSerialize(4,new int[] {2})); Assert.assertEquals("Item 2e/Item 3e", db1.partialSerialize(4,new int[] {1,2})); @@ -470,9 +469,66 @@ public void partialSerializeTest() { } @Test - public void partialsStaticSerializeTest() { + public void partialStaticSerializeTest() { String[] row = new String[] {"Item 1b", "Item 2b", "Item 3b", "Item 4b"}; int[] fields = {1,3}; Assert.assertEquals("Item 2b/Item 4b", DB.partialSerialize(row, fields)); } + + @Test + public void findWholeRowTest() { + Random random = new Random(); + String fileName = Integer.toString(Math.abs(random.nextInt())) + ".csv"; + + DB db1 = null; + try { + db1 = new DB(fileName, 4); + } catch (IOException e) { + e.printStackTrace(); + } + ArrayList data = new ArrayList<>(); + data.add(new String[] {"Item 1", "Item 2", "Item 3", "Item 4"}); + data.add(new String[] {"Item 1b", "Item 2b", "Item 3b", "Item 4b"}); + data.add(new String[] {"Item 1c", "Item 2c", "Item 3c", "Item 4c"}); + data.add(new String[] {"Item 1d", "Item 2d", "Item 3d", "Item 4d"}); + data.add(new String[] {"Item 1e", "Item 2e", "Item 3e", "Item 4e"}); + + for (int i = 0; i < data.size(); i++) { + db1.addRow(data.get(i)); + } + + for (int i = 0; i < data.size(); i++) { + Assert.assertEquals(i, db1.findWholeRow(data.get(i))); + } + db1.delete(); + } + + @Test + public void findPartialRowTest() { + Random random = new Random(); + String fileName = Integer.toString(Math.abs(random.nextInt())) + ".csv"; + + DB db1 = null; + try { + db1 = new DB(fileName, 4); + } catch (IOException e) { + e.printStackTrace(); + } + ArrayList data = new ArrayList<>(); + data.add(new String[] {"Item 1", "Item 2", "Item 3", "Item 4"}); + data.add(new String[] {"Item 1b", "Item 2b", "Item 3b", "Item 4b"}); + data.add(new String[] {"Item 1c", "Item 2c", "Item 3c", "Item 4c"}); + data.add(new String[] {"Item 1d", "Item 2d", "Item 3d", "Item 4d"}); + data.add(new String[] {"Item 1e", "Item 2e", "Item 3e", "Item 4e"}); + + for (int i = 0; i < data.size(); i++) { + db1.addRow(data.get(i)); + } + + Assert.assertEquals(3, db1.findPartialRow(new String[] {"Item 1d", "Item 2d", "Item 4d"}, new int[] {0,1,3})); + Assert.assertEquals(1, db1.findPartialRow(new String[] {"Item 3b"}, new int[] {2})); + Assert.assertEquals(4, db1.findPartialRow(new String[] {"Item 1e", "Item 2e", "Item 3e", "Item 4e"}, new int[] {0,1,2,3})); + + db1.delete(); + } } \ No newline at end of file From 888b5c5cf8bd689234d083d18d5aae62c912cba5 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Fri, 25 Oct 2019 15:40:54 -0400 Subject: [PATCH 14/82] find partial multiple rows --- src/main/java/DB.java | 222 +++++++++++++++++++++++++++++++++++--- src/test/java/DBTest.java | 35 ++++++ 2 files changed, 244 insertions(+), 13 deletions(-) diff --git a/src/main/java/DB.java b/src/main/java/DB.java index f2a6077..f30bf3b 100644 --- a/src/main/java/DB.java +++ b/src/main/java/DB.java @@ -7,7 +7,6 @@ import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; -import java.util.regex.Pattern; public class DB { @@ -16,6 +15,15 @@ public class DB { private Integer rowLength; private Boolean deleted; + /** + * DB object constructor + * + * Creates a DB object - connects to an existing file or creates an empty file + * + * @param fileName String: desired csv filename (with extension) + * @param rowLength Integer: the length of each row in the DB; if it conflicts with data in an existing file, will be overridden + * @throws IOException + */ public DB(String fileName, Integer rowLength) throws IOException { this.fileName = fileName; this.path = fileNameToPath(this.fileName, System.getProperty("user.dir")); @@ -47,6 +55,24 @@ public DB(String fileName, Integer rowLength) throws IOException { } } + /** + * isDeleted + * + * Returns true if the DB file has been deleted - called in many methods to prevent trying to access deleted file + * + * @return Boolean: deleted status + */ + public Boolean isDeleted() { + return deleted; + } + + /** + * getFileName + * + * Returns the file name of the DB (not the full path) + * + * @return String fileName + */ public String getFileName() { if (!this.deleted) { return this.fileName; @@ -55,19 +81,39 @@ public String getFileName() { } } - public Boolean isDeleted() { - return deleted; - } - + /** + * pathToFileName (static) + * + * removes last / and everything before from a string, to get the filename + * + * @param Path String: the path to some file + * @return String: fileName + */ public static String pathToFileName (String Path) { String[] splitter = Path.split("/"); return splitter[splitter.length - 1]; } + /** + * fileNameToPath (static) + * + * given a file name, append the path to the data folder + * + * @param FN String: the file name + * @param PWD String: present working directory String + * @return + */ public static String fileNameToPath (String FN, String PWD) { return PWD + "/data/" + FN; } + /** + * length + * + * Determine the length of this DB + * + * @return Integer: length + */ public Integer length() { if (!this.deleted) { return readAllRows().size(); @@ -76,6 +122,13 @@ public Integer length() { } } + /** + * getRowLength + * + * Return the length of rows in this DB + * + * @return Integer: rowLength + */ public Integer getRowLength() { if (!this.deleted) { return this.rowLength; @@ -84,6 +137,13 @@ public Integer getRowLength() { } } + /** + * checkIntegrity + * + * Return true if the DB has rows all of the same length, which is the rowLength property + * + * @return Boolean + */ public Boolean checkIntegrity() { if (!this.deleted) { ArrayList records = readAllRows(); @@ -99,6 +159,13 @@ public Boolean checkIntegrity() { } } + /** + * addRow + * + * Input a String[] that is a new row to add; if it is the proper length, it is added to the DB + * + * @param row String[]: the row to be input + */ public void addRow(String[] row) { if (!this.deleted) { try { @@ -118,7 +185,15 @@ public void addRow(String[] row) { } } - public void replaceRow(Integer rowNum, String[] replacement) { + /** + * replaceRow + * + * Replaces a row (identified by index) with an input String[]; must be correct size array + * + * @param rowNum int: which row to replace + * @param replacement String[]: the row with which to replace it + */ + public void replaceRow(int rowNum, String[] replacement) { ArrayList records = null; if (!this.deleted && rowNum < length() && replacement.length == this.rowLength) { records = readAllRows(); @@ -130,6 +205,13 @@ public void replaceRow(Integer rowNum, String[] replacement) { } } + /** + * deleteRow + * + * Delete an existing row, by index + * + * @param rowNum int: the row index to be deleted + */ public void deleteRow(int rowNum) { ArrayList records = null; if (!this.deleted && rowNum < length()) { @@ -142,6 +224,14 @@ public void deleteRow(int rowNum) { } } + /** + * readRow (by index) + * + * Return the ith row + * + * @param rowNum int: the row number + * @return String[] row: the desired row + */ public String[] readRow(int rowNum) { ArrayList records = null; if (!this.deleted && rowNum < length()) { @@ -151,6 +241,14 @@ public String[] readRow(int rowNum) { return new String[this.rowLength]; } + /** + * findWholeRow (by row) + * + * Returns the index of an entire row which is input + * + * @param row String[]: the row to find + * @return int rowNum: the index of that row + */ public int findWholeRow (String[] row) { if (!this.deleted) { String[] expected; @@ -165,6 +263,15 @@ public int findWholeRow (String[] row) { } } + /** + * findPartialRow + * + * Find a row's index, given a set of field values and numbers + * + * @param fragment String[]: an array of field values to find within a row + * @param fields int[]: the field positions that correspond with those values + * @return int rowNum: the index of the row + */ public int findPartialRow (String[] fragment, int[] fields) { if (!this.deleted) { String signature = serialize(fragment); @@ -179,6 +286,48 @@ public int findPartialRow (String[] fragment, int[] fields) { } } + /** + * findPartialRowMultiple + * + * Find a set of row indices, given a set of field values and numbers to match. + * Matches first result, if multiple are present + * + * @param fragment String[]: an array of field values to find within a row + * @param fields int[]: the field positions that correspond with those values + * @return int[] rowNums: the indices of the rows + */ + public int[] findPartialRowMultiple (String[] fragment, int[] fields) { + if (!this.deleted) { + String signature = serialize(fragment); + ArrayList matchedRows = new ArrayList(); + for (int i = 0; i < this.length(); i++) { + if (signature.equals(this.partialSerialize(i,fields))) { + matchedRows.add(i); + } + } + + if (matchedRows.size() > 0) { + int[] result = new int[matchedRows.size()]; + for (int i = 0; i < matchedRows.size(); i++ ) { + result[i] = matchedRows.get(i); + } + return result; + } else { + return null; + } + } else { + return null; + } + } + + /** + * serialize (object) + * + * Take any row, by index, and return a string representation + * + * @param rowNum int: the row to serialize + * @return String: serial representation + */ public String serialize(int rowNum) { if (!this.deleted) { return DB.serialize(this.readRow(rowNum)); @@ -187,6 +336,15 @@ public String serialize(int rowNum) { } } + /** + * partialSerialize (object) + * + * Turn part of a row into a string representation + * + * @param rowNum int: the row to serialize + * @param fields int[]: which fields from the row to serialize + * @return String: serialized representation of those fields + */ public String partialSerialize(int rowNum, int[] fields) { if (!this.deleted) { return DB.partialSerialize(this.readRow(rowNum),fields); @@ -195,18 +353,42 @@ public String partialSerialize(int rowNum, int[] fields) { } } + /** + * serialize (static) + * + * Take any String [] and return a string representation + * + * @param row String[]: the 'row' to serialize + * @return String: serial representation + */ public static String serialize(String[] row) { return String.join("/", row); } - public static String partialSerialize(String[] row, int[] fields) { + /** + * partialSerialize (static) + * + * Turn part of a String[] into a string representation + * + * @param fragment String[]: an array of field values to serialize + * @param fields int[]: the field positions that correspond with those values + * @return String: serialized representation of those fields + */ + public static String partialSerialize(String[] fragment, int[] fields) { String[] partialArray = new String[fields.length]; for (int i = 0; i < fields.length; i++) { - partialArray[i] = row[fields[i]]; + partialArray[i] = fragment[fields[i]]; } return String.join("/", partialArray); } + /** + * readAllRows + * + * Return an ArrayList of all of the rows in the DB + * + * @return ArrayList of String[] arrays: the data + */ public ArrayList readAllRows() { if (!this.deleted) { try { // look for an existing file with that name @@ -228,18 +410,26 @@ public ArrayList readAllRows() { } + /** + * printDB + * + * For debugging: print out the whole DB + * + */ public void printDB() { if (!this.deleted) { for (String[] row : readAllRows()) { - System.out.println(""); - - for (String field : row) { - System.out.print(field + "/"); - } + System.out.println(serialize(row)); } } } + /** + * clear + * + * Method to clear the DB + * + */ public void clear() { if (!this.deleted) { try { @@ -256,6 +446,12 @@ public void clear() { } } + /** + * delete + * + * Method to delete the DB file + * + */ public void delete() { File file = new File(this.path); diff --git a/src/test/java/DBTest.java b/src/test/java/DBTest.java index b33b1ec..be93f95 100644 --- a/src/test/java/DBTest.java +++ b/src/test/java/DBTest.java @@ -527,8 +527,43 @@ public void findPartialRowTest() { Assert.assertEquals(3, db1.findPartialRow(new String[] {"Item 1d", "Item 2d", "Item 4d"}, new int[] {0,1,3})); Assert.assertEquals(1, db1.findPartialRow(new String[] {"Item 3b"}, new int[] {2})); + Assert.assertEquals(-1, db1.findPartialRow(new String[] {"Item 3sdsdasdasdb"}, new int[] {2})); Assert.assertEquals(4, db1.findPartialRow(new String[] {"Item 1e", "Item 2e", "Item 3e", "Item 4e"}, new int[] {0,1,2,3})); db1.delete(); } + + @Test + public void findPartialRowMultipleTest() { + Random random = new Random(); + String fileName = Integer.toString(Math.abs(random.nextInt())) + ".csv"; + + DB db1 = null; + try { + db1 = new DB(fileName, 4); + } catch (IOException e) { + e.printStackTrace(); + } + ArrayList data = new ArrayList<>(); + data.add(new String[] {"Item 1", "Item", "Item 3", "Item 4"}); + data.add(new String[] {"Item 1b", "Item 2b", "Item 3b", "Item 4b"}); + data.add(new String[] {"Item 1c", "Item 2c", "Item 3c", "Item 4c"}); + data.add(new String[] {"Item 1b", "Item", "Item 3b", "Item 4d"}); + data.add(new String[] {"Item 1e", "Item", "Item 3e", "Item 4e"}); + + for (int i = 0; i < data.size(); i++) { + db1.addRow(data.get(i)); + } + + Assert.assertEquals(1, db1.findPartialRowMultiple(new String[] {"Item 1b", "Item 3b"}, new int[] {0,2})[0]); + Assert.assertEquals(3, db1.findPartialRowMultiple(new String[] {"Item 1b", "Item 3b"}, new int[] {0,2})[1]); + + Assert.assertEquals(0, db1.findPartialRowMultiple(new String[] {"Item"}, new int[] {1})[0]); + Assert.assertEquals(3, db1.findPartialRowMultiple(new String[] {"Item"}, new int[] {1})[1]); + Assert.assertEquals(4, db1.findPartialRowMultiple(new String[] {"Item"}, new int[] {1})[2]); + + Assert.assertEquals(null, db1.findPartialRowMultiple(new String[] {"Iteasdm"}, new int[] {1})); + + db1.delete(); + } } \ No newline at end of file From 70981f8f97064cf87dd35ade13a20e206eaa2e05 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Fri, 25 Oct 2019 19:53:50 -0400 Subject: [PATCH 15/82] Too much work - stubbed ATM and Console --- src/main/java/ATM.java | 102 +++++++++++++++++++++++++++++++++ src/main/java/Console.java | 57 ++++++++++++++++++ src/main/java/Main.java | 17 +++++- src/main/java/User.java | 2 + src/test/java/ConsoleTest.java | 38 ++++++++++++ 5 files changed, 215 insertions(+), 1 deletion(-) create mode 100644 src/main/java/ATM.java create mode 100644 src/main/java/Console.java create mode 100644 src/main/java/User.java create mode 100644 src/test/java/ConsoleTest.java diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java new file mode 100644 index 0000000..d726dff --- /dev/null +++ b/src/main/java/ATM.java @@ -0,0 +1,102 @@ +import java.io.IOException; + +public class ATM { + + private User currentUser; + private DB userDB; + private DB transactionDB; + private DB accountDB; + + public ATM() { + this.currentUser = null; + try { + this.userDB = new DB("users.csv", 5); + this.transactionDB = new DB("transactions.csv", 5); + this.accountDB = new DB("accounts.csv", 5); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public User getCurrentUser() { + return currentUser; + } + + public DB getUserDB() { + return userDB; + } + + public DB getTransactionDB() { + return transactionDB; + } + + public DB getAccountDB() { + return accountDB; + } + + public void setCurrentUser(User currentUser) { + this.currentUser = currentUser; + } + + public User authenticate(int cardNumber, String password) { + return new User(); + } + + public void logOut (User currentUser) { + + } + + // log in user + public void getUser() { + // find user in DB + + // check PW + + // instantiate user + + } + + // add new user - called by getUser() + public User newUser() { + return null; + } + + + // load database info from disk + public void loadDBs() { + + } + + // deal with the user's choices + public void userMenu() { + + } + + // log out user + public void logOut() { + + } + + // save DBs to disk + public void saveDBs() { + + } + + public void serviceLoop() { + + // authenticate a user (or die trying) + // only returns null if the magic secret exit code is called + getUser(); + if (this.currentUser == null) { return; } + + loadDBs(); + + userMenu(); + + logOut(); + + saveDBs(); + + serviceLoop(); + } +} diff --git a/src/main/java/Console.java b/src/main/java/Console.java new file mode 100644 index 0000000..c72d698 --- /dev/null +++ b/src/main/java/Console.java @@ -0,0 +1,57 @@ +import java.util.Scanner; + +/** + * Created by leon on 2/9/18. + */ +public class Console { + + public static void print(String output, Object... args) { + System.out.printf(output, args); + } + + public static void println(String output, Object... args) { + print(output + "\n", args); + } + + public static String getInput() { + Console.print("> "); + Scanner scanner = new Scanner(System.in); + + String input = scanner.nextLine().toLowerCase(); //get input from user + + return input; + } + + static Boolean integerCheck(String input) { + return input.matches("^\\d+$"); + } + + static Boolean currencyCheck(String input) { + return input.matches("^[0-9]{1,3}(?:,?[0-9]{3})*(?:\\.[0-9]{2})?$"); + } + + public static Double getCurrency() { + String input = getInput(); + while (true) { + if (currencyCheck(input)) break; + else { + println("Enter a number"); + input = getInput(); + } + } + return Double.valueOf(input); + } + + public static Integer getInteger() { + String input = getInput(); + while (true) { + if (integerCheck(input)) break; + else { + println("Enter a number"); + input = getInput(); + } + } + return Integer.valueOf(input); + } + +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 05e41a9..9212ae8 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -4,6 +4,21 @@ public class Main { public static void main(String[] args){ - + ATM atm = new ATM(); + + atm.serviceLoop(); + } + + // convenience methods for dev environment to clear the DBs - only called from the IDE manually + public static void clearUserDB(DB userDB) { + userDB.clear(); + } + + public static void clearAccountDB(DB accountDB) { + accountDB.clear(); + } + + public static void clearTransactionDB(DB transactionDB) { + transactionDB.clear(); } } diff --git a/src/main/java/User.java b/src/main/java/User.java new file mode 100644 index 0000000..52dda73 --- /dev/null +++ b/src/main/java/User.java @@ -0,0 +1,2 @@ +public class User { +} diff --git a/src/test/java/ConsoleTest.java b/src/test/java/ConsoleTest.java new file mode 100644 index 0000000..1d462ed --- /dev/null +++ b/src/test/java/ConsoleTest.java @@ -0,0 +1,38 @@ +import junitparams.JUnitParamsRunner; +import junitparams.Parameters; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; +@RunWith(JUnitParamsRunner.class) +public class ConsoleTest { + + @Test + public void print() { + } + + @Test + public void println() { + } + + @Test + public void getInput() { + } + + @Test + public void getCurrency() { + } + + @Test + @Parameters({"1,true","2.3,false","-3,false","0,true","203,true","sad1,false","1223.231,false","3.33,true","2.3412,false"}) + public void currencyCheckTest(String input, Boolean valid) { + Assert.assertTrue(valid == Console.currencyCheck(input)); + } + + @Test + @Parameters({"1,true","2.3,false","-3,false","0,true","203,true","sad1,false","1223.231,false","3.33,false","2.3412,false"}) + public void integerCheckTest(String input, Boolean valid) { + Assert.assertTrue(valid == Console.integerCheck(input)); + } +} From dab56e17fac6284670ff9c76ac37b09fb93dc752 Mon Sep 17 00:00:00 2001 From: Ryan Date: Fri, 25 Oct 2019 23:30:48 -0400 Subject: [PATCH 16/82] Stub User and Transaction --- src/main/java/Transaction.java | 11 +++++++++++ src/main/java/User.java | 20 ++++++++++++++++++++ src/test/java/TransactionTest.java | 16 ++++++++++++++++ src/test/java/UserTest.java | 15 +++++++++++++++ 4 files changed, 62 insertions(+) create mode 100644 src/main/java/Transaction.java create mode 100644 src/main/java/User.java create mode 100644 src/test/java/TransactionTest.java create mode 100644 src/test/java/UserTest.java diff --git a/src/main/java/Transaction.java b/src/main/java/Transaction.java new file mode 100644 index 0000000..4c4be4b --- /dev/null +++ b/src/main/java/Transaction.java @@ -0,0 +1,11 @@ +import java.util.Date; + +public class Transaction { + + private Double amount; + private Date timeStamp; + private Integer accountId; + private String description; + private Boolean isCredit; + +} diff --git a/src/main/java/User.java b/src/main/java/User.java new file mode 100644 index 0000000..3d688f3 --- /dev/null +++ b/src/main/java/User.java @@ -0,0 +1,20 @@ +import java.util.ArrayList; + +public class User { + + private String firstName; + private String lastName; + private String password; + private Integer userID; + private Integer cardNumber; + + //private ArrayList accounts; + + public User(String firstName, String lastName, String password, Integer userID, Integer cardNumber) { + this.firstName = firstName; + this.lastName = lastName; + this.password = password; + this.userID = userID; + this.cardNumber = cardNumber; + } +} diff --git a/src/test/java/TransactionTest.java b/src/test/java/TransactionTest.java new file mode 100644 index 0000000..1b749ea --- /dev/null +++ b/src/test/java/TransactionTest.java @@ -0,0 +1,16 @@ +import org.junit.After; +import org.junit.Before; + +import static org.junit.Assert.*; + +public class TransactionTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + +} \ No newline at end of file diff --git a/src/test/java/UserTest.java b/src/test/java/UserTest.java new file mode 100644 index 0000000..36643fb --- /dev/null +++ b/src/test/java/UserTest.java @@ -0,0 +1,15 @@ +import org.junit.After; +import org.junit.Before; + +import static org.junit.Assert.*; + +public class UserTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } +} \ No newline at end of file From 315d3745ef75995ad5d86d1fed15a64dd872dee7 Mon Sep 17 00:00:00 2001 From: Mike King Date: Sat, 26 Oct 2019 00:44:48 -0400 Subject: [PATCH 17/82] accounts stubbed --- src/main/java/Account.java | 24 +++++++++++++++++++ src/main/java/Checkings.java | 37 ++++++++++++++++++++++++++++ src/main/java/Investment.java | 45 +++++++++++++++++++++++++++++++++++ src/main/java/Savings.java | 40 +++++++++++++++++++++++++++++++ 4 files changed, 146 insertions(+) create mode 100644 src/main/java/Account.java create mode 100644 src/main/java/Checkings.java create mode 100644 src/main/java/Investment.java create mode 100644 src/main/java/Savings.java diff --git a/src/main/java/Account.java b/src/main/java/Account.java new file mode 100644 index 0000000..5122b76 --- /dev/null +++ b/src/main/java/Account.java @@ -0,0 +1,24 @@ +abstract public class Account { +public Double balance; +public Integer OwnerID; +public Integer acctNum; + +public Double getBalance(){ + return balance; +} +public void deposit(Double amount){ + +} +public void withdraw(Double amount){ + +} +public void transferWithin(){ + +} +public void transferToAnother(){ + +} +public void getAcctHist(){ + +} +} diff --git a/src/main/java/Checkings.java b/src/main/java/Checkings.java new file mode 100644 index 0000000..2ded15a --- /dev/null +++ b/src/main/java/Checkings.java @@ -0,0 +1,37 @@ +public class Checkings extends Account{ + + public Checkings(){ + + } + + @Override + public Double getBalance() { + return super.getBalance(); + } + + @Override + public void transferToAnother() { + super.transferToAnother(); + } + + @Override + public void transferWithin() { + super.transferWithin(); + } + + @Override + public void withdraw(Double amount) { + super.withdraw(amount); + } + + @Override + public void getAcctHist() { + super.getAcctHist(); + } + + @Override + public void deposit(Double amount) { + super.deposit(amount); + } + +} diff --git a/src/main/java/Investment.java b/src/main/java/Investment.java new file mode 100644 index 0000000..40d7c90 --- /dev/null +++ b/src/main/java/Investment.java @@ -0,0 +1,45 @@ +public class Investment extends Account { + private Double risk; + + public void setRisk(Double risk) { + this.risk = risk; + } + public Double calcReturn(){ + return null; + } + + @Override + public Double getBalance() { + return super.getBalance(); + } + + @Override + public void transferToAnother() { + super.transferToAnother(); + } + + @Override + public void transferWithin() { + super.transferWithin(); + } + + @Override + public void withdraw(Double amount) { + super.withdraw(amount); + } + + @Override + public void getAcctHist() { + super.getAcctHist(); + } + + @Override + public void deposit(Double amount) { + super.deposit(amount); + } + + + + + +} diff --git a/src/main/java/Savings.java b/src/main/java/Savings.java new file mode 100644 index 0000000..24d69f8 --- /dev/null +++ b/src/main/java/Savings.java @@ -0,0 +1,40 @@ +public class Savings extends Account{ + private Double interestRate; + public Savings(){ + + } + + @Override + public Double getBalance() { + return super.getBalance(); + } + + @Override + public void transferToAnother() { + super.transferToAnother(); + } + + @Override + public void transferWithin() { + super.transferWithin(); + } + + @Override + public void withdraw(Double amount) { + super.withdraw(amount); + } + + @Override + public void getAcctHist() { + super.getAcctHist(); + } + + @Override + public void deposit(Double amount) { + super.deposit(amount); + } + + + + +} From f4613c409eab34c0672d7b6fd77d6aae271ed45d Mon Sep 17 00:00:00 2001 From: deltagphys Date: Sat, 26 Oct 2019 12:29:31 -0400 Subject: [PATCH 18/82] interface added --- src/main/java/ATM.java | 64 ++++++++++++++++++++++++++++----- src/main/java/Account.java | 2 ++ src/main/java/Main.java | 8 ++--- src/main/java/Searchable.java | 66 ++++++++++++++++++++++++++++++++++ src/main/java/Transaction.java | 2 ++ 5 files changed, 129 insertions(+), 13 deletions(-) create mode 100644 src/main/java/Account.java create mode 100644 src/main/java/Searchable.java create mode 100644 src/main/java/Transaction.java diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index d726dff..91d1029 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -1,18 +1,24 @@ import java.io.IOException; +import java.util.ArrayList; public class ATM { private User currentUser; + // 0: ID 1: Last Name 2: First Name 3: cardNum 4: PW private DB userDB; + + // 0: credit/debit 1: accountID 2: amount (signed) 3: timeStamp 4: description private DB transactionDB; + + // 0: accountID 1: ownerID 2: balance 3: type 4: risk/interest/null (type-dependent) private DB accountDB; - public ATM() { + public ATM(String userDBName, String accountDBName, String transactionDBName) { this.currentUser = null; try { - this.userDB = new DB("users.csv", 5); - this.transactionDB = new DB("transactions.csv", 5); - this.accountDB = new DB("accounts.csv", 5); + this.userDB = new DB(userDBName, 5); + this.transactionDB = new DB(transactionDBName, 5); + this.accountDB = new DB(accountDBName, 5); } catch (IOException e) { e.printStackTrace(); } @@ -42,6 +48,18 @@ public User authenticate(int cardNumber, String password) { return new User(); } + //find user info by id (helper for constructor) + public String [] getUserInfoByID (Integer ID) { + int rowNumOfUser = this.userDB.findPartialRow(ID.toString(), new int[] {0}); + return this.userDB.readRow(rowNumOfUser); + } + + //find user info by card number (helper for constructor) + public String [] getUserInfoByCardNum (Integer cardNum) { + int rowNumOfUser = this.userDB.findPartialRow(cardNum.toString(), new int[] {3}); + return this.userDB.readRow(rowNumOfUser); + } + public void logOut (User currentUser) { } @@ -61,10 +79,28 @@ public User newUser() { return null; } + //find accounts by owner id (to then be used by constructor) + public ArrayList getAccountInfoByUser (User user) { + int [] recordRowNums; + recordRowNums = this.accountDB.findPartialRowMultiple(new String[] {user.ID.toString()}, new int[] {1}) + + ArrayList accountsInfo = new ArrayList<>(); + for (int rowNum : recordRowNums) { + accountsInfo.add(this.accountDB.readRow(rowNum)); + } + + return accountsInfo; + } // load database info from disk public void loadDBs() { - + // find accounts, create instances + ArrayList accountsInfo = getAccountInfoByUser(this.currentUser); + ArrayList accounts = new ArrayList<>(); + for (String[] acctInfo : accountsInfo) { + accounts.add(new Account(...)); + } + // } // deal with the user's choices @@ -74,12 +110,24 @@ public void userMenu() { // log out user public void logOut() { - + saveDBs(); + this.currentUser = null; } // save DBs to disk public void saveDBs() { - +// // write the pending transaction queue +// for (Transaction transaction : this.currentUser.pendingTransactions) { +// transactionDB.addRow(transaction.toStringArray()); +// } +// // write the accounts +// int row; +// for (Account account : this.currentUser.accounts) { +// // find account row, replace it +// row = +// this.accountDB.replaceRow(accountDB.findPartialRow(), account.toString()); +// +// } } public void serviceLoop() { @@ -95,8 +143,6 @@ public void serviceLoop() { logOut(); - saveDBs(); - serviceLoop(); } } diff --git a/src/main/java/Account.java b/src/main/java/Account.java new file mode 100644 index 0000000..e5142a8 --- /dev/null +++ b/src/main/java/Account.java @@ -0,0 +1,2 @@ +public class Account { +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 9212ae8..a17f7f9 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,10 +1,8 @@ -/** - * Created by iyasuwatts on 10/17/17. - */ + public class Main { public static void main(String[] args){ - ATM atm = new ATM(); + ATM atm = new ATM("users.csv", "accounts.csv", "transactions.csv"); atm.serviceLoop(); } @@ -21,4 +19,6 @@ public static void clearAccountDB(DB accountDB) { public static void clearTransactionDB(DB transactionDB) { transactionDB.clear(); } + + //find (past) transactions by user } diff --git a/src/main/java/Searchable.java b/src/main/java/Searchable.java new file mode 100644 index 0000000..428cf12 --- /dev/null +++ b/src/main/java/Searchable.java @@ -0,0 +1,66 @@ +public interface Searchable { + + default String[] toStringArray() { + if (this.instanceof(Account)) { + + String acctType; + String typeSpecificProperty; + if (this.instanceof(Checking)) { + acctType = "Investment"; + typeSpecificProperty = this.risk.toString(); + } else if (this.instanceof(Savings)) { + acctType = "Savings"; + typeSpecificProperty = this.interestRate; + } else { + acctType = "Checking"; + typeSpecificProperty = ""; + } + + String[] result = new String[] { + this.ID.toString(), + this.owner.ID.toString(), + this.balance.toString(), + acctType, + typeSpecificProperty + } + + return result; + + + } else if (this.instanceof(Transaction)) { + + String[] result = new String[] { + this.ID.toString(), + this.lastName, + this.firstName, + this.cardNumber.toString(), + this.password + } + + return result; + + } else if (this.instanceof(User)) { + + String type; + if (this.amount > 0) { + type = "credit"; + } else { + type = "debit"; + } + + String[] result = new String[] { + type, + this.accountID.toString(), + this.amount.toString(), + this.timeStamp.toString(), + this.description + } + + return result; + + } else { + return null; + } + } + +} diff --git a/src/main/java/Transaction.java b/src/main/java/Transaction.java new file mode 100644 index 0000000..143b111 --- /dev/null +++ b/src/main/java/Transaction.java @@ -0,0 +1,2 @@ +public class Transaction { +} From 9898dafe751988220a13382462656e6a97791ea0 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Sat, 26 Oct 2019 13:21:00 -0400 Subject: [PATCH 19/82] branch switching --- src/main/java/ATM.java | 48 ++++++------- src/main/java/Account.java | 10 +++ src/main/java/Checking.java | 3 + src/main/java/Investment.java | 4 ++ src/main/java/Main.java | 25 ++++++- src/main/java/Savings.java | 4 ++ src/main/java/Searchable.java | 129 ++++++++++++++++++---------------- src/test/java/ATMTest.java | 86 +++++++++++++++++++++++ src/test/java/DBTest.java | 5 +- 9 files changed, 224 insertions(+), 90 deletions(-) create mode 100644 src/main/java/Checking.java create mode 100644 src/main/java/Investment.java create mode 100644 src/main/java/Savings.java create mode 100644 src/test/java/ATMTest.java diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index 91d1029..60b75cc 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -25,19 +25,19 @@ public ATM(String userDBName, String accountDBName, String transactionDBName) { } public User getCurrentUser() { - return currentUser; + return this.currentUser; } public DB getUserDB() { - return userDB; + return this.userDB; } public DB getTransactionDB() { - return transactionDB; + return this.transactionDB; } public DB getAccountDB() { - return accountDB; + return this.accountDB; } public void setCurrentUser(User currentUser) { @@ -50,13 +50,13 @@ public User authenticate(int cardNumber, String password) { //find user info by id (helper for constructor) public String [] getUserInfoByID (Integer ID) { - int rowNumOfUser = this.userDB.findPartialRow(ID.toString(), new int[] {0}); + int rowNumOfUser = this.userDB.findPartialRow(new String[] {ID.toString()}, new int[] {0}); return this.userDB.readRow(rowNumOfUser); } //find user info by card number (helper for constructor) public String [] getUserInfoByCardNum (Integer cardNum) { - int rowNumOfUser = this.userDB.findPartialRow(cardNum.toString(), new int[] {3}); + int rowNumOfUser = this.userDB.findPartialRow(new String[] {cardNum.toString()}, new int[] {3}); return this.userDB.readRow(rowNumOfUser); } @@ -80,27 +80,27 @@ public User newUser() { } //find accounts by owner id (to then be used by constructor) - public ArrayList getAccountInfoByUser (User user) { - int [] recordRowNums; - recordRowNums = this.accountDB.findPartialRowMultiple(new String[] {user.ID.toString()}, new int[] {1}) - - ArrayList accountsInfo = new ArrayList<>(); - for (int rowNum : recordRowNums) { - accountsInfo.add(this.accountDB.readRow(rowNum)); - } - - return accountsInfo; - } +// public ArrayList getAccountInfoByUser (User user) { +// int [] recordRowNums; +// recordRowNums = this.accountDB.findPartialRowMultiple(new String[] {user.ID.toString()}, new int[] {1}); +// +// ArrayList accountsInfo = new ArrayList<>(); +// for (int rowNum : recordRowNums) { +// accountsInfo.add(this.accountDB.readRow(rowNum)); +// } +// +// return accountsInfo; +// } // load database info from disk public void loadDBs() { - // find accounts, create instances - ArrayList accountsInfo = getAccountInfoByUser(this.currentUser); - ArrayList accounts = new ArrayList<>(); - for (String[] acctInfo : accountsInfo) { - accounts.add(new Account(...)); - } - // +// // find accounts, create instances +// ArrayList accountsInfo = getAccountInfoByUser(this.currentUser); +// ArrayList accounts = new ArrayList<>(); +// for (String[] acctInfo : accountsInfo) { +// accounts.add(new Account(...)); +// } +// // } // deal with the user's choices diff --git a/src/main/java/Account.java b/src/main/java/Account.java index e5142a8..d1c4b34 100644 --- a/src/main/java/Account.java +++ b/src/main/java/Account.java @@ -1,2 +1,12 @@ public class Account { + private Integer ID; + private Integer owner; + private Double balance; + + public Account() { + this.ID = -1; + this.owner = -1; + this.balance = 0.00; + } + } diff --git a/src/main/java/Checking.java b/src/main/java/Checking.java new file mode 100644 index 0000000..023816f --- /dev/null +++ b/src/main/java/Checking.java @@ -0,0 +1,3 @@ +public class Checking extends Account { + +} diff --git a/src/main/java/Investment.java b/src/main/java/Investment.java new file mode 100644 index 0000000..593223f --- /dev/null +++ b/src/main/java/Investment.java @@ -0,0 +1,4 @@ +public class Investment extends Account { + private Double risk; + +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index a17f7f9..d1534a6 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,3 +1,4 @@ +import java.io.IOException; public class Main { @@ -8,15 +9,33 @@ public static void main(String[] args){ } // convenience methods for dev environment to clear the DBs - only called from the IDE manually - public static void clearUserDB(DB userDB) { + public static void clearUserDB() { + DB userDB = null; + try { + userDB = new DB("users.csv", 5); + } catch (IOException e) { + e.printStackTrace(); + } userDB.clear(); } - public static void clearAccountDB(DB accountDB) { + public static void clearAccountDB() { + DB accountDB = null; + try { + accountDB = new DB("accounts.csv", 5); + } catch (IOException e) { + e.printStackTrace(); + } accountDB.clear(); } - public static void clearTransactionDB(DB transactionDB) { + public static void clearTransactionDB() { + DB transactionDB = null; + try { + transactionDB = new DB("transactions.csv", 5); + } catch (IOException e) { + e.printStackTrace(); + } transactionDB.clear(); } diff --git a/src/main/java/Savings.java b/src/main/java/Savings.java new file mode 100644 index 0000000..c9ac212 --- /dev/null +++ b/src/main/java/Savings.java @@ -0,0 +1,4 @@ +public class Savings extends Account { + private Double interestRate; + +} diff --git a/src/main/java/Searchable.java b/src/main/java/Searchable.java index 428cf12..4686386 100644 --- a/src/main/java/Searchable.java +++ b/src/main/java/Searchable.java @@ -1,66 +1,71 @@ public interface Searchable { - default String[] toStringArray() { - if (this.instanceof(Account)) { - - String acctType; - String typeSpecificProperty; - if (this.instanceof(Checking)) { - acctType = "Investment"; - typeSpecificProperty = this.risk.toString(); - } else if (this.instanceof(Savings)) { - acctType = "Savings"; - typeSpecificProperty = this.interestRate; - } else { - acctType = "Checking"; - typeSpecificProperty = ""; - } - - String[] result = new String[] { - this.ID.toString(), - this.owner.ID.toString(), - this.balance.toString(), - acctType, - typeSpecificProperty - } - - return result; - - - } else if (this.instanceof(Transaction)) { - - String[] result = new String[] { - this.ID.toString(), - this.lastName, - this.firstName, - this.cardNumber.toString(), - this.password - } - - return result; - - } else if (this.instanceof(User)) { - - String type; - if (this.amount > 0) { - type = "credit"; - } else { - type = "debit"; - } - - String[] result = new String[] { - type, - this.accountID.toString(), - this.amount.toString(), - this.timeStamp.toString(), - this.description - } - - return result; - - } else { - return null; - } - } +// default String[] toStringArray() { +// if (this instanceof Account) { +// +// String acctType; +// String typeSpecificProperty; +// if (this instanceof Investment) { +// acctType = "Investment"; +// typeSpecificProperty = ((Investment) this).risk.toString(); +// } else if (this instanceof Savings) { +// acctType = "Savings"; +// typeSpecificProperty = ((Savings) this).interestRate; +// } else { +// acctType = "Checking"; +// typeSpecificProperty = ""; +// } +// +// String[] result = new String[] { +// ((Account) this).ID.toString(), +// ((Account) this).owner.getID().toString(), +// ((Account) this).balance.toString(), +// acctType, +// typeSpecificProperty +// }; +// +// return result; +// +// +// } else if (this instanceof Transaction) { +// +// String[] result = new String[] { +// this.ID.toString(), +// this.lastName, +// this.firstName, +// this.cardNumber.toString(), +// this.password +// }; +// +// return result; +// +// } else if (this instanceof User) { +// +// String type; +// if (this.amount > 0) { +// type = "credit"; +// } else { +// type = "debit"; +// } +// +// String[] result = new String[] { +// type, +// this.accountID.toString(), +// this.amount.toString(), +// this.timeStamp.toString(), +// this.description +// }; +// +// return result; +// +// } else { +// return null; +// } +// } +// +// default void saveToDB() { +// +// +// } } diff --git a/src/test/java/ATMTest.java b/src/test/java/ATMTest.java new file mode 100644 index 0000000..bd099a4 --- /dev/null +++ b/src/test/java/ATMTest.java @@ -0,0 +1,86 @@ +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class ATMTest { + + private ATM atm; + + @Before + public void setUp() throws Exception { + ATM atm = new ATM ("testuserDB.csv", "testaccountDB.csv", "testtransactionDB.csv"); + } + + @After + public void tearDown() throws Exception { + atm.getUserDB().clear(); + atm.getAccountDB().clear(); + atm.getTransactionDB().clear(); + } + + @Test + public void getCurrentUser() { + } + + @Test + public void getUserDB() { + } + + @Test + public void getTransactionDB() { + } + + @Test + public void getAccountDB() { + } + + @Test + public void setCurrentUser() { + } + + @Test + public void authenticate() { + } + + @Test + public void getUserInfoByID() { + } + + @Test + public void getUserInfoByCardNum() { + } + + @Test + public void logOut() { + } + + @Test + public void getUser() { + } + + @Test + public void newUser() { + } + + @Test + public void getAccountInfoByUser() { + } + + @Test + public void loadDBs() { + } + + @Test + public void userMenu() { + } + + @Test + public void testLogOut() { + } + + @Test + public void saveDBs() { + } +} \ No newline at end of file diff --git a/src/test/java/DBTest.java b/src/test/java/DBTest.java index be93f95..e648f4e 100644 --- a/src/test/java/DBTest.java +++ b/src/test/java/DBTest.java @@ -45,7 +45,9 @@ public void constructorBadRowLengthTest() { @Test public void DBFileNameTest() { - String fileName = "test.csv"; + Random random = new Random(); + String fileName = Integer.toString(Math.abs(random.nextInt())) + ".csv"; + DB testDB = null; try { testDB = new DB(fileName, 4); @@ -55,6 +57,7 @@ public void DBFileNameTest() { String actual = testDB.getFileName(); Assert.assertEquals(testDB.getFileName(), fileName); + testDB.delete(); } @Test From a0ce94b123f9a7cc01a49bb97a44a9e2ea607533 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Sat, 26 Oct 2019 14:30:16 -0400 Subject: [PATCH 20/82] storeable ref conflicts fixed --- src/main/java/ATM.java | 2 + src/main/java/Account.java | 2 +- src/main/java/Checking.java | 36 ++++++++++++++++- src/main/java/Checkings.java | 37 ------------------ src/main/java/Investment.java | 2 +- src/main/java/Savings.java | 2 +- src/main/java/Searchable.java | 71 ---------------------------------- src/main/java/Storeable.java | 71 ++++++++++++++++++++++++++++++++++ src/main/java/Transaction.java | 12 +++--- src/main/java/User.java | 10 ++--- 10 files changed, 122 insertions(+), 123 deletions(-) delete mode 100644 src/main/java/Checkings.java delete mode 100644 src/main/java/Searchable.java create mode 100644 src/main/java/Storeable.java diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index 60b75cc..79e324a 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -45,6 +45,7 @@ public void setCurrentUser(User currentUser) { } public User authenticate(int cardNumber, String password) { + return new User(); } @@ -68,6 +69,7 @@ public void logOut (User currentUser) { public void getUser() { // find user in DB + // check PW // instantiate user diff --git a/src/main/java/Account.java b/src/main/java/Account.java index 76e06a8..069100d 100644 --- a/src/main/java/Account.java +++ b/src/main/java/Account.java @@ -1,6 +1,6 @@ abstract public class Account { public Double balance; -public Integer OwnerID; +public Integer ownerID; public Integer acctNum; public Double getBalance(){ diff --git a/src/main/java/Checking.java b/src/main/java/Checking.java index 023816f..e96d014 100644 --- a/src/main/java/Checking.java +++ b/src/main/java/Checking.java @@ -1,3 +1,37 @@ -public class Checking extends Account { +public class Checking extends Account{ + + public Checking(){ + + } + + @Override + public Double getBalance() { + return super.getBalance(); + } + + @Override + public void transferToAnother() { + super.transferToAnother(); + } + + @Override + public void transferWithin() { + super.transferWithin(); + } + + @Override + public void withdraw(Double amount) { + super.withdraw(amount); + } + + @Override + public void getAcctHist() { + super.getAcctHist(); + } + + @Override + public void deposit(Double amount) { + super.deposit(amount); + } } diff --git a/src/main/java/Checkings.java b/src/main/java/Checkings.java deleted file mode 100644 index 2ded15a..0000000 --- a/src/main/java/Checkings.java +++ /dev/null @@ -1,37 +0,0 @@ -public class Checkings extends Account{ - - public Checkings(){ - - } - - @Override - public Double getBalance() { - return super.getBalance(); - } - - @Override - public void transferToAnother() { - super.transferToAnother(); - } - - @Override - public void transferWithin() { - super.transferWithin(); - } - - @Override - public void withdraw(Double amount) { - super.withdraw(amount); - } - - @Override - public void getAcctHist() { - super.getAcctHist(); - } - - @Override - public void deposit(Double amount) { - super.deposit(amount); - } - -} diff --git a/src/main/java/Investment.java b/src/main/java/Investment.java index 7f54880..312cb11 100644 --- a/src/main/java/Investment.java +++ b/src/main/java/Investment.java @@ -1,5 +1,5 @@ public class Investment extends Account { - private Double risk; + public Double risk; public void setRisk(Double risk) { this.risk = risk; diff --git a/src/main/java/Savings.java b/src/main/java/Savings.java index eb60f0c..d4e6b52 100644 --- a/src/main/java/Savings.java +++ b/src/main/java/Savings.java @@ -1,5 +1,5 @@ public class Savings extends Account{ - private Double interestRate; + public Double interestRate; public Savings(){ } diff --git a/src/main/java/Searchable.java b/src/main/java/Searchable.java deleted file mode 100644 index 4686386..0000000 --- a/src/main/java/Searchable.java +++ /dev/null @@ -1,71 +0,0 @@ -public interface Searchable { - -// default String[] toStringArray() { -// if (this instanceof Account) { -// -// String acctType; -// String typeSpecificProperty; -// if (this instanceof Investment) { -// acctType = "Investment"; -// typeSpecificProperty = ((Investment) this).risk.toString(); -// } else if (this instanceof Savings) { -// acctType = "Savings"; -// typeSpecificProperty = ((Savings) this).interestRate; -// } else { -// acctType = "Checking"; -// typeSpecificProperty = ""; -// } -// -// String[] result = new String[] { -// ((Account) this).ID.toString(), -// ((Account) this).owner.getID().toString(), -// ((Account) this).balance.toString(), -// acctType, -// typeSpecificProperty -// }; -// -// return result; -// -// -// } else if (this instanceof Transaction) { -// -// String[] result = new String[] { -// this.ID.toString(), -// this.lastName, -// this.firstName, -// this.cardNumber.toString(), -// this.password -// }; -// -// return result; -// -// } else if (this instanceof User) { -// -// String type; -// if (this.amount > 0) { -// type = "credit"; -// } else { -// type = "debit"; -// } -// -// String[] result = new String[] { -// type, -// this.accountID.toString(), -// this.amount.toString(), -// this.timeStamp.toString(), -// this.description -// }; -// -// return result; -// -// } else { -// return null; -// } -// } -// -// default void saveToDB() { -// -// -// } - -} diff --git a/src/main/java/Storeable.java b/src/main/java/Storeable.java new file mode 100644 index 0000000..b09e20d --- /dev/null +++ b/src/main/java/Storeable.java @@ -0,0 +1,71 @@ +public interface Storeable { + + default String[] toStringArray() { + if (this instanceof Account) { + + String acctType; + String typeSpecificProperty; + if (this instanceof Investment) { + acctType = "Investment"; + typeSpecificProperty = ((Investment) this).risk.toString(); + } else if (this instanceof Savings) { + acctType = "Savings"; + typeSpecificProperty = ((Savings) this).interestRate.toString(); + } else { + acctType = "Checking"; + typeSpecificProperty = ""; + } + + String[] result = new String[] { + ((Account) this).acctNum.toString(), + ((Account) this).ownerID.toString(), + ((Account) this).balance.toString(), + acctType, + typeSpecificProperty + }; + + return result; + + + } else if (this instanceof User) { + + String[] result = new String[] { + ((User) this).userID.toString(), + ((User) this).lastName, + ((User) this).firstName, + ((User) this).cardNumber.toString(), + ((User) this).password + }; + + return result; + + } else if (this instanceof Transaction) { + + String type; + if (((Transaction) this).amount > 0) { + type = "credit"; + } else { + type = "debit"; + } + + String[] result = new String[] { + type, + ((Transaction) this).accountID.toString(), + ((Transaction) this).amount.toString(), + ((Transaction) this).timeStamp.toString(), + ((Transaction) this).description + }; + + return result; + + } else { + return null; + } + } + + default void saveToDB() { + + + } + +} diff --git a/src/main/java/Transaction.java b/src/main/java/Transaction.java index 4c4be4b..3d0e0ca 100644 --- a/src/main/java/Transaction.java +++ b/src/main/java/Transaction.java @@ -1,11 +1,11 @@ import java.util.Date; -public class Transaction { +public class Transaction implements Storeable { - private Double amount; - private Date timeStamp; - private Integer accountId; - private String description; - private Boolean isCredit; + public Double amount; + public Date timeStamp; + public Integer accountID; + public String description; + public Boolean isCredit; } diff --git a/src/main/java/User.java b/src/main/java/User.java index 3d688f3..4828401 100644 --- a/src/main/java/User.java +++ b/src/main/java/User.java @@ -2,11 +2,11 @@ public class User { - private String firstName; - private String lastName; - private String password; - private Integer userID; - private Integer cardNumber; + public String firstName; + public String lastName; + public String password; + public Integer userID; + public Integer cardNumber; //private ArrayList accounts; From 2ccb396e7cf3ed09b449b944ff7d6db9ba9b1557 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Sat, 26 Oct 2019 15:27:32 -0400 Subject: [PATCH 21/82] Add some ATM tests --- src/main/java/ATM.java | 8 ++++---- src/main/java/Account.java | 2 +- src/main/java/Checking.java | 2 +- src/main/java/Investment.java | 2 +- src/main/java/User.java | 2 +- src/test/java/ATMTest.java | 31 +++++++++++++++++++++++++------ 6 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index 79e324a..01e497b 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -44,10 +44,10 @@ public void setCurrentUser(User currentUser) { this.currentUser = currentUser; } - public User authenticate(int cardNumber, String password) { - - return new User(); - } +// public User authenticate(int cardNumber, String password) { +// +// return new User(); +// } //find user info by id (helper for constructor) public String [] getUserInfoByID (Integer ID) { diff --git a/src/main/java/Account.java b/src/main/java/Account.java index 069100d..a504fbd 100644 --- a/src/main/java/Account.java +++ b/src/main/java/Account.java @@ -1,4 +1,4 @@ -abstract public class Account { +abstract public class Account implements Storeable { public Double balance; public Integer ownerID; public Integer acctNum; diff --git a/src/main/java/Checking.java b/src/main/java/Checking.java index e96d014..0ee8606 100644 --- a/src/main/java/Checking.java +++ b/src/main/java/Checking.java @@ -1,4 +1,4 @@ -public class Checking extends Account{ +public class Checking extends Account { public Checking(){ diff --git a/src/main/java/Investment.java b/src/main/java/Investment.java index 312cb11..b79ab74 100644 --- a/src/main/java/Investment.java +++ b/src/main/java/Investment.java @@ -1,4 +1,4 @@ -public class Investment extends Account { +public class Investment extends Account implements Storeable { public Double risk; public void setRisk(Double risk) { diff --git a/src/main/java/User.java b/src/main/java/User.java index 4828401..203158a 100644 --- a/src/main/java/User.java +++ b/src/main/java/User.java @@ -1,6 +1,6 @@ import java.util.ArrayList; -public class User { +public class User implements Storeable { public String firstName; public String lastName; diff --git a/src/test/java/ATMTest.java b/src/test/java/ATMTest.java index bd099a4..33cea0b 100644 --- a/src/test/java/ATMTest.java +++ b/src/test/java/ATMTest.java @@ -1,16 +1,20 @@ +import junitparams.JUnitParamsRunner; import org.junit.After; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; import static org.junit.Assert.*; +@RunWith(JUnitParamsRunner.class) public class ATMTest { - private ATM atm; + private ATM atm; @Before public void setUp() throws Exception { - ATM atm = new ATM ("testuserDB.csv", "testaccountDB.csv", "testtransactionDB.csv"); + atm = new ATM ("testuserDB.csv", "testaccountDB.csv", "testtransactionDB.csv"); } @After @@ -26,18 +30,37 @@ public void getCurrentUser() { @Test public void getUserDB() { + + DB foundDB = atm.getUserDB(); + String fileName = foundDB.getFileName(); + Assert.assertEquals("testuserDB.csv",fileName); } @Test public void getTransactionDB() { + + DB foundDB = atm.getTransactionDB(); + String fileName = foundDB.getFileName(); + Assert.assertEquals("testtransactionDB.csv",fileName); } @Test public void getAccountDB() { + + DB foundDB = atm.getAccountDB(); + String fileName = foundDB.getFileName(); + Assert.assertEquals("testaccountDB.csv",fileName); } @Test public void setCurrentUser() { + + User user = new User("Jim","Brown","goolybib", 12, 12343); + atm.setCurrentUser(user); + + User foundUser = atm.getCurrentUser(); + + Assert.assertEquals(foundUser, user); } @Test @@ -56,10 +79,6 @@ public void getUserInfoByCardNum() { public void logOut() { } - @Test - public void getUser() { - } - @Test public void newUser() { } From b75a0352d3f91ba98333520de40f1ae5a19a48e7 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Sat, 26 Oct 2019 16:14:22 -0400 Subject: [PATCH 22/82] connections between models made, all tests run without catastrophe --- src/main/java/Account.java | 37 +++++++++++++++++++--------------- src/main/java/Investment.java | 6 ++++++ src/main/java/Savings.java | 4 +++- src/main/java/Transaction.java | 7 +++++++ src/main/java/User.java | 4 ++-- 5 files changed, 39 insertions(+), 19 deletions(-) diff --git a/src/main/java/Account.java b/src/main/java/Account.java index a504fbd..9014b7e 100644 --- a/src/main/java/Account.java +++ b/src/main/java/Account.java @@ -1,25 +1,30 @@ abstract public class Account implements Storeable { -public Double balance; -public Integer ownerID; -public Integer acctNum; + public Double balance; + public Integer ownerID; + public Integer acctNum; -public Double getBalance(){ - return balance; -} -public void deposit(Double amount){ + public Double getBalance(){ + return balance; + } -} -public void withdraw(Double amount){ + public void deposit(Double amount){ -} -public void transferWithin(){ + } -} -public void transferToAnother(){ + public void withdraw(Double amount){ -} -public void getAcctHist(){ + } -} + public void transferWithin(){ + + } + + public void transferToAnother(){ + + } + + public void getAcctHist(){ + + } } diff --git a/src/main/java/Investment.java b/src/main/java/Investment.java index b79ab74..da7f09a 100644 --- a/src/main/java/Investment.java +++ b/src/main/java/Investment.java @@ -1,9 +1,15 @@ public class Investment extends Account implements Storeable { + public Double risk; + public Investment(Double risk) { + this.risk = risk; + } + public void setRisk(Double risk) { this.risk = risk; } + public Double calcReturn(){ return null; } diff --git a/src/main/java/Savings.java b/src/main/java/Savings.java index d4e6b52..017b57a 100644 --- a/src/main/java/Savings.java +++ b/src/main/java/Savings.java @@ -1,7 +1,9 @@ public class Savings extends Account{ + public Double interestRate; - public Savings(){ + public Savings(Double interestRate) { + this.interestRate = interestRate; } @Override diff --git a/src/main/java/Transaction.java b/src/main/java/Transaction.java index 3d0e0ca..4883a3a 100644 --- a/src/main/java/Transaction.java +++ b/src/main/java/Transaction.java @@ -8,4 +8,11 @@ public class Transaction implements Storeable { public String description; public Boolean isCredit; + public Transaction(Double amount, Date timeStamp, Integer accountID, String description, Boolean isCredit) { + this.amount = amount; + this.timeStamp = timeStamp; + this.accountID = accountID; + this.description = description; + this.isCredit = isCredit; + } } diff --git a/src/main/java/User.java b/src/main/java/User.java index 203158a..a449fa9 100644 --- a/src/main/java/User.java +++ b/src/main/java/User.java @@ -7,8 +7,7 @@ public class User implements Storeable { public String password; public Integer userID; public Integer cardNumber; - - //private ArrayList accounts; + private ArrayList accounts; public User(String firstName, String lastName, String password, Integer userID, Integer cardNumber) { this.firstName = firstName; @@ -16,5 +15,6 @@ public User(String firstName, String lastName, String password, Integer userID, this.password = password; this.userID = userID; this.cardNumber = cardNumber; + this.accounts = null; } } From dc1f11eefb45c07556dff5612dcbd76fbd6a5a87 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Sat, 26 Oct 2019 16:24:30 -0400 Subject: [PATCH 23/82] one ATM test --- src/test/java/ATMTest.java | 14 ++++++++------ src/test/java/StoreableTest.java | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 src/test/java/StoreableTest.java diff --git a/src/test/java/ATMTest.java b/src/test/java/ATMTest.java index 33cea0b..f146540 100644 --- a/src/test/java/ATMTest.java +++ b/src/test/java/ATMTest.java @@ -24,10 +24,6 @@ public void tearDown() throws Exception { atm.getTransactionDB().clear(); } - @Test - public void getCurrentUser() { - } - @Test public void getUserDB() { @@ -55,11 +51,13 @@ public void getAccountDB() { @Test public void setCurrentUser() { + User foundUser = atm.getCurrentUser(); + Assert.assertEquals(foundUser, null); + User user = new User("Jim","Brown","goolybib", 12, 12343); atm.setCurrentUser(user); - User foundUser = atm.getCurrentUser(); - + foundUser = atm.getCurrentUser(); Assert.assertEquals(foundUser, user); } @@ -69,6 +67,10 @@ public void authenticate() { @Test public void getUserInfoByID() { + User user = new User("Jim","Brown","goolybib", 12, 12343); + + Integer ID = 123432; + } @Test diff --git a/src/test/java/StoreableTest.java b/src/test/java/StoreableTest.java new file mode 100644 index 0000000..406ea64 --- /dev/null +++ b/src/test/java/StoreableTest.java @@ -0,0 +1,14 @@ +import org.junit.Test; + +import static org.junit.Assert.*; + +public class StoreableTest { + + @Test + public void toStringArray() { + } + + @Test + public void saveToDB() { + } +} \ No newline at end of file From b17e8951693c199ba9d1b0f64dd240d07d46b067 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Sat, 26 Oct 2019 16:33:05 -0400 Subject: [PATCH 24/82] branch switch --- src/test/java/StoreableTest.java | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/test/java/StoreableTest.java b/src/test/java/StoreableTest.java index 406ea64..5815aff 100644 --- a/src/test/java/StoreableTest.java +++ b/src/test/java/StoreableTest.java @@ -1,3 +1,4 @@ +import org.junit.Assert; import org.junit.Test; import static org.junit.Assert.*; @@ -5,7 +6,35 @@ public class StoreableTest { @Test - public void toStringArray() { + public void toStringArrayUser() { + User user = new User("Jim","Brown","goolybib", 12, 12343); + + String[] actual = user.toStringArray(); + String[] expected = new String[] { + "12", + "Brown", + "Jim", + "12343", + "goolybib" + }; + + Assert.assertEquals(actual, expected); + } + + @Test + public void toStringArrayAccountChecking() { + Account account = new Checking(""); + + String[] actual = user.toStringArray(); + String[] expected = new String[] { + "12", + "Brown", + "Jim", + "12343", + "goolybib" + }; + + Assert.assertEquals(actual, expected); } @Test From cc07aa958d6ca96db4743fc44bc4233e7d6df791 Mon Sep 17 00:00:00 2001 From: Mike King Date: Sat, 26 Oct 2019 16:36:50 -0400 Subject: [PATCH 25/82] added fields --- src/main/java/Account.java | 6 ++++++ src/main/java/Savings.java | 28 ---------------------------- 2 files changed, 6 insertions(+), 28 deletions(-) diff --git a/src/main/java/Account.java b/src/main/java/Account.java index 9014b7e..7ebd0a4 100644 --- a/src/main/java/Account.java +++ b/src/main/java/Account.java @@ -3,6 +3,12 @@ abstract public class Account implements Storeable { public Integer ownerID; public Integer acctNum; + public Account(Double balance, Integer ownerID, Integer acctNum) { + this.balance = balance; + this.ownerID = ownerID; + this.acctNum = acctNum; + } + public Double getBalance(){ return balance; } diff --git a/src/main/java/Savings.java b/src/main/java/Savings.java index 017b57a..76635a4 100644 --- a/src/main/java/Savings.java +++ b/src/main/java/Savings.java @@ -6,33 +6,5 @@ public Savings(Double interestRate) { this.interestRate = interestRate; } - @Override - public Double getBalance() { - return super.getBalance(); - } - - @Override - public void transferToAnother() { - super.transferToAnother(); - } - - @Override - public void transferWithin() { - super.transferWithin(); - } - @Override - public void withdraw(Double amount) { - super.withdraw(amount); - } - - @Override - public void getAcctHist() { - super.getAcctHist(); - } - - @Override - public void deposit(Double amount) { - super.deposit(amount); - } } From fd499243aa880374eed57fc620230a5b8fb980ae Mon Sep 17 00:00:00 2001 From: Ryan Date: Sat, 26 Oct 2019 17:00:31 -0400 Subject: [PATCH 26/82] Fillout user methods --- src/main/java/User.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/User.java b/src/main/java/User.java index a449fa9..b4f9c5a 100644 --- a/src/main/java/User.java +++ b/src/main/java/User.java @@ -8,6 +8,7 @@ public class User implements Storeable { public Integer userID; public Integer cardNumber; private ArrayList accounts; + private ArrayList pendingTransactions; public User(String firstName, String lastName, String password, Integer userID, Integer cardNumber) { this.firstName = firstName; @@ -17,4 +18,13 @@ public User(String firstName, String lastName, String password, Integer userID, this.cardNumber = cardNumber; this.accounts = null; } + + public User(Integer userId, String password, DB data) { + + } + + public ArrayList getUserHistory() { + + return pendingTransactions; + } } From 392df6dde0cb96d4a2b4669dc4bde252a454ce31 Mon Sep 17 00:00:00 2001 From: Mike King Date: Sat, 26 Oct 2019 17:16:01 -0400 Subject: [PATCH 27/82] fixed constuctors for accounts --- src/main/java/Checking.java | 34 ++---------------------------- src/main/java/Investment.java | 3 ++- src/main/java/Savings.java | 4 +++- src/test/java/AccountTest.java | 38 ++++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 34 deletions(-) create mode 100644 src/test/java/AccountTest.java diff --git a/src/main/java/Checking.java b/src/main/java/Checking.java index 0ee8606..4f26eaf 100644 --- a/src/main/java/Checking.java +++ b/src/main/java/Checking.java @@ -1,37 +1,7 @@ public class Checking extends Account { - public Checking(){ - - } - - @Override - public Double getBalance() { - return super.getBalance(); - } - - @Override - public void transferToAnother() { - super.transferToAnother(); - } - - @Override - public void transferWithin() { - super.transferWithin(); - } - - @Override - public void withdraw(Double amount) { - super.withdraw(amount); - } - - @Override - public void getAcctHist() { - super.getAcctHist(); - } - - @Override - public void deposit(Double amount) { - super.deposit(amount); + public Checking(Double balance, Integer ownerID, Integer acctNum){ + super(balance, ownerID, acctNum); } } diff --git a/src/main/java/Investment.java b/src/main/java/Investment.java index da7f09a..f215d42 100644 --- a/src/main/java/Investment.java +++ b/src/main/java/Investment.java @@ -2,7 +2,8 @@ public class Investment extends Account implements Storeable { public Double risk; - public Investment(Double risk) { + public Investment(Double balance, Integer ownerID, Integer acctNum) { + super(balance, ownerID, acctNum); this.risk = risk; } diff --git a/src/main/java/Savings.java b/src/main/java/Savings.java index 76635a4..7cec847 100644 --- a/src/main/java/Savings.java +++ b/src/main/java/Savings.java @@ -2,7 +2,9 @@ public class Savings extends Account{ public Double interestRate; - public Savings(Double interestRate) { + public Savings(Double balance, Integer ownerID, Integer acctNum) { + super(balance, ownerID, acctNum); + this.interestRate = interestRate; } diff --git a/src/test/java/AccountTest.java b/src/test/java/AccountTest.java new file mode 100644 index 0000000..1d95cf7 --- /dev/null +++ b/src/test/java/AccountTest.java @@ -0,0 +1,38 @@ +import org.junit.Test; + +import static org.junit.Assert.*; + +public class AccountTest { + + @Test + public void getBalance() { + // Given + Account account = new Checking(0.0, 3,3); + Double expected = 0.0; + + + // Then + Double actual = account.getBalance(); + assertEquals(expected, actual); + } + + @Test + public void deposit() { + } + + @Test + public void withdraw() { + } + + @Test + public void transferWithin() { + } + + @Test + public void transferToAnother() { + } + + @Test + public void getAcctHist() { + } +} \ No newline at end of file From ea1f497012bc5b3297699e40f00099eb4fc18ce0 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sat, 26 Oct 2019 17:23:02 -0400 Subject: [PATCH 28/82] overide toStringArray --- src/main/java/Transaction.java | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/main/java/Transaction.java b/src/main/java/Transaction.java index 4883a3a..6ed8513 100644 --- a/src/main/java/Transaction.java +++ b/src/main/java/Transaction.java @@ -2,11 +2,11 @@ public class Transaction implements Storeable { - public Double amount; - public Date timeStamp; - public Integer accountID; - public String description; - public Boolean isCredit; + private Double amount; + private Date timeStamp; + private Integer accountID; + private String description; + private Boolean isCredit; public Transaction(Double amount, Date timeStamp, Integer accountID, String description, Boolean isCredit) { this.amount = amount; @@ -15,4 +15,28 @@ public Transaction(Double amount, Date timeStamp, Integer accountID, String desc this.description = description; this.isCredit = isCredit; } + + public String print() { + return amount + " " + description + timeStamp; + } + + + @Override + public String[] toStringArray() { + String type; + if (isCredit) { + type = "credit"; + } else { + type = "debit"; + } + + String[] result = new String[] { + type, + this.accountID.toString(), + this.amount.toString(), + this.timeStamp.toString(), + this.description + }; + return result; + } } From 57599e0964ebb4279fc6a88a49729b41fa957a46 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Sat, 26 Oct 2019 18:05:33 -0400 Subject: [PATCH 29/82] current status for Brian --- src/main/java/ATM.java | 7 +++++-- src/main/java/Investment.java | 2 +- src/main/java/Savings.java | 2 +- src/main/java/Storeable.java | 19 ------------------- src/main/java/Transaction.java | 10 +++++----- src/test/java/ATMTest.java | 14 ++++++++++++++ src/test/java/StoreableTest.java | 20 ++++++++++++++++++-- 7 files changed, 44 insertions(+), 30 deletions(-) diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index 01e497b..8142844 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -65,11 +65,10 @@ public void logOut (User currentUser) { } - // log in user + // log in user - don't return until you do public void getUser() { // find user in DB - // check PW // instantiate user @@ -105,6 +104,10 @@ public void loadDBs() { // // } + public int getUserCount() { + return this.userDB.length(); + } + // deal with the user's choices public void userMenu() { diff --git a/src/main/java/Investment.java b/src/main/java/Investment.java index f215d42..1ce8223 100644 --- a/src/main/java/Investment.java +++ b/src/main/java/Investment.java @@ -2,7 +2,7 @@ public class Investment extends Account implements Storeable { public Double risk; - public Investment(Double balance, Integer ownerID, Integer acctNum) { + public Investment(Double balance, Integer ownerID, Integer acctNum, Double risk) { super(balance, ownerID, acctNum); this.risk = risk; } diff --git a/src/main/java/Savings.java b/src/main/java/Savings.java index 7cec847..aa32249 100644 --- a/src/main/java/Savings.java +++ b/src/main/java/Savings.java @@ -2,7 +2,7 @@ public class Savings extends Account{ public Double interestRate; - public Savings(Double balance, Integer ownerID, Integer acctNum) { + public Savings(Double balance, Integer ownerID, Integer acctNum, Double interestRate) { super(balance, ownerID, acctNum); this.interestRate = interestRate; diff --git a/src/main/java/Storeable.java b/src/main/java/Storeable.java index b09e20d..fa52b7a 100644 --- a/src/main/java/Storeable.java +++ b/src/main/java/Storeable.java @@ -39,25 +39,6 @@ default String[] toStringArray() { return result; - } else if (this instanceof Transaction) { - - String type; - if (((Transaction) this).amount > 0) { - type = "credit"; - } else { - type = "debit"; - } - - String[] result = new String[] { - type, - ((Transaction) this).accountID.toString(), - ((Transaction) this).amount.toString(), - ((Transaction) this).timeStamp.toString(), - ((Transaction) this).description - }; - - return result; - } else { return null; } diff --git a/src/main/java/Transaction.java b/src/main/java/Transaction.java index 4883a3a..b3a5d1c 100644 --- a/src/main/java/Transaction.java +++ b/src/main/java/Transaction.java @@ -2,11 +2,11 @@ public class Transaction implements Storeable { - public Double amount; - public Date timeStamp; - public Integer accountID; - public String description; - public Boolean isCredit; + private Double amount; + private Date timeStamp; + private Integer accountID; + private String description; + private Boolean isCredit; public Transaction(Double amount, Date timeStamp, Integer accountID, String description, Boolean isCredit) { this.amount = amount; diff --git a/src/test/java/ATMTest.java b/src/test/java/ATMTest.java index f146540..6339804 100644 --- a/src/test/java/ATMTest.java +++ b/src/test/java/ATMTest.java @@ -48,6 +48,20 @@ public void getAccountDB() { Assert.assertEquals("testaccountDB.csv",fileName); } + @Test + public void getUserCount() { + + DB userDB = atm.getUserDB(); + userDB.clear(); + Assert.assertEquals((int) 0, (int) userDB.length()); + User user1 = new User("Jim","Brown","goolybib", 12, 12343); + userDB.addRow(user1.toStringArray()); + Assert.assertEquals((int) 1, (int) userDB.length()); + User user2 = new User("Ji123m","Bro23wn","gool321ybib", 122, 1234313); + userDB.addRow(user2.toStringArray()); + Assert.assertEquals((int) 2, (int) userDB.length()); + } + @Test public void setCurrentUser() { diff --git a/src/test/java/StoreableTest.java b/src/test/java/StoreableTest.java index 5815aff..e5a89e8 100644 --- a/src/test/java/StoreableTest.java +++ b/src/test/java/StoreableTest.java @@ -23,9 +23,25 @@ public void toStringArrayUser() { @Test public void toStringArrayAccountChecking() { - Account account = new Checking(""); + Account account = new Checking(12.23, 23, 3432); - String[] actual = user.toStringArray(); + String[] actual = account.toStringArray(); + String[] expected = new String[] { + "12", + "Brown", + "Jim", + "12343", + "goolybib" + }; + + Assert.assertEquals(actual, expected); + } + + @Test + public void toStringArrayAccountSavings() { + Account account = new Savings(12.23, 23, 3432, .03); + + String[] actual = account.toStringArray(); String[] expected = new String[] { "12", "Brown", From 106d5bc5c2af083c706cddb2a7ac53e28e020331 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sat, 26 Oct 2019 18:05:36 -0400 Subject: [PATCH 30/82] override stoarable in user --- src/main/java/Storeable.java | 32 -------------------------------- src/main/java/User.java | 22 +++++++++++++++++----- 2 files changed, 17 insertions(+), 37 deletions(-) diff --git a/src/main/java/Storeable.java b/src/main/java/Storeable.java index b09e20d..b4ae1ef 100644 --- a/src/main/java/Storeable.java +++ b/src/main/java/Storeable.java @@ -26,38 +26,6 @@ default String[] toStringArray() { return result; - - } else if (this instanceof User) { - - String[] result = new String[] { - ((User) this).userID.toString(), - ((User) this).lastName, - ((User) this).firstName, - ((User) this).cardNumber.toString(), - ((User) this).password - }; - - return result; - - } else if (this instanceof Transaction) { - - String type; - if (((Transaction) this).amount > 0) { - type = "credit"; - } else { - type = "debit"; - } - - String[] result = new String[] { - type, - ((Transaction) this).accountID.toString(), - ((Transaction) this).amount.toString(), - ((Transaction) this).timeStamp.toString(), - ((Transaction) this).description - }; - - return result; - } else { return null; } diff --git a/src/main/java/User.java b/src/main/java/User.java index b4f9c5a..aece86a 100644 --- a/src/main/java/User.java +++ b/src/main/java/User.java @@ -2,11 +2,11 @@ public class User implements Storeable { - public String firstName; - public String lastName; - public String password; - public Integer userID; - public Integer cardNumber; + private String firstName; + private String lastName; + private String password; + private Integer userID; + private Integer cardNumber; private ArrayList accounts; private ArrayList pendingTransactions; @@ -27,4 +27,16 @@ public ArrayList getUserHistory() { return pendingTransactions; } + + @Override + public String[] toStringArray() { + String[] result = new String[] { + this.userID.toString(), + this.lastName, + this.firstName, + this.cardNumber.toString(), + this.password + }; + return result; + } } From b2b99e8608d0aa194fe6bb24c0c26fa9e897fc47 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Sat, 26 Oct 2019 18:23:46 -0400 Subject: [PATCH 31/82] storeable tests --- src/main/java/Storeable.java | 5 +---- src/test/java/StoreableTest.java | 38 +++++++++++++++++++++++--------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/main/java/Storeable.java b/src/main/java/Storeable.java index b4ae1ef..1d24463 100644 --- a/src/main/java/Storeable.java +++ b/src/main/java/Storeable.java @@ -31,9 +31,6 @@ default String[] toStringArray() { } } - default void saveToDB() { - - - } + public void saveToDB(); } diff --git a/src/test/java/StoreableTest.java b/src/test/java/StoreableTest.java index e5a89e8..03d8f90 100644 --- a/src/test/java/StoreableTest.java +++ b/src/test/java/StoreableTest.java @@ -27,11 +27,11 @@ public void toStringArrayAccountChecking() { String[] actual = account.toStringArray(); String[] expected = new String[] { - "12", - "Brown", - "Jim", - "12343", - "goolybib" + "3432", + "23", + "12.23", + "Checking", + "" }; Assert.assertEquals(actual, expected); @@ -39,15 +39,31 @@ public void toStringArrayAccountChecking() { @Test public void toStringArrayAccountSavings() { - Account account = new Savings(12.23, 23, 3432, .03); + Account account = new Savings(12.23, 23, 3432, 0.05); String[] actual = account.toStringArray(); String[] expected = new String[] { - "12", - "Brown", - "Jim", - "12343", - "goolybib" + "3432", + "23", + "12.23", + "Savings", + "0.05" + }; + + Assert.assertEquals(actual, expected); + } + + @Test + public void toStringArrayAccountInvestment() { + Account account = new Investment(12.23, 23, 3432, 0.2); + + String[] actual = account.toStringArray(); + String[] expected = new String[] { + "3432", + "23", + "12.23", + "Investment", + "0.2" }; Assert.assertEquals(actual, expected); From 591b76f46f397a239d23c30ca17d87ce25887650 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sat, 26 Oct 2019 18:47:05 -0400 Subject: [PATCH 32/82] make db methods private --- src/main/java/Main.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index d1534a6..7ff277a 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -8,8 +8,10 @@ public static void main(String[] args){ atm.serviceLoop(); } + + // convenience methods for dev environment to clear the DBs - only called from the IDE manually - public static void clearUserDB() { + private static void clearUserDB() { DB userDB = null; try { userDB = new DB("users.csv", 5); @@ -19,7 +21,7 @@ public static void clearUserDB() { userDB.clear(); } - public static void clearAccountDB() { + private static void clearAccountDB() { DB accountDB = null; try { accountDB = new DB("accounts.csv", 5); @@ -29,7 +31,7 @@ public static void clearAccountDB() { accountDB.clear(); } - public static void clearTransactionDB() { + private static void clearTransactionDB() { DB transactionDB = null; try { transactionDB = new DB("transactions.csv", 5); From 1ad47f62b7214c224c101abc81b1c6ab93688916 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Sat, 26 Oct 2019 19:03:36 -0400 Subject: [PATCH 33/82] save user method and test --- src/main/java/ATM.java | 11 +++++++++++ src/main/java/Storeable.java | 2 -- src/main/java/User.java | 17 +++++++++++++++++ src/test/java/ATMTest.java | 14 ++++++++++++-- 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index 8142844..a645aa4 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -55,6 +55,11 @@ public void setCurrentUser(User currentUser) { return this.userDB.readRow(rowNumOfUser); } + //find user row by id + public Integer getUserRowByID (Integer ID) { + return this.userDB.findPartialRow(new String[]{ID.toString()}, new int[]{0}); + } + //find user info by card number (helper for constructor) public String [] getUserInfoByCardNum (Integer cardNum) { int rowNumOfUser = this.userDB.findPartialRow(new String[] {cardNum.toString()}, new int[] {3}); @@ -150,4 +155,10 @@ public void serviceLoop() { serviceLoop(); } + + public void saveUserToDB(User user) { + String[] stringRepOfUser = user.toStringArray(); + int rowNum = getUserRowByID(this.currentUser.getUserID()); + this.userDB.replaceRow(rowNum, stringRepOfUser); + } } diff --git a/src/main/java/Storeable.java b/src/main/java/Storeable.java index 1d24463..40a48e3 100644 --- a/src/main/java/Storeable.java +++ b/src/main/java/Storeable.java @@ -31,6 +31,4 @@ default String[] toStringArray() { } } - public void saveToDB(); - } diff --git a/src/main/java/User.java b/src/main/java/User.java index aece86a..f25d5b3 100644 --- a/src/main/java/User.java +++ b/src/main/java/User.java @@ -28,6 +28,22 @@ public ArrayList getUserHistory() { return pendingTransactions; } + public String getPassword() { + return password; + } + + public Integer getUserID() { + return userID; + } + + public ArrayList getAccounts() { + return accounts; + } + + public ArrayList getPendingTransactions() { + return pendingTransactions; + } + @Override public String[] toStringArray() { String[] result = new String[] { @@ -39,4 +55,5 @@ public String[] toStringArray() { }; return result; } + } diff --git a/src/test/java/ATMTest.java b/src/test/java/ATMTest.java index 6339804..1e6e098 100644 --- a/src/test/java/ATMTest.java +++ b/src/test/java/ATMTest.java @@ -81,10 +81,20 @@ public void authenticate() { @Test public void getUserInfoByID() { - User user = new User("Jim","Brown","goolybib", 12, 12343); + DB userDB = atm.getUserDB(); + userDB.clear(); + + User user1 = new User("Jim","Brown","goolybib", 12, 12343); + userDB.addRow(user1.toStringArray()); + User user2 = new User("Ji123m","Bro23wn","gool321ybib", 122, 1234313); + userDB.addRow(user2.toStringArray()); + User user3 = new User("Jane","Himne","gasdsdool321ybib", 32, 313); + userDB.addRow(user3.toStringArray()); - Integer ID = 123432; + String[] actual = atm.getUserInfoByID(122); + String[] expected = user2.toStringArray(); + Assert.assertEquals(actual,expected); } @Test From f5ff777138ce88c2947c4b175921cf31dbded746 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sat, 26 Oct 2019 19:16:24 -0400 Subject: [PATCH 34/82] read user pass --- src/main/java/ATM.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index 8142844..df66c9d 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -4,14 +4,9 @@ public class ATM { private User currentUser; - // 0: ID 1: Last Name 2: First Name 3: cardNum 4: PW - private DB userDB; - - // 0: credit/debit 1: accountID 2: amount (signed) 3: timeStamp 4: description - private DB transactionDB; - - // 0: accountID 1: ownerID 2: balance 3: type 4: risk/interest/null (type-dependent) - private DB accountDB; + private DB userDB; // 0: ID 1: Last Name 2: First Name 3: cardNum 4: PW + private DB transactionDB; // 0: credit/debit 1: accountID 2: amount (signed) 3: timeStamp 4: description + private DB accountDB; // 0: accountID 1: ownerID 2: balance 3: type 4: risk/interest/null (type-dependent) public ATM(String userDBName, String accountDBName, String transactionDBName) { this.currentUser = null; @@ -67,9 +62,15 @@ public void logOut (User currentUser) { // log in user - don't return until you do public void getUser() { + //Read User's card + Console.println("Card Number:"); + String cardNum = Console.getInput(); + // find user in DB // check PW + Console.println("Enter Password"); + String password = Console.getInput(); // instantiate user From e85082bfc7b0f464c2b4a0027a013d61f690d229 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Sat, 26 Oct 2019 19:45:35 -0400 Subject: [PATCH 35/82] saveUserToDB and test --- src/main/java/ATM.java | 15 +++++- src/test/java/ATMTest.java | 98 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 2 deletions(-) diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index a645aa4..91e7e70 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -158,7 +158,18 @@ public void serviceLoop() { public void saveUserToDB(User user) { String[] stringRepOfUser = user.toStringArray(); - int rowNum = getUserRowByID(this.currentUser.getUserID()); - this.userDB.replaceRow(rowNum, stringRepOfUser); + int userID = user.getUserID(); + int rowNum = getUserRowByID(userID); + if (rowNum == -1) { // user isn't in DB yet + this.userDB.addRow(stringRepOfUser); + } else { // update a found row + this.userDB.replaceRow(rowNum, stringRepOfUser); + } + } + + public void savePendingTransactionsToDB(ArrayList pendingTransactions) { + for (Transaction transaction : pendingTransactions) { + this.transactionDB.addRow(transaction.toStringArray()); + } } } diff --git a/src/test/java/ATMTest.java b/src/test/java/ATMTest.java index 1e6e098..5bba068 100644 --- a/src/test/java/ATMTest.java +++ b/src/test/java/ATMTest.java @@ -5,6 +5,9 @@ import org.junit.Test; import org.junit.runner.RunWith; +import java.util.ArrayList; +import java.util.Date; + import static org.junit.Assert.*; @RunWith(JUnitParamsRunner.class) @@ -97,6 +100,101 @@ public void getUserInfoByID() { Assert.assertEquals(actual,expected); } + @Test + public void getUserRowByID() { + DB userDB = atm.getUserDB(); + userDB.clear(); + + User user1 = new User("Jim","Brown","goolybib", 12, 12343); + userDB.addRow(user1.toStringArray()); + User user2 = new User("Ji123m","Bro23wn","gool321ybib", 122, 1234313); + userDB.addRow(user2.toStringArray()); + User user3 = new User("Jane","Himne","gasdsdool321ybib", 32, 313); + userDB.addRow(user3.toStringArray()); + + int actual = atm.getUserRowByID(122); + int expected = 1; + + Assert.assertEquals(actual,expected); + + actual = atm.getUserRowByID(12); + expected = 0; + + Assert.assertEquals(actual,expected); + + actual = atm.getUserRowByID(32); + expected = 2; + + Assert.assertEquals(actual,expected); + } + + @Test + public void saveUserToDBTest() { + DB userDB = atm.getUserDB(); + userDB.clear(); + + User user1 = new User("Jim","Brown","goolybib", 12, 12343); + atm.saveUserToDB(user1); + User user2 = new User("Ji123m","Bro23wn","gool321ybib", 122, 1234313); + atm.saveUserToDB(user2); + User user3 = new User("Jane","Himne","gasdsdool321ybib", 32, 313); + atm.saveUserToDB(user3); + + String[] actual = atm.getUserInfoByID(122); + String[] expected = user2.toStringArray(); + + Assert.assertEquals(actual,expected); + + actual = atm.getUserInfoByID(12); + expected = user1.toStringArray(); + + Assert.assertEquals(actual,expected); + + int actual2 = userDB.length(); + int expected2 = 3; + + Assert.assertEquals(actual,expected); + + User user4 = new User("Ji123m","Bro23wn","gool321ysdasdbib", 12, 1234313); + atm.saveUserToDB(user4); + + actual2 = userDB.length(); + expected2 = 3; + + Assert.assertEquals(actual,expected); + + actual = atm.getUserInfoByID(12); + expected = user4.toStringArray(); + + Assert.assertEquals(actual,expected); + + expected = user1.toStringArray(); + + Assert.assertNotEquals(actual,expected); + + } + + @Test + public void savePendingTransactionsTest() { + DB transactionDB = atm.getTransactionDB(); + transactionDB.clear(); + Assert.assertEquals((int)0, (int)transactionDB.length()); + + Transaction trans1 = new Transaction(123.42, new Date(2014, 1, 6, 11,23, 40 ),23,"Opened account", true); + Transaction trans2 = new Transaction(-23.57, new Date(2015, 2, 7, 10,23, 3 ),12,"Withdrawal", false); + ArrayList pendingTransactions = new ArrayList(); + pendingTransactions.add(trans1); + pendingTransactions.add(trans2); + + atm.savePendingTransactionsToDB(pendingTransactions); + + Assert.assertEquals((int)2, (int)transactionDB.length()); + + transactionDB.clear(); + Assert.assertEquals((int)0, (int)transactionDB.length()); + + } + @Test public void getUserInfoByCardNum() { } From e1a895de6039f656562709893784726dd00597e4 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Sat, 26 Oct 2019 19:48:35 -0400 Subject: [PATCH 36/82] getters for accountID and owner --- src/main/java/ATM.java | 11 +++++++++++ src/main/java/Account.java | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index 91e7e70..0e6886b 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -167,6 +167,17 @@ public void saveUserToDB(User user) { } } + public void saveAccountrToDB(Account account) { + String[] stringRepOfAccount = account.toStringArray(); + int accountID = account.getacctID(); + int rowNum = getUserRowByID(userID); + if (rowNum == -1) { // user isn't in DB yet + this.userDB.addRow(stringRepOfUser); + } else { // update a found row + this.userDB.replaceRow(rowNum, stringRepOfUser); + } + } + public void savePendingTransactionsToDB(ArrayList pendingTransactions) { for (Transaction transaction : pendingTransactions) { this.transactionDB.addRow(transaction.toStringArray()); diff --git a/src/main/java/Account.java b/src/main/java/Account.java index 7ebd0a4..945b4d6 100644 --- a/src/main/java/Account.java +++ b/src/main/java/Account.java @@ -13,6 +13,14 @@ public Double getBalance(){ return balance; } + public Integer getOwnerID() { + return ownerID; + } + + public Integer getAcctNum() { + return acctNum; + } + public void deposit(Double amount){ } From d0684d5d38f6a2bf2bda825422f356a425871a1f Mon Sep 17 00:00:00 2001 From: deltaGPhys Date: Sat, 26 Oct 2019 19:59:15 -0400 Subject: [PATCH 37/82] Delete .DS_Store --- .DS_Store | Bin 6148 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index d92ddf0dfea967a4df3ab90415b58ee3abddd721..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK%}T>S5Z<*_w-vDmp&s|*t%sJD77s$G_25m2=s~4TY_Wlulq5B1t>iKEjeG-N z$C=%YSnR=*h@FAmZ)Rs^H}gUEhcU*x(_w?L3S&$_L*%F|5gcCXS~J0j9OsB9t{=K! zy}1+P`|E(;USKhc*o~Wh{eItzlY0G~uN8{x8=FFuMR~h?ZI8p$PMl;Kcb(`Gty3o% zyGB0?L;q8E+;eY~_A9$bVURdpFp5;NHylC8)rA)f!?YVtgJG=Zv9&{pk|^~n)!D3l zP?OD8V_uW9PN!azt!8^ZFNx}Y!{8Q1 z^5{H@kQg8ahyh|?wHPqxfY@5CvZ-2PfEf4z1Gqm(Xo!}MGO!F|BL}%?Ag686lKoVZ{^`xE1?abp Date: Sat, 26 Oct 2019 19:59:28 -0400 Subject: [PATCH 38/82] Added some investment methods --- src/main/java/Account.java | 2 ++ src/main/java/Investment.java | 37 +++++++----------------------- src/main/java/Savings.java | 9 ++++++-- src/test/java/AccountTest.java | 42 ++++++++++++++++++++++++++++------ 4 files changed, 52 insertions(+), 38 deletions(-) diff --git a/src/main/java/Account.java b/src/main/java/Account.java index 7ebd0a4..209997e 100644 --- a/src/main/java/Account.java +++ b/src/main/java/Account.java @@ -14,10 +14,12 @@ public Double getBalance(){ } public void deposit(Double amount){ + balance += amount; } public void withdraw(Double amount){ + balance -= amount; } diff --git a/src/main/java/Investment.java b/src/main/java/Investment.java index f215d42..aaf7cf2 100644 --- a/src/main/java/Investment.java +++ b/src/main/java/Investment.java @@ -1,10 +1,13 @@ +import java.util.Random; + public class Investment extends Account implements Storeable { - public Double risk; + Double risk; - public Investment(Double balance, Integer ownerID, Integer acctNum) { + public Investment(Double balance, Integer ownerID, Integer acctNum, Double risk) { super(balance, ownerID, acctNum); this.risk = risk; + } public void setRisk(Double risk) { @@ -12,37 +15,13 @@ public void setRisk(Double risk) { } public Double calcReturn(){ - return null; - } - - @Override - public Double getBalance() { - return super.getBalance(); - } - @Override - public void transferToAnother() { - super.transferToAnother(); - } - @Override - public void transferWithin() { - super.transferWithin(); - } + Double multiplier = risk * (Math.random()/.99999); - @Override - public void withdraw(Double amount) { - super.withdraw(amount); + balance += Math.round((multiplier * balance)*100d)/100d; + return getBalance(); } - @Override - public void getAcctHist() { - super.getAcctHist(); - } - - @Override - public void deposit(Double amount) { - super.deposit(amount); - } } diff --git a/src/main/java/Savings.java b/src/main/java/Savings.java index 7cec847..b4d4f21 100644 --- a/src/main/java/Savings.java +++ b/src/main/java/Savings.java @@ -2,11 +2,16 @@ public class Savings extends Account{ public Double interestRate; - public Savings(Double balance, Integer ownerID, Integer acctNum) { + public Savings(Double balance, Integer ownerID, Integer acctNum, Double interestRate) { super(balance, ownerID, acctNum); - this.interestRate = interestRate; } + public Double calcInterestReturned(){ + balance += (interestRate * balance); + return getBalance(); + } + + } diff --git a/src/test/java/AccountTest.java b/src/test/java/AccountTest.java index 1d95cf7..cabbcd1 100644 --- a/src/test/java/AccountTest.java +++ b/src/test/java/AccountTest.java @@ -17,21 +17,49 @@ public void getBalance() { } @Test - public void deposit() { - } + public void deposit_test() { + // Given + Account account = new Checking(0.0, 3,3); + Double expected = 40.0; + + //When + account.deposit(40.0); + + // Then + Double actual = account.getBalance(); + assertEquals(expected, actual); - @Test - public void withdraw() { } @Test - public void transferWithin() { - } + public void withdraw_test() { + // Given + Account account = new Checking(80.0, 3,3); + Double expected = 40.0; + + //When + account.withdraw(40.0); + // Then + Double actual = account.getBalance(); + assertEquals(expected, actual); + } @Test - public void transferToAnother() { + public void risk_test() { + // Given + Investment account = new Investment(8000.0, 3,3, .09); + Double expected = .09; + + //When + account.calcReturn(); + + // Then + Double actual = account.getBalance(); + assertEquals(expected, actual); } + + @Test public void getAcctHist() { } From b11a6c6fa8f85e1e611cf5adaae58e0b988ef8b9 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sat, 26 Oct 2019 20:00:38 -0400 Subject: [PATCH 39/82] input loop --- .DS_Store | Bin 6148 -> 6148 bytes src/main/java/ATM.java | 33 +++++++++++++++++++++++++-------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/.DS_Store b/.DS_Store index d92ddf0dfea967a4df3ab90415b58ee3abddd721..052176fdc2683e9e90b03828b73fbb593d3abb67 100644 GIT binary patch delta 98 zcmZoMXfc=|#>B)qu~2NHo}v&l0|Nsi1A_oVN^x>dQht68<7P&tRg7XFaTbOYhD3%E ohD3xM592e|&Fmcf96%F*3coW?<`=Q#02%;93=EqCM7A&k04#JB00000 delta 84 zcmZoMXfc=|#>B`mF;Q%yo}wrd0|Nsi1A_oVaY0f}eiD#(BB@|v;_}G`A}pI#m>#ok jED&Yf%+A5j0o1cuk@-9GWPTA#4xl=aHm1!1B3qaNQhO6G diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index edb404e..f62e131 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -4,8 +4,9 @@ public class ATM { private User currentUser; - private DB userDB; // 0: ID 1: Last Name 2: First Name 3: cardNum 4: PW - private DB transactionDB; // 0: credit/debit 1: accountID 2: amount (signed) 3: timeStamp 4: description + + private DB userDB; // 0: ID 1: Last Name 2: First Name 3: cardNum 4: PW + private DB transactionDB; // 0: credit/debit 1: accountID 2: amount (signed) 3: timeStamp 4: description private DB accountDB; // 0: accountID 1: ownerID 2: balance 3: type 4: risk/interest/null (type-dependent) public ATM(String userDBName, String accountDBName, String transactionDBName) { @@ -73,16 +74,21 @@ public void getUser() { // find user in DB + String[] userInfo = this.getUserInfoByCardNum(Integer.parseInt(cardNum)); + // check PW Console.println("Enter Password"); String password = Console.getInput(); - - // instantiate user - + if(password.equals(userInfo[4])) { + // instantiate user + } } // add new user - called by getUser() public User newUser() { + + + return null; } @@ -142,11 +148,22 @@ public void saveDBs() { } public void serviceLoop() { - // authenticate a user (or die trying) // only returns null if the magic secret exit code is called - getUser(); - if (this.currentUser == null) { return; } + + //Login or new user? + Console.println("(1) login\n(2) Create Account"); + String input = Console.getInput(); + if(input.equals("1")) { //Try login + getUser(); + if (this.currentUser == null) { + return; + } + } else if (input.equals("2")) { //Create User + this.newUser(); + } else { + //error + } loadDBs(); From aed31bbb6c1d44be9c89f8f1356648149cc2d741 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Sat, 26 Oct 2019 20:05:32 -0400 Subject: [PATCH 40/82] branch moment --- src/main/java/ATM.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index 0e6886b..5b0bd31 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -167,14 +167,14 @@ public void saveUserToDB(User user) { } } - public void saveAccountrToDB(Account account) { + public void saveAccountToDB(Account account) { String[] stringRepOfAccount = account.toStringArray(); - int accountID = account.getacctID(); - int rowNum = getUserRowByID(userID); - if (rowNum == -1) { // user isn't in DB yet - this.userDB.addRow(stringRepOfUser); + int accountNum = account.getAcctNum(); + int rowNum = getAccountRowbyID(accountNum); + if (rowNum == -1) { // account isn't in DB yet + this.accountDB.addRow(stringRepOfAccount); } else { // update a found row - this.userDB.replaceRow(rowNum, stringRepOfUser); + this.accountDB.replaceRow(rowNum, stringRepOfAccount); } } From e0e65d343f1589f9700aa7d0b37dad9da9bd06f6 Mon Sep 17 00:00:00 2001 From: Mike King Date: Sat, 26 Oct 2019 23:38:24 -0400 Subject: [PATCH 41/82] added some account methods and test --- src/main/java/ATM.java | 4 ++++ src/main/java/Account.java | 8 ------- src/main/java/Savings.java | 3 +-- src/test/java/AccountTest.java | 41 +++++++++++++++++++++++++++++++++- 4 files changed, 45 insertions(+), 11 deletions(-) diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index 90a4bd3..28da2b3 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -196,6 +196,10 @@ public void saveAccountToDB(Account account) { } } + private int getAccountRowbyID(int accountNum) { + return 0; + } + public void savePendingTransactionsToDB(ArrayList pendingTransactions) { for (Transaction transaction : pendingTransactions) { this.transactionDB.addRow(transaction.toStringArray()); diff --git a/src/main/java/Account.java b/src/main/java/Account.java index ca13ac7..ebd5a0b 100644 --- a/src/main/java/Account.java +++ b/src/main/java/Account.java @@ -31,14 +31,6 @@ public void withdraw(Double amount){ } - public void transferWithin(){ - - } - - public void transferToAnother(){ - - } - public void getAcctHist(){ } diff --git a/src/main/java/Savings.java b/src/main/java/Savings.java index b4d4f21..1152299 100644 --- a/src/main/java/Savings.java +++ b/src/main/java/Savings.java @@ -8,9 +8,8 @@ public Savings(Double balance, Integer ownerID, Integer acctNum, Double interest } - public Double calcInterestReturned(){ + public void calcInterestReturned(){ balance += (interestRate * balance); - return getBalance(); } diff --git a/src/test/java/AccountTest.java b/src/test/java/AccountTest.java index cabbcd1..ab72cb1 100644 --- a/src/test/java/AccountTest.java +++ b/src/test/java/AccountTest.java @@ -1,3 +1,4 @@ +import org.junit.Assert; import org.junit.Test; import static org.junit.Assert.*; @@ -63,4 +64,42 @@ public void risk_test() { @Test public void getAcctHist() { } -} \ No newline at end of file + + @Test + public void getOwnerID() { + // Given + Account account = new Checking(0.0, 3,3); + Integer expected = 3; + + + // Then + Integer actual = account.getOwnerID(); + assertEquals(expected, actual); + } + + @Test + public void getAcctNum() { + // Given + Account account = new Checking(0.0, 3,3); + Integer expected = 3; + + + // Then + Integer actual = account.getAcctNum(); + assertEquals(expected, actual); + + } + + @Test + public void calcInterestReturned() { + // Given + Savings account = new Savings(80000.0, 3,3, 0.002); + Double expected = 80160.0; + + account.calcInterestReturned(); + + // Then + Double actual = account.getBalance(); + assertEquals(expected, actual); + } +} From e053bfe89f4f7866f9e989646f7abe55a521000d Mon Sep 17 00:00:00 2001 From: deltagphys Date: Sun, 27 Oct 2019 00:20:33 -0400 Subject: [PATCH 42/82] ATM helper methods for loading accounts and users --- src/main/java/ATM.java | 57 +++++++++---- src/main/java/Account.java | 4 + src/main/java/DB.java | 2 +- src/test/java/ATMTest.java | 165 +++++++++++++++++++++++++++++++++++++ 4 files changed, 210 insertions(+), 18 deletions(-) diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index 90a4bd3..7bac2b0 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -45,23 +45,34 @@ public void setCurrentUser(User currentUser) { // return new User(); // } + //find user row by id + public Integer getUserRowByID (Integer ID) { + return this.userDB.findPartialRow(new String[]{ID.toString()}, new int[]{0}); + } + //find user info by id (helper for constructor) public String [] getUserInfoByID (Integer ID) { int rowNumOfUser = this.userDB.findPartialRow(new String[] {ID.toString()}, new int[] {0}); return this.userDB.readRow(rowNumOfUser); } - //find user row by id - public Integer getUserRowByID (Integer ID) { - return this.userDB.findPartialRow(new String[]{ID.toString()}, new int[]{0}); - } - //find user info by card number (helper for constructor) public String [] getUserInfoByCardNum (Integer cardNum) { int rowNumOfUser = this.userDB.findPartialRow(new String[] {cardNum.toString()}, new int[] {3}); return this.userDB.readRow(rowNumOfUser); } + //find account row by id + public Integer getAccountRowByID (Integer ID) { + return this.accountDB.findPartialRow(new String[]{ID.toString()}, new int[]{0}); + } + + //find account info by id (helper for constructor) + public String [] getAccountInfoByID (Integer ID) { + int rowNumOfAccount = this.accountDB.findPartialRow(new String[] {ID.toString()}, new int[] {0}); + return this.accountDB.readRow(rowNumOfAccount); + } + public void logOut (User currentUser) { } @@ -93,17 +104,29 @@ public User newUser() { } //find accounts by owner id (to then be used by constructor) -// public ArrayList getAccountInfoByUser (User user) { -// int [] recordRowNums; -// recordRowNums = this.accountDB.findPartialRowMultiple(new String[] {user.ID.toString()}, new int[] {1}); -// -// ArrayList accountsInfo = new ArrayList<>(); -// for (int rowNum : recordRowNums) { -// accountsInfo.add(this.accountDB.readRow(rowNum)); -// } -// -// return accountsInfo; -// } + public int[] getAccountRowsByUser (User user) { + int [] recordRowNums; + recordRowNums = this.accountDB.findPartialRowMultiple(new String[] {user.getUserID().toString()}, new int[] {1}); + + return recordRowNums; + } + + // get string representation of one account + public String[] getAccountInfoByRow (int rowNum) { + return this.accountDB.readRow(rowNum); + } + + // account instance from info (pre-existing account) + public Account getAccountByInfo (String[] info) { + if (info[3].equals("Checking")) { + return new Checking(Double.parseDouble(info[2]), Integer.parseInt(info[1]), Integer.parseInt(info[0])); + } else if (info[3].equals("Savings")) { + return new Savings(Double.parseDouble(info[2]), Integer.parseInt(info[1]), Integer.parseInt(info[0]), Double.parseDouble(info[4])); + } else if (info[3].equals("Investment")) { + return new Investment(Double.parseDouble(info[2]), Integer.parseInt(info[1]), Integer.parseInt(info[0]), Double.parseDouble(info[4])); + } + return null; + } // load database info from disk public void loadDBs() { @@ -188,7 +211,7 @@ public void saveUserToDB(User user) { public void saveAccountToDB(Account account) { String[] stringRepOfAccount = account.toStringArray(); int accountNum = account.getAcctNum(); - int rowNum = getAccountRowbyID(accountNum); + int rowNum = getAccountRowByID(accountNum); if (rowNum == -1) { // account isn't in DB yet this.accountDB.addRow(stringRepOfAccount); } else { // update a found row diff --git a/src/main/java/Account.java b/src/main/java/Account.java index 945b4d6..01d4a10 100644 --- a/src/main/java/Account.java +++ b/src/main/java/Account.java @@ -40,5 +40,9 @@ public void transferToAnother(){ public void getAcctHist(){ } + + public Boolean equals(Account account) { + return DB.serialize(this.toStringArray()).equals(DB.serialize(account.toStringArray())); + } } diff --git a/src/main/java/DB.java b/src/main/java/DB.java index f30bf3b..b157672 100644 --- a/src/main/java/DB.java +++ b/src/main/java/DB.java @@ -313,7 +313,7 @@ public int[] findPartialRowMultiple (String[] fragment, int[] fields) { } return result; } else { - return null; + return new int[0]; } } else { return null; diff --git a/src/test/java/ATMTest.java b/src/test/java/ATMTest.java index 5bba068..3109050 100644 --- a/src/test/java/ATMTest.java +++ b/src/test/java/ATMTest.java @@ -100,6 +100,29 @@ public void getUserInfoByID() { Assert.assertEquals(actual,expected); } + @Test + public void getUserInfoByCardNumTest() { + DB userDB = atm.getUserDB(); + userDB.clear(); + + User user1 = new User("Jim","Brown","goolybib", 12, 12343); + userDB.addRow(user1.toStringArray()); + User user2 = new User("Ji123m","Bro23wn","gool321ybib", 122, 1234313); + userDB.addRow(user2.toStringArray()); + User user3 = new User("Jane","Himne","gasdsdool321ybib", 32, 313); + userDB.addRow(user3.toStringArray()); + + String[] actual = atm.getUserInfoByCardNum(1234313); + String[] expected = user2.toStringArray(); + + Assert.assertEquals(actual,expected); + + actual = atm.getUserInfoByCardNum(313); + expected = user3.toStringArray(); + + Assert.assertEquals(actual,expected); + } + @Test public void getUserRowByID() { DB userDB = atm.getUserDB(); @@ -126,6 +149,148 @@ public void getUserRowByID() { expected = 2; Assert.assertEquals(actual,expected); + + actual = atm.getUserRowByID(323232); + expected = -1; + + Assert.assertEquals(actual,expected); + } + + @Test + public void getAccountInfoByID() { + DB accountDB = atm.getAccountDB(); + accountDB.clear(); + + Account account1 = new Checking(1532.34,23,1232123); + accountDB.addRow(account1.toStringArray()); + Account account2 = new Savings(120.43,12,333223, 0.01); + accountDB.addRow(account2.toStringArray()); + Account account3 = new Investment(234023.23,42,9948, 0.06); + accountDB.addRow(account3.toStringArray()); + + String[] actual = atm.getAccountInfoByID(333223); + String[] expected = account2.toStringArray(); + + Assert.assertEquals(actual,expected); + } + + @Test + public void getAccountRowByID() { + DB accountDB = atm.getAccountDB(); + accountDB.clear(); + + Account account1 = new Checking(1532.34,23,1232123); + accountDB.addRow(account1.toStringArray()); + Account account2 = new Savings(120.43,12,333223, 0.01); + accountDB.addRow(account2.toStringArray()); + Account account3 = new Investment(234023.23,42,9948, 0.06); + accountDB.addRow(account3.toStringArray()); + + int actual = atm.getAccountRowByID(333223); + int expected = 1; + + Assert.assertEquals(expected, actual); + + actual = atm.getAccountRowByID(1232123); + expected = 0; + + Assert.assertEquals(expected, actual); + + actual = atm.getAccountRowByID(9948); + expected = 2; + + Assert.assertEquals(expected, actual); + + actual = atm.getAccountRowByID(99323248); + expected = -1; + + Assert.assertEquals(expected, actual); + } + + @Test + public void getAccountIDsByUserTest() { + DB accountDB = atm.getAccountDB(); + accountDB.clear(); + + DB userDB = atm.getUserDB(); + userDB.clear(); + + User user1 = new User("Jim","Brown","goolybib", 98, 12343); + userDB.addRow(user1.toStringArray()); + User user2 = new User("Ji123m","Bro23wn","gool321ybib", 42, 1234313); + userDB.addRow(user2.toStringArray()); + User user3 = new User("Jane","Himne","gasdsdool321ybib", 33, 313); + userDB.addRow(user3.toStringArray()); + + + Account account1 = new Checking(1532.34,23,1232123); + accountDB.addRow(account1.toStringArray()); + Account account2 = new Savings(120.43,12,33, 0.01); + accountDB.addRow(account2.toStringArray()); + Account account3 = new Investment(234023.23,42,48, 0.06); + accountDB.addRow(account3.toStringArray()); + Account account4 = new Checking(1532.34,42,5423); + accountDB.addRow(account4.toStringArray()); + Account account5 = new Savings(120.43,98,333223, 0.01); + accountDB.addRow(account5.toStringArray()); + Account account6 = new Investment(234023.23,42,9948, 0.06); + accountDB.addRow(account6.toStringArray()); + Account account7 = new Checking(1532.34,23,515); + accountDB.addRow(account7.toStringArray()); + Account account8 = new Savings(120.43,12,749, 0.01); + accountDB.addRow(account8.toStringArray()); + Account account9 = new Investment(234023.23,42,904, 0.06); + accountDB.addRow(account9.toStringArray()); + + int[] rows = atm.getAccountRowsByUser(user1); + String [] accountInfo; + int[] accts = {333223}; + for (int i = 0; i < rows.length; i++) { + accountInfo = atm.getAccountInfoByRow(rows[i]); + Assert.assertEquals("user1", (int)user1.getUserID(), (int) Integer.parseInt(accountInfo[1])); + Assert.assertEquals("user1", (int)accts[i], (int) Integer.parseInt(accountInfo[0])); + } + + int[] rows2 = atm.getAccountRowsByUser(user2); + String [] accountInfo2; + int[] accts2 = {48,5423,9948,904}; + for (int i = 0; i < rows2.length; i++) { + accountInfo2 = atm.getAccountInfoByRow(rows2[i]); + Assert.assertEquals("user2", (int)user2.getUserID(), (int) Integer.parseInt(accountInfo2[1])); + Assert.assertEquals("user2", (int)accts2[i], (int) Integer.parseInt(accountInfo2[0])); + } + + int[] rows3 = atm.getAccountRowsByUser(user3); + String [] accountInfo3; + int[] accts3 = {}; + for (int i = 0; i < rows3.length; i++) { + accountInfo3 = atm.getAccountInfoByRow(rows3[i]); + Assert.assertEquals("user3", (int)user3.getUserID(), (int) Integer.parseInt(accountInfo3[1])); + Assert.assertEquals("user3", (int)accts3[i], (int) Integer.parseInt(accountInfo3[0])); + } + } + + @Test + public void getAccountByInfoTest() { + DB accountDB = atm.getAccountDB(); + accountDB.clear(); + + Account account1 = new Checking(1532.34,23,1232123); + accountDB.addRow(account1.toStringArray()); + Account account2 = new Savings(120.43,12,33, 0.01); + accountDB.addRow(account2.toStringArray()); + Account account3 = new Investment(234023.23,42,48, 0.06); + accountDB.addRow(account3.toStringArray()); + Account account4 = new Checking(1532.34,42,5423); + accountDB.addRow(account4.toStringArray()); + Account account5 = new Savings(120.43,98,333223, 0.01); + accountDB.addRow(account5.toStringArray()); + + Assert.assertTrue("acct1", account1.equals(atm.getAccountByInfo(account1.toStringArray()))); + Assert.assertTrue("acct2", account2.equals(atm.getAccountByInfo(account2.toStringArray()))); + Assert.assertTrue("acct5", account5.equals(atm.getAccountByInfo(account5.toStringArray()))); + Assert.assertEquals(null, atm.getAccountByInfo(new String[] {"","","","",""})); + } @Test From 6e438f250dfb899598ba19d9a6e3cbc2e4b456ed Mon Sep 17 00:00:00 2001 From: deltagphys Date: Sun, 27 Oct 2019 00:28:11 -0400 Subject: [PATCH 43/82] DBtest addition --- src/test/java/DBTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/DBTest.java b/src/test/java/DBTest.java index e648f4e..36e12d9 100644 --- a/src/test/java/DBTest.java +++ b/src/test/java/DBTest.java @@ -565,7 +565,7 @@ public void findPartialRowMultipleTest() { Assert.assertEquals(3, db1.findPartialRowMultiple(new String[] {"Item"}, new int[] {1})[1]); Assert.assertEquals(4, db1.findPartialRowMultiple(new String[] {"Item"}, new int[] {1})[2]); - Assert.assertEquals(null, db1.findPartialRowMultiple(new String[] {"Iteasdm"}, new int[] {1})); + Assert.assertEquals((int)0, (int)db1.findPartialRowMultiple(new String[] {"Iteasdm"}, new int[] {1}).length); db1.delete(); } From e4eddb657e72c7fb280d2263c57276b8c774e4df Mon Sep 17 00:00:00 2001 From: deltagphys Date: Sun, 27 Oct 2019 08:11:03 -0400 Subject: [PATCH 44/82] get all accounts for a user and test --- src/main/java/ATM.java | 10 ++++++++ src/test/java/ATMTest.java | 48 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index 7bac2b0..6519e56 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -128,6 +128,16 @@ public Account getAccountByInfo (String[] info) { return null; } + // AL of accounts for a user + public ArrayList getAccountsforUser (User user) { + int[] rows = getAccountRowsByUser(user); + ArrayList accounts = new ArrayList<>(); + for (int row : rows) { + accounts.add(getAccountByInfo(getAccountInfoByRow(row))); + } + return accounts; + } + // load database info from disk public void loadDBs() { // // find accounts, create instances diff --git a/src/test/java/ATMTest.java b/src/test/java/ATMTest.java index 3109050..b3787b5 100644 --- a/src/test/java/ATMTest.java +++ b/src/test/java/ATMTest.java @@ -7,6 +7,7 @@ import java.util.ArrayList; import java.util.Date; +import java.util.Arrays; import static org.junit.Assert.*; @@ -293,6 +294,53 @@ public void getAccountByInfoTest() { } + @Test + public void getAccountsForUserTest() { + DB accountDB = atm.getAccountDB(); + accountDB.clear(); + + DB userDB = atm.getUserDB(); + userDB.clear(); + + User user1 = new User("Jim","Brown","goolybib", 98, 12343); + userDB.addRow(user1.toStringArray()); + User user2 = new User("Ji123m","Bro23wn","gool321ybib", 42, 1234313); + userDB.addRow(user2.toStringArray()); + User user3 = new User("Jane","Himne","gasdsdool321ybib", 33, 313); + userDB.addRow(user3.toStringArray()); + + + Account account1 = new Checking(1532.34,23,1232123); + accountDB.addRow(account1.toStringArray()); + Account account2 = new Savings(120.43,12,33, 0.01); + accountDB.addRow(account2.toStringArray()); + Account account3 = new Investment(234023.23,42,48, 0.06); + accountDB.addRow(account3.toStringArray()); + Account account4 = new Checking(1532.34,42,5423); + accountDB.addRow(account4.toStringArray()); + Account account5 = new Savings(120.43,98,333223, 0.01); + accountDB.addRow(account5.toStringArray()); + Account account6 = new Investment(234023.23,42,9948, 0.06); + accountDB.addRow(account6.toStringArray()); + Account account7 = new Checking(1532.34,23,515); + accountDB.addRow(account7.toStringArray()); + Account account8 = new Savings(120.43,12,749, 0.01); + accountDB.addRow(account8.toStringArray()); + Account account9 = new Investment(234023.23,42,904, 0.06); + accountDB.addRow(account9.toStringArray()); + + ArrayList actual = atm.getAccountsforUser(user1); + + Assert.assertEquals("user1", (int) 1, (int) actual.size()); + Assert.assertTrue("user1.1", Arrays.equals(account5.toStringArray(),actual.get(0).toStringArray())); + + actual = atm.getAccountsforUser(user2); + + Assert.assertEquals("user2", (int) 4, (int) actual.size()); + Assert.assertTrue("user2.1", Arrays.equals(account6.toStringArray(),actual.get(2).toStringArray())); + Assert.assertTrue("user2.3", Arrays.equals(account3.toStringArray(),actual.get(0).toStringArray())); + } + @Test public void saveUserToDBTest() { DB userDB = atm.getUserDB(); From 45415c37b3f9a2d25e0847b42238947878575389 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Sun, 27 Oct 2019 08:53:23 -0400 Subject: [PATCH 45/82] save account to DB method and test --- src/test/java/ATMTest.java | 48 +++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/src/test/java/ATMTest.java b/src/test/java/ATMTest.java index b3787b5..da1c03b 100644 --- a/src/test/java/ATMTest.java +++ b/src/test/java/ATMTest.java @@ -384,7 +384,6 @@ public void saveUserToDBTest() { expected = user1.toStringArray(); Assert.assertNotEquals(actual,expected); - } @Test @@ -409,7 +408,48 @@ public void savePendingTransactionsTest() { } @Test - public void getUserInfoByCardNum() { + public void saveAccountToDBTest() { + DB accountDB = atm.getAccountDB(); + accountDB.clear(); + + Account account1 = new Checking(1532.34,23,1232123); + atm.saveAccountToDB(account1); + Account account2 = new Savings(120.43,12,749, 0.01); + atm.saveAccountToDB(account2); + Account account3 = new Investment(234023.23,42,48, 0.06); + atm.saveAccountToDB(account3); + Account account4 = new Checking(1532.34,42,5423); + atm.saveAccountToDB(account4); + + + String[] actual = atm.getAccountInfoByID(48); + String[] expected = account3.toStringArray(); + + Assert.assertEquals(actual,expected); + + actual = atm.getAccountInfoByID(1232123); + expected = account1.toStringArray(); + + Assert.assertEquals(actual,expected); + + int actual2 = accountDB.length(); + int expected2 = 4; + + Assert.assertEquals(actual,expected); + + Account account10 = new Savings(9990.43,12,749, 0.01); + atm.saveAccountToDB(account10); + + actual2 = accountDB.length(); + expected2 = 4; + + Assert.assertEquals(actual,expected); + + actual = atm.getAccountInfoByID(749); + expected = account10.toStringArray(); + + Assert.assertEquals(actual,expected); + } @Test @@ -420,10 +460,6 @@ public void logOut() { public void newUser() { } - @Test - public void getAccountInfoByUser() { - } - @Test public void loadDBs() { } From 8400e2963d92f77f84aedde90d0cce67dde8e636 Mon Sep 17 00:00:00 2001 From: Mike King Date: Sun, 27 Oct 2019 10:39:45 -0400 Subject: [PATCH 46/82] added investment test --- .DS_Store | Bin 6148 -> 6148 bytes src/main/java/Investment.java | 5 +++++ src/test/java/AccountTest.java | 16 +++++++++++++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/.DS_Store b/.DS_Store index 052176fdc2683e9e90b03828b73fbb593d3abb67..bf72b90661b7c7fb2a910eecd2938fa56eb5634e 100644 GIT binary patch literal 6148 zcmeHK%Wl&^6ur}g)-gf^38`IrgTx|KNubRFA*CT*bb*90f(4+~j-$lL@mR4#fDn`o z9|3>B4)F&p`2xNGwk%)|XC8_Yr)5_J)m&-j%*=g^=Uk7+Lqx1K3Ti}UBC=2f7Hw3= zh|r6&B5Qi&B9Ms~2NCc15%BP>+ImbWI8`p!TWNlul-8wiLym&czHGTb7<$>WtB}FWJQQo05UtrX$xg9=877XUP0dKeU z;w|UfPy74FPkW2L10MWQpY%_T{mdQ45s!O_C!TYI65=$VZF-J6%BANfEUqr3=Mt=k zuzyNj>L8uhsE#5MD%NX9_U_VagS#T$#RNFnx1i${qTqwx20f?!@HG zh@)p_`i8>v#e+H0oLEz#sSN{$fn^2?s#_N4|NhDM|79oBGYl98&J+Wz*mm13EJ>fO zD~l6nt%GueB1G8D6iOFV`Z$&iaTIT%NP*4~4PaB_Od&=f=0`x%U@F7FUuEDoh6Vox delta 153 zcmZoMXfc=|#>B)qu~2NHo}v&l0|Nsi1A_oVN^x>dQht68 Date: Sun, 27 Oct 2019 11:20:35 -0400 Subject: [PATCH 47/82] merge --- src/main/java/ATM.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index f62e131..48413b4 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -87,6 +87,13 @@ public void getUser() { // add new user - called by getUser() public User newUser() { + Console.println("Enter first your Name"); + String firstName = Console.getInput(); + + Console.println("Enter last your Name"); + String lastName = Console.getInput(); + + //User newUser = new User(); return null; From 2717f9a319404df60c96cca27aca36d0b5efcc9a Mon Sep 17 00:00:00 2001 From: Ryan Date: Sun, 27 Oct 2019 11:54:36 -0400 Subject: [PATCH 48/82] Card Number Gen --- src/main/java/User.java | 17 +++++++++++++++++ src/test/java/UserTest.java | 15 +++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/main/java/User.java b/src/main/java/User.java index f25d5b3..8d9e267 100644 --- a/src/main/java/User.java +++ b/src/main/java/User.java @@ -16,6 +16,7 @@ public User(String firstName, String lastName, String password, Integer userID, this.password = password; this.userID = userID; this.cardNumber = cardNumber; + this.accounts = null; } @@ -23,6 +24,22 @@ public User(Integer userId, String password, DB data) { } + public static Integer genCardNum() { + String numString = ""; + for (int i = 0; i < 8; i++) { + Integer num; + if(i == 0 || i == 7) { + num = (int)(Math.random() * 9 + 1); + } else { + num = (int)(Math.random() * 10); + } + + numString += num.toString(); + System .out.print(num + ", "); + } + return Integer.parseInt(numString); + } + public ArrayList getUserHistory() { return pendingTransactions; diff --git a/src/test/java/UserTest.java b/src/test/java/UserTest.java index 36643fb..eb3fc2b 100644 --- a/src/test/java/UserTest.java +++ b/src/test/java/UserTest.java @@ -1,5 +1,7 @@ import org.junit.After; +import org.junit.Assert; import org.junit.Before; +import org.junit.Test; import static org.junit.Assert.*; @@ -12,4 +14,17 @@ public void setUp() throws Exception { @After public void tearDown() throws Exception { } + + @Test + public void genCardNumTest() { + + System.out.println("Testing Card Number Gen"); + + for (int i = 0; i < 10; i++) { + Integer cardNum = User.genCardNum(); + + System.out.println(cardNum); + Assert.assertEquals(8, cardNum.toString().length()); + } + } } \ No newline at end of file From a3fcf8c2ffa61a9e4cb67f4109dae13dbcb3824d Mon Sep 17 00:00:00 2001 From: Ryan Date: Sun, 27 Oct 2019 12:13:06 -0400 Subject: [PATCH 49/82] Basic imp to create user --- src/main/java/ATM.java | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index 58fdc59..4c901c0 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -98,16 +98,26 @@ public void getUser() { // add new user - called by getUser() public User newUser() { - Console.println("Enter first your Name"); + Console.println("Enter Your First Name"); String firstName = Console.getInput(); - Console.println("Enter last your Name"); + Console.println("Enter Your Last Name"); String lastName = Console.getInput(); - //User newUser = new User(); - + Console.println("Enter Your Password"); + String password = Console.getInput(); - return null; + Integer cardNumber = User.genCardNum(); + Console.println("Your Card NUmber is " + cardNumber); + + Integer userID = (int) (Math.random() * 1000); + + + User newUser = new User(firstName, lastName, password, userID, cardNumber); + currentUser = newUser; + this.saveUserToDB(currentUser); + + return newUser; } //find accounts by owner id (to then be used by constructor) From 6f20a15dd78a832b23e38c9dfc62c7e6f9d1a371 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sun, 27 Oct 2019 12:15:01 -0400 Subject: [PATCH 50/82] remove print --- src/main/java/User.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/User.java b/src/main/java/User.java index 8d9e267..8ed8c52 100644 --- a/src/main/java/User.java +++ b/src/main/java/User.java @@ -33,9 +33,7 @@ public static Integer genCardNum() { } else { num = (int)(Math.random() * 10); } - numString += num.toString(); - System .out.print(num + ", "); } return Integer.parseInt(numString); } From 94ad687678b42f7c95bf0a62f78a619c971bec8c Mon Sep 17 00:00:00 2001 From: deltagphys Date: Sun, 27 Oct 2019 12:18:16 -0400 Subject: [PATCH 51/82] get max user number method and test --- src/main/java/ATM.java | 18 ++++++++++++----- src/test/java/ATMTest.java | 40 ++++++++++++++++++++++++++++++++++---- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index c7627bf..4c39e33 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -129,7 +129,7 @@ public Account getAccountByInfo (String[] info) { } // AL of accounts for a user - public ArrayList getAccountsforUser (User user) { + public ArrayList getAccountsForUser(User user) { int[] rows = getAccountRowsByUser(user); ArrayList accounts = new ArrayList<>(); for (int row : rows) { @@ -153,6 +153,18 @@ public int getUserCount() { return this.userDB.length(); } + public int getMaxUserNumber() { + ArrayList userInfo = new ArrayList<>(); + userInfo = this.userDB.readAllRows(); + int maxID = 0; + for (String[] user : userInfo) { + if (Integer.parseInt(user[0]) > maxID) { + maxID = Integer.parseInt(user[0]); + } + } + return maxID; + } + // deal with the user's choices public void userMenu() { @@ -229,10 +241,6 @@ public void saveAccountToDB(Account account) { } } - private int getAccountRowbyID(int accountNum) { - return 0; - } - public void savePendingTransactionsToDB(ArrayList pendingTransactions) { for (Transaction transaction : pendingTransactions) { this.transactionDB.addRow(transaction.toStringArray()); diff --git a/src/test/java/ATMTest.java b/src/test/java/ATMTest.java index da1c03b..65ce574 100644 --- a/src/test/java/ATMTest.java +++ b/src/test/java/ATMTest.java @@ -9,8 +9,6 @@ import java.util.Date; import java.util.Arrays; -import static org.junit.Assert.*; - @RunWith(JUnitParamsRunner.class) public class ATMTest { @@ -101,6 +99,40 @@ public void getUserInfoByID() { Assert.assertEquals(actual,expected); } + @Test + public void getMaxUserNumberTest() { + DB userDB = atm.getUserDB(); + userDB.clear(); + + User user1 = new User("Jim","Brown","goolybib", 12, 12343); + atm.saveUserToDB(user1); + User user2 = new User("Ji123m","Bro23wn","gool321ybib", 122, 1234313); + atm.saveUserToDB(user2); + User user3 = new User("Jane","Himne","gasdsdool321ybib", 32, 313); + atm.saveUserToDB(user3); + + int actual = atm.getMaxUserNumber(); + int expected = 122; + + Assert.assertEquals(actual,expected); + + User user4 = new User("Jane","Himne","gasdsdool321ybib", 29, 313); + atm.saveUserToDB(user4); + + actual = atm.getMaxUserNumber(); + expected = 122; + + Assert.assertEquals(actual,expected); + + User user5 = new User("Jane","Himne","gasdsdool321ybib", 199, 313); + atm.saveUserToDB(user5); + + actual = atm.getMaxUserNumber(); + expected = 199; + + Assert.assertEquals(actual,expected); + } + @Test public void getUserInfoByCardNumTest() { DB userDB = atm.getUserDB(); @@ -329,12 +361,12 @@ public void getAccountsForUserTest() { Account account9 = new Investment(234023.23,42,904, 0.06); accountDB.addRow(account9.toStringArray()); - ArrayList actual = atm.getAccountsforUser(user1); + ArrayList actual = atm.getAccountsForUser(user1); Assert.assertEquals("user1", (int) 1, (int) actual.size()); Assert.assertTrue("user1.1", Arrays.equals(account5.toStringArray(),actual.get(0).toStringArray())); - actual = atm.getAccountsforUser(user2); + actual = atm.getAccountsForUser(user2); Assert.assertEquals("user2", (int) 4, (int) actual.size()); Assert.assertTrue("user2.1", Arrays.equals(account6.toStringArray(),actual.get(2).toStringArray())); From 6a6ef18a5b6a6aba60067a6c766ada05d31d180d Mon Sep 17 00:00:00 2001 From: deltagphys Date: Sun, 27 Oct 2019 12:25:04 -0400 Subject: [PATCH 52/82] get max account number method and test --- src/main/java/ATM.java | 12 ++++++++ src/test/java/ATMTest.java | 56 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index 4c39e33..15d36d0 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -165,6 +165,18 @@ public int getMaxUserNumber() { return maxID; } + public int getMaxAccountNumber() { + ArrayList accountInfo = new ArrayList<>(); + accountInfo = this.accountDB.readAllRows(); + int maxID = 0; + for (String[] account : accountInfo) { + if (Integer.parseInt(account[0]) > maxID) { + maxID = Integer.parseInt(account[0]); + } + } + return maxID; + } + // deal with the user's choices public void userMenu() { diff --git a/src/test/java/ATMTest.java b/src/test/java/ATMTest.java index 65ce574..238f202 100644 --- a/src/test/java/ATMTest.java +++ b/src/test/java/ATMTest.java @@ -104,6 +104,11 @@ public void getMaxUserNumberTest() { DB userDB = atm.getUserDB(); userDB.clear(); + int actual = atm.getMaxUserNumber(); + int expected = 0; + + Assert.assertEquals(actual,expected); + User user1 = new User("Jim","Brown","goolybib", 12, 12343); atm.saveUserToDB(user1); User user2 = new User("Ji123m","Bro23wn","gool321ybib", 122, 1234313); @@ -111,8 +116,8 @@ public void getMaxUserNumberTest() { User user3 = new User("Jane","Himne","gasdsdool321ybib", 32, 313); atm.saveUserToDB(user3); - int actual = atm.getMaxUserNumber(); - int expected = 122; + actual = atm.getMaxUserNumber(); + expected = 122; Assert.assertEquals(actual,expected); @@ -133,6 +138,53 @@ public void getMaxUserNumberTest() { Assert.assertEquals(actual,expected); } + @Test + public void getMaxAccountNumberTest() { + DB accountDB = atm.getAccountDB(); + accountDB.clear(); + + int actual = atm.getMaxAccountNumber(); + int expected = 0; + + Assert.assertEquals(actual,expected); + + Account account1 = new Checking(1532.34,23,2123); + atm.saveAccountToDB(account1); + + actual = atm.getMaxAccountNumber(); + expected = 2123; + + Assert.assertEquals(actual,expected); + + Account account2 = new Savings(120.43,12,33, 0.01); + atm.saveAccountToDB(account2); + + actual = atm.getMaxAccountNumber(); + expected = 2123; + + Assert.assertEquals(actual,expected); + + Account account3 = new Investment(234023.23,42,48, 0.06); + atm.saveAccountToDB(account3); + Account account4 = new Checking(1532.34,42,5423); + atm.saveAccountToDB(account4); + Account account5 = new Savings(120.43,98,333223, 0.01); + atm.saveAccountToDB(account5); + Account account6 = new Investment(234023.23,42,9948, 0.06); + atm.saveAccountToDB(account6); + Account account7 = new Checking(1532.34,23,515); + atm.saveAccountToDB(account7); + Account account8 = new Savings(120.43,12,749, 0.01); + atm.saveAccountToDB(account8); + Account account9 = new Investment(234023.23,42,904, 0.06); + atm.saveAccountToDB(account9); + + actual = atm.getMaxAccountNumber(); + expected = 333223; + + Assert.assertEquals(actual,expected); + } + @Test public void getUserInfoByCardNumTest() { DB userDB = atm.getUserDB(); From 6f36962284f8a01a026d7f17799d7c4bfb181360 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Sun, 27 Oct 2019 16:21:43 -0400 Subject: [PATCH 53/82] move around methods in ATM --- src/main/java/ATM.java | 185 +++++++++++++++++---------------- src/main/java/Investment.java | 3 +- src/test/java/AccountTest.java | 16 +-- 3 files changed, 109 insertions(+), 95 deletions(-) diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index 70009ed..e1ed6b5 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -45,34 +45,6 @@ public void setCurrentUser(User currentUser) { // return new User(); // } - //find user row by id - public Integer getUserRowByID (Integer ID) { - return this.userDB.findPartialRow(new String[]{ID.toString()}, new int[]{0}); - } - - //find user info by id (helper for constructor) - public String [] getUserInfoByID (Integer ID) { - int rowNumOfUser = this.userDB.findPartialRow(new String[] {ID.toString()}, new int[] {0}); - return this.userDB.readRow(rowNumOfUser); - } - - //find user info by card number (helper for constructor) - public String [] getUserInfoByCardNum (Integer cardNum) { - int rowNumOfUser = this.userDB.findPartialRow(new String[] {cardNum.toString()}, new int[] {3}); - return this.userDB.readRow(rowNumOfUser); - } - - //find account row by id - public Integer getAccountRowByID (Integer ID) { - return this.accountDB.findPartialRow(new String[]{ID.toString()}, new int[]{0}); - } - - //find account info by id (helper for constructor) - public String [] getAccountInfoByID (Integer ID) { - int rowNumOfAccount = this.accountDB.findPartialRow(new String[] {ID.toString()}, new int[] {0}); - return this.accountDB.readRow(rowNumOfAccount); - } - public void logOut (User currentUser) { } @@ -120,40 +92,7 @@ public User newUser() { return newUser; } - //find accounts by owner id (to then be used by constructor) - public int[] getAccountRowsByUser (User user) { - int [] recordRowNums; - recordRowNums = this.accountDB.findPartialRowMultiple(new String[] {user.getUserID().toString()}, new int[] {1}); - - return recordRowNums; - } - - // get string representation of one account - public String[] getAccountInfoByRow (int rowNum) { - return this.accountDB.readRow(rowNum); - } - - // account instance from info (pre-existing account) - public Account getAccountByInfo (String[] info) { - if (info[3].equals("Checking")) { - return new Checking(Double.parseDouble(info[2]), Integer.parseInt(info[1]), Integer.parseInt(info[0])); - } else if (info[3].equals("Savings")) { - return new Savings(Double.parseDouble(info[2]), Integer.parseInt(info[1]), Integer.parseInt(info[0]), Double.parseDouble(info[4])); - } else if (info[3].equals("Investment")) { - return new Investment(Double.parseDouble(info[2]), Integer.parseInt(info[1]), Integer.parseInt(info[0]), Double.parseDouble(info[4])); - } - return null; - } - // AL of accounts for a user - public ArrayList getAccountsForUser(User user) { - int[] rows = getAccountRowsByUser(user); - ArrayList accounts = new ArrayList<>(); - for (int row : rows) { - accounts.add(getAccountByInfo(getAccountInfoByRow(row))); - } - return accounts; - } // load database info from disk public void loadDBs() { @@ -166,33 +105,7 @@ public void loadDBs() { // // } - public int getUserCount() { - return this.userDB.length(); - } - public int getMaxUserNumber() { - ArrayList userInfo = new ArrayList<>(); - userInfo = this.userDB.readAllRows(); - int maxID = 0; - for (String[] user : userInfo) { - if (Integer.parseInt(user[0]) > maxID) { - maxID = Integer.parseInt(user[0]); - } - } - return maxID; - } - - public int getMaxAccountNumber() { - ArrayList accountInfo = new ArrayList<>(); - accountInfo = this.accountDB.readAllRows(); - int maxID = 0; - for (String[] account : accountInfo) { - if (Integer.parseInt(account[0]) > maxID) { - maxID = Integer.parseInt(account[0]); - } - } - return maxID; - } // deal with the user's choices public void userMenu() { @@ -248,6 +161,104 @@ public void serviceLoop() { serviceLoop(); } + /* + * DB interaction methods for the ATM + * + * We should create a storage class or generic methods in the DB class or something in the interface, but... + */ + + public int getUserCount() { + return this.userDB.length(); + } + + + //find accounts by owner id (to then be used by constructor) + public int[] getAccountRowsByUser (User user) { + int [] recordRowNums; + recordRowNums = this.accountDB.findPartialRowMultiple(new String[] {user.getUserID().toString()}, new int[] {1}); + + return recordRowNums; + } + + // get string representation of one account + public String[] getAccountInfoByRow (int rowNum) { + return this.accountDB.readRow(rowNum); + } + + // account instance from info (pre-existing account) + public Account getAccountByInfo (String[] info) { + if (info[3].equals("Checking")) { + return new Checking(Double.parseDouble(info[2]), Integer.parseInt(info[1]), Integer.parseInt(info[0])); + } else if (info[3].equals("Savings")) { + return new Savings(Double.parseDouble(info[2]), Integer.parseInt(info[1]), Integer.parseInt(info[0]), Double.parseDouble(info[4])); + } else if (info[3].equals("Investment")) { + return new Investment(Double.parseDouble(info[2]), Integer.parseInt(info[1]), Integer.parseInt(info[0]), Double.parseDouble(info[4])); + } + return null; + } + + // AL of accounts for a user + public ArrayList getAccountsForUser(User user) { + int[] rows = getAccountRowsByUser(user); + ArrayList accounts = new ArrayList<>(); + for (int row : rows) { + accounts.add(getAccountByInfo(getAccountInfoByRow(row))); + } + return accounts; + } + + public int getMaxUserNumber() { + ArrayList userInfo = new ArrayList<>(); + userInfo = this.userDB.readAllRows(); + int maxID = 0; + for (String[] user : userInfo) { + if (Integer.parseInt(user[0]) > maxID) { + maxID = Integer.parseInt(user[0]); + } + } + return maxID; + } + + public int getMaxAccountNumber() { + ArrayList accountInfo = new ArrayList<>(); + accountInfo = this.accountDB.readAllRows(); + int maxID = 0; + for (String[] account : accountInfo) { + if (Integer.parseInt(account[0]) > maxID) { + maxID = Integer.parseInt(account[0]); + } + } + return maxID; + } + + //find user row by id + public Integer getUserRowByID (Integer ID) { + return this.userDB.findPartialRow(new String[]{ID.toString()}, new int[]{0}); + } + + //find user info by id (helper for constructor) + public String [] getUserInfoByID (Integer ID) { + int rowNumOfUser = this.userDB.findPartialRow(new String[] {ID.toString()}, new int[] {0}); + return this.userDB.readRow(rowNumOfUser); + } + + //find user info by card number (helper for constructor) + public String [] getUserInfoByCardNum (Integer cardNum) { + int rowNumOfUser = this.userDB.findPartialRow(new String[] {cardNum.toString()}, new int[] {3}); + return this.userDB.readRow(rowNumOfUser); + } + + //find account row by id + public Integer getAccountRowByID (Integer ID) { + return this.accountDB.findPartialRow(new String[]{ID.toString()}, new int[]{0}); + } + + //find account info by id (helper for constructor) + public String [] getAccountInfoByID (Integer ID) { + int rowNumOfAccount = this.accountDB.findPartialRow(new String[] {ID.toString()}, new int[] {0}); + return this.accountDB.readRow(rowNumOfAccount); + } + public void saveUserToDB(User user) { String[] stringRepOfUser = user.toStringArray(); int userID = user.getUserID(); diff --git a/src/main/java/Investment.java b/src/main/java/Investment.java index 25e3554..7a7f311 100644 --- a/src/main/java/Investment.java +++ b/src/main/java/Investment.java @@ -22,9 +22,10 @@ public void setRisk(Double risk) { public Double calcReturn(){ - Double multiplier = risk * (Math.random()/.99999); + Double multiplier = risk * (2 * Math.random() - .8); balance += Math.round((multiplier * balance)*100d)/100d; + return getBalance(); } diff --git a/src/test/java/AccountTest.java b/src/test/java/AccountTest.java index 398492f..d18395b 100644 --- a/src/test/java/AccountTest.java +++ b/src/test/java/AccountTest.java @@ -47,17 +47,19 @@ public void withdraw_test() { } @Test public void risk_test() { - // Given Investment account = new Investment(8000.0, 3,3, .09); + Double actual; - //When - account.calcReturn(); - Double expected = account.getBalance(); + for (int i = 0; i < 1000; i++) { + actual = account.calcReturn(); + Double minExpected = account.getBalance() - .8 * account.getRisk() * account.getBalance(); + assertTrue(minExpected <= actual); - // Then - Double actual = account.getBalance(); - assertEquals(expected, actual); + actual = account.calcReturn(); + Double maxExpected = account.getBalance() + 1.2 * account.getRisk() * account.getBalance(); + assertTrue(maxExpected >= actual); + } } From f7be97b24935f9121bff4c3d4dbae40a99a467c5 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sun, 27 Oct 2019 16:58:13 -0400 Subject: [PATCH 54/82] Clean atm --- src/main/java/ATM.java | 101 +++++++++++++++++++---------------------- 1 file changed, 46 insertions(+), 55 deletions(-) diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index e1ed6b5..473e001 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -23,47 +23,45 @@ public ATM(String userDBName, String accountDBName, String transactionDBName) { public User getCurrentUser() { return this.currentUser; } - public DB getUserDB() { return this.userDB; } - public DB getTransactionDB() { return this.transactionDB; } - public DB getAccountDB() { return this.accountDB; } - public void setCurrentUser(User currentUser) { this.currentUser = currentUser; } -// public User authenticate(int cardNumber, String password) { -// -// return new User(); -// } - - public void logOut (User currentUser) { + // load database info from disk + public void loadDBs() { +// // find accounts, create instances +// ArrayList accountsInfo = getAccountInfoByUser(this.currentUser); +// ArrayList accounts = new ArrayList<>(); +// for (String[] acctInfo : accountsInfo) { +// accounts.add(new Account(...)); +// } +// // } - // log in user - don't return until you do - public void getUser() { + + public void authenticate() { //Read User's card Console.println("Card Number:"); String cardNum = Console.getInput(); // find user in DB - String[] userInfo = this.getUserInfoByCardNum(Integer.parseInt(cardNum)); // check PW Console.println("Enter Password"); String password = Console.getInput(); if(password.equals(userInfo[4])) { - // instantiate user + //currentUser = newUser() } } @@ -92,24 +90,41 @@ public User newUser() { return newUser; } + // log in user - don't return until you do + public void getUser() { + Console.println("(1) login\n(2) Create Account"); + String input = Console.getInput(); + if(input.equals("1")) { //Try login + this.authenticate(); + if (this.currentUser == null) { + return; + } + } else if (input.equals("2")) { //Create User + this.newUser(); + } else { + //error + } + } + // deal with the user's choices + public void userMenu() { - // load database info from disk - public void loadDBs() { -// // find accounts, create instances -// ArrayList accountsInfo = getAccountInfoByUser(this.currentUser); -// ArrayList accounts = new ArrayList<>(); -// for (String[] acctInfo : accountsInfo) { -// accounts.add(new Account(...)); -// } -// // } + public void serviceLoop() { + // authenticate a user (or die trying) + // only returns null if the magic secret exit code is called - // deal with the user's choices - public void userMenu() { + getUser(); + + loadDBs(); + + userMenu(); + logOut(); + + serviceLoop(); } // log out user @@ -134,43 +149,15 @@ public void saveDBs() { // } } - public void serviceLoop() { - // authenticate a user (or die trying) - // only returns null if the magic secret exit code is called - //Login or new user? - Console.println("(1) login\n(2) Create Account"); - String input = Console.getInput(); - if(input.equals("1")) { //Try login - getUser(); - if (this.currentUser == null) { - return; - } - } else if (input.equals("2")) { //Create User - this.newUser(); - } else { - //error - } - - loadDBs(); - - userMenu(); - - logOut(); - - serviceLoop(); - } - - /* + /* /////////////////////////////////////////////////////////////////////////////////////////////////////////////// * DB interaction methods for the ATM * * We should create a storage class or generic methods in the DB class or something in the interface, but... - */ - + */ /////////////////////////////////////////////////////////////////////////////////////////////////////////////// public int getUserCount() { return this.userDB.length(); } - //find accounts by owner id (to then be used by constructor) public int[] getAccountRowsByUser (User user) { @@ -286,4 +273,8 @@ public void savePendingTransactionsToDB(ArrayList pendingTransactio this.transactionDB.addRow(transaction.toStringArray()); } } + /* /////////////////////////////////////////////////////////////////////////////////////////////////////////////// + * End DB interaction methods for the ATM + */ /////////////////////////////////////////////////////////////////////////////////////////////////////////////// + } From 66809e7e16ee424ab51dd61fc709617890dc8ccb Mon Sep 17 00:00:00 2001 From: deltagphys Date: Sun, 27 Oct 2019 17:31:09 -0400 Subject: [PATCH 55/82] start of real menu --- src/main/java/Console.java | 40 ++++++++++++++++++++++++++++++++++ src/main/java/Main.java | 5 ++++- src/test/java/ConsoleTest.java | 6 +++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/main/java/Console.java b/src/main/java/Console.java index c72d698..3651020 100644 --- a/src/main/java/Console.java +++ b/src/main/java/Console.java @@ -1,3 +1,5 @@ +import java.io.IOException; +import java.util.Arrays; import java.util.Scanner; /** @@ -22,6 +24,44 @@ public static String getInput() { return input; } + public static String getInput2() { + Console.print("> "); + Scanner scanner = new Scanner(System.in); + + String input = scanner.nextLine().toLowerCase(); //get input from user + + return input; + } + + public static String getInput(String[] options) { + + System.out.print("\033[H\033[2J"); + System.out.flush(); + + int numOptions = options.length; + int numRows = (numOptions+1) >> 1; // this is how the cool kids divide by two + String output = ""; + + String[] rows = new String[numRows]; + + for (int i = 0; i < numRows; i++){ + rows[i] = Integer.toString(2*i+1) + " " + options[2*i] + " "; + if (2*i + 1 < numRows) { + rows[i] += (options[2*i + 1] + " " + Integer.toString(2*(i+1))); + } + rows[i] += "\n"; + } + + for (int i = numRows - 1; i >= 0; i--) { + output += rows[i]; + } + + println(output); + + return Console.getInput(); + + } + static Boolean integerCheck(String input) { return input.matches("^\\d+$"); } diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 7ff277a..cf6c277 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -2,7 +2,10 @@ public class Main { - public static void main(String[] args){ + public static void main(String[] args) { + String[] options = new String[] {"Live", "Die", "Repeat"}; + Console.getInput(options); + ATM atm = new ATM("users.csv", "accounts.csv", "transactions.csv"); atm.serviceLoop(); diff --git a/src/test/java/ConsoleTest.java b/src/test/java/ConsoleTest.java index 1d462ed..3319c55 100644 --- a/src/test/java/ConsoleTest.java +++ b/src/test/java/ConsoleTest.java @@ -16,6 +16,12 @@ public void print() { public void println() { } + @Test + public void menuOptionsTest() { + String[] options = new String[] {"Live", "Die", "Repeat"}; + Console.getInput(options); + } + @Test public void getInput() { } From 9e9b57f614f26ecf72651e248d06840f9a02ee75 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sun, 27 Oct 2019 17:50:38 -0400 Subject: [PATCH 56/82] Stub Menu Methods --- src/main/java/ATM.java | 63 ++++++++++++++++++++++++++++++++++++++++- src/main/java/User.java | 4 --- 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index 473e001..fa6c2d4 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -61,7 +61,8 @@ public void authenticate() { Console.println("Enter Password"); String password = Console.getInput(); if(password.equals(userInfo[4])) { - //currentUser = newUser() + // 0: ID 1: Last Name 2: First Name 3: cardNum 4: PW + currentUser = new User(userInfo[2], userInfo[1], userInfo[4], Integer.parseInt(userInfo[0]), Integer.parseInt(userInfo[3])); } } @@ -108,7 +109,67 @@ public void getUser() { // deal with the user's choices public void userMenu() { + boolean loggedIn = true; + while (loggedIn) { + Console.println("(1) Logout"); + Console.println("(2) Transaction History"); + Console.println("(3) Add Account"); + Console.println("() list Of Accounts..."); + + String input = Console.getInput(); + + switch (input) { + case "1": + loggedIn = false; + break; + } + } + } + + public void addAct() { + boolean choosingActType = true; + while (choosingActType) { + Console.println("Type of Account:"); + Console.println("(1) Checking"); + Console.println("(2) Savings"); + Console.println("(3) Investment"); + String input = Console.getInput(); + + switch (input) { + case "1": + + break; + case "2": + + break; + case "3": + + break; + default: + break; + } + } + + } + + public void accountMenu(Account account) { + boolean inAccountMenu = true; + while (inAccountMenu) { + Console.println("(1) View transaction History"); + Console.println("(2) Deposit"); + Console.println("(3) Withdraw"); + Console.println("(4) Close"); + Console.println("(5) Back To Main Menu"); + + String input = Console.getInput(); + + switch (input) { + case "5": + inAccountMenu = false; + break; + } + } } diff --git a/src/main/java/User.java b/src/main/java/User.java index 8ed8c52..7bd282b 100644 --- a/src/main/java/User.java +++ b/src/main/java/User.java @@ -20,10 +20,6 @@ public User(String firstName, String lastName, String password, Integer userID, this.accounts = null; } - public User(Integer userId, String password, DB data) { - - } - public static Integer genCardNum() { String numString = ""; for (int i = 0; i < 8; i++) { From 97cae6f498cb88b773111cb6a1765c68df53b007 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Sun, 27 Oct 2019 18:11:31 -0400 Subject: [PATCH 57/82] menus with and without headers --- src/main/java/Console.java | 35 ++++++++++++++++++++++++++++++++-- src/test/java/ConsoleTest.java | 6 +++++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/main/java/Console.java b/src/main/java/Console.java index 3651020..eadda41 100644 --- a/src/main/java/Console.java +++ b/src/main/java/Console.java @@ -1,3 +1,5 @@ +import org.apache.commons.lang3.StringUtils; + import java.io.IOException; import java.util.Arrays; import java.util.Scanner; @@ -45,9 +47,38 @@ public static String getInput(String[] options) { String[] rows = new String[numRows]; for (int i = 0; i < numRows; i++){ - rows[i] = Integer.toString(2*i+1) + " " + options[2*i] + " "; + rows[i] = String.format("%d | %-30s", 2*i+1, options[2*i]); if (2*i + 1 < numRows) { - rows[i] += (options[2*i + 1] + " " + Integer.toString(2*(i+1))); + rows[i] += String.format("%30s | %d", options[2*i + 1], 2*(i+1)); + } + rows[i] += "\n"; + } + + for (int i = numRows - 1; i >= 0; i--) { + output += rows[i]; + } + + println(output); + + return Console.getInput(); + + } + + public static String getInput(String header, String[] options) { + + System.out.print("\033[H\033[2J"); + System.out.flush(); + + int numOptions = options.length; + int numRows = (numOptions+1) >> 1; // this is how the cool kids divide by two + String output = StringUtils.center(header,66) + "\n\n"; + + String[] rows = new String[numRows]; + + for (int i = 0; i < numRows; i++){ + rows[i] = String.format("%d | %-30s", 2*i+1, options[2*i]); + if (2*i + 1 < numOptions) { + rows[i] += String.format("%30s | %d", options[2*i + 1], 2*(i+1)); } rows[i] += "\n"; } diff --git a/src/test/java/ConsoleTest.java b/src/test/java/ConsoleTest.java index 3319c55..3c30280 100644 --- a/src/test/java/ConsoleTest.java +++ b/src/test/java/ConsoleTest.java @@ -14,12 +14,16 @@ public void print() { @Test public void println() { + } @Test public void menuOptionsTest() { String[] options = new String[] {"Live", "Die", "Repeat"}; - Console.getInput(options); + String header = "Account Creation Menu"; + options = new String[] {"Live", "Die", "Repeat", "Bump", "Set", "Spike", "Towel"}; + //Console.getInput(options); + //Console.getInput(header, options); } @Test From a4886094b5559606916f5c5a0737645ff7fd06cc Mon Sep 17 00:00:00 2001 From: Ryan Date: Sun, 27 Oct 2019 18:38:56 -0400 Subject: [PATCH 58/82] create/choose account --- src/main/java/ATM.java | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index fa6c2d4..09fa343 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -109,24 +109,38 @@ public void getUser() { // deal with the user's choices public void userMenu() { + ArrayList usrAccts = getAccountsForUser(currentUser); boolean loggedIn = true; while (loggedIn) { Console.println("(1) Logout"); Console.println("(2) Transaction History"); Console.println("(3) Add Account"); - Console.println("() list Of Accounts..."); - String input = Console.getInput(); + for (int i = 0; i < usrAccts.size(); i++) { + Console.println("(" + (4 + i) + ") " + usrAccts.get(i).getClass().getName() + " " + usrAccts.get(i).acctNum); + } + + Integer input = Console.getInteger(); switch (input) { - case "1": + case 1: loggedIn = false; break; + case 2: + //print transaction hist + break; + case 3: + addAccount(usrAccts); + break; + default: + accountMenu(usrAccts.get(input - 4)); + break; } } + } - public void addAct() { + public void addAccount(ArrayList usrAccounts) { boolean choosingActType = true; while (choosingActType) { Console.println("Type of Account:"); @@ -135,18 +149,20 @@ public void addAct() { Console.println("(3) Investment"); String input = Console.getInput(); + choosingActType = false; switch (input) { case "1": - + usrAccounts.add(new Checking(0.0, currentUser.getUserID(), (int)(Math.random()*1000))); break; case "2": - + //usrAccounts.add(new Savings(0.0, currentUser.getUserID(), (int)(Math.random()*1000))); break; case "3": - + //usrAccounts.add(new Investment(0.0, currentUser.getUserID(), (int)(Math.random()*1000))); break; default: - + Console.println("Enter Valid Input"); + choosingActType = true; break; } } From d4e71cc9cc189e502dae189d3ea35c0b681f7d18 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Sun, 27 Oct 2019 18:45:02 -0400 Subject: [PATCH 59/82] . --- src/main/java/Console.java | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/main/java/Console.java b/src/main/java/Console.java index eadda41..706690c 100644 --- a/src/main/java/Console.java +++ b/src/main/java/Console.java @@ -60,7 +60,7 @@ public static String getInput(String[] options) { println(output); - return Console.getInput(); + return Integer.toString(Console.getInteger(numOptions)); } @@ -89,7 +89,7 @@ public static String getInput(String header, String[] options) { println(output); - return Console.getInput(); + return Integer.toString(Console.getInteger(numOptions)); } @@ -125,4 +125,23 @@ public static Integer getInteger() { return Integer.valueOf(input); } + public static Integer getInteger(int max) { + String input = getInput(); + while (true) { + if (integerCheck(input)) { + if (Integer.parseInt(input) >= 1 && Integer.parseInt(input) <= max) { + break; + } else { + println("Enter a number between 1 and " + Integer.toString(max)); + input = getInput(); + } + } + else { + println("Enter a number"); + input = getInput(); + } + } + return Integer.valueOf(input); + } + } From 07971615ff2816562aeb5b6c64d0d3506441ead4 Mon Sep 17 00:00:00 2001 From: Mike King Date: Sun, 27 Oct 2019 18:50:35 -0400 Subject: [PATCH 60/82] added Test --- src/main/java/Account.java | 5 +++-- src/test/java/AccountTest.java | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/main/java/Account.java b/src/main/java/Account.java index d817198..d481a03 100644 --- a/src/main/java/Account.java +++ b/src/main/java/Account.java @@ -27,8 +27,9 @@ public void deposit(Double amount){ } public void withdraw(Double amount){ - balance -= amount; - + if (balance > amount) { + balance -= amount; + } } public void getAcctHist(){ diff --git a/src/test/java/AccountTest.java b/src/test/java/AccountTest.java index d18395b..17c574d 100644 --- a/src/test/java/AccountTest.java +++ b/src/test/java/AccountTest.java @@ -46,6 +46,24 @@ public void withdraw_test() { assertEquals(expected, actual); } @Test + public void withdraw_Overdraft_test() { + // Given + Account account = new Checking(0.0, 3,3); + Account account2 = new Checking(40.0, 2,2); + Double expected = 0.0; + + //When + if (account.getBalance() > 40.0){ + account.withdraw(40.0); + account2.deposit(40.0); + }else { + System.out.println("Not enough funds"); + } + // Then + Double actual = account2.getBalance(); + assertEquals(account2.getBalance(), account.getBalance()); + } + @Test public void risk_test() { Investment account = new Investment(8000.0, 3,3, .09); From daf94b201eb6b5a7147358263f2aef75104d279f Mon Sep 17 00:00:00 2001 From: deltagphys Date: Sun, 27 Oct 2019 19:11:35 -0400 Subject: [PATCH 61/82] moved convenience methods --- src/main/java/Main.java | 38 -------------------------- src/test/java/ATMTest.java | 56 +++++++++++++++++++++++--------------- 2 files changed, 34 insertions(+), 60 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index cf6c277..fb46af8 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -3,46 +3,8 @@ public class Main { public static void main(String[] args) { - String[] options = new String[] {"Live", "Die", "Repeat"}; - Console.getInput(options); - ATM atm = new ATM("users.csv", "accounts.csv", "transactions.csv"); atm.serviceLoop(); } - - - - // convenience methods for dev environment to clear the DBs - only called from the IDE manually - private static void clearUserDB() { - DB userDB = null; - try { - userDB = new DB("users.csv", 5); - } catch (IOException e) { - e.printStackTrace(); - } - userDB.clear(); - } - - private static void clearAccountDB() { - DB accountDB = null; - try { - accountDB = new DB("accounts.csv", 5); - } catch (IOException e) { - e.printStackTrace(); - } - accountDB.clear(); - } - - private static void clearTransactionDB() { - DB transactionDB = null; - try { - transactionDB = new DB("transactions.csv", 5); - } catch (IOException e) { - e.printStackTrace(); - } - transactionDB.clear(); - } - - //find (past) transactions by user } diff --git a/src/test/java/ATMTest.java b/src/test/java/ATMTest.java index 238f202..2d2a5f6 100644 --- a/src/test/java/ATMTest.java +++ b/src/test/java/ATMTest.java @@ -5,6 +5,7 @@ import org.junit.Test; import org.junit.runner.RunWith; +import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.Arrays; @@ -536,27 +537,38 @@ public void saveAccountToDBTest() { } - @Test - public void logOut() { - } - - @Test - public void newUser() { - } - - @Test - public void loadDBs() { - } + // convenience methods for dev environment to clear the DBs - only called from the IDE manually +// @Test +// public void clearUserDB() { +// DB userDB = null; +// try { +// userDB = new DB("users.csv", 5); +// } catch (IOException e) { +// e.printStackTrace(); +// } +// userDB.clear(); +// } +// +// @Test +// public void clearAccountDB() { +// DB accountDB = null; +// try { +// accountDB = new DB("accounts.csv", 5); +// } catch (IOException e) { +// e.printStackTrace(); +// } +// accountDB.clear(); +// } +// +// @Test +// public void clearTransactionDB() { +// DB transactionDB = null; +// try { +// transactionDB = new DB("transactions.csv", 5); +// } catch (IOException e) { +// e.printStackTrace(); +// } +// transactionDB.clear(); +// } - @Test - public void userMenu() { - } - - @Test - public void testLogOut() { - } - - @Test - public void saveDBs() { - } } \ No newline at end of file From 99fe9703d2db83acae826f75fb5b4fbc46ba2ab0 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Sun, 27 Oct 2019 20:07:48 -0400 Subject: [PATCH 62/82] account creation firmed up --- src/main/java/ATM.java | 77 ++++++++++++++++------------------ src/main/java/Console.java | 26 +++++++++--- src/test/java/ConsoleTest.java | 1 - 3 files changed, 57 insertions(+), 47 deletions(-) diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index 09fa343..9eaf55e 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -130,7 +130,8 @@ public void userMenu() { //print transaction hist break; case 3: - addAccount(usrAccts); + Double deposit = Console.getCurrency("Initial deposit amount for this account: "); + addAccount(usrAccts, deposit); break; default: accountMenu(usrAccts.get(input - 4)); @@ -140,51 +141,47 @@ public void userMenu() { } - public void addAccount(ArrayList usrAccounts) { - boolean choosingActType = true; - while (choosingActType) { - Console.println("Type of Account:"); - Console.println("(1) Checking"); - Console.println("(2) Savings"); - Console.println("(3) Investment"); - String input = Console.getInput(); - - choosingActType = false; - switch (input) { - case "1": - usrAccounts.add(new Checking(0.0, currentUser.getUserID(), (int)(Math.random()*1000))); - break; - case "2": - //usrAccounts.add(new Savings(0.0, currentUser.getUserID(), (int)(Math.random()*1000))); - break; - case "3": - //usrAccounts.add(new Investment(0.0, currentUser.getUserID(), (int)(Math.random()*1000))); - break; - default: - Console.println("Enter Valid Input"); - choosingActType = true; - break; - } + public void addAccount(ArrayList usrAccounts, Double deposit) { + String header = "Choose Account Type:"; + String input = Console.getInput(header, new String[] {"Checking", "Savings", "Investment", "Back to Main Menu" }); + Account newAccount; + + switch (input) { + case "1": + newAccount = new Checking(deposit, this.currentUser.getUserID(), (int)(Math.random()*1000)); + this.saveAccountToDB(newAccount); + usrAccounts.add(newAccount); + break; + case "2": + Double interestRate = .01 * (1 + Math.floor(deposit/1000)); + Console.println(String.format("Your interest rate: %.2f", interestRate)+"%%"); + newAccount = new Savings(deposit, this.currentUser.getUserID(), (int)(Math.random()*1000), interestRate); + this.saveAccountToDB(newAccount); + usrAccounts.add(newAccount); + break; + case "3": + Console.print("On a scale of 1-10, enter your risk tolerance "); + int riskInput = Console.getInteger(10); + Double risk = riskInput * .01; + newAccount = new Investment(deposit, this.currentUser.getUserID(), (int)(Math.random()*1000), risk); + this.saveAccountToDB(newAccount); + usrAccounts.add(newAccount); + //usrAccounts.add(new Investment(0.0, currentUser.getUserID(), (int)(Math.random()*1000))); + break; + case "4": + break; } + } public void accountMenu(Account account) { - boolean inAccountMenu = true; - while (inAccountMenu) { - Console.println("(1) View transaction History"); - Console.println("(2) Deposit"); - Console.println("(3) Withdraw"); - Console.println("(4) Close"); - Console.println("(5) Back To Main Menu"); - - String input = Console.getInput(); + String header = account.getClass().getName() + " Account #" + account.getAcctNum().toString() + " Balance: $" + String.format("%.2f", account.getBalance()); + String input = Console.getInput(header, new String[] {"View Transaction History", "Deposit", "Withdraw", "Close Account", "Back to Main Menu" }); - switch (input) { - case "5": - inAccountMenu = false; - break; - } + switch (input) { + case "5": + break; } } diff --git a/src/main/java/Console.java b/src/main/java/Console.java index 706690c..5a76fd0 100644 --- a/src/main/java/Console.java +++ b/src/main/java/Console.java @@ -26,8 +26,8 @@ public static String getInput() { return input; } - public static String getInput2() { - Console.print("> "); + public static String getInput(String prompt) { + Console.print(prompt); Scanner scanner = new Scanner(System.in); String input = scanner.nextLine().toLowerCase(); //get input from user @@ -54,7 +54,7 @@ public static String getInput(String[] options) { rows[i] += "\n"; } - for (int i = numRows - 1; i >= 0; i--) { + for (int i = 0; i < numRows; i++) { output += rows[i]; } @@ -83,7 +83,7 @@ public static String getInput(String header, String[] options) { rows[i] += "\n"; } - for (int i = numRows - 1; i >= 0; i--) { + for (int i = 0; i < numRows; i++) { output += rows[i]; } @@ -102,12 +102,26 @@ static Boolean currencyCheck(String input) { } public static Double getCurrency() { - String input = getInput(); + String input = getInput("$"); while (true) { if (currencyCheck(input)) break; else { println("Enter a number"); - input = getInput(); + input = getInput("$"); + } + } + return Double.valueOf(input); + } + + public static Double getCurrency(String prompt) { + Console.print(prompt); + String input = getInput("$"); + while (true) { + if (currencyCheck(input)) break; + else { + println("Enter a valid number"); + Console.print(prompt); + input = getInput("$"); } } return Double.valueOf(input); diff --git a/src/test/java/ConsoleTest.java b/src/test/java/ConsoleTest.java index 3c30280..1aabc08 100644 --- a/src/test/java/ConsoleTest.java +++ b/src/test/java/ConsoleTest.java @@ -14,7 +14,6 @@ public void print() { @Test public void println() { - } @Test From 1c6be2661382d6aae5e786fc8c08c22d4e4443c9 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Sun, 27 Oct 2019 20:54:31 -0400 Subject: [PATCH 63/82] more menu updates --- src/main/java/ATM.java | 98 ++++++++++++++++++-------------------- src/main/java/Console.java | 13 +++-- 2 files changed, 55 insertions(+), 56 deletions(-) diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index 9eaf55e..2b07113 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -52,34 +52,30 @@ public void loadDBs() { public void authenticate() { //Read User's card Console.println("Card Number:"); - String cardNum = Console.getInput(); + int cardNum = Console.getInteger(); // find user in DB - String[] userInfo = this.getUserInfoByCardNum(Integer.parseInt(cardNum)); + String[] userInfo = this.getUserInfoByCardNum(cardNum); // check PW - Console.println("Enter Password"); - String password = Console.getInput(); + String password = Console.getInput("Enter Password: "); if(password.equals(userInfo[4])) { // 0: ID 1: Last Name 2: First Name 3: cardNum 4: PW currentUser = new User(userInfo[2], userInfo[1], userInfo[4], Integer.parseInt(userInfo[0]), Integer.parseInt(userInfo[3])); + } else { + this.authenticate(); } } // add new user - called by getUser() public User newUser() { - Console.println("Enter Your First Name"); - String firstName = Console.getInput(); - - Console.println("Enter Your Last Name"); - String lastName = Console.getInput(); - - Console.println("Enter Your Password"); - String password = Console.getInput(); + String firstName = Console.getInput("Enter Your First Name: "); + String lastName = Console.getInput("Enter Your Last Name: "); + String password = Console.getInput("Choose Your Password: "); Integer cardNumber = User.genCardNum(); - Console.println("Your Card NUmber is " + cardNumber); + Console.println("Your Card Number: " + cardNumber + "\n"); Integer userID = (int) (Math.random() * 1000); @@ -93,52 +89,51 @@ public User newUser() { // log in user - don't return until you do public void getUser() { - Console.println("(1) login\n(2) Create Account"); - String input = Console.getInput(); - if(input.equals("1")) { //Try login - this.authenticate(); - if (this.currentUser == null) { - return; - } - } else if (input.equals("2")) { //Create User - this.newUser(); - } else { - //error + String header = "Welcome to ZipCode National Bank"; + String input = Console.getInput(header, new String[] {"Insert Card", "Open an Account"}); + + switch (input) { + case "1": + this.authenticate(); + if (this.currentUser == null) { + return; + } + break; + case "2": + this.newUser(); + break; } } // deal with the user's choices public void userMenu() { - ArrayList usrAccts = getAccountsForUser(currentUser); - boolean loggedIn = true; - while (loggedIn) { - Console.println("(1) Logout"); - Console.println("(2) Transaction History"); - Console.println("(3) Add Account"); - - for (int i = 0; i < usrAccts.size(); i++) { - Console.println("(" + (4 + i) + ") " + usrAccts.get(i).getClass().getName() + " " + usrAccts.get(i).acctNum); - } + String header = "ZCNB Main Menu"; - Integer input = Console.getInteger(); - - switch (input) { - case 1: - loggedIn = false; - break; - case 2: - //print transaction hist - break; - case 3: - Double deposit = Console.getCurrency("Initial deposit amount for this account: "); - addAccount(usrAccts, deposit); - break; - default: - accountMenu(usrAccts.get(input - 4)); - break; - } + ArrayList choices = new ArrayList<>(); + choices.add("Transaction History"); + choices.add("Add Account"); + + String nextAcctChoice; + ArrayList usrAccts = getAccountsForUser(currentUser); + for (int i = 0; i < usrAccts.size(); i++) { + nextAcctChoice = String.format("%s %d",1 usrAccts.get(i).getClass().getName(), usrAccts.get(i).acctNum); + choices.add(nextAcctChoice); } + choices.add("Log Out"); + + String input = Console.getInput(header, choices.toArray(new String[choices.size()])); + + if (input.equals(Integer.toString(choices.size()))) { + // log out + } else if (input.equals("1")) { + //print transaction hist + } else if (input.equals("2")) { + Double deposit = Console.getCurrency("Initial deposit amount for this account: "); + addAccount(usrAccts, deposit); + } else { + accountMenu(usrAccts.get(Integer.parseInt(input) - 3)); + } } public void addAccount(ArrayList usrAccounts, Double deposit) { @@ -166,7 +161,6 @@ public void addAccount(ArrayList usrAccounts, Double deposit) { newAccount = new Investment(deposit, this.currentUser.getUserID(), (int)(Math.random()*1000), risk); this.saveAccountToDB(newAccount); usrAccounts.add(newAccount); - //usrAccounts.add(new Investment(0.0, currentUser.getUserID(), (int)(Math.random()*1000))); break; case "4": break; diff --git a/src/main/java/Console.java b/src/main/java/Console.java index 5a76fd0..85748d2 100644 --- a/src/main/java/Console.java +++ b/src/main/java/Console.java @@ -9,6 +9,12 @@ */ public class Console { + public static void clearScreen() { + for (int i = 0; i <100; i++) { + Console.println(" "); + } + } + public static void print(String output, Object... args) { System.out.printf(output, args); } @@ -35,10 +41,10 @@ public static String getInput(String prompt) { return input; } + public static String getInput(String[] options) { - System.out.print("\033[H\033[2J"); - System.out.flush(); + Console.clearScreen(); int numOptions = options.length; int numRows = (numOptions+1) >> 1; // this is how the cool kids divide by two @@ -66,8 +72,7 @@ public static String getInput(String[] options) { public static String getInput(String header, String[] options) { - System.out.print("\033[H\033[2J"); - System.out.flush(); + Console.clearScreen(); int numOptions = options.length; int numRows = (numOptions+1) >> 1; // this is how the cool kids divide by two From 059f63cb15142ff0e4bb401260eb356a975dcaab Mon Sep 17 00:00:00 2001 From: deltagphys Date: Mon, 28 Oct 2019 08:22:32 -0400 Subject: [PATCH 64/82] account display stuff --- src/main/java/ATM.java | 13 ++++++++++--- src/main/java/DB.java | 4 +++- src/main/java/Savings.java | 3 +++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index 2b07113..4e59a29 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -56,7 +56,9 @@ public void authenticate() { // find user in DB String[] userInfo = this.getUserInfoByCardNum(cardNum); - + if (userInfo == null){ + this.authenticate(); + } // check PW String password = Console.getInput("Enter Password: "); if(password.equals(userInfo[4])) { @@ -116,7 +118,7 @@ public void userMenu() { String nextAcctChoice; ArrayList usrAccts = getAccountsForUser(currentUser); for (int i = 0; i < usrAccts.size(); i++) { - nextAcctChoice = String.format("%s %d",1 usrAccts.get(i).getClass().getName(), usrAccts.get(i).acctNum); + nextAcctChoice = String.format("%s #%d ($%,.2f)", usrAccts.get(i).getClass().getName(), usrAccts.get(i).getAcctNum(), usrAccts.get(i).getBalance()); choices.add(nextAcctChoice); } @@ -170,7 +172,12 @@ public void addAccount(ArrayList usrAccounts, Double deposit) { } public void accountMenu(Account account) { - String header = account.getClass().getName() + " Account #" + account.getAcctNum().toString() + " Balance: $" + String.format("%.2f", account.getBalance()); + String header = account.getClass().getName() + " Account #" + account.getAcctNum().toString() + " Balance: $" + String.format("%,.2f", account.getBalance()); + if (account instanceof Savings) { + header += " Interest Rate: " + String.format("%.2f", ((Savings) account).getInterestRate())+"%%"; + } else if (account instanceof Investment) { + header += " Risk: " + String.format("%d", Math.round(100*((Investment) account).getRisk()))+"/10"; + } String input = Console.getInput(header, new String[] {"View Transaction History", "Deposit", "Withdraw", "Close Account", "Back to Main Menu" }); switch (input) { diff --git a/src/main/java/DB.java b/src/main/java/DB.java index b157672..853aac7 100644 --- a/src/main/java/DB.java +++ b/src/main/java/DB.java @@ -234,9 +234,11 @@ public void deleteRow(int rowNum) { */ public String[] readRow(int rowNum) { ArrayList records = null; - if (!this.deleted && rowNum < length()) { + if (!this.deleted && rowNum < length() && rowNum >= 0) { records = readAllRows(); return records.get(rowNum); + } else if (rowNum == -1) { + return null; } return new String[this.rowLength]; } diff --git a/src/main/java/Savings.java b/src/main/java/Savings.java index 1152299..bbf1649 100644 --- a/src/main/java/Savings.java +++ b/src/main/java/Savings.java @@ -7,6 +7,9 @@ public Savings(Double balance, Integer ownerID, Integer acctNum, Double interest this.interestRate = interestRate; } + public Double getInterestRate() { + return this.interestRate; + } public void calcInterestReturned(){ balance += (interestRate * balance); From e8b1d8d9cddf2b36b41078806b6cc088adab647e Mon Sep 17 00:00:00 2001 From: deltagphys Date: Mon, 28 Oct 2019 09:00:47 -0400 Subject: [PATCH 65/82] formatting --- src/main/java/Console.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/Console.java b/src/main/java/Console.java index 85748d2..2d3e571 100644 --- a/src/main/java/Console.java +++ b/src/main/java/Console.java @@ -76,14 +76,14 @@ public static String getInput(String header, String[] options) { int numOptions = options.length; int numRows = (numOptions+1) >> 1; // this is how the cool kids divide by two - String output = StringUtils.center(header,66) + "\n\n"; + String output = StringUtils.center(header,86) + "\n\n"; String[] rows = new String[numRows]; for (int i = 0; i < numRows; i++){ - rows[i] = String.format("%d | %-30s", 2*i+1, options[2*i]); + rows[i] = String.format("%d | %-40s", 2*i+1, options[2*i]); if (2*i + 1 < numOptions) { - rows[i] += String.format("%30s | %d", options[2*i + 1], 2*(i+1)); + rows[i] += String.format("%40s | %d", options[2*i + 1], 2*(i+1)); } rows[i] += "\n"; } From 7ef35fb53eb44a4a0f205a28cffc0555463bad08 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Mon, 28 Oct 2019 15:08:08 -0400 Subject: [PATCH 66/82] Transaction History for User --- src/main/java/ATM.java | 73 +++++++++++++++++++++++++++++++++- src/main/java/Console.java | 23 +++++++++++ src/main/java/Transaction.java | 2 +- src/test/java/ATMTest.java | 8 ++++ src/test/java/ConsoleTest.java | 3 ++ 5 files changed, 107 insertions(+), 2 deletions(-) diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index 4e59a29..024d24b 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -1,5 +1,8 @@ import java.io.IOException; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; public class ATM { @@ -129,7 +132,7 @@ public void userMenu() { if (input.equals(Integer.toString(choices.size()))) { // log out } else if (input.equals("1")) { - //print transaction hist + Console.outputTransactionsWithHeader("Transaction History", getTransactionsForUser(this.currentUser)); } else if (input.equals("2")) { Double deposit = Console.getCurrency("Initial deposit amount for this account: "); addAccount(usrAccts, deposit); @@ -142,12 +145,17 @@ public void addAccount(ArrayList usrAccounts, Double deposit) { String header = "Choose Account Type:"; String input = Console.getInput(header, new String[] {"Checking", "Savings", "Investment", "Back to Main Menu" }); Account newAccount; + Transaction transaction; + switch (input) { case "1": newAccount = new Checking(deposit, this.currentUser.getUserID(), (int)(Math.random()*1000)); this.saveAccountToDB(newAccount); usrAccounts.add(newAccount); + + transaction = new Transaction(deposit, new Date(), newAccount.getAcctNum(), "Opened account", true); + saveTransactionToDB(transaction); break; case "2": Double interestRate = .01 * (1 + Math.floor(deposit/1000)); @@ -155,6 +163,9 @@ public void addAccount(ArrayList usrAccounts, Double deposit) { newAccount = new Savings(deposit, this.currentUser.getUserID(), (int)(Math.random()*1000), interestRate); this.saveAccountToDB(newAccount); usrAccounts.add(newAccount); + + transaction = new Transaction(deposit, new Date(), newAccount.getAcctNum(), "Opened account", true); + saveTransactionToDB(transaction); break; case "3": Console.print("On a scale of 1-10, enter your risk tolerance "); @@ -163,6 +174,9 @@ public void addAccount(ArrayList usrAccounts, Double deposit) { newAccount = new Investment(deposit, this.currentUser.getUserID(), (int)(Math.random()*1000), risk); this.saveAccountToDB(newAccount); usrAccounts.add(newAccount); + + transaction = new Transaction(deposit, new Date(), newAccount.getAcctNum(), "Opened account", true); + saveTransactionToDB(transaction); break; case "4": break; @@ -181,6 +195,7 @@ public void accountMenu(Account account) { String input = Console.getInput(header, new String[] {"View Transaction History", "Deposit", "Withdraw", "Close Account", "Back to Main Menu" }); switch (input) { + case "5": break; } @@ -192,6 +207,8 @@ public void serviceLoop() { // only returns null if the magic secret exit code is called getUser(); + //applyInterest(); + //calculateReturns(); loadDBs(); @@ -224,6 +241,14 @@ public void saveDBs() { // } } +// public void showTransactions(Transaction[] transactions) { +// String[][] rows = new String[5][transactions.length]; +// for (int i = 0; i < transactions.length; i++) { +// rows[i] = transactions[i].toStringArray(); +// } +// Console.outputTransactionsWithHeader("Transaction History", rows); +// } + /* /////////////////////////////////////////////////////////////////////////////////////////////////////////////// * DB interaction methods for the ATM @@ -343,11 +368,57 @@ public void saveAccountToDB(Account account) { } } + public int[] getTransactionRowsByUser (User user) { + int[] accountRows = getAccountRowsByUser(user); + ArrayList accountNums = new ArrayList<>(); + for (int row : accountRows) { + accountNums.add(Integer.parseInt(getAccountInfoByRow(row)[0])); + } + + int [] recordRowNums = null; + for (int accountNum : accountNums) { + recordRowNums = this.transactionDB.findPartialRowMultiple(new String[]{Integer.toString(accountNum)}, new int[]{1}); + } + + return recordRowNums; + } + + // get string array representation of one transaction + public String[] getTransactionInfoByRow (int rowNum) { + return this.transactionDB.readRow(rowNum); + } + + public ArrayList getTransactionsForUser(User user) { + int[] rows = getTransactionRowsByUser(user); + ArrayList transactions = new ArrayList<>(); + String[] info = new String[5]; + for (int row : rows) { + info = getTransactionInfoByRow(row); + try { + transactions.add(new Transaction( + Double.parseDouble(info[2]), + new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(info[3]), + Integer.parseInt(info[1]), + info[4], + info[0] == "credit")); + } catch (ParseException e) { + e.printStackTrace(); + } + } + + return transactions; + } + public void savePendingTransactionsToDB(ArrayList pendingTransactions) { for (Transaction transaction : pendingTransactions) { this.transactionDB.addRow(transaction.toStringArray()); } } + + public void saveTransactionToDB(Transaction transaction) { + this.transactionDB.addRow(transaction.toStringArray()); + } + /* /////////////////////////////////////////////////////////////////////////////////////////////////////////////// * End DB interaction methods for the ATM */ /////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/main/java/Console.java b/src/main/java/Console.java index 2d3e571..a791b97 100644 --- a/src/main/java/Console.java +++ b/src/main/java/Console.java @@ -1,7 +1,10 @@ import org.apache.commons.lang3.StringUtils; import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Date; import java.util.Scanner; /** @@ -98,6 +101,26 @@ public static String getInput(String header, String[] options) { } + public static void outputTransactionsWithHeader(String header, ArrayList transactions) { + + Console.clearScreen(); + + String output = StringUtils.center(header,86) + "\n\n"; + String[] row; + for (Transaction transaction : transactions) { + row = transaction.toStringArray(); + output += (String.format("%-8s", row[0]) + + String.format("%-10s", row[1]) + + String.format("%-10s", row[2]) + + String.format("%-20s", row[3]) + + String.format(" %s", row[4]) + ); + } + + println(output); + getInput("\nPress Enter"); + } + static Boolean integerCheck(String input) { return input.matches("^\\d+$"); } diff --git a/src/main/java/Transaction.java b/src/main/java/Transaction.java index 6ed8513..1f8b8ea 100644 --- a/src/main/java/Transaction.java +++ b/src/main/java/Transaction.java @@ -7,7 +7,7 @@ public class Transaction implements Storeable { private Integer accountID; private String description; private Boolean isCredit; - + // 0: credit/debit 1: accountID 2: amount (signed) 3: timeStamp 4: description public Transaction(Double amount, Date timeStamp, Integer accountID, String description, Boolean isCredit) { this.amount = amount; this.timeStamp = timeStamp; diff --git a/src/test/java/ATMTest.java b/src/test/java/ATMTest.java index 2d2a5f6..95efc72 100644 --- a/src/test/java/ATMTest.java +++ b/src/test/java/ATMTest.java @@ -537,6 +537,14 @@ public void saveAccountToDBTest() { } +// @Test +// public void transactionHistoryShowTest() { +// DB transactionDB = new DB("transactions.csv"); +// +// +// atm.showTransactions(); +// } + // convenience methods for dev environment to clear the DBs - only called from the IDE manually // @Test // public void clearUserDB() { diff --git a/src/test/java/ConsoleTest.java b/src/test/java/ConsoleTest.java index 1aabc08..aa82a13 100644 --- a/src/test/java/ConsoleTest.java +++ b/src/test/java/ConsoleTest.java @@ -4,6 +4,8 @@ import org.junit.Test; import org.junit.runner.RunWith; +import java.util.Date; + import static org.junit.Assert.*; @RunWith(JUnitParamsRunner.class) public class ConsoleTest { @@ -44,4 +46,5 @@ public void currencyCheckTest(String input, Boolean valid) { public void integerCheckTest(String input, Boolean valid) { Assert.assertTrue(valid == Console.integerCheck(input)); } + } From 849691e49656ed90fb6bc73770574c95dd9083d5 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Mon, 28 Oct 2019 15:20:33 -0400 Subject: [PATCH 67/82] transaction history for individual accounts --- src/main/java/ATM.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index 024d24b..0a9bcc7 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -195,7 +195,9 @@ public void accountMenu(Account account) { String input = Console.getInput(header, new String[] {"View Transaction History", "Deposit", "Withdraw", "Close Account", "Back to Main Menu" }); switch (input) { - + case "1": + Console.outputTransactionsWithHeader("Transaction History", getTransactionsForAccount(account)); + break; case "5": break; } @@ -383,13 +385,23 @@ public int[] getTransactionRowsByUser (User user) { return recordRowNums; } + public int[] getTransactionRowsByAccount (Account account) { + return this.transactionDB.findPartialRowMultiple(new String[]{Integer.toString(account.getAcctNum())}, new int[]{1}); + } + // get string array representation of one transaction public String[] getTransactionInfoByRow (int rowNum) { return this.transactionDB.readRow(rowNum); } public ArrayList getTransactionsForUser(User user) { - int[] rows = getTransactionRowsByUser(user); + return getTransactionsForRows(getTransactionRowsByUser(user)); + } + + public ArrayList getTransactionsForAccount(Account account) { + return getTransactionsForRows(getTransactionRowsByAccount(account)); + } + public ArrayList getTransactionsForRows(int[] rows) { ArrayList transactions = new ArrayList<>(); String[] info = new String[5]; for (int row : rows) { @@ -409,6 +421,8 @@ public ArrayList getTransactionsForUser(User user) { return transactions; } + + public void savePendingTransactionsToDB(ArrayList pendingTransactions) { for (Transaction transaction : pendingTransactions) { this.transactionDB.addRow(transaction.toStringArray()); From fbbe32bc0f2ff3039bd02fa388bf4ba657694686 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Mon, 28 Oct 2019 15:25:50 -0400 Subject: [PATCH 68/82] interest stub --- src/main/java/ATM.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index 0a9bcc7..8bed57d 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -209,7 +209,7 @@ public void serviceLoop() { // only returns null if the magic secret exit code is called getUser(); - //applyInterest(); + applyInterest(); //calculateReturns(); loadDBs(); @@ -221,6 +221,10 @@ public void serviceLoop() { serviceLoop(); } + public void applyInterest() { + + } + // log out user public void logOut() { saveDBs(); From 35f441c1d32a0082a68bcf54dffbfb6acb60667d Mon Sep 17 00:00:00 2001 From: deltagphys Date: Mon, 28 Oct 2019 16:23:13 -0400 Subject: [PATCH 69/82] investment returns and interest --- src/main/java/ATM.java | 35 +++++++++++++++++++++++++++++++++- src/main/java/Account.java | 16 +++++++--------- src/main/java/Console.java | 2 +- src/main/java/Investment.java | 9 --------- src/main/java/Savings.java | 4 ---- src/test/java/AccountTest.java | 28 +++++++++++++-------------- 6 files changed, 56 insertions(+), 38 deletions(-) diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index 8bed57d..38fda0a 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -210,7 +210,7 @@ public void serviceLoop() { getUser(); applyInterest(); - //calculateReturns(); + applyReturns(); loadDBs(); @@ -222,9 +222,42 @@ public void serviceLoop() { } public void applyInterest() { + ArrayList userAccounts = getAccountsForUser(this.currentUser); + for (Account account : userAccounts) { + if (account instanceof Savings) { + calcInterest(account); + } + } + } + + public void calcInterest(Account account) { + Double interest = ((Savings) account).getInterestRate() * account.getBalance()/100; + account.deposit(interest); + saveAccountToDB(account); + Transaction transaction = new Transaction(Double.parseDouble(String.format("%.2f",interest)), new Date(), account.getAcctNum(), "Interest earned", true); + saveTransactionToDB(transaction); + } + public void applyReturns() { + ArrayList userAccounts = getAccountsForUser(this.currentUser); + for (Account account : userAccounts) { + if (account instanceof Investment) { + calcReturns(account); + } + } } + public void calcReturns(Account account) { + Double multiplier = ((Investment) account).getRisk() * (2 * Math.random() - .8); + Double earnings = Math.round((multiplier * account.getBalance()*100d))/100d; + account.deposit(earnings); + saveAccountToDB(account); + Boolean isCredit = (earnings > 0); + Transaction transaction = new Transaction(Double.parseDouble(String.format("%.2f",earnings)), new Date(), account.getAcctNum(), "Investment returns", isCredit); + saveTransactionToDB(transaction); + } + + // log out user public void logOut() { saveDBs(); diff --git a/src/main/java/Account.java b/src/main/java/Account.java index d481a03..96747eb 100644 --- a/src/main/java/Account.java +++ b/src/main/java/Account.java @@ -14,27 +14,25 @@ public Double getBalance(){ } public Integer getOwnerID() { - return ownerID; + return this.ownerID; } public Integer getAcctNum() { - return acctNum; + return this.acctNum; } public void deposit(Double amount){ - balance += amount; - + this.balance += amount; + String bal = String.format("%.2f",this.balance); + this.balance = Double.parseDouble(bal); } public void withdraw(Double amount){ - if (balance > amount) { - balance -= amount; + if (this.balance > amount) { + this.balance -= amount; } } - public void getAcctHist(){ - - } public Boolean equals(Account account) { return DB.serialize(this.toStringArray()).equals(DB.serialize(account.toStringArray())); diff --git a/src/main/java/Console.java b/src/main/java/Console.java index a791b97..ac56185 100644 --- a/src/main/java/Console.java +++ b/src/main/java/Console.java @@ -114,7 +114,7 @@ public static void outputTransactionsWithHeader(String header, ArrayList= actual); - } +// Investment account = new Investment(8000.0, 3,3, .09); +// +// Double actual; +// +// for (int i = 0; i < 1000; i++) { +// actual = account.calcReturn(); +// Double minExpected = account.getBalance() - .8 * account.getRisk() * account.getBalance(); +// assertTrue(minExpected <= actual); +// +// actual = account.calcReturn(); +// Double maxExpected = account.getBalance() + 1.2 * account.getRisk() * account.getBalance(); +// assertTrue(maxExpected >= actual); +// } } @@ -117,7 +117,7 @@ public void calcInterestReturned() { Savings account = new Savings(80000.0, 3,3, 0.002); Double expected = 80160.0; - account.calcInterestReturned(); +// account.calcInterestReturned(); // Then Double actual = account.getBalance(); From a9fe31bb968016cd153ec0013f5e6b34840eef82 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Mon, 28 Oct 2019 16:45:08 -0400 Subject: [PATCH 70/82] deposit and withdraw --- src/main/java/ATM.java | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index 38fda0a..2292385 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -192,12 +192,33 @@ public void accountMenu(Account account) { } else if (account instanceof Investment) { header += " Risk: " + String.format("%d", Math.round(100*((Investment) account).getRisk()))+"/10"; } - String input = Console.getInput(header, new String[] {"View Transaction History", "Deposit", "Withdraw", "Close Account", "Back to Main Menu" }); + String input = Console.getInput(header, new String[] {"View Transaction History", "Deposit", "Withdrawal", "Close Account", "Back to Main Menu" }); + Double deposit; + Transaction transaction; switch (input) { case "1": Console.outputTransactionsWithHeader("Transaction History", getTransactionsForAccount(account)); break; + case "2": + deposit = Console.getCurrency("Deposit amount: "); + account.deposit(deposit); + saveAccountToDB(account); + transaction = new Transaction(deposit, new Date(), account.getAcctNum(), "ATM deposit", true); + saveTransactionToDB(transaction); + break; + case "3": + deposit = Console.getCurrency("Withdrawal amount: "); + if (deposit <= account.getBalance()) { + account.deposit(-1 * deposit); + saveAccountToDB(account); + transaction = new Transaction(deposit, new Date(), account.getAcctNum(), "ATM withdrawal", false); + saveTransactionToDB(transaction); + } else { + Console.println("Insufficient funds"); + Console.getInput("\nPress Enter"); + } + break; case "5": break; } From 94f92ef795873d01d4e576511a5aefcfc3053f04 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Mon, 28 Oct 2019 19:39:04 -0400 Subject: [PATCH 71/82] menu loops pretty well --- src/main/java/ATM.java | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index 2292385..0941a1d 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -130,7 +130,7 @@ public void userMenu() { String input = Console.getInput(header, choices.toArray(new String[choices.size()])); if (input.equals(Integer.toString(choices.size()))) { - // log out + serviceLoop(); //not ... great, but it'll do for now } else if (input.equals("1")) { Console.outputTransactionsWithHeader("Transaction History", getTransactionsForUser(this.currentUser)); } else if (input.equals("2")) { @@ -139,6 +139,8 @@ public void userMenu() { } else { accountMenu(usrAccts.get(Integer.parseInt(input) - 3)); } + + userMenu(); } public void addAccount(ArrayList usrAccounts, Double deposit) { @@ -219,6 +221,18 @@ public void accountMenu(Account account) { Console.getInput("\nPress Enter"); } break; + case "4": + + if (account.getBalance() == 0) { + + saveAccountToDB(account); + transaction = new Transaction(0.0, new Date(), account.getAcctNum(), "Account Closed", false); + saveTransactionToDB(transaction); + } else { + Console.println("Account still contains funds. Withdraw or transfer all funds before closing."); + Console.getInput("\nPress Enter"); + } + break; case "5": break; } @@ -435,12 +449,28 @@ public int[] getTransactionRowsByUser (User user) { accountNums.add(Integer.parseInt(getAccountInfoByRow(row)[0])); } - int [] recordRowNums = null; - for (int accountNum : accountNums) { - recordRowNums = this.transactionDB.findPartialRowMultiple(new String[]{Integer.toString(accountNum)}, new int[]{1}); + ArrayList rows = new ArrayList<>(); +// int [] recordRowNums = null; +// for (int accountNum : accountNums) { +// recordRowNums = this.transactionDB.findPartialRowMultiple(new String[]{Integer.toString(accountNum)}, new int[]{1}); +// +// } + ArrayList transData = transactionDB.readAllRows(); + + for (int i = 0; i < transData.size(); i++) { + for (int acctNum : accountNums) { + if ((int) Integer.parseInt(transData.get(i)[1]) == acctNum) { + rows.add(i); + } + } } - return recordRowNums; + int[] results = new int[rows.size()]; + for (int i = 0; i < rows.size(); i++) { + results[i] = rows.get(i); + } + + return results; } public int[] getTransactionRowsByAccount (Account account) { From 47312a97676e6ae67b791143ecd74ac535bd2e67 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Mon, 28 Oct 2019 19:44:05 -0400 Subject: [PATCH 72/82] close accounts --- src/main/java/ATM.java | 14 +++++++++++++- src/main/java/Transaction.java | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index 0941a1d..ce84aaa 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -225,7 +225,7 @@ public void accountMenu(Account account) { if (account.getBalance() == 0) { - saveAccountToDB(account); + deleteAccountFromDB(account); transaction = new Transaction(0.0, new Date(), account.getAcctNum(), "Account Closed", false); saveTransactionToDB(transaction); } else { @@ -442,6 +442,18 @@ public void saveAccountToDB(Account account) { } } + public void deleteAccountFromDB(Account account) { + String[] stringRepOfAccount = account.toStringArray(); + int accountNum = account.getAcctNum(); + int rowNum = getAccountRowByID(accountNum); + if (rowNum == -1) { // account isn't in DB yet + this.accountDB.addRow(stringRepOfAccount); + return; + } else { // update a found row + this.accountDB.deleteRow(rowNum); + } + } + public int[] getTransactionRowsByUser (User user) { int[] accountRows = getAccountRowsByUser(user); ArrayList accountNums = new ArrayList<>(); diff --git a/src/main/java/Transaction.java b/src/main/java/Transaction.java index 1f8b8ea..1675c5f 100644 --- a/src/main/java/Transaction.java +++ b/src/main/java/Transaction.java @@ -33,7 +33,7 @@ public String[] toStringArray() { String[] result = new String[] { type, this.accountID.toString(), - this.amount.toString(), + String.format("%.2f",this.amount), this.timeStamp.toString(), this.description }; From 475e64eb83ee97b0e4f83e234c6aec79bd513f4c Mon Sep 17 00:00:00 2001 From: Ryan Date: Tue, 29 Oct 2019 09:54:18 -0400 Subject: [PATCH 73/82] merge --- src/main/java/User.java | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/main/java/User.java b/src/main/java/User.java index 7bd282b..6955094 100644 --- a/src/main/java/User.java +++ b/src/main/java/User.java @@ -7,7 +7,6 @@ public class User implements Storeable { private String password; private Integer userID; private Integer cardNumber; - private ArrayList accounts; private ArrayList pendingTransactions; public User(String firstName, String lastName, String password, Integer userID, Integer cardNumber) { @@ -16,8 +15,6 @@ public User(String firstName, String lastName, String password, Integer userID, this.password = password; this.userID = userID; this.cardNumber = cardNumber; - - this.accounts = null; } public static Integer genCardNum() { @@ -47,10 +44,6 @@ public Integer getUserID() { return userID; } - public ArrayList getAccounts() { - return accounts; - } - public ArrayList getPendingTransactions() { return pendingTransactions; } From 9033da772f022c2a4a1743fddd78020cd9af7129 Mon Sep 17 00:00:00 2001 From: deltaGPhys Date: Tue, 29 Oct 2019 13:14:42 -0400 Subject: [PATCH 74/82] readme info --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 92d73b7..637788a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,13 @@ # project-2-ATM Week 2 project: ATM Simulator +## Notes for Use +- The DB class defines database objects and a number of attendant methods to delete, search, modify, and add rows in the database +- Information is stored in csv files in the /data folder. An example data set is included and will be run when you run `main()`. Any changes to accounts, users, or additional transactions will be saved there. These files are in the `.gitignore`, so any changes you make locally wouldn't overwrite them +- One example user, for convenience of entry during testing, has card number 1 and password 1234 +- There are also a couple of test database files (`test.db` and `testbad.csv` which are used in certain tests. Other tests create and destory temporary database files + + ## ATM Requirements Every feature must have corresponding unit tests From 1f67276eda61a9dfbf84860f7308b2edd4507b92 Mon Sep 17 00:00:00 2001 From: Ryan Date: Tue, 29 Oct 2019 13:15:10 -0400 Subject: [PATCH 75/82] Transfer working --- src/main/java/ATM.java | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index ce84aaa..8658b2c 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -194,7 +194,7 @@ public void accountMenu(Account account) { } else if (account instanceof Investment) { header += " Risk: " + String.format("%d", Math.round(100*((Investment) account).getRisk()))+"/10"; } - String input = Console.getInput(header, new String[] {"View Transaction History", "Deposit", "Withdrawal", "Close Account", "Back to Main Menu" }); + String input = Console.getInput(header, new String[] {"View Transaction History", "Deposit", "Withdrawal", "Close Account", "Transfer", "Back to Main Menu" }); Double deposit; Transaction transaction; @@ -234,6 +234,31 @@ public void accountMenu(Account account) { } break; case "5": + + Console.println("Number of Account to transfer to"); + int ActToTransferTo = Console.getInteger(); + String[] actInfo = getAccountInfoByID(ActToTransferTo); + // 0: accountID 1: ownerID 2: balance 3: type 4: risk/interest/null (type-dependent) + Account act = getAccountByInfo(actInfo); + deposit = Console.getCurrency("Transfer amount"); + + if(deposit < account.getBalance()) { + account.deposit(-1 * deposit); + act.deposit(deposit); + + saveAccountToDB(account); + transaction = new Transaction(-1 * deposit, new Date(), account.getAcctNum(), "ATM Transfer", false); + saveTransactionToDB(transaction); + + saveAccountToDB(act); + transaction = new Transaction(deposit, new Date(), act.getAcctNum(), "ATM Transfer", true); + saveTransactionToDB(transaction); + } else { + Console.println("Insufficient funds in account"); + } + + break; + case "6": break; } } From 9b5dd0630722a860e4c6bfff33eaf0f5f406c991 Mon Sep 17 00:00:00 2001 From: deltaGPhys Date: Tue, 29 Oct 2019 13:22:42 -0400 Subject: [PATCH 76/82] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 637788a..faffebc 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ Week 2 project: ATM Simulator - Information is stored in csv files in the /data folder. An example data set is included and will be run when you run `main()`. Any changes to accounts, users, or additional transactions will be saved there. These files are in the `.gitignore`, so any changes you make locally wouldn't overwrite them - One example user, for convenience of entry during testing, has card number 1 and password 1234 - There are also a couple of test database files (`test.db` and `testbad.csv` which are used in certain tests. Other tests create and destory temporary database files +- Every time a user logs in, interest is earned on savings accounts and investments get returns, based on random chance and risk tolerance defined when creating the account ## ATM Requirements From 05d33d35cbd255f4371beb49b555b8e2d04829b4 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Tue, 29 Oct 2019 13:28:24 -0400 Subject: [PATCH 77/82] db info and final bug fix --- .gitignore | 2 +- data/accounts.csv | 3 +++ data/test.csv | 4 +++ data/testBad.csv | 4 +++ data/testaccountDB.csv | 0 data/testtransactionDB.csv | 0 data/testuserDB.csv | 0 data/transactions.csv | 11 ++++++++ data/users.csv | 4 +++ src/main/java/ATM.java | 2 +- src/test/java/AccountTest.java | 47 ---------------------------------- 11 files changed, 28 insertions(+), 49 deletions(-) create mode 100644 data/accounts.csv create mode 100644 data/test.csv create mode 100644 data/testBad.csv create mode 100644 data/testaccountDB.csv create mode 100644 data/testtransactionDB.csv create mode 100644 data/testuserDB.csv create mode 100644 data/transactions.csv create mode 100644 data/users.csv diff --git a/.gitignore b/.gitignore index c1960de..6fc9ec5 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,5 @@ target/ .project .classpath .settings -data/* + *.DS_Store \ No newline at end of file diff --git a/data/accounts.csv b/data/accounts.csv new file mode 100644 index 0000000..99d7add --- /dev/null +++ b/data/accounts.csv @@ -0,0 +1,3 @@ +"350","275","1320.0","Checking","" +"33","275","5560.36","Savings","0.06" +"2","275","53250.23","Investment","0.06" diff --git a/data/test.csv b/data/test.csv new file mode 100644 index 0000000..3d9f95c --- /dev/null +++ b/data/test.csv @@ -0,0 +1,4 @@ +"Item 1","Item 2","Item 3","Item 4" +"Item 1b","Item 2b","Item 3b","Item 4b" +"Item 1c","Item 2c","Item 3c","Item 4c" +"Item 1d","Item 2d","Item 3d","Item 4d" \ No newline at end of file diff --git a/data/testBad.csv b/data/testBad.csv new file mode 100644 index 0000000..4fb0122 --- /dev/null +++ b/data/testBad.csv @@ -0,0 +1,4 @@ +"Item 1","Item 2","Item 3","Item 4" +"Item 1b","Item 2b","Item 3b","Item 4b" +"Item 1c","Item 2c","Item 3c" +"Item 1d","Item 2d","Item 3d","Item 4d" \ No newline at end of file diff --git a/data/testaccountDB.csv b/data/testaccountDB.csv new file mode 100644 index 0000000..e69de29 diff --git a/data/testtransactionDB.csv b/data/testtransactionDB.csv new file mode 100644 index 0000000..e69de29 diff --git a/data/testuserDB.csv b/data/testuserDB.csv new file mode 100644 index 0000000..e69de29 diff --git a/data/transactions.csv b/data/transactions.csv new file mode 100644 index 0000000..831d694 --- /dev/null +++ b/data/transactions.csv @@ -0,0 +1,11 @@ +"credit","350","1200.00","Tue Oct 29 13:19:54 EDT 2019","Opened account" +"credit","33","5670.30","Tue Oct 29 13:20:08 EDT 2019","Opened account" +"credit","2","45607.30","Tue Oct 29 13:20:21 EDT 2019","Opened account" +"credit","33","3.40","Tue Oct 29 13:20:27 EDT 2019","Interest earned" +"credit","2","2788.62","Tue Oct 29 13:20:27 EDT 2019","Investment returns" +"debit","33","-120.00","Tue Oct 29 13:23:18 EDT 2019","ATM Transfer" +"credit","350","120.00","Tue Oct 29 13:23:18 EDT 2019","ATM Transfer" +"credit","33","3.33","Tue Oct 29 13:24:46 EDT 2019","Interest earned" +"credit","2","3303.34","Tue Oct 29 13:24:46 EDT 2019","Investment returns" +"credit","33","3.33","Tue Oct 29 13:27:00 EDT 2019","Interest earned" +"credit","2","1550.97","Tue Oct 29 13:27:00 EDT 2019","Investment returns" diff --git a/data/users.csv b/data/users.csv new file mode 100644 index 0000000..deae835 --- /dev/null +++ b/data/users.csv @@ -0,0 +1,4 @@ +"719","john","jimmy","90486264","blorp" +"606","jones","jim","84170304","jimmy" +"275","peasy","easy","1","1234" +"827","davis","john","16690334","12345" diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index 8658b2c..ce77c8f 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -537,7 +537,7 @@ public ArrayList getTransactionsForRows(int[] rows) { new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(info[3]), Integer.parseInt(info[1]), info[4], - info[0] == "credit")); + info[0].equals("credit"))); } catch (ParseException e) { e.printStackTrace(); } diff --git a/src/test/java/AccountTest.java b/src/test/java/AccountTest.java index ed24d97..de2c872 100644 --- a/src/test/java/AccountTest.java +++ b/src/test/java/AccountTest.java @@ -45,41 +45,6 @@ public void withdraw_test() { Double actual = account.getBalance(); assertEquals(expected, actual); } - @Test - public void withdraw_Overdraft_test() { - // Given - Account account = new Checking(0.0, 3,3); - Account account2 = new Checking(40.0, 2,2); - Double expected = 0.0; - - //When - if (account.getBalance() > 40.0){ - account.withdraw(40.0); - account2.deposit(40.0); - }else { - System.out.println("Not enough funds"); - } - // Then - Double actual = account2.getBalance(); - assertEquals(account2.getBalance(), account.getBalance()); - } - @Test - public void risk_test() { -// Investment account = new Investment(8000.0, 3,3, .09); -// -// Double actual; -// -// for (int i = 0; i < 1000; i++) { -// actual = account.calcReturn(); -// Double minExpected = account.getBalance() - .8 * account.getRisk() * account.getBalance(); -// assertTrue(minExpected <= actual); -// -// actual = account.calcReturn(); -// Double maxExpected = account.getBalance() + 1.2 * account.getRisk() * account.getBalance(); -// assertTrue(maxExpected >= actual); -// } - } - @Test @@ -111,18 +76,6 @@ public void getAcctNum() { } - @Test - public void calcInterestReturned() { - // Given - Savings account = new Savings(80000.0, 3,3, 0.002); - Double expected = 80160.0; - -// account.calcInterestReturned(); - - // Then - Double actual = account.getBalance(); - assertEquals(expected, actual); - } @Test public void setRisk() { From 55ce15ac2541aea32f18b434e0e79583e0a7ebd4 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Tue, 29 Oct 2019 13:30:05 -0400 Subject: [PATCH 78/82] gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 6fc9ec5..c1960de 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,5 @@ target/ .project .classpath .settings - +data/* *.DS_Store \ No newline at end of file From 1bab24fa0e39f85ae7733b845564b6e925851216 Mon Sep 17 00:00:00 2001 From: deltaGPhys Date: Tue, 29 Oct 2019 13:41:34 -0400 Subject: [PATCH 79/82] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index faffebc..f73f764 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ Week 2 project: ATM Simulator ## Notes for Use - The DB class defines database objects and a number of attendant methods to delete, search, modify, and add rows in the database - Information is stored in csv files in the /data folder. An example data set is included and will be run when you run `main()`. Any changes to accounts, users, or additional transactions will be saved there. These files are in the `.gitignore`, so any changes you make locally wouldn't overwrite them +- There's one path test that has a hardcoded path from dev, which wasn't a great choice, but the user's `pwd` is used in the actual method, so the file paths will be fine when you test - One example user, for convenience of entry during testing, has card number 1 and password 1234 - There are also a couple of test database files (`test.db` and `testbad.csv` which are used in certain tests. Other tests create and destory temporary database files - Every time a user logs in, interest is earned on savings accounts and investments get returns, based on random chance and risk tolerance defined when creating the account From f490b348a8028e5292c1d3248de33a9e1b1400c8 Mon Sep 17 00:00:00 2001 From: deltaGPhys Date: Tue, 29 Oct 2019 13:49:13 -0400 Subject: [PATCH 80/82] UML Diagram --- ATMUML.uml | 180 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 ATMUML.uml diff --git a/ATMUML.uml b/ATMUML.uml new file mode 100644 index 0000000..6f165a8 --- /dev/null +++ b/ATMUML.uml @@ -0,0 +1,180 @@ + + + JAVA + + + Account + Savings + User + Storeable + Checking + Console + Transaction + ATM + Main + DB + Investment + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Main + + + Fields + Methods + + All + private + + From f123d572ee0639b373ee4a4f739808dfe3527663 Mon Sep 17 00:00:00 2001 From: deltaGPhys Date: Tue, 29 Oct 2019 13:49:26 -0400 Subject: [PATCH 81/82] Delete .DS_Store --- .DS_Store | Bin 6148 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index bf72b90661b7c7fb2a910eecd2938fa56eb5634e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK%Wl&^6ur}g)-gf^38`IrgTx|KNubRFA*CT*bb*90f(4+~j-$lL@mR4#fDn`o z9|3>B4)F&p`2xNGwk%)|XC8_Yr)5_J)m&-j%*=g^=Uk7+Lqx1K3Ti}UBC=2f7Hw3= zh|r6&B5Qi&B9Ms~2NCc15%BP>+ImbWI8`p!TWNlul-8wiLym&czHGTb7<$>WtB}FWJQQo05UtrX$xg9=877XUP0dKeU z;w|UfPy74FPkW2L10MWQpY%_T{mdQ45s!O_C!TYI65=$VZF-J6%BANfEUqr3=Mt=k zuzyNj>L8uhsE#5MD%NX9_U_VagS#T$#RNFnx1i${qTqwx20f?!@HG zh@)p_`i8>v#e+H0oLEz#sSN{$fn^2?s#_N4|NhDM|79oBGYl98&J+Wz*mm13EJ>fO zD~l6nt%GueB1G8D6iOFV`Z$&iaTIT%NP*4~4PaB_Od&=f=0`x%U@F7FUuEDoh6Vox From 1e56d10c8a1e69a26cf23efcf094faa947172e72 Mon Sep 17 00:00:00 2001 From: deltagphys Date: Sat, 18 Jan 2020 15:44:57 -0500 Subject: [PATCH 82/82] . --- ATMUML.uml | 180 +++++++++++++++++++++++++++++++++++++++++ src/main/java/ATM.java | 1 + 2 files changed, 181 insertions(+) create mode 100644 ATMUML.uml diff --git a/ATMUML.uml b/ATMUML.uml new file mode 100644 index 0000000..6f165a8 --- /dev/null +++ b/ATMUML.uml @@ -0,0 +1,180 @@ + + + JAVA + + + Account + Savings + User + Storeable + Checking + Console + Transaction + ATM + Main + DB + Investment + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Main + + + Fields + Methods + + All + private + + diff --git a/src/main/java/ATM.java b/src/main/java/ATM.java index ce77c8f..433cd81 100644 --- a/src/main/java/ATM.java +++ b/src/main/java/ATM.java @@ -526,6 +526,7 @@ public ArrayList getTransactionsForUser(User user) { public ArrayList getTransactionsForAccount(Account account) { return getTransactionsForRows(getTransactionRowsByAccount(account)); } + public ArrayList getTransactionsForRows(int[] rows) { ArrayList transactions = new ArrayList<>(); String[] info = new String[5];