diff --git a/.DS_Store b/.DS_Store
new file mode 100644
index 0000000..5008ddf
Binary files /dev/null and b/.DS_Store differ
diff --git a/.gitignore b/.gitignore
index b99dc45..e98e493 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,6 +1,7 @@
.idea/*
*.iml
target/
+data/
.project
.classpath
.settings
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/README.md b/README.md
index c58ea5f..86a08a8 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,17 @@
# Access Control Lab - Bank Account
The original lab can be found [here](https://gist.github.com/DavidGinzberg/5ccd3191eed52b04c4c3541fa2b2cbf7)
+## 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 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
+- Interest rates go up and down by random amounts with a probability of 20%
+- Overdraft policies allow blocking such requests, allowing them, or attempting automatic transfer from another account
+- All of those changes are recorded as transactions
+- When closing an account, there is an option to transfer to another account, if the closing account isn't empty
+- Frozen accounts can't be viewed or modified, and don't earn interest/returns
+
## Description
This lab focuses on implementing a simulated bank account and practicing using access control features of the Java language. By the end of this lab students should feel comfortable setting class members to be private or public, creating accessor and mutator functions for fields as needed, and using those methods to access the underlying fields.
@@ -28,13 +40,13 @@ Accounts must have:
Code that uses the Account class should not be able to change the properties of an account directly; this should be something handled by methods provided by the account class. The methods should enforce the following behavior:
-- Account type and account number must be set during account creation (in the constructor) and cannot be changed afterward.
+- accounts.Account type and account number must be set during account creation (in the constructor) and cannot be changed afterward.
- Balance inquiries are allowed at any time except while an account is under an OFAC freeze
- The balance can be changed with a credit (add money) or debit (remove money)
- Balance changes can only occur on `Open` accounts.
- The `debit` and `credit` methods should return an approval status indicating whether the transaction was approved.
- Accounts can transfer funds to or from another account with the same account holder -- Neither account's balance should fall below zero as a result of a transfer.
-- Account holder's name must be set during account creation. It can be changed later (but not on closed accounts)
+- accounts.Account holder's name must be set during account creation. It can be changed later (but not on closed accounts)
- Accounts with overdraft prevention enabled cannot over-draw (a debit that is greater than the account balance will be declined and the balance will not change)
- Accounts, once closed, cannot be reopened (frozen accounts can be unfrozen).
- No changes to the balance of an account can take place while it is closed or frozen
diff --git a/data/.DS_Store b/data/.DS_Store
new file mode 100644
index 0000000..5008ddf
Binary files /dev/null and b/data/.DS_Store differ
diff --git a/data/accounts.csv b/data/accounts.csv
new file mode 100644
index 0000000..c52f025
--- /dev/null
+++ b/data/accounts.csv
@@ -0,0 +1,6 @@
+"350","275","3810.0","Checking","ON","OPEN"
+"33","275","5556.84","Savings","0.24","OPEN"
+"2","275","88609.38","Investment","0.06","OPEN"
+"998","275","320.0","Checking","ON","CLOSED"
+"9198","275","30020.0","Checking","ON","OFAC"
+
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..97776f7
--- /dev/null
+++ b/data/transactions.csv
@@ -0,0 +1,23 @@
+"credit","172","2000.00","Sun Nov 10 11:53:50 EST 2019","Opened account"
+"debit","172","-1000.00","Sun Nov 10 11:54:02 EST 2019","Transfer to account 350"
+"credit","350","1000.00","Sun Nov 10 11:54:02 EST 2019","Transfer from account 172"
+"debit","172","-1000.00","Sun Nov 10 11:54:21 EST 2019","Transfer to account 350"
+"credit","350","1000.00","Sun Nov 10 11:54:21 EST 2019","Transfer from account 172"
+"debit","172","0.00","Sun Nov 10 11:54:21 EST 2019","Account Closed"
+"credit","33","5690.19","Sun Nov 10 11:56:40 EST 2019","Interest rate changed to 00.23"
+"credit","33","13.09","Sun Nov 10 11:56:40 EST 2019","Interest earned"
+"debit","2","-1175.84","Sun Nov 10 11:56:40 EST 2019","Investment returns"
+"credit","165","1000.00","Sun Nov 10 11:56:48 EST 2019","Opened account"
+"credit","33","13.12","Sun Nov 10 12:00:39 EST 2019","Interest earned"
+"credit","2","3000.22","Sun Nov 10 12:00:39 EST 2019","Investment returns"
+"debit","165","-1000.00","Sun Nov 10 12:00:53 EST 2019","Transfer to account 350"
+"credit","350","1000.00","Sun Nov 10 12:00:53 EST 2019","Transfer from account 165"
+"debit","165","0.00","Sun Nov 10 12:00:53 EST 2019","Account Closed"
+"credit","206","1000.00","Sun Nov 10 22:02:32 EST 2019","Opened account"
+"credit","33","5729.55","Sun Nov 10 22:54:34 EST 2019","Interest rate changed to 00.24"
+"credit","33","13.75","Sun Nov 10 22:54:34 EST 2019","Interest earned"
+"debit","2","-419.17","Sun Nov 10 22:54:34 EST 2019","Investment returns"
+"debit","33","-100.00","Sun Nov 10 22:56:04 EST 2019","ATM deposit"
+"credit","33","13.54","Sun Nov 10 22:56:43 EST 2019","Interest earned"
+"credit","2","2532.37","Sun Nov 10 22:56:43 EST 2019","Investment returns"
+"debit","33","-100.00","Sun Nov 10 22:56:49 EST 2019","ATM withdrawal"
diff --git a/data/users.csv b/data/users.csv
new file mode 100644
index 0000000..7efca6b
--- /dev/null
+++ b/data/users.csv
@@ -0,0 +1,6 @@
+"719","john","jimmy","90486264","blorp"
+"606","jones","jim","84170304","jimmy"
+"275","peasy","easy","1","1234"
+"827","davis","john","16690334","12345"
+"828","john","big","83500714","69"
+"829","jay","john","68428999","2345"
diff --git a/pom.xml b/pom.xml
index 2bcb569..5ad041c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -5,8 +5,35 @@
4.0.0
io.zipcoder
- AccessControlLab-Bank Account
+ 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/.DS_Store b/src/.DS_Store
new file mode 100644
index 0000000..566bda1
Binary files /dev/null and b/src/.DS_Store differ
diff --git a/src/main/.DS_Store b/src/main/.DS_Store
new file mode 100644
index 0000000..cce1d29
Binary files /dev/null and b/src/main/.DS_Store differ
diff --git a/src/main/java/.DS_Store b/src/main/java/.DS_Store
new file mode 100644
index 0000000..5008ddf
Binary files /dev/null and b/src/main/java/.DS_Store differ
diff --git a/src/main/java/ATM/ATM.java b/src/main/java/ATM/ATM.java
new file mode 100644
index 0000000..e342517
--- /dev/null
+++ b/src/main/java/ATM/ATM.java
@@ -0,0 +1,88 @@
+package ATM;
+
+import ATM.menus.MainMenu;
+import ATM.menus.UserMenu;
+import ATM.services.AccountServices;
+import ATM.services.TransactionServices;
+import ATM.services.UserServices;
+
+import java.io.IOException;
+
+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 accountDB; // 0: accountID 1: ownerID 2: balance 3: type 4: risk/interest/null (type-dependent)
+
+ private UserServices userServices;
+ private TransactionServices transactionServices;
+ private AccountServices accountServices;
+
+
+ public ATM(String userDBName, String accountDBName, String transactionDBName) {
+ this.currentUser = null;
+ try {
+ this.userDB = new DB(userDBName, 5);
+ this.transactionDB = new DB(transactionDBName, 5);
+ this.accountDB = new DB(accountDBName, 6);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ this.userServices = new UserServices(userDB, this);
+ this.transactionServices = new TransactionServices(transactionDB, this);
+ this.accountServices = new AccountServices(accountDB, this);
+ }
+
+ 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 UserServices getUserServices() {
+ return userServices;
+ }
+
+ public TransactionServices getTransactionServices() {
+ return transactionServices;
+ }
+
+ public AccountServices getAccountServices() {
+ return accountServices;
+ }
+
+ public void setCurrentUser(User currentUser) {
+ this.currentUser = currentUser;
+ }
+
+ // no test - requires input
+ public void serviceLoop() {
+ this.transactionServices.linkServices();
+ this.accountServices.linkServices();
+ //this.userServices.linkServices();
+
+ new UserMenu(this).displayMenu();
+
+ new MainMenu(this).displayMenu();
+
+ logOut();
+
+ serviceLoop();
+ }
+
+ public void logOut() {
+ this.currentUser = null;
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/ATM/Console.java b/src/main/java/ATM/Console.java
new file mode 100644
index 0000000..aa2bec5
--- /dev/null
+++ b/src/main/java/ATM/Console.java
@@ -0,0 +1,203 @@
+package ATM;
+
+import org.apache.commons.lang3.StringUtils;
+
+import java.util.ArrayList;
+import java.util.Scanner;
+
+/**
+ * Created by leon on 2/9/18.
+ */
+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);
+ }
+
+ 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;
+ }
+
+ public static String getInput(String prompt) {
+ Console.print(prompt);
+ Scanner scanner = new Scanner(System.in);
+
+ String input = scanner.nextLine().toLowerCase(); //get input from user
+
+ return input;
+ }
+
+ public static boolean getInputYN(String prompt) {
+ Console.print(prompt);
+ Scanner scanner = new Scanner(System.in);
+
+ String input = scanner.nextLine().toLowerCase(); //get input from user
+
+ if (input.equals("y")) {
+ return true;
+ } else if (input.equals("n")) {
+ return false;
+ } else {
+ return getInputYN(prompt);
+ }
+
+ }
+
+
+ public static int getInput(String[] options) {
+
+ Console.clearScreen();
+
+ 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] = String.format("%d | %-30s", 2*i+1, options[2*i]);
+ if (2*i + 1 < numRows) {
+ rows[i] += String.format("%30s | %d", options[2*i + 1], 2*(i+1));
+ }
+ rows[i] += "\n";
+ }
+
+ for (int i = 0; i < numRows; i++) {
+ output += rows[i];
+ }
+
+ println(output);
+
+ return Console.getInteger(numOptions);
+
+ }
+
+ public static int getInput(String header, String[] options) {
+
+ Console.clearScreen();
+
+ int numOptions = options.length;
+ int numRows = (numOptions+1) >> 1; // this is how the cool kids divide by two
+ 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 | %-40s", 2*i+1, options[2*i]);
+ if (2*i + 1 < numOptions) {
+ rows[i] += String.format("%40s | %d", options[2*i + 1], 2*(i+1));
+ }
+ rows[i] += "\n";
+ }
+
+ for (int i = 0; i < numRows; i++) {
+ output += rows[i];
+ }
+
+ println(output);
+
+ return Console.getInteger(numOptions);
+
+ }
+
+ 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])
+ + "\n");
+ }
+
+ println(output);
+ getInput("\nPress Enter");
+ }
+
+ 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 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);
+ }
+
+ public static Integer getInteger() {
+ String input = getInput();
+ while (true) {
+ if (integerCheck(input)) break;
+ else {
+ println("Enter a number");
+ input = getInput();
+ }
+ }
+ 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);
+ }
+
+}
diff --git a/src/main/java/ATM/DB.java b/src/main/java/ATM/DB.java
new file mode 100644
index 0000000..7ae127b
--- /dev/null
+++ b/src/main/java/ATM/DB.java
@@ -0,0 +1,468 @@
+package ATM;
+
+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;
+ private Boolean deleted;
+
+ /**
+ * ATM.DB object constructor
+ *
+ * Creates a ATM.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 ATM.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"));
+ 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, use that rowLength to override given one, if nec.
+ List records = csvReader.readAll();
+ if (records.size() > 0) {
+ this.rowLength = records.get(0).length;
+ }
+
+ }
+ 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();
+ }
+ }
+
+ /**
+ * isDeleted
+ *
+ * Returns true if the ATM.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 ATM.DB (not the full path)
+ *
+ * @return String fileName
+ */
+ public String getFileName() {
+ if (!this.deleted) {
+ return this.fileName;
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * 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 ATM.DB
+ *
+ * @return Integer: length
+ */
+ public Integer length() {
+ if (!this.deleted) {
+ return readAllRows().size();
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * getRowLength
+ *
+ * Return the length of rows in this ATM.DB
+ *
+ * @return Integer: rowLength
+ */
+ public Integer getRowLength() {
+ if (!this.deleted) {
+ return this.rowLength;
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * checkIntegrity
+ *
+ * Return true if the ATM.DB has rows all of the same length, which is the rowLength property
+ *
+ * @return Boolean
+ */
+ public Boolean checkIntegrity() {
+ if (!this.deleted) {
+ ArrayList records = readAllRows();
+
+ for (String[] row : records) {
+ if (row.length != this.rowLength) {
+ return false;
+ }
+ }
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * addRow
+ *
+ * Input a String[] that is a new row to add; if it is the proper length, it is added to the ATM.DB
+ *
+ * @param row String[]: the row to be input
+ */
+ public void addRow(String[] 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();
+ }
+ }
+ }
+
+ /**
+ * 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();
+ records.set(rowNum, replacement);
+ }
+ clear();
+ for (String[] row : records) {
+ addRow(row);
+ }
+ }
+
+ /**
+ * 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()) {
+ records = readAllRows();
+ records.remove(rowNum);
+ }
+ clear();
+ for (String[] row : records) {
+ addRow(row);
+ }
+ }
+
+ /**
+ * 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() && rowNum >= 0) {
+ records = readAllRows();
+ return records.get(rowNum);
+ } else if (rowNum == -1) {
+ return null;
+ }
+ 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;
+ for (int i = 0; i < this.length(); i++) {
+ if (this.serialize(row).equals(this.serialize(i))) {
+ return i;
+ }
+ }
+ return -1;
+ } else {
+ return -1;
+ }
+ }
+
+ /**
+ * 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);
+ for (int i = 0; i < this.length(); i++) {
+ if (signature.equals(this.partialSerialize(i,fields))) {
+ return i;
+ }
+ }
+ return -1;
+ } else {
+ return -1;
+ }
+ }
+
+ /**
+ * 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 new int[0];
+ }
+ } 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));
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * 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);
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * 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);
+ }
+
+ /**
+ * 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] = fragment[fields[i]];
+ }
+ return String.join("/", partialArray);
+ }
+
+ /**
+ * readAllRows
+ *
+ * Return an ArrayList of all of the rows in the ATM.DB
+ *
+ * @return ArrayList of String[] arrays: the data
+ */
+ public ArrayList readAllRows() {
+ 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;
+ }
+
+ }
+
+ /**
+ * printDB
+ *
+ * For debugging: print out the whole ATM.DB
+ *
+ */
+ public void printDB() {
+ if (!this.deleted) {
+ for (String[] row : readAllRows()) {
+ System.out.println(serialize(row));
+ }
+ }
+ }
+
+ /**
+ * clear
+ *
+ * Method to clear the ATM.DB
+ *
+ */
+ public void clear() {
+ 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);
+
+ writer.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ /**
+ * delete
+ *
+ * Method to delete the ATM.DB file
+ *
+ */
+ public void delete() {
+
+ File file = new File(this.path);
+ file.delete();
+ this.deleted = true;
+
+ }
+
+
+}
diff --git a/src/main/java/ATM/Exceptions/BalanceRemainingException.java b/src/main/java/ATM/Exceptions/BalanceRemainingException.java
new file mode 100644
index 0000000..c3c393d
--- /dev/null
+++ b/src/main/java/ATM/Exceptions/BalanceRemainingException.java
@@ -0,0 +1,4 @@
+package ATM.Exceptions;
+
+public final class BalanceRemainingException extends Exception{
+}
diff --git a/src/main/java/ATM/Exceptions/ClosedAccountException.java b/src/main/java/ATM/Exceptions/ClosedAccountException.java
new file mode 100644
index 0000000..b7e8c3a
--- /dev/null
+++ b/src/main/java/ATM/Exceptions/ClosedAccountException.java
@@ -0,0 +1,5 @@
+package ATM.Exceptions;
+
+public final class ClosedAccountException extends Exception {
+
+}
diff --git a/src/main/java/ATM/Exceptions/FrozenAccountException.java b/src/main/java/ATM/Exceptions/FrozenAccountException.java
new file mode 100644
index 0000000..8ae1f6f
--- /dev/null
+++ b/src/main/java/ATM/Exceptions/FrozenAccountException.java
@@ -0,0 +1,4 @@
+package ATM.Exceptions;
+
+public final class FrozenAccountException extends Exception {
+}
diff --git a/src/main/java/ATM/Exceptions/InsufficientFundsException.java b/src/main/java/ATM/Exceptions/InsufficientFundsException.java
new file mode 100644
index 0000000..80a1851
--- /dev/null
+++ b/src/main/java/ATM/Exceptions/InsufficientFundsException.java
@@ -0,0 +1,4 @@
+package ATM.Exceptions;
+
+public class InsufficientFundsException extends Exception {
+}
diff --git a/src/main/java/ATM/Main.java b/src/main/java/ATM/Main.java
new file mode 100644
index 0000000..ba97d47
--- /dev/null
+++ b/src/main/java/ATM/Main.java
@@ -0,0 +1,12 @@
+package ATM;
+
+import ATM.ATM;
+
+public class Main {
+
+ public static void main(String[] args) {
+ ATM atm = new ATM("users.csv", "accounts.csv", "transactions.csv");
+
+ atm.serviceLoop();
+ }
+}
diff --git a/src/main/java/ATM/Transaction.java b/src/main/java/ATM/Transaction.java
new file mode 100644
index 0000000..40c268a
--- /dev/null
+++ b/src/main/java/ATM/Transaction.java
@@ -0,0 +1,41 @@
+package ATM;
+
+import ATM.interfaces.Storeable;
+
+import java.util.Date;
+
+public class Transaction implements Storeable {
+
+ private Double amount;
+ private Date timeStamp;
+ 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;
+ this.accountID = accountID;
+ this.description = description;
+ this.isCredit = isCredit;
+ }
+
+ @Override
+ public String[] toStringArray() {
+ String type;
+ if (isCredit) {
+ type = "credit";
+ } else {
+ type = "debit";
+ }
+
+ String[] result = new String[] {
+ type,
+ this.accountID.toString(),
+ String.format("%.2f",this.amount),
+ this.timeStamp.toString(),
+ this.description
+ };
+ return result;
+ }
+}
diff --git a/src/main/java/ATM/User.java b/src/main/java/ATM/User.java
new file mode 100644
index 0000000..eedbf98
--- /dev/null
+++ b/src/main/java/ATM/User.java
@@ -0,0 +1,55 @@
+package ATM;
+
+import ATM.interfaces.Storeable;
+import ATM.services.UserServices;
+
+import java.util.ArrayList;
+
+public class User implements Storeable {
+
+ private String firstName;
+ private String lastName;
+ private String password;
+ private Integer userID;
+ private Integer cardNumber;
+
+ 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;
+ }
+
+ public Integer getUserID() {
+ return userID;
+ }
+
+ @Override
+ public String[] toStringArray() {
+ String[] result = new String[] {
+ this.userID.toString(),
+ this.lastName,
+ this.firstName,
+ this.cardNumber.toString(),
+ this.password
+ };
+ return result;
+ }
+
+ public Integer getCardNumber() {
+ return cardNumber;
+ }
+
+ public String getPassword() {
+ return password;
+ }
+
+ public void setFirstName(String firstName) {
+ this.firstName = firstName;
+ }
+
+ public void setLastName(String lastName) {
+ this.lastName = lastName;
+ }
+}
diff --git a/src/main/java/ATM/accounts/Account.java b/src/main/java/ATM/accounts/Account.java
new file mode 100644
index 0000000..a87944d
--- /dev/null
+++ b/src/main/java/ATM/accounts/Account.java
@@ -0,0 +1,67 @@
+package ATM.accounts;
+
+import java.util.Random;
+
+import ATM.DB;
+import ATM.interfaces.Storeable;
+
+abstract public class Account implements Storeable {
+
+ private Double balance;
+ private Integer ownerID;
+ private Integer acctNum;
+ public enum Status {
+ OPEN, CLOSED, OFAC
+ }
+ private Status acctStatus;
+ Random random = new Random();
+
+ public Account(Double balance, Integer ownerID, Integer acctNum, Status acctStatus) {
+
+ this.balance = balance;
+ this.ownerID = ownerID;
+ this.acctNum = acctNum;
+ this.acctStatus = acctStatus;
+ }
+
+ public Double getBalance(){
+ return balance;
+ }
+
+ public Integer getOwnerID() {
+ return this.ownerID;
+ }
+
+ public Integer getAcctNum() {
+ return this.acctNum;
+ }
+
+ public Status getAcctStatus() {
+ return acctStatus;
+ }
+
+ public void setBalance(Double balance) {
+ this.balance = balance;
+ }
+
+ public void setAcctStatus(Status acctStatus) {
+ this.acctStatus = acctStatus;
+ }
+
+ public void deposit(Double amount){
+ this.balance += amount;
+ String bal = String.format("%.2f",this.balance);
+ this.balance = Double.parseDouble(bal);
+ }
+
+ public void withdraw(Double amount){
+ if (this.balance >= amount) {
+ this.balance -= amount;
+ }
+ }
+
+ public Boolean equals(Account account) {
+ return DB.serialize(this.toStringArray()).equals(DB.serialize(account.toStringArray()));
+ }
+}
+
diff --git a/src/main/java/ATM/accounts/Checking.java b/src/main/java/ATM/accounts/Checking.java
new file mode 100644
index 0000000..3b4c974
--- /dev/null
+++ b/src/main/java/ATM/accounts/Checking.java
@@ -0,0 +1,25 @@
+package ATM.accounts;
+
+public class Checking extends Account {
+
+ private Overdraft overdraft;
+
+ public enum Overdraft {
+ ON,
+ OFF,
+ AUTO
+ }
+
+ public Checking(Double balance, Integer ownerID, Integer acctNum, Status acctStatus, Overdraft overdraft){
+ super(balance, ownerID, acctNum, acctStatus);
+ this.overdraft = overdraft;
+ }
+
+ public Overdraft getOverdraft() {
+ return overdraft;
+ }
+
+ public void setOverdraft(Overdraft overdraft) {
+ this.overdraft = overdraft;
+ }
+}
diff --git a/src/main/java/ATM/accounts/Investment.java b/src/main/java/ATM/accounts/Investment.java
new file mode 100644
index 0000000..36c6862
--- /dev/null
+++ b/src/main/java/ATM/accounts/Investment.java
@@ -0,0 +1,22 @@
+package ATM.accounts;
+
+public class Investment extends Account{
+
+ private Double risk;
+
+ public Investment(Double balance, Integer ownerID, Integer acctNum, Double risk, Status acctStatus) {
+ super(balance, ownerID, acctNum, acctStatus);
+ this.risk = risk;
+
+ }
+ public Double getRisk() {
+ return risk;
+ }
+
+ public void setRisk(Double risk) {
+ this.risk = risk;
+ }
+
+
+
+}
diff --git a/src/main/java/ATM/accounts/Savings.java b/src/main/java/ATM/accounts/Savings.java
new file mode 100644
index 0000000..cd24284
--- /dev/null
+++ b/src/main/java/ATM/accounts/Savings.java
@@ -0,0 +1,19 @@
+package ATM.accounts;
+
+public class Savings extends Account {
+
+ private Double interestRate;
+
+ public Savings(Double balance, Integer ownerID, Integer acctNum, Double interestRate, Status acctStatus) {
+ super(balance, ownerID, acctNum, acctStatus);
+ this.interestRate = interestRate;
+ }
+
+ public Double getInterestRate() {
+ return this.interestRate;
+ }
+
+ public void setInterestRate(Double interestRate) {
+ this.interestRate = interestRate;
+ }
+}
diff --git a/src/main/java/ATM/interfaces/Menu.java b/src/main/java/ATM/interfaces/Menu.java
new file mode 100644
index 0000000..81b268f
--- /dev/null
+++ b/src/main/java/ATM/interfaces/Menu.java
@@ -0,0 +1,12 @@
+package ATM.interfaces;
+
+import ATM.ATM;
+
+public interface Menu {
+
+ void displayMenu();
+
+ void handleChoice(int choice);
+
+ String getName();
+}
\ No newline at end of file
diff --git a/src/main/java/ATM/interfaces/Storeable.java b/src/main/java/ATM/interfaces/Storeable.java
new file mode 100644
index 0000000..9871ac2
--- /dev/null
+++ b/src/main/java/ATM/interfaces/Storeable.java
@@ -0,0 +1,42 @@
+package ATM.interfaces;
+
+import ATM.accounts.Account;
+import ATM.accounts.Investment;
+import ATM.accounts.Savings;
+import ATM.accounts.Checking;
+
+public interface Storeable {
+
+ default String[] toStringArray() {
+ if (this instanceof Account) {
+
+ String acctType;
+ String typeSpecificProperty;
+ if (this instanceof Investment) {
+ acctType = "Investment";
+ typeSpecificProperty = ((Investment) this).getRisk().toString();
+ } else if (this instanceof Savings) {
+ acctType = "Savings";
+ typeSpecificProperty = ((Savings) this).getInterestRate().toString();
+ } else {
+ acctType = "Checking";
+ typeSpecificProperty = ((Checking) this).getOverdraft().toString();
+ }
+
+ String[] result = new String[] {
+ ((Account) this).getAcctNum().toString(),
+ ((Account) this).getOwnerID().toString(),
+ ((Account) this).getBalance().toString(),
+ acctType,
+ typeSpecificProperty,
+ String.valueOf(((Account) this).getAcctStatus())
+ };
+
+ return result;
+
+ } else {
+ return null;
+ }
+ }
+
+}
diff --git a/src/main/java/ATM/menus/AccountMenu.java b/src/main/java/ATM/menus/AccountMenu.java
new file mode 100644
index 0000000..a8f1aa3
--- /dev/null
+++ b/src/main/java/ATM/menus/AccountMenu.java
@@ -0,0 +1,211 @@
+package ATM.menus;
+
+import ATM.ATM;
+import ATM.Console;
+import ATM.Exceptions.BalanceRemainingException;
+import ATM.Exceptions.ClosedAccountException;
+import ATM.Exceptions.FrozenAccountException;
+import ATM.Exceptions.InsufficientFundsException;
+import ATM.accounts.Checking;
+import ATM.interfaces.Menu;
+import ATM.User;
+import ATM.Transaction;
+import ATM.accounts.Account;
+import ATM.accounts.Investment;
+import ATM.accounts.Savings;
+import ATM.services.AccountServices;
+import ATM.services.TransactionServices;
+
+import java.util.Date;
+
+public class AccountMenu implements Menu {
+
+ private String name = "Account Menu";
+ private Account account;
+ private User currentUser;
+ private ATM atm;
+ private TransactionServices transactionServices;
+ private AccountServices accountServices;
+
+ /**
+ * Account Menu - the menu to deal with a single account
+ *
+ * @param atm - ATM instance
+ * @param account - the account we're dealing with
+ */
+ public AccountMenu(ATM atm, Account account) throws FrozenAccountException {
+ this.atm = atm;
+ this.currentUser = this.atm.getCurrentUser();
+ this.account = account;
+ if (this.account.getAcctStatus() == Account.Status.OFAC) {
+ throw new FrozenAccountException();
+ }
+ this.transactionServices = this.atm.getTransactionServices();
+ this.accountServices = this.atm.getAccountServices();
+ }
+
+ // needs input - no test
+ public void displayMenu() {
+ Console.clearScreen();
+
+ String header = getHeader();
+ int input;
+ if (account instanceof Checking) {
+ input = Console.getInput(header, new String[]{"View Transaction History", "Deposit", "Withdrawal", "Close Account", "Transfer", "Change Overdraft Status", "Back to Main Menu"});
+ } else {
+ input = Console.getInput(header, new String[]{"View Transaction History", "Deposit", "Withdrawal", "Close Account", "Transfer", "Back to Main Menu"});
+ }
+ handleChoice(input);
+ }
+
+ public String getHeader() {
+ String header = account.getClass().getSimpleName() + " 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";
+ }
+ Account.Status status = account.getAcctStatus();
+ if (status == Account.Status.CLOSED || status == Account.Status.OFAC) {
+ header += String.format(" (%s)", status.toString());
+ }
+ return header;
+ }
+
+ public String getName() {
+ return this.name;
+ }
+
+ // needs input - no test
+ public void handleChoice(int choice) {
+ double amount;
+ Transaction transaction;
+ switch (choice) {
+ case 1:
+ Console.outputTransactionsWithHeader("Transaction History", transactionServices.getTransactionsForAccount(account));
+ break;
+ case 2: // deposit
+ amount = Console.getCurrency("Deposit amount: ");
+ attemptDeposit(amount);
+ break;
+ case 3: // withdrawal
+ amount = Console.getCurrency("Withdrawal amount: ");
+ attemptWithdrawal(amount);
+ break;
+ case 4: // close account
+ attemptCloseAccount();
+ break;
+ case 5: // transfer money
+ attemptTransfer();
+ break;
+ case 6: // overdraft toggle or quit
+ if (account instanceof Checking) {
+ overDraftChoice();
+ try {
+ new AccountMenu(atm, account);
+ } catch (FrozenAccountException e) {}
+ }
+ break;
+ default:
+ break;
+ }
+ }
+
+ private void overDraftChoice() {
+ String header = String.format("Current overdraft status : %s", ((Checking)account).getOverdraft());
+ int input = Console.getInput(header,new String[] {"ON: disallow overdrafts", "OFF: allow overdrafts", "Attempt auto-transfer"});
+ switch (input) {
+ case 1:
+ ((Checking)account).setOverdraft(Checking.Overdraft.ON);
+ break;
+ case 2:
+ ((Checking)account).setOverdraft(Checking.Overdraft.OFF);
+ break;
+ case 3:
+ ((Checking)account).setOverdraft(Checking.Overdraft.AUTO);
+ break;
+ }
+ accountServices.saveAccountToDB(account);
+ transactionServices.saveTransactionToDB(new Transaction(0.00, new Date(), account.getAcctNum(), String.format("Overdraft status changed to %s",((Checking)account).getOverdraft()),true));
+ }
+
+ // needs input - no test (underlying method is tested)
+ private void attemptTransfer() {
+ try {
+ new TransferServicesMenu(this.atm, account, accountServices.getAccountsForUser(currentUser)).displayMenu();
+ } catch (ClosedAccountException e) {
+ Console.getInput("Error - this account is closed. Press Enter to continue");
+ } catch (FrozenAccountException e) {
+ Console.getInput("Error - this account is frozen by OFAC. Press Enter to continue");
+ }
+ }
+
+ // needs input - no test (underlying method is tested)
+ private void attemptCloseAccount() {
+ try {
+ if (accountServices.closeAccount(account)) {
+ Console.getInput("Account closed; press Enter to continue");
+ }
+ } catch (BalanceRemainingException e) {
+ closedAcctNotice();
+ } catch (ClosedAccountException e) {
+ Console.getInput("Error - account is already closed; press Enter to continue");
+ } catch (FrozenAccountException e) {
+ Console.getInput("Error - account is frozen by OFAC; press Enter to continue");
+ }
+ }
+
+ // needs input - no test (underlying method is tested)
+ private void closedAcctNotice() {
+ boolean closeAccountInput = Console.getInputYN("Account still contains funds. Do you wish to transfer funds to a different account? ");
+ try {
+ if (!closeAccountInput){//gives user the money
+ accountServices.attemptAccountWithdrawal(account, account.getBalance());
+ accountServices.closeAccount(account);
+ Console.getInput("Funds withdrawn and account closed; press Enter to continue");
+ } else { //transfers money
+ new TransferServicesMenu(atm, account, accountServices.getAccountsForUser(currentUser)).displayMenu();
+ accountServices.closeAccount(account);
+ Console.getInput("Funds withdrawn and account closed; press Enter to continue");
+ }
+ } catch (ClosedAccountException e) {
+ Console.getInput("Error - account is closed; press Enter to continue");
+ } catch (FrozenAccountException e) {
+ Console.getInput("Error - account is frozen by OFAC; press Enter to continue");
+ } catch (BalanceRemainingException e) {
+ Console.getInput("Error - account has fund remaining; press Enter to continue");
+ } catch (InsufficientFundsException e) { // shouldn't happen
+ Console.getInput("Error - insufficient funds; press Enter to continue");
+ }
+ }
+
+ // needs input - no test (underlying method is tested)
+ private void attemptWithdrawal(double amount) {
+ try {
+ if (accountServices.attemptAccountWithdrawal(account, amount)) {
+ Console.getInput("Withdrawal successful; press Enter to continue");
+ }
+ } catch (InsufficientFundsException e) {
+ Console.getInput("Error - insufficient funds; press Enter to continue");
+ } catch (ClosedAccountException e) {
+ Console.getInput("Error - account is closed; press Enter to continue");
+ } catch (FrozenAccountException e) {
+ Console.getInput("Error - account is frozen by OFAC; press Enter to continue");
+
+ }
+ }
+
+ // needs input - no test (underlying method is tested)
+ private void attemptDeposit(double amount) {
+ try {
+ if (accountServices.accountDeposit(account, amount)) {
+ Console.getInput("Deposit successful; press Enter to continue");
+ }
+ } catch (ClosedAccountException e) {
+ Console.getInput("Error - account is closed; press Enter to continue");
+ } catch (FrozenAccountException e) {
+ Console.getInput("Error - account is frozen by OFAC; press Enter to continue");
+
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/ATM/menus/MainMenu.java b/src/main/java/ATM/menus/MainMenu.java
new file mode 100644
index 0000000..271c8d1
--- /dev/null
+++ b/src/main/java/ATM/menus/MainMenu.java
@@ -0,0 +1,118 @@
+package ATM.menus;
+
+import ATM.ATM;
+import ATM.Console;
+import ATM.Exceptions.FrozenAccountException;
+import ATM.interfaces.Menu;
+import ATM.accounts.Account;
+import ATM.services.AccountServices;
+import ATM.services.TransactionServices;
+import ATM.services.UserServices;
+
+import java.util.ArrayList;
+
+public class MainMenu implements Menu {
+
+ private Console console;
+ private String name = "Main Menu";
+ private ATM atm;
+ private UserServices userServices;
+ private TransactionServices transactionServices;
+ private AccountServices accountServices;
+
+ /**
+ * Main Menu - landing page after logging in; other menus collapse back to here
+ * @param atm - ATM instance
+ */
+ public MainMenu(ATM atm){
+ this.atm = atm;
+ this.userServices = atm.getUserServices();
+ this.accountServices = atm.getAccountServices();
+ this.transactionServices = atm.getTransactionServices();
+ }
+
+ // needs input - no test
+ public void displayMenu() {
+ String header = String.format("ZCNB Main Menu (Card Number: %d)",this.atm.getCurrentUser().getCardNumber());
+ //maybe Younger Bank and Trust (YBT)
+ //logo is giant ASCII of Kris' face
+
+ ArrayList choices = new ArrayList<>();
+ choices.add("Transaction History");
+ choices.add("Add Account");
+ choices.add("Change Name");
+
+ choices = addAccountOptions(choices);
+
+ choices.add("Log Out");
+
+ handleChoice(Console.getInput(header, choices.toArray(new String[choices.size()])));
+ }
+
+ public ArrayList addAccountOptions(ArrayList choices) {
+ // auto-generate account choices
+ String nextAcctChoice;
+ ArrayList usrAccts = accountServices.getAccountsForUser(atm.getCurrentUser());
+ for (int i = 0; i < usrAccts.size(); i++) {
+ if (usrAccts.get(i).getAcctStatus() != Account.Status.OFAC) {
+ nextAcctChoice = String.format("%s #%d ($%,.2f)", usrAccts.get(i).getClass().getSimpleName(), usrAccts.get(i).getAcctNum(), usrAccts.get(i).getBalance());
+ } else {
+ nextAcctChoice = String.format("%s #%d (FROZEN)", usrAccts.get(i).getClass().getSimpleName(), usrAccts.get(i).getAcctNum(), usrAccts.get(i).getBalance());
+ }
+ choices.add(nextAcctChoice);
+ }
+ return choices;
+ }
+
+ // needs input - no test
+ public void handleChoice(int input) {
+ ArrayList usrAccts = accountServices.getAccountsForUser(atm.getCurrentUser());
+ if (input == 1) { // View overall transaction history
+ Console.outputTransactionsWithHeader("Transaction History", transactionServices.getTransactionsForUser(atm.getCurrentUser()));
+ displayMenu();
+ } else if (input == 2) { // create a new account
+ addAccountChoice();
+ displayMenu();
+ } else if (input == 3) { // change name
+ attemptNameChange();
+ displayMenu();
+ }else if (input == usrAccts.size()+4) { // quit/log out
+ // log out user and drop though to service loop
+ atm.setCurrentUser(null);
+ } else { // deal with an existing account
+ try {
+ new AccountMenu(this.atm, usrAccts.get(input - 4)).displayMenu();
+ } catch (FrozenAccountException e) {
+ Console.getInput("Error - this account is frozen by OFAC. Press Enter to continue");
+ }
+ displayMenu();
+ }
+ }
+
+ // needs input - no test
+ private void attemptNameChange() {
+ String firstName = Console.getInput("First name: ");
+ String lastName = Console.getInput("Last name: ");
+ if (userServices.changeName(this.atm.getCurrentUser(), firstName, lastName)) {
+ Console.println("Name change successful");
+ } else {
+ Console.getInput("Name change failed. Please try again");
+ }
+ }
+
+ // needs input - no test
+ private void addAccountChoice() {
+ Double deposit = Console.getCurrency("Initial deposit amount for this account: ");
+ String header = "Choose Account Type:";
+ String[] choices = new String[] {"Checking", "Savings", "Investment", "Back to Main Menu" };
+ int acctTypeInput = Console.getInput(header, choices);
+ if (acctTypeInput != 4) {
+ accountServices.addAccount(deposit, choices[acctTypeInput-1], this.atm.getCurrentUser());
+ }
+ }
+
+ @Override
+ public String getName() {
+ return this.name;
+ }
+}
diff --git a/src/main/java/ATM/menus/NewUserMenu.java b/src/main/java/ATM/menus/NewUserMenu.java
new file mode 100644
index 0000000..ce02c6c
--- /dev/null
+++ b/src/main/java/ATM/menus/NewUserMenu.java
@@ -0,0 +1,49 @@
+package ATM.menus;
+
+import ATM.ATM;
+import ATM.interfaces.Menu;
+import ATM.Console;
+import ATM.services.TransactionServices;
+import ATM.services.UserServices;
+
+public class NewUserMenu implements Menu {
+
+ private String name = "New User Menu";
+ private ATM atm;
+ private UserServices userServices;
+ private TransactionServices transactionServices;
+
+ public NewUserMenu(ATM atm){
+ this.atm = atm;
+ this.userServices = this.atm.getUserServices();
+ this.transactionServices = this.atm.getTransactionServices();
+ }
+
+ // needs input - no test
+ public void displayMenu() {
+ String firstName = Console.getInput("Enter Your First Name: ");
+ String lastName = Console.getInput("Enter Your Last Name: ");
+ String password = Console.getInput("Choose Your Password: ");
+
+ createNewUser(firstName, lastName, password);
+ }
+
+ public void createNewUser(String firstName, String lastName, String password) {
+ if (firstName.equals("") || lastName.equals("") || password.equals("")){
+ Console.getInput("Names and passwords cannot be empty. [press Enter to retry]");
+ displayMenu();
+ } else {
+ this.atm.setCurrentUser(userServices.createNewUser(firstName, lastName, password));
+ Console.getInput(String.format("Your card number is %d.\nPlease write this down somewhere. You will need it to log in later.\n[press return to continue]", this.atm.getCurrentUser().getCardNumber()));
+ }
+ }
+
+ public void handleChoice(int choice) {
+
+ }
+
+ @Override
+ public String getName() {
+ return this.name;
+ }
+}
diff --git a/src/main/java/ATM/menus/TransferServicesMenu.java b/src/main/java/ATM/menus/TransferServicesMenu.java
new file mode 100644
index 0000000..7ac27ae
--- /dev/null
+++ b/src/main/java/ATM/menus/TransferServicesMenu.java
@@ -0,0 +1,111 @@
+package ATM.menus;
+
+import ATM.ATM;
+import ATM.Exceptions.ClosedAccountException;
+import ATM.Exceptions.FrozenAccountException;
+import ATM.Exceptions.InsufficientFundsException;
+import ATM.accounts.Investment;
+import ATM.accounts.Savings;
+import ATM.services.AccountServices;
+import ATM.services.TransferServices;
+import ATM.User;
+import ATM.interfaces.Menu;
+import ATM.accounts.Account;
+import ATM.Console;
+import java.util.ArrayList;
+import java.util.Collections;
+
+
+public class TransferServicesMenu implements Menu {
+
+ private String name = "Transfer Menu";
+ private ATM atm;
+ private Account sourceAccount;
+ private AccountServices accountServices;
+ private TransferServices transferServices;
+ private ArrayList userAccounts;
+ private User currentUser;
+
+ public TransferServicesMenu(ATM atm, Account sourceAccount, ArrayList userAccounts) throws ClosedAccountException, FrozenAccountException {
+ this.atm = atm;
+ this.sourceAccount = sourceAccount;
+ if (this.sourceAccount.getAcctStatus() == Account.Status.CLOSED) {
+ throw new ClosedAccountException();
+ } else if (this.sourceAccount.getAcctStatus() == Account.Status.OFAC) {
+ throw new FrozenAccountException();
+ }
+ this.accountServices = this.atm.getAccountServices();
+ this.transferServices = new TransferServices(this.atm, sourceAccount);
+ this.currentUser = this.atm.getCurrentUser();
+ this.userAccounts = userAccounts;
+ }
+
+ // needs input - no test
+ public void displayMenu() {
+ String header = getHeader();
+
+ ArrayList choices = new ArrayList<>();
+ choices = addAccountOptions(choices);
+ choices.add("Exit");
+
+ handleChoice(Console.getInput(header, choices.toArray(new String[choices.size()])));
+ }
+
+ public String getHeader() {
+ String header = "Transfer from: " + sourceAccount.getClass().getSimpleName() + " Account #" + sourceAccount.getAcctNum().toString() + " Balance: $" + String.format("%,.2f", sourceAccount.getBalance());
+ if (sourceAccount instanceof Savings) {
+ header += " Interest Rate: " + String.format("%.2f", ((Savings) sourceAccount).getInterestRate()) + "%%";
+ } else if (sourceAccount instanceof Investment) {
+ header += " Risk: " + String.format("%d", Math.round(100 * ((Investment) sourceAccount).getRisk())) + "/10";
+ }
+ return header;
+ }
+
+ public ArrayList addAccountOptions(ArrayList choices) {
+ // auto-generate account choices
+ String nextAcctChoice;
+ ArrayList usrAccts = getDestinationAccounts();
+ for (int i = 0; i < usrAccts.size(); i++) {
+ nextAcctChoice = String.format("%s #%d ($%,.2f)", usrAccts.get(i).getClass().getSimpleName(), usrAccts.get(i).getAcctNum(), usrAccts.get(i).getBalance());
+ choices.add(nextAcctChoice);
+ }
+ return choices;
+ }
+
+ public ArrayList getDestinationAccounts() {
+ ArrayList userAccountsTrimmed = new ArrayList();
+
+ for (Account account : this.userAccounts) {
+ if (!account.getAcctNum().equals(this.sourceAccount.getAcctNum())) {
+ userAccountsTrimmed.add(account);
+ }
+ }
+ return userAccountsTrimmed;
+ }
+
+ // needs input - no test (underlying method is tested)
+ public void handleChoice(int choice) {
+ ArrayList usrAccts = getDestinationAccounts();
+ if (choice == usrAccts.size() - 2) { // exit transfer menu
+ // drop though to account menu
+ } else { // deal with an existing account
+ double amount = Console.getCurrency("Amount to transfer: ");
+ try {
+ transferServices.executeTransfer(this.sourceAccount, usrAccts.get(choice - 1), amount);
+ } catch (ClosedAccountException e) {
+ Console.getInput("Error - cannot transfer to/from a closed account. Press Enter to continue");
+ } catch (InsufficientFundsException e) {
+ Console.getInput("Error - insufficient funds. Press Enter to continue");
+ } catch (FrozenAccountException e) {
+ Console.getInput("Error - cannot transfer to/from a frozen account. Press Enter to continue");
+ }
+
+ }
+ }
+
+ @Override
+ public String getName() {
+ return this.name;
+ }
+
+}
\ No newline at end of file
diff --git a/src/main/java/ATM/menus/UserMenu.java b/src/main/java/ATM/menus/UserMenu.java
new file mode 100644
index 0000000..7444af2
--- /dev/null
+++ b/src/main/java/ATM/menus/UserMenu.java
@@ -0,0 +1,59 @@
+package ATM.menus;
+
+import ATM.ATM;
+import ATM.Console;
+import ATM.interfaces.Menu;
+import ATM.services.AccountServices;
+import ATM.services.UserServices;
+
+public class UserMenu implements Menu {
+
+ private String name = "User Menu";
+ private ATM atm;
+ private UserServices userServices;
+ private AccountServices accountServices;
+
+ public UserMenu(ATM atm){
+ this.atm = atm;
+ this.userServices = this.atm.getUserServices();
+ this.accountServices = this.atm.getAccountServices();
+ }
+
+ // needs input - no test
+ public void displayMenu() {
+ String header = "Welcome to ZipCode National Bank";
+ int input = Console.getInput(header, new String[] {"Insert Card", "Open an Account"});
+ handleChoice(input);
+ }
+
+ // needs input - no test
+ public void handleChoice(int choice) {
+ switch (choice) {
+ case 1:
+ userServices.authenticate();
+ if (this.atm.getCurrentUser() == null) {
+ Console.getInput("Authentication error - press Enter to try again");
+ displayMenu();
+ return;
+ } else {
+ accountMaintenance();
+ }
+ break;
+ case 2:
+ new NewUserMenu(this.atm).displayMenu();
+ break;
+ }
+ }
+
+ // underlying methods tested
+ public void accountMaintenance() {
+ accountServices.interestRateChange();
+ accountServices.applyInterest();
+ accountServices.applyReturns();
+ }
+
+ public String getName() {
+ return this.name;
+ }
+
+}
diff --git a/src/main/java/ATM/services/AccountServices.java b/src/main/java/ATM/services/AccountServices.java
new file mode 100644
index 0000000..b657fa9
--- /dev/null
+++ b/src/main/java/ATM/services/AccountServices.java
@@ -0,0 +1,328 @@
+package ATM.services;
+
+import ATM.ATM;
+import ATM.DB;
+import ATM.Exceptions.BalanceRemainingException;
+import ATM.Exceptions.ClosedAccountException;
+import ATM.Exceptions.FrozenAccountException;
+import ATM.Exceptions.InsufficientFundsException;
+import ATM.Transaction;
+import ATM.User;
+import ATM.accounts.Account;
+import ATM.accounts.Checking;
+import ATM.accounts.Investment;
+import ATM.accounts.Savings;
+import ATM.Console;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.Random;
+
+public class AccountServices {
+
+ // 0: accountID 1: ownerID 2: balance 3: type 4: risk/interest/null (type-dependent) 5: status (OPEN/CLOSED/OFAC)
+ private DB accountDB;
+ private ATM atm;
+ private TransactionServices transactionServices;
+ private UserServices userServices;
+ private Account accountType;
+
+ public AccountServices(DB accountDB, ATM atm) {
+ this.accountDB = accountDB;
+ this.atm = atm;
+ }
+
+ public void linkServices() {
+ this.transactionServices = this.atm.getTransactionServices();
+ }
+
+ // no test - needs input (underlying methods tested)
+ public void addAccount(double deposit, String acctType, User currentUser) {
+
+ switch (acctType) {
+ case "Checking":
+ createCheckingAccount(deposit, currentUser);
+ break;
+ case "Savings":
+ createSavingsAccount(deposit, currentUser);
+ break;
+ case "Investment":
+ Console.print("On a scale of 1-10, enter your risk tolerance ");
+ int riskInput = Console.getInteger(10);
+ createInvestmentAccount(deposit, currentUser, riskInput);
+ break;
+ }
+ }
+
+ public void createInvestmentAccount(double deposit, User currentUser, int riskInput) {
+ Account newAccount;
+ Transaction transaction;
+ Double risk = riskInput * .01;
+ newAccount = new Investment(deposit, currentUser.getUserID(), (int) (Math.random() * 1000), risk, Account.Status.OPEN);
+ this.saveAccountToDB(newAccount);
+
+ transaction = new Transaction(deposit, new Date(), newAccount.getAcctNum(), "Opened account", true);
+ this.transactionServices.saveTransactionToDB(transaction);
+ }
+
+ public void createSavingsAccount(double deposit, User currentUser) {
+ Account newAccount;
+ Transaction transaction;
+ Double interestRate = .01 * (1 + Math.floor(deposit / 1000));
+ Console.println(String.format("Your interest rate: %.2f", interestRate) + "%%");
+ newAccount = new Savings(deposit, currentUser.getUserID(), (int) (Math.random() * 1000), interestRate, Account.Status.OPEN);
+ this.saveAccountToDB(newAccount);
+
+ transaction = new Transaction(deposit, new Date(), newAccount.getAcctNum(), "Opened account", true);
+ this.transactionServices.saveTransactionToDB(transaction);
+ }
+
+ public void createCheckingAccount(double deposit, User currentUser) {
+ Account newAccount;
+ Transaction transaction;
+ newAccount = new Checking(deposit, currentUser.getUserID(), (int) (Math.random() * 1000), Account.Status.OPEN, Checking.Overdraft.OFF);
+ this.saveAccountToDB(newAccount);
+
+ transaction = new Transaction(deposit, new Date(), newAccount.getAcctNum(), "Opened account", true);
+ transactionServices.saveTransactionToDB(transaction);
+ }
+
+ 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;
+ }
+
+ public int[] getAccountRowsByUser(User user) {
+ int[] recordRowNums;
+ recordRowNums = this.accountDB.findPartialRowMultiple(new String[]{user.getUserID().toString()}, new int[]{1});
+
+ return recordRowNums;
+ }
+
+ 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]), Account.Status.valueOf(info[5]), Checking.Overdraft.valueOf(info[4]));
+ } 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]), Account.Status.valueOf(info[5]));
+ } 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]), Account.Status.valueOf(info[5]));
+ }
+ return null;
+ }
+
+ //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);
+ }
+
+ // 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 void saveAccountToDB(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);
+ } else { // update a found row
+ this.accountDB.replaceRow(rowNum, stringRepOfAccount);
+ }
+ }
+
+ public DB getAccountDB() {
+ return this.accountDB;
+ }
+
+ public int getAccountDBLength() {
+ return accountDB.length();
+ }
+
+ public void clearAccountDB() {
+ accountDB.clear();
+ }
+
+ 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 Boolean closeAccount(Account account) throws BalanceRemainingException, FrozenAccountException, ClosedAccountException {
+ if (account.getBalance() != 0) {
+ throw new BalanceRemainingException();
+ } else if (account.getAcctStatus() == Account.Status.CLOSED) {
+ throw new ClosedAccountException();
+ } else if (account.getAcctStatus() == Account.Status.OFAC) {
+ throw new FrozenAccountException();
+ }
+ account.setAcctStatus(Account.Status.CLOSED);
+ Transaction transaction = new Transaction(0.0, new Date(), account.getAcctNum(), "Account Closed", false);
+ transactionServices.saveTransactionToDB(transaction);
+ saveAccountToDB(account);
+ return true;
+ }
+
+ public Boolean accountDeposit(Account account, double amount) throws ClosedAccountException, FrozenAccountException {
+ if (account.getAcctStatus() == Account.Status.CLOSED) {
+ throw new ClosedAccountException();
+ } else if (account.getAcctStatus() == Account.Status.OFAC) {
+ throw new FrozenAccountException();
+ }
+ saveAccountToDB(account);
+ account.setBalance(account.getBalance() + amount);
+ Transaction transaction = new Transaction(amount, new Date(), account.getAcctNum(), "ATM deposit", true);
+ transactionServices.saveTransactionToDB(transaction);
+ saveAccountToDB(account);
+ return true;
+
+ }
+
+
+ public Boolean attemptAccountWithdrawal(Account account, double amount) throws FrozenAccountException, InsufficientFundsException, ClosedAccountException {
+ if (account.getAcctStatus() == Account.Status.CLOSED) {
+ throw new ClosedAccountException();
+ } else if (account.getAcctStatus() == Account.Status.OFAC) {
+ throw new FrozenAccountException();
+ } else if (account.getBalance() < amount) {
+ if (!(account instanceof Checking)) { // savings or investment
+ throw new InsufficientFundsException();
+ } else if (((Checking) account).getOverdraft().equals(Checking.Overdraft.ON)) { // overdraft on
+ throw new InsufficientFundsException();
+ } else if (((Checking) account).getOverdraft().equals(Checking.Overdraft.AUTO)) { // autotransfer on
+ return attemptAutoTransfer(account, amount);
+ }
+ }
+ performWithdrawal(account, amount);
+ return true;
+ }
+
+ public Boolean attemptAutoTransfer(Account account, double amount) throws InsufficientFundsException {
+ User user = this.atm.getCurrentUser();
+ ArrayList userAccounts = getAccountsForUser(user);
+ double amountNeeded = amount - account.getBalance();
+
+ for (Account acct : userAccounts) { // find an account that we can transfer from
+ if (!acct.getAcctNum().equals(account.getAcctNum())
+ && acct.getAcctStatus().equals(Account.Status.OPEN)
+ && acct.getBalance() > amountNeeded) {
+ performAutoTransfer(account, amountNeeded, acct);
+ return true;
+ }
+ }
+ throw new InsufficientFundsException();
+ }
+
+ public void performAutoTransfer(Account account, double amountNeeded, Account acct) {
+ account.withdraw(account.getBalance()); // remove money from first account
+ transactionServices.saveTransactionToDB(new Transaction(-1 * account.getBalance(),new Date(), account.getAcctNum(), "ATM withdrawal", false));
+ saveAccountToDB(account);
+ acct.withdraw(amountNeeded); // remove the rest from the other account
+ transactionServices.saveTransactionToDB(new Transaction(-1 * amountNeeded,new Date(), acct.getAcctNum(), "ATM overdraft debit", false));
+ saveAccountToDB(acct);
+ }
+
+ public void performWithdrawal(Account account, double amount) {
+ account.deposit(-1 * amount);
+ saveAccountToDB(account);
+ //account.setBalance(account.getBalance() + amount);
+ Transaction transaction = new Transaction(-1 * amount, new Date(), account.getAcctNum(), "ATM withdrawal", false);
+ transactionServices.saveTransactionToDB(transaction);
+ saveAccountToDB(account);
+ }
+
+
+ public void applyInterest() {
+ ArrayList userAccounts = getAccountsForUser(this.atm.getCurrentUser());
+ for (Account account : userAccounts) {
+ if (account instanceof Savings && account.getAcctStatus().equals(Account.Status.OPEN)) {
+ calcInterest(account);
+ }
+ }
+ }
+
+ public void interestRateChange() {
+ ArrayList userAccounts = getAccountsForUser(this.atm.getCurrentUser());
+ Random random = new Random();
+
+ for (Account account : userAccounts) {
+ if (account instanceof Savings && account.getAcctStatus().equals(Account.Status.OPEN)) {
+ if (random.nextInt(5) >= 4) {
+ double newRate = getNewRate(random, (Savings) account);
+ setNewInterestRate(account, newRate);
+ }
+ }
+ }
+ }
+
+ public double getNewRate(Random random, Savings account) {
+ double newRate = account.getInterestRate() - .05 + .01 * random.nextInt(11);
+ if (newRate <= 0.0) {
+ newRate = 0.01;
+ }
+ return newRate;
+ }
+
+ public void setNewInterestRate(Account account, double newRate) {
+ ((Savings) account).setInterestRate(newRate);
+ saveAccountToDB(account);
+ Transaction transaction = new Transaction(Double.parseDouble(String.format("%.2f", account.getBalance())), new Date(), account.getAcctNum(), String.format("Interest rate changed to 0%.2f", newRate), true);
+ transactionServices.saveTransactionToDB(transaction);
+ }
+
+ 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);
+ transactionServices.saveTransactionToDB(transaction);
+ }
+
+ public void applyReturns() {
+ ArrayList userAccounts = getAccountsForUser(this.atm.getCurrentUser());
+ for (Account account : userAccounts) {
+ if (account instanceof Investment && account.getAcctStatus().equals(Account.Status.OPEN)) {
+ 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);
+ transactionServices.saveTransactionToDB(new Transaction(Double.parseDouble(String.format("%.2f", earnings)), new Date(), account.getAcctNum(), "Investment returns", earnings > 0));
+ }
+}
diff --git a/src/main/java/ATM/services/TransactionServices.java b/src/main/java/ATM/services/TransactionServices.java
new file mode 100644
index 0000000..872866e
--- /dev/null
+++ b/src/main/java/ATM/services/TransactionServices.java
@@ -0,0 +1,117 @@
+package ATM.services;
+
+import ATM.ATM;
+import ATM.DB;
+import ATM.Transaction;
+import ATM.User;
+import ATM.accounts.Account;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+
+public class TransactionServices {
+
+ private ATM atm;
+ private DB transactionDB; // 0: credit/debit 1: accountID 2: amount (signed) 3: timeStamp 4: description
+ private AccountServices accountServices;
+ private UserServices userServices;
+
+
+ public TransactionServices(DB transactionDB, ATM atm) {
+ this.transactionDB = transactionDB;
+ this.atm = atm;
+ }
+
+ public DB getTransactionDB() {
+ return this.transactionDB;
+ }
+
+ public void linkServices() {
+ this.userServices = atm.getUserServices();
+ this.accountServices = atm.getAccountServices();
+
+ }
+
+
+
+ public int[] getTransactionRowsByUser (User user) {
+ int[] accountRows = this.accountServices.getAccountRowsByUser(user);
+ ArrayList accountNums = new ArrayList<>();
+ for (int row : accountRows) {
+ accountNums.add(Integer.parseInt(this.accountServices.getAccountInfoByRow(row)[0]));
+ }
+
+ ArrayList rows = new ArrayList<>();
+ 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);
+ }
+ }
+ }
+
+ 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) {
+ 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) {
+ 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) {
+ 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].equals("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());
+ }
+
+ public void clearTransactionsDB(){
+ transactionDB.clear();
+ }
+
+}
diff --git a/src/main/java/ATM/services/TransferServices.java b/src/main/java/ATM/services/TransferServices.java
new file mode 100644
index 0000000..523f125
--- /dev/null
+++ b/src/main/java/ATM/services/TransferServices.java
@@ -0,0 +1,78 @@
+package ATM.services;
+
+
+import ATM.Exceptions.ClosedAccountException;
+import ATM.Exceptions.FrozenAccountException;
+import ATM.Exceptions.InsufficientFundsException;
+import ATM.User;
+import ATM.Transaction;
+import ATM.ATM;
+import ATM.accounts.Account;
+
+import java.util.ArrayList;
+import java.util.Date;
+
+public class TransferServices {
+
+
+// private ATM.DB userDB; // 0: ID 1: Last Name 2: First Name 3: cardNum 4: PW
+// private ATM.DB transactionDB; // 0: credit/debit 1: accountID 2: amount (signed) 3: timeStamp 4: description
+// private ATM.DB accountDB; // 0: accountID 1: ownerID 2: balance 3: type 4: risk/interest/null (type-dependent)
+
+ //Instance Fields
+ Account account;
+ private User currentUser;
+ private String acctStatus;
+ private ATM atm;
+ private Integer acctTransferTo;
+ private AccountServices accountServices;
+ private TransactionServices transactionServices;
+
+ //Constructor
+ public TransferServices(ATM atm, Account account) {
+ this.atm = atm;
+ this.currentUser = this.atm.getCurrentUser();
+ this.account = account;
+ this.transactionServices = this.atm.getTransactionServices();
+ this.accountServices = this.atm.getAccountServices();
+ }
+
+
+ public boolean executeTransfer(Account sourceAccount, Account targetAccount, double amountToDeposit) throws InsufficientFundsException, FrozenAccountException, ClosedAccountException {
+
+ checkTransferExceptions(sourceAccount, targetAccount, amountToDeposit);
+
+ targetAccount.setBalance(targetAccount.getBalance() + amountToDeposit);
+ sourceAccount.setBalance(sourceAccount.getBalance() - amountToDeposit);
+ accountServices.saveAccountToDB(targetAccount);
+ accountServices.saveAccountToDB(sourceAccount);
+
+ transactionServices.saveTransactionToDB(new Transaction(amountToDeposit * -1, new Date(), sourceAccount.getAcctNum(),String.format("Transfer to account %d", targetAccount.getAcctNum()), false ));
+ transactionServices.saveTransactionToDB(new Transaction(amountToDeposit, new Date(), targetAccount.getAcctNum(),String.format("Transfer from account %d", sourceAccount.getAcctNum()), true ));
+
+ return true;
+ }
+
+ public void checkTransferExceptions(Account sourceAccount, Account targetAccount, double amountToDeposit) throws InsufficientFundsException, ClosedAccountException, FrozenAccountException {
+ if (amountToDeposit > sourceAccount.getBalance()) {
+ throw new InsufficientFundsException();
+ }
+ else if (targetAccount.getAcctStatus() == Account.Status.OFAC) {
+ throw new FrozenAccountException();
+ }
+ else if (targetAccount.getAcctStatus() == Account.Status.CLOSED) {
+ throw new ClosedAccountException();
+ }
+ }
+}
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/main/java/ATM/services/UserServices.java b/src/main/java/ATM/services/UserServices.java
new file mode 100644
index 0000000..8c8a633
--- /dev/null
+++ b/src/main/java/ATM/services/UserServices.java
@@ -0,0 +1,125 @@
+package ATM.services;
+
+import ATM.ATM;
+import ATM.DB;
+import ATM.User;
+import ATM.Console;
+
+import java.util.ArrayList;
+
+public class UserServices {
+
+ private DB userDB;
+ private ATM atm;
+
+ public UserServices(DB userDB, ATM atm) {
+ this.userDB = userDB;
+ this.atm = atm;
+ }
+
+ 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 Integer getUserRowByID (Integer ID) {
+ return this.userDB.findPartialRow(new String[]{ID.toString()}, new int[]{0});
+ }
+
+ public String [] getUserInfoByID (Integer ID) {
+ int rowNumOfUser = this.userDB.findPartialRow(new String[] {ID.toString()}, new int[] {0});
+ return this.userDB.readRow(rowNumOfUser);
+ }
+
+ public String [] getUserInfoByCardNum (Integer cardNum) {
+ int rowNumOfUser = this.userDB.findPartialRow(new String[] {cardNum.toString()}, new int[] {3});
+ return this.userDB.readRow(rowNumOfUser);
+ }
+
+ public void saveUserToDB(User user) {
+ String[] stringRepOfUser = user.toStringArray();
+ int userID = user.getUserID();
+ int rowNum = getUserRowByID(userID);
+ if (rowNum == -1) { // user isn't in ATM.DB yet
+ this.userDB.addRow(stringRepOfUser);
+ } else { // update a found row
+ this.userDB.replaceRow(rowNum, stringRepOfUser);
+ }
+ }
+ public void clearUserDB(){
+ this.userDB.clear();
+ }
+
+ public int getUserDBLength(){
+ return userDB.length();
+ }
+
+ // no test - needs input
+ public User authenticate() {
+ //Read ATM.User's card
+ Console.print("Card Number: ");
+ int cardNum = Console.getInteger();
+
+ // find user in ATM.DB
+ String[] userInfo = getUserInfoByCardNum(cardNum);
+ if (userInfo == null){
+ return null;
+ }
+ // check PW
+ String password = Console.getInput("Enter Password: ");
+ if(password.equals(userInfo[4])) {
+ // 0: ID 1: Last Name 2: First Name 3: cardNum 4: PW
+ this.atm.setCurrentUser(new User(userInfo[2], userInfo[1], userInfo[4], Integer.parseInt(userInfo[0]), Integer.parseInt(userInfo[3])));
+ return this.atm.getCurrentUser();
+ } else {
+ return null;
+ }
+ }
+
+ public User createNewUser(String firstName, String lastName, String password) {
+ int userID = genUserID();
+ int cardNumber = genCardNum();
+ User user = new User(firstName, lastName, password, userID, cardNumber);
+ saveUserToDB(user);
+ return user;
+ }
+
+ public Integer genUserID(){
+ Integer newUserID;
+ newUserID = getMaxUserNumber() + 1;
+ return newUserID;
+ }
+
+ public 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();
+ }
+ return Integer.parseInt(numString);
+ }
+
+ public boolean changeName (User user, String firstName, String lastName) {
+ if (!firstName.equals("") && !lastName.equals("")) {
+ user.setFirstName(firstName);
+ user.setLastName(lastName);
+ saveUserToDB(user);
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+}
diff --git a/src/main/java/Main.java b/src/main/java/Main.java
deleted file mode 100644
index 1dbc0cb..0000000
--- a/src/main/java/Main.java
+++ /dev/null
@@ -1,9 +0,0 @@
-/**
- * Created by iyasuwatts on 10/17/17.
- */
-public class Main {
-
- public static void main(String[] args){
-
- }
-}
diff --git a/src/test/.DS_Store b/src/test/.DS_Store
new file mode 100644
index 0000000..55c1fcb
Binary files /dev/null and b/src/test/.DS_Store differ
diff --git a/src/test/java/.DS_Store b/src/test/java/.DS_Store
new file mode 100644
index 0000000..5008ddf
Binary files /dev/null and b/src/test/java/.DS_Store differ
diff --git a/src/test/java/ATM/ATMTest.java b/src/test/java/ATM/ATMTest.java
new file mode 100644
index 0000000..e8d6eb6
--- /dev/null
+++ b/src/test/java/ATM/ATMTest.java
@@ -0,0 +1,137 @@
+package ATM;
+
+import ATM.ATM;
+import ATM.accounts.Account;
+import ATM.accounts.Checking;
+import ATM.accounts.Investment;
+import ATM.accounts.Savings;
+import ATM.services.AccountServices;
+import ATM.services.TransactionServices;
+import ATM.services.UserServices;
+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 java.util.ArrayList;
+import java.util.Date;
+import java.util.Arrays;
+
+@RunWith(JUnitParamsRunner.class)
+public class ATMTest {
+
+ private ATM atm;
+ private UserServices userServices;
+ private TransactionServices transactionServices;
+ private AccountServices accountServices;
+
+ @Before
+ public void setUp() throws Exception {
+ atm = new ATM ("testuserDB.csv", "testaccountDB.csv", "testtransactionDB.csv");
+ transactionServices = atm.getTransactionServices();
+ userServices = atm.getUserServices();
+ accountServices = atm.getAccountServices();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ atm.getUserDB().clear();
+ atm.getAccountDB().clear();
+ atm.getTransactionDB().clear();
+ }
+
+ @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 foundUser = atm.getCurrentUser();
+ Assert.assertEquals(foundUser, null);
+
+ User user = new User("Jim","Brown","goolybib", 12, 12343);
+ atm.setCurrentUser(user);
+
+ foundUser = atm.getCurrentUser();
+ Assert.assertEquals(foundUser, user);
+ }
+
+ @Test
+ public void logOutTest() {
+ User foundUser = atm.getCurrentUser();
+ Assert.assertEquals(foundUser, null);
+
+ User user = new User("Jim","Brown","goolybib", 12, 12343);
+ atm.setCurrentUser(user);
+
+ foundUser = atm.getCurrentUser();
+ Assert.assertEquals(user, foundUser);
+
+ atm.logOut();
+
+ foundUser = atm.getCurrentUser();
+ Assert.assertEquals(null, foundUser);
+
+ }
+
+
+ // 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();
+// }
+
+}
\ No newline at end of file
diff --git a/src/test/java/ATM/ConsoleTest.java b/src/test/java/ATM/ConsoleTest.java
new file mode 100644
index 0000000..f650534
--- /dev/null
+++ b/src/test/java/ATM/ConsoleTest.java
@@ -0,0 +1,50 @@
+package ATM;
+
+import ATM.Console;
+import junitparams.JUnitParamsRunner;
+import junitparams.Parameters;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@RunWith(JUnitParamsRunner.class)
+public class ConsoleTest {
+
+ @Test
+ public void print() {
+ }
+
+ @Test
+ public void println() {
+ }
+
+ @Test
+ public void menuOptionsTest() {
+ String[] options = new String[] {"Live", "Die", "Repeat"};
+ String header = "accounts.Account Creation ATM.interfaces.Menu";
+ options = new String[] {"Live", "Die", "Repeat", "Bump", "Set", "Spike", "Towel"};
+ //ATM.Console.getInput(options);
+ //ATM.Console.getInput(header, options);
+ }
+
+ @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));
+ }
+
+}
diff --git a/src/test/java/ATM/DBTest.java b/src/test/java/ATM/DBTest.java
new file mode 100644
index 0000000..59eefe4
--- /dev/null
+++ b/src/test/java/ATM/DBTest.java
@@ -0,0 +1,575 @@
+package ATM;
+
+import ATM.DB;
+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);
+ db1.delete();
+ }
+
+ @Test
+ 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() {
+ Random random = new Random();
+ String fileName = Integer.toString(Math.abs(random.nextInt())) + ".csv";
+
+ DB testDB = null;
+ try {
+ testDB = new DB(fileName, 4);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ String actual = testDB.getFileName();
+ Assert.assertEquals(testDB.getFileName(), fileName);
+ testDB.delete();
+ }
+
+ @Test
+ public void pathToFileNameTest() {
+ String input = "/Users/josh/Desktop/Projects/CR-MacroLabs-OOP-ATM.ATM/data/610393892.csv";
+ String expected = "610393892.csv";
+ Assert.assertEquals(expected, DB.pathToFileName(input));
+ }
+
+ @Test
+ public void fileNametoPathTest() {
+ String input = "610393892.csv";
+ String expected = System.getProperty("user.dir")+"/data/610393892.csv";
+ Assert.assertEquals(expected, DB.fileNameToPath(input, System.getProperty("user.dir")));
+ }
+
+ @Test
+ public void tempStuff() {
+// String fileName = "test.csv";
+// ATM.DB testDB = null;
+// try {
+// testDB = new ATM.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());
+ db1.delete();
+ }
+
+ @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));
+ }
+ }
+ db1.delete();
+ }
+
+ @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());
+ db1.delete();
+ }
+
+ @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());
+ 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());
+ }
+
+ @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());
+ }
+
+ @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();
+ }
+
+ @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();
+ }
+
+ @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));
+ }
+
+ @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 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(-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((int)0, (int)db1.findPartialRowMultiple(new String[] {"Iteasdm"}, new int[] {1}).length);
+
+ db1.delete();
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/ATM/TransactionTest.java b/src/test/java/ATM/TransactionTest.java
new file mode 100644
index 0000000..08e3f8f
--- /dev/null
+++ b/src/test/java/ATM/TransactionTest.java
@@ -0,0 +1,18 @@
+package ATM;
+
+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/ATM/UserTest.java b/src/test/java/ATM/UserTest.java
new file mode 100644
index 0000000..f4aa143
--- /dev/null
+++ b/src/test/java/ATM/UserTest.java
@@ -0,0 +1,30 @@
+package ATM;
+
+import ATM.User;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+public class UserTest {
+
+ @Before
+ public void setUp() throws Exception {
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ }
+
+ @Test
+ public void getCardNumberTest() {
+ User user = new User("J","J","123",12,456);
+ Assert.assertEquals(456,(int) user.getCardNumber());
+ }
+
+ @Test
+ public void getPasswordTest() {
+ User user = new User("J","J","123",12,456);
+ Assert.assertEquals("123",user.getPassword());
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/ATM/accounts/AccountTest.java b/src/test/java/ATM/accounts/AccountTest.java
new file mode 100644
index 0000000..5582d40
--- /dev/null
+++ b/src/test/java/ATM/accounts/AccountTest.java
@@ -0,0 +1,93 @@
+package ATM.accounts;
+
+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, Account.Status.valueOf("OPEN"), Checking.Overdraft.OFF);
+ Double expected = 0.0;
+
+
+ // Then
+ Double actual = account.getBalance();
+ assertEquals(expected, actual);
+ }
+
+ @Test
+ public void deposit_test() {
+ // Given
+ Account account = new Checking(0.0, 3,3, Account.Status.valueOf("OPEN"), Checking.Overdraft.OFF);
+ Double expected = 40.0;
+
+ //When
+ account.deposit(40.0);
+
+ // Then
+ Double actual = account.getBalance();
+ assertEquals(expected, actual);
+
+ }
+
+ @Test
+ public void withdraw_test() {
+ // Given
+ Account account = new Checking(80.0, 3,3, Account.Status.valueOf("OPEN"), Checking.Overdraft.OFF);
+ Double expected = 40.0;
+
+ //When
+ account.withdraw(40.0);
+
+ // Then
+ Double actual = account.getBalance();
+ assertEquals(expected, actual);
+ }
+
+
+ @Test
+ public void getAcctHist() {
+ }
+
+ @Test
+ public void getOwnerID() {
+ // Given
+ Account account = new Checking(0.0, 3,3, Account.Status.valueOf("OPEN"), Checking.Overdraft.OFF);
+ Integer expected = 3;
+
+
+ // Then
+ Integer actual = account.getOwnerID();
+ assertEquals(expected, actual);
+ }
+
+ @Test
+ public void getAcctNum() {
+ // Given
+ Account account = new Checking(0.0, 3,3, Account.Status.valueOf("OPEN"), Checking.Overdraft.OFF);
+ Integer expected = 3;
+
+
+ // Then
+ Integer actual = account.getAcctNum();
+ assertEquals(expected, actual);
+
+ }
+
+
+ @Test
+ public void setRisk() {
+ // Given
+ Investment account = new Investment(80000.0, 3,3, 0.09, Account.Status.valueOf("OPEN"));
+ Double expected = 0.9;
+
+ account.setRisk(0.9);
+
+ // Then
+ Double actual = account.getRisk();
+ assertEquals(expected, actual);
+ }
+}
diff --git a/src/test/java/ATM/accounts/CheckingTest.java b/src/test/java/ATM/accounts/CheckingTest.java
new file mode 100644
index 0000000..5db1a22
--- /dev/null
+++ b/src/test/java/ATM/accounts/CheckingTest.java
@@ -0,0 +1,35 @@
+package ATM.accounts;
+
+import ATM.ATM;
+import ATM.User;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+public class CheckingTest {
+
+ private ATM atm;
+ private User user;
+ private Checking account;
+
+ @Before
+ public void setUp() throws Exception {
+ atm = new ATM("testuserDB.csv", "testaccountDB.csv", "testtransactionDB.csv");
+ user = new User("","","12",44, 123);
+ }
+
+ @Test
+ public void getOverdraft() {
+ account = new Checking(12.00, 44, 123, Account.Status.OPEN, Checking.Overdraft.ON);
+ Assert.assertEquals(Checking.Overdraft.ON, account.getOverdraft());
+ }
+
+ @Test
+ public void setOverdraft() {
+ account = new Checking(12.00, 44, 123, Account.Status.OPEN, Checking.Overdraft.ON);
+ Assert.assertEquals(Checking.Overdraft.ON, account.getOverdraft());
+ account.setOverdraft(Checking.Overdraft.OFF);
+ Assert.assertEquals(Checking.Overdraft.OFF, account.getOverdraft());
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/ATM/interfaces/StoreableTest.java b/src/test/java/ATM/interfaces/StoreableTest.java
new file mode 100644
index 0000000..c2d38c5
--- /dev/null
+++ b/src/test/java/ATM/interfaces/StoreableTest.java
@@ -0,0 +1,83 @@
+package ATM.interfaces;
+
+import ATM.User;
+import ATM.accounts.Account;
+import ATM.accounts.Checking;
+import ATM.accounts.Investment;
+import ATM.accounts.Savings;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class StoreableTest {
+
+ @Test
+ 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(12.23, 23, 3432, Account.Status.CLOSED, Checking.Overdraft.OFF);
+
+ String[] actual = account.toStringArray();
+ String[] expected = new String[] {
+ "3432",
+ "23",
+ "12.23",
+ "Checking",
+ "OFF",
+ "CLOSED"
+ };
+
+ Assert.assertEquals(actual, expected);
+ }
+
+ @Test
+ public void toStringArrayAccountSavings() {
+ Account account = new Savings(12.23, 23, 3432, 0.05, Account.Status.OFAC);
+
+ String[] actual = account.toStringArray();
+ String[] expected = new String[] {
+ "3432",
+ "23",
+ "12.23",
+ "Savings",
+ "0.05",
+ "OFAC"
+ };
+
+ Assert.assertEquals(actual, expected);
+ }
+
+ @Test
+ public void toStringArrayAccountInvestment() {
+ Account account = new Investment(12.23, 23, 3432, 0.2, Account.Status.OPEN);
+
+ String[] actual = account.toStringArray();
+ String[] expected = new String[] {
+ "3432",
+ "23",
+ "12.23",
+ "Investment",
+ "0.2",
+ "OPEN"
+ };
+
+ Assert.assertEquals(actual, expected);
+ }
+
+ @Test
+ public void saveToDB() {
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/ATM/menus/AccountMenuTest.java b/src/test/java/ATM/menus/AccountMenuTest.java
new file mode 100644
index 0000000..ac9cf03
--- /dev/null
+++ b/src/test/java/ATM/menus/AccountMenuTest.java
@@ -0,0 +1,73 @@
+package ATM.menus;
+
+import ATM.ATM;
+import ATM.Exceptions.FrozenAccountException;
+import ATM.accounts.Account;
+import ATM.accounts.Checking;
+import ATM.accounts.Investment;
+import ATM.accounts.Savings;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+public class AccountMenuTest {
+
+ @Before
+ public void setUp() throws Exception {
+
+ }
+
+ @Test
+ public void getName() throws FrozenAccountException {
+ Account account = new Checking(123.45, 123, 9675, Account.Status.OPEN, Checking.Overdraft.OFF);
+ AccountMenu acctMenu = new AccountMenu(new ATM("users.csv", "accounts.csv", "transactions.csv"), account);
+ Assert.assertEquals("Account Menu", acctMenu.getName());
+
+ }
+
+ @Test
+ public void getHeaderTest() throws FrozenAccountException{
+ Account account = new Checking(123.45, 123, 9675, Account.Status.OPEN, Checking.Overdraft.OFF);
+ AccountMenu acctMenu = new AccountMenu(new ATM("users.csv", "accounts.csv", "transactions.csv"), account);
+ String actual = acctMenu.getHeader();
+ String expected = "Checking Account #9675 Balance: $123.45";
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void getHeaderTest2() throws FrozenAccountException{
+ Account account = new Savings(123.45, 123, 9675, .03, Account.Status.OPEN);
+ AccountMenu acctMenu = new AccountMenu(new ATM("users.csv", "accounts.csv", "transactions.csv"), account);
+ String actual = acctMenu.getHeader();
+ String expected = "Savings Account #9675 Balance: $123.45 Interest Rate: 0.03%%";
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void getHeaderTest3() throws FrozenAccountException{
+ Account account = new Investment(123.45, 123, 9675, .08, Account.Status.OPEN);
+ AccountMenu acctMenu = new AccountMenu(new ATM("users.csv", "accounts.csv", "transactions.csv"), account);
+ String actual = acctMenu.getHeader();
+ String expected = "Investment Account #9675 Balance: $123.45 Risk: 8/10";
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void getHeaderTest4() throws FrozenAccountException{
+ Account account = new Investment(123.45, 123, 9675, .08, Account.Status.CLOSED);
+ AccountMenu acctMenu = new AccountMenu(new ATM("users.csv", "accounts.csv", "transactions.csv"), account);
+ String actual = acctMenu.getHeader();
+ String expected = "Investment Account #9675 Balance: $123.45 Risk: 8/10 (CLOSED)";
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test(expected = FrozenAccountException.class)
+ public void getHeaderTest5() throws FrozenAccountException{
+ Account account = new Investment(123.45, 123, 9675, .08, Account.Status.OFAC);
+ AccountMenu acctMenu = new AccountMenu(new ATM("users.csv", "accounts.csv", "transactions.csv"), account);
+ String actual = acctMenu.getHeader();
+ String expected = "Investment Account #9675 Balance: $123.45 Risk: 8/10 (OFAC)";
+ Assert.assertEquals(expected, actual);
+ }
+
+}
\ No newline at end of file
diff --git a/src/test/java/ATM/menus/MainMenuTest.java b/src/test/java/ATM/menus/MainMenuTest.java
new file mode 100644
index 0000000..4ba590e
--- /dev/null
+++ b/src/test/java/ATM/menus/MainMenuTest.java
@@ -0,0 +1,86 @@
+package ATM.menus;
+
+import ATM.ATM;
+import ATM.User;
+import ATM.Exceptions.ClosedAccountException;
+import ATM.Exceptions.FrozenAccountException;
+import ATM.Exceptions.InsufficientFundsException;
+import ATM.accounts.Account;
+import ATM.accounts.Checking;
+import ATM.accounts.Savings;
+import ATM.services.AccountServices;
+import ATM.services.TransactionServices;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+
+public class MainMenuTest {
+
+ private ATM atm;
+ private AccountServices accountServices;
+ private TransactionServices transactionServices;
+ private MainMenu menu;
+
+ @Before
+ public void setUp() throws Exception {
+ atm = new ATM("testuserDB.csv", "testaccountDB.csv", "testtransactionDB.csv");
+ accountServices = atm.getAccountServices();
+ transactionServices = atm.getTransactionServices();
+ menu = new MainMenu(atm);
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ atm.getUserDB().clear();
+ atm.getAccountDB().clear();
+ atm.getTransactionDB().clear();
+ }
+
+ @Test
+ public void addAccountOptionsTest1() throws FrozenAccountException, ClosedAccountException, InsufficientFundsException {
+ User user = new User("Jim","Dandy","1234",23,123);
+ atm.setCurrentUser(user);
+ Account account1 = new Checking(500.00,23,12, Account.Status.CLOSED, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ Account account2 = new Checking(600.00,23,15, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account2);
+ Account account3 = new Savings(700.00,23,16, .10, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account3);
+ Account account4 = new Checking(800.00,23,19, Account.Status.OFAC, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account4);
+ Account account5 = new Checking(900.00,23,22, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account5);
+ Account account6 = new Checking(10900.00,67,21, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account6);
+
+ ArrayList result = menu.addAccountOptions(new ArrayList(Arrays.asList("zarp")));
+ String expected = "Checking #12 ($500.00)";
+ String actual = result.get(1);
+
+ Assert.assertEquals(expected,actual);
+
+ expected = "Savings #16 ($700.00)";
+ actual = result.get(3);
+
+ Assert.assertEquals(expected,actual);
+
+ expected = "Checking #19 (FROZEN)";
+ actual = result.get(4);
+
+ Assert.assertEquals(expected,actual);
+
+ }
+
+ @Test
+ public void getNameTest() throws ClosedAccountException, FrozenAccountException {
+ User user = new User("Jim","Dandy","1234",23,123);
+ Account account1 = new Checking(500.00,23,12, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+
+ Assert.assertEquals("Main Menu", menu.getName());
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/ATM/menus/NewUserMenuTest.java b/src/test/java/ATM/menus/NewUserMenuTest.java
new file mode 100644
index 0000000..e61a7c2
--- /dev/null
+++ b/src/test/java/ATM/menus/NewUserMenuTest.java
@@ -0,0 +1,30 @@
+package ATM.menus;
+
+import ATM.ATM;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class NewUserMenuTest {
+ private ATM atm;
+ private NewUserMenu menu;
+
+ @Before
+ public void setUp() throws Exception {
+ atm = new ATM("testuserDB.csv", "testaccountDB.csv", "testtransactionDB.csv");
+ menu = new NewUserMenu(atm);
+ }
+
+ @Test
+ public void handleChoice() {
+ // yeah, that's a cheap ploy for coverage
+ menu.handleChoice(1);
+ }
+
+ @Test
+ public void getName() {
+ Assert.assertEquals("New User Menu", menu.getName());
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/ATM/menus/TransferServicesMenuTest.java b/src/test/java/ATM/menus/TransferServicesMenuTest.java
new file mode 100644
index 0000000..943bc3f
--- /dev/null
+++ b/src/test/java/ATM/menus/TransferServicesMenuTest.java
@@ -0,0 +1,173 @@
+package ATM.menus;
+
+import ATM.ATM;
+import ATM.Exceptions.InsufficientFundsException;
+import ATM.User;
+import ATM.Exceptions.ClosedAccountException;
+import ATM.Exceptions.FrozenAccountException;
+import ATM.accounts.Account;
+import ATM.accounts.Checking;
+import ATM.accounts.Investment;
+import ATM.accounts.Savings;
+import ATM.services.AccountServices;
+import ATM.services.TransactionServices;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+
+public class TransferServicesMenuTest {
+
+ private ATM atm;
+ private AccountServices accountServices;
+ private TransactionServices transactionServices;
+ private TransferServicesMenu menu;
+
+ @Before
+ public void setUp() throws Exception {
+ atm = new ATM("testuserDB.csv", "testaccountDB.csv", "testtransactionDB.csv");
+ accountServices = atm.getAccountServices();
+ transactionServices = atm.getTransactionServices();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ atm.getUserDB().clear();
+ atm.getAccountDB().clear();
+ atm.getTransactionDB().clear();
+ }
+
+ @Test
+ public void getHeaderTest() throws ClosedAccountException, FrozenAccountException {
+ User user = new User("Jim","Dandy","1234",23,123);
+ Account account1 = new Checking(500.00,23,12, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ Account account2 = new Savings(600.00,23,15, .01, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account2);
+
+ menu = new TransferServicesMenu(atm, account1, accountServices.getAccountsForUser(user));
+
+ String expected = "Transfer from: Checking Account #12 Balance: $500.00";
+ String actual = menu.getHeader();
+
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void getHeaderTest2() throws ClosedAccountException, FrozenAccountException {
+ User user = new User("Jim","Dandy","1234",23,123);
+ Account account1 = new Checking(500.00,23,12, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ Account account2 = new Savings(600.00,23,15, .01, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account2);
+
+ menu = new TransferServicesMenu(atm, account2, accountServices.getAccountsForUser(user));
+
+ String expected = "Transfer from: Savings Account #15 Balance: $600.00 Interest Rate: 0.01%%";
+ String actual = menu.getHeader();
+
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void getHeaderTest3() throws ClosedAccountException, FrozenAccountException {
+ User user = new User("Jim","Dandy","1234",23,123);
+ Account account1 = new Checking(500.00,23,12, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ Account account2 = new Investment(600.00,23,15, .04, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account2);
+
+ menu = new TransferServicesMenu(atm, account2, accountServices.getAccountsForUser(user));
+
+ String expected = "Transfer from: Investment Account #15 Balance: $600.00 Risk: 4/10";
+ String actual = menu.getHeader();
+
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test (expected = ClosedAccountException.class)
+ public void constructorCrashTest() throws ClosedAccountException, FrozenAccountException {
+ User user = new User("Jim","Dandy","1234",23,123);
+ Account account1 = new Checking(500.00,23,12, Account.Status.CLOSED, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ Account account2 = new Savings(600.00,23,15, .01, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account2);
+
+ menu = new TransferServicesMenu(atm, account1, accountServices.getAccountsForUser(user));
+ }
+
+ @Test (expected = FrozenAccountException.class)
+ public void constructorCrashTest2() throws ClosedAccountException, FrozenAccountException {
+ User user = new User("Jim","Dandy","1234",23,123);
+ Account account1 = new Checking(500.00,23,12, Account.Status.CLOSED, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ Account account2 = new Savings(600.00,23,15, .01, Account.Status.OFAC);
+ accountServices.saveAccountToDB(account2);
+
+ menu = new TransferServicesMenu(atm, account2, accountServices.getAccountsForUser(user));
+ }
+
+ @Test
+ public void accountTransferTest1() throws FrozenAccountException, ClosedAccountException, InsufficientFundsException {
+ User user = new User("Jim","Dandy","1234",23,123);
+ Account account1 = new Checking(500.00,23,12, Account.Status.CLOSED, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ Account account2 = new Checking(600.00,23,15, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account2);
+ Account account3 = new Savings(700.00,23,16, .10, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account3);
+ Account account4 = new Checking(800.00,23,19, Account.Status.OFAC, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account4);
+ Account account5 = new Checking(900.00,23,22, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account5);
+
+ menu = new TransferServicesMenu(atm, account2, accountServices.getAccountsForUser(user));
+
+ String expected = "Checking #12 ($500.00)";
+ String actual = menu.addAccountOptions(new ArrayList()).get(0);
+
+ Assert.assertEquals(expected,actual);
+
+ expected = "Savings #16 ($700.00)";
+ actual = menu.addAccountOptions(new ArrayList()).get(1);
+
+ Assert.assertEquals(expected,actual);
+ }
+
+ @Test
+ public void getDestinationAccountsTest() throws ClosedAccountException, FrozenAccountException {
+ User user = new User("Jim","Dandy","1234",23,123);
+ Account account1 = new Checking(500.00,23,12, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ Account account2 = new Checking(600.00,23,15, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account2);
+ Account account3 = new Savings(700.00,23,16, .10, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account3);
+ Account account4 = new Checking(800.00,23,19, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account4);
+ Account account5 = new Checking(900.00,23,22, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account5);
+
+ menu = new TransferServicesMenu(atm, account2, accountServices.getAccountsForUser(user));
+
+ ArrayList expected = new ArrayList(Arrays.asList(account1,account3,account4,account5));
+ ArrayList actual = menu.getDestinationAccounts();
+ for (int i = 0; i < actual.size(); i++) {
+ Assert.assertTrue(expected.get(i).equals(actual.get(i)));
+ }
+ }
+
+ @Test
+ public void getNameTest() throws ClosedAccountException, FrozenAccountException {
+ User user = new User("Jim","Dandy","1234",23,123);
+ Account account1 = new Checking(500.00,23,12, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+
+ menu = new TransferServicesMenu(atm, account1, accountServices.getAccountsForUser(user));
+
+ Assert.assertEquals("Transfer Menu", menu.getName());
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/ATM/menus/UserMenuTest.java b/src/test/java/ATM/menus/UserMenuTest.java
new file mode 100644
index 0000000..686b684
--- /dev/null
+++ b/src/test/java/ATM/menus/UserMenuTest.java
@@ -0,0 +1,16 @@
+package ATM.menus;
+
+import ATM.ATM;
+import org.junit.Assert;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class UserMenuTest {
+
+ @Test
+ public void getName() {
+ ATM atm = new ATM("testuserDB.csv", "testaccountDB.csv", "testtransactionDB.csv");
+ Assert.assertEquals("User Menu", new UserMenu(atm).getName());
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/ATM/services/AccountServicesTest.java b/src/test/java/ATM/services/AccountServicesTest.java
new file mode 100644
index 0000000..d69af3e
--- /dev/null
+++ b/src/test/java/ATM/services/AccountServicesTest.java
@@ -0,0 +1,815 @@
+package ATM.services;
+
+import ATM.ATM;
+import ATM.Exceptions.BalanceRemainingException;
+import ATM.Exceptions.ClosedAccountException;
+import ATM.Exceptions.FrozenAccountException;
+import ATM.Exceptions.InsufficientFundsException;
+import ATM.User;
+import ATM.accounts.Account;
+import ATM.accounts.Checking;
+import ATM.accounts.Investment;
+import ATM.accounts.Savings;
+import ATM.Transaction;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Random;
+
+public class AccountServicesTest {
+ private ATM atm;
+ private AccountServices accountServices;
+ private UserServices userServices;
+
+ @Before
+ public void setUp() throws Exception {
+ atm = new ATM("testuserDB.csv", "testaccountDB.csv", "testtransactionDB.csv");
+ accountServices = atm.getAccountServices();
+ userServices = atm.getUserServices();
+ accountServices.linkServices();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ atm.getUserDB().clear();
+ atm.getAccountDB().clear();
+ atm.getTransactionDB().clear();
+ }
+
+
+ @Test
+ public void getMaxAccountNumberTest() {
+ accountServices.clearAccountDB();
+
+ int actual = accountServices.getMaxAccountNumber();
+ int expected = 0;
+
+ Assert.assertEquals(actual, expected);
+
+ Account account1 = new Checking(1532.34, 23, 2123, Account.Status.valueOf("OPEN"), Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+
+ actual = accountServices.getMaxAccountNumber();
+ expected = 2123;
+
+ Assert.assertEquals(actual, expected);
+
+ Account account2 = new Savings(120.43, 12, 33, 0.01, Account.Status.valueOf("OPEN"));
+ accountServices.saveAccountToDB(account2);
+
+ actual = accountServices.getMaxAccountNumber();
+ expected = 2123;
+
+ Assert.assertEquals(actual, expected);
+
+ Account account3 = new Investment(234023.23, 42, 48, 0.06, Account.Status.valueOf("OPEN"));
+ accountServices.saveAccountToDB(account3);
+ Account account4 = new Checking(1532.34, 42, 5423, Account.Status.valueOf("OPEN"), Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account4);
+ Account account5 = new Savings(120.43, 98, 333223, 0.01, Account.Status.valueOf("OPEN"));
+ accountServices.saveAccountToDB(account5);
+ Account account6 = new Investment(234023.23, 42, 9948, 0.06, Account.Status.valueOf("OPEN"));
+ accountServices.saveAccountToDB(account6);
+ Account account7 = new Checking(1532.34, 23, 515, Account.Status.valueOf("OPEN"), Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account7);
+ Account account8 = new Savings(120.43, 12, 749, 0.01, Account.Status.valueOf("OPEN"));
+ accountServices.saveAccountToDB(account8);
+ Account account9 = new Investment(234023.23, 42, 904, 0.06, Account.Status.valueOf("OPEN"));
+ accountServices.saveAccountToDB(account9);
+
+ actual = accountServices.getMaxAccountNumber();
+ expected = 333223;
+
+ Assert.assertEquals(actual, expected);
+ }
+
+ @Test
+ public void getAccountInfoByID() {
+ accountServices.clearAccountDB();
+
+ Account account1 = new Checking(1532.34, 23, 1232123, Account.Status.valueOf("OPEN"), Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ Account account2 = new Savings(120.43, 12, 333223, 0.01, Account.Status.valueOf("OPEN"));
+ accountServices.saveAccountToDB(account2);
+ Account account3 = new Investment(234023.23, 42, 9948, 0.06, Account.Status.valueOf("OPEN"));
+ accountServices.saveAccountToDB(account3);
+
+ String[] actual = accountServices.getAccountInfoByID(333223);
+ String[] expected = account2.toStringArray();
+
+ Assert.assertEquals(actual, expected);
+ }
+
+ @Test
+ public void getAccountRowByID() {
+ Account account1 = new Checking(1532.34, 23, 1232123, Account.Status.valueOf("OPEN"), Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ Account account2 = new Savings(120.43, 12, 333223, 0.01, Account.Status.valueOf("OPEN"));
+ accountServices.saveAccountToDB(account2);
+ Account account3 = new Investment(234023.23, 42, 9948, 0.06, Account.Status.valueOf("OPEN"));
+ accountServices.saveAccountToDB(account3);
+
+ int actual = accountServices.getAccountRowByID(333223);
+ int expected = 1;
+
+ Assert.assertEquals(expected, actual);
+
+ actual = accountServices.getAccountRowByID(1232123);
+ expected = 0;
+
+ Assert.assertEquals(expected, actual);
+
+ actual = accountServices.getAccountRowByID(9948);
+ expected = 2;
+
+ Assert.assertEquals(expected, actual);
+
+ actual = accountServices.getAccountRowByID(99323248);
+ expected = -1;
+
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void getAccountIDsByUserTest() {
+ accountServices.clearAccountDB();
+ userServices.clearUserDB();
+
+
+ User user1 = new User("Jim", "Brown", "goolybib", 98, 12343);
+ userServices.saveUserToDB(user1);
+ User user2 = new User("Ji123m", "Bro23wn", "gool321ybib", 42, 1234313);
+ userServices.saveUserToDB(user2);
+ User user3 = new User("Jane", "Himne", "gasdsdool321ybib", 33, 313);
+ userServices.saveUserToDB(user3);
+
+
+ Account account1 = new Checking(1532.34, 23, 1232123, Account.Status.valueOf("OPEN"), Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ Account account2 = new Savings(120.43, 12, 33, 0.01, Account.Status.valueOf("OPEN"));
+ accountServices.saveAccountToDB(account2);
+ Account account3 = new Investment(234023.23, 42, 48, 0.06, Account.Status.valueOf("OPEN"));
+ accountServices.saveAccountToDB(account3);
+ Account account4 = new Checking(1532.34, 42, 5423, Account.Status.valueOf("OPEN"), Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account4);
+ Account account5 = new Savings(120.43, 98, 333223, 0.01, Account.Status.valueOf("OPEN"));
+ accountServices.saveAccountToDB(account5);
+ Account account6 = new Investment(234023.23, 42, 9948, 0.06, Account.Status.valueOf("OPEN"));
+ accountServices.saveAccountToDB(account6);
+ Account account7 = new Checking(1532.34, 23, 515, Account.Status.valueOf("OPEN"), Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account7);
+ Account account8 = new Savings(120.43, 12, 749, 0.01, Account.Status.valueOf("OPEN"));
+ accountServices.saveAccountToDB(account8);
+ Account account9 = new Investment(234023.23, 42, 904, 0.06, Account.Status.valueOf("OPEN"));
+ accountServices.saveAccountToDB(account9);
+
+ int[] rows = accountServices.getAccountRowsByUser(user1);
+ String[] accountInfo;
+ int[] accts = {333223};
+ for (int i = 0; i < rows.length; i++) {
+ accountInfo = accountServices.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 = accountServices.getAccountRowsByUser(user2);
+ String[] accountInfo2;
+ int[] accts2 = {48, 5423, 9948, 904};
+ for (int i = 0; i < rows2.length; i++) {
+ accountInfo2 = accountServices.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 = accountServices.getAccountRowsByUser(user3);
+ String[] accountInfo3;
+ int[] accts3 = {};
+ for (int i = 0; i < rows3.length; i++) {
+ accountInfo3 = accountServices.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 getAccountsForUserTest() {
+ accountServices.clearAccountDB();
+ userServices.clearUserDB();
+
+ User user1 = new User("Jim", "Brown", "goolybib", 98, 12343);
+ userServices.saveUserToDB(user1);
+ User user2 = new User("Ji123m", "Bro23wn", "gool321ybib", 42, 1234313);
+ userServices.saveUserToDB(user2);
+ User user3 = new User("Jane", "Himne", "gasdsdool321ybib", 33, 313);
+ userServices.saveUserToDB(user3);
+
+ Account account1 = new Checking(1532.34, 23, 1232123, Account.Status.valueOf("OPEN"), Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ Account account2 = new Savings(120.43, 12, 33, 0.01, Account.Status.valueOf("OPEN"));
+ accountServices.saveAccountToDB(account2);
+ Account account3 = new Investment(234023.23, 42, 48, 0.06, Account.Status.valueOf("OPEN"));
+ accountServices.saveAccountToDB(account3);
+ Account account4 = new Checking(1532.34, 42, 5423, Account.Status.valueOf("OPEN"), Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account4);
+ Account account5 = new Savings(120.43, 98, 333223, 0.01, Account.Status.valueOf("OPEN"));
+ accountServices.saveAccountToDB(account5);
+ Account account6 = new Investment(234023.23, 42, 9948, 0.06, Account.Status.valueOf("OPEN"));
+ accountServices.saveAccountToDB(account6);
+ Account account7 = new Checking(1532.34, 23, 515, Account.Status.valueOf("OPEN"), Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account7);
+ Account account8 = new Savings(120.43, 12, 749, 0.01, Account.Status.valueOf("OPEN"));
+ accountServices.saveAccountToDB(account8);
+ Account account9 = new Investment(234023.23, 42, 904, 0.06, Account.Status.valueOf("OPEN"));
+ accountServices.saveAccountToDB(account9);
+
+ ArrayList actual = accountServices.getAccountsForUser(user1);
+
+ Assert.assertEquals("user1", (int) 1, (int) actual.size());
+ Assert.assertTrue("user1.1", Arrays.equals(account5.toStringArray(), actual.get(0).toStringArray()));
+
+ actual = accountServices.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 saveAccountToDBTest() {
+ accountServices.clearAccountDB();
+
+ Account account1 = new Checking(1532.34, 23, 1232123, Account.Status.valueOf("OPEN"), Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ Account account2 = new Savings(120.43, 12, 749, 0.01, Account.Status.valueOf("OPEN"));
+ accountServices.saveAccountToDB(account2);
+ Account account3 = new Investment(234023.23, 42, 48, 0.06, Account.Status.valueOf("OPEN"));
+ accountServices.saveAccountToDB(account3);
+ Account account4 = new Checking(1532.34, 42, 5423, Account.Status.valueOf("OPEN"), Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account4);
+
+
+ String[] actual = accountServices.getAccountInfoByID(48);
+ String[] expected = account3.toStringArray();
+
+ Assert.assertEquals(actual, expected);
+
+ actual = accountServices.getAccountInfoByID(1232123);
+ expected = account1.toStringArray();
+
+ Assert.assertEquals(actual, expected);
+
+ int actual2 = accountServices.getAccountDBLength();
+ int expected2 = 4;
+
+ Assert.assertEquals(actual, expected);
+
+ Account account10 = new Savings(9990.43, 12, 749, 0.01, Account.Status.valueOf("OPEN"));
+ accountServices.saveAccountToDB(account10);
+
+ actual2 = accountServices.getAccountDBLength();
+ expected2 = 4;
+
+ Assert.assertEquals(actual, expected);
+
+ actual = accountServices.getAccountInfoByID(749);
+ expected = account10.toStringArray();
+
+ Assert.assertEquals(actual, expected);
+
+ }
+
+ @Test(expected = InsufficientFundsException.class)
+ public void insufficientFundWithdrawTest() throws InsufficientFundsException, FrozenAccountException, ClosedAccountException {
+
+ accountServices.clearAccountDB();
+ userServices.clearUserDB();
+ User user1 = new User("Jim", "Brown", "goolybib", 98, 12343);
+ userServices.saveUserToDB(user1);
+ Account account1 = new Savings(1532.34, 23, 1232123, .01, Account.Status.valueOf("OPEN"));
+ accountServices.saveAccountToDB(account1);
+ accountServices.attemptAccountWithdrawal(account1, 1600);
+ }
+
+ @Test(expected = InsufficientFundsException.class)
+ public void insufficientFundWithdrawTest2() throws InsufficientFundsException, FrozenAccountException, ClosedAccountException {
+
+ accountServices.clearAccountDB();
+ userServices.clearUserDB();
+ User user1 = new User("Jim", "Brown", "goolybib", 98, 12343);
+ userServices.saveUserToDB(user1);
+ Account account1 = new Investment(1532.34, 23, 1232123, .02, Account.Status.valueOf("OPEN"));
+ accountServices.saveAccountToDB(account1);
+ accountServices.attemptAccountWithdrawal(account1, 1600);
+ }
+
+ @Test(expected = InsufficientFundsException.class)
+ public void insufficientFundWithdrawTest3() throws InsufficientFundsException, FrozenAccountException, ClosedAccountException {
+
+ accountServices.clearAccountDB();
+ userServices.clearUserDB();
+ User user1 = new User("Jim", "Brown", "goolybib", 98, 12343);
+ userServices.saveUserToDB(user1);
+ Account account1 = new Checking(1532.34, 23, 1232123, Account.Status.valueOf("OPEN"), Checking.Overdraft.ON);
+ accountServices.saveAccountToDB(account1);
+ accountServices.attemptAccountWithdrawal(account1, 1600);
+ }
+
+ @Test
+ public void insufficientFundWithdrawTest4() throws InsufficientFundsException, FrozenAccountException, ClosedAccountException {
+
+ accountServices.clearAccountDB();
+ userServices.clearUserDB();
+ User user1 = new User("Jim", "Brown", "goolybib", 98, 12343);
+ userServices.saveUserToDB(user1);
+ Account account1 = new Checking(1500.00, 23, 1232123, Account.Status.valueOf("OPEN"), Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ accountServices.attemptAccountWithdrawal(account1, 1600);
+ Assert.assertEquals(-100.0, account1.getBalance(), .01);
+ }
+
+ @Test
+ public void overdraftAutoTransferTest() throws InsufficientFundsException, FrozenAccountException, ClosedAccountException {
+
+ accountServices.clearAccountDB();
+ userServices.clearUserDB();
+ User user1 = new User("Jim", "Brown", "goolybib", 98, 12343);
+ atm.setCurrentUser(user1);
+ userServices.saveUserToDB(user1);
+ Account account1 = new Checking(1500.00, 98, 1232123, Account.Status.OPEN, Checking.Overdraft.AUTO);
+ Account account2 = new Checking(2500.00, 98, 1232124, Account.Status.OPEN, Checking.Overdraft.OFF);
+
+ accountServices.saveAccountToDB(account1);
+ accountServices.saveAccountToDB(account2);
+
+ accountServices.attemptAccountWithdrawal(account1, 1600);
+ Assert.assertEquals(0.0, account1.getBalance(), .01);
+ String[] account2Info = accountServices.getAccountInfoByID(1232124);
+ Assert.assertEquals(2400.0, Double.parseDouble(account2Info[2]), .01);
+ }
+
+
+ @Test(expected = InsufficientFundsException.class)
+ public void overdraftAutoTransferTest2() throws InsufficientFundsException, FrozenAccountException, ClosedAccountException {
+
+ accountServices.clearAccountDB();
+ userServices.clearUserDB();
+ User user1 = new User("Jim", "Brown", "goolybib", 98, 12343);
+ atm.setCurrentUser(user1);
+ userServices.saveUserToDB(user1);
+ Account account1 = new Checking(1500.00, 98, 1232123, Account.Status.OPEN, Checking.Overdraft.AUTO);
+ Account account2 = new Checking(100.00, 98, 1232124, Account.Status.OPEN, Checking.Overdraft.OFF);
+
+ accountServices.saveAccountToDB(account1);
+ accountServices.saveAccountToDB(account2);
+
+ accountServices.attemptAccountWithdrawal(account1, 1700);
+ Assert.assertEquals(0.0, account1.getBalance(), .01);
+ String[] account2Info = accountServices.getAccountInfoByID(1232124);
+ Assert.assertEquals(2400.0, Double.parseDouble(account2Info[2]), .01);
+ }
+
+ @Test
+ public void overdraftAutoTransferTest3() throws InsufficientFundsException, FrozenAccountException, ClosedAccountException {
+
+ accountServices.clearAccountDB();
+ userServices.clearUserDB();
+ User user1 = new User("Jim", "Brown", "goolybib", 98, 12343);
+ atm.setCurrentUser(user1);
+ userServices.saveUserToDB(user1);
+ Account account1 = new Checking(1500.00, 98, 1232123, Account.Status.OPEN, Checking.Overdraft.AUTO);
+ Account account2 = new Checking(100.00, 98, 1232124, Account.Status.OPEN, Checking.Overdraft.OFF);
+ Account account3 = new Savings(1000.00, 98, 1232125, .02, Account.Status.OPEN);
+
+ accountServices.saveAccountToDB(account1);
+ accountServices.saveAccountToDB(account2);
+ accountServices.saveAccountToDB(account3);
+
+ accountServices.attemptAccountWithdrawal(account1, 1700);
+ Assert.assertEquals(0.0, account1.getBalance(), .01);
+ String[] account2Info = accountServices.getAccountInfoByID(1232124);
+ Assert.assertEquals(100.0, Double.parseDouble(account2Info[2]), .01);
+ String[] account3Info = accountServices.getAccountInfoByID(1232125);
+ Assert.assertEquals(800.0, Double.parseDouble(account3Info[2]), .01);
+ }
+
+ @Test(expected = FrozenAccountException.class)
+ public void withdrawFrozenAcctException() throws InsufficientFundsException, FrozenAccountException, ClosedAccountException {
+
+ accountServices.clearAccountDB();
+ userServices.clearUserDB();
+ User user1 = new User("Jim", "Brown", "goolybib", 98, 12343);
+ userServices.saveUserToDB(user1);
+ Account account1 = new Checking(1532.34, 23, 1232123, Account.Status.valueOf("OFAC"), Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ accountServices.attemptAccountWithdrawal(account1, 1500);
+
+ }
+
+
+ @Test(expected = ClosedAccountException.class)
+ public void withdrawClosedAcctTest() throws InsufficientFundsException, FrozenAccountException, ClosedAccountException {
+
+ accountServices.clearAccountDB();
+ userServices.clearUserDB();
+ User user1 = new User("Jim", "Brown", "goolybib", 98, 12343);
+ userServices.saveUserToDB(user1);
+ Account account1 = new Checking(1532.34, 23, 1232123, Account.Status.valueOf("CLOSED"), Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ accountServices.attemptAccountWithdrawal(account1, 1500);
+ }
+
+ @Test
+ public void accountWithdrawTest() throws FrozenAccountException, ClosedAccountException, InsufficientFundsException {
+ accountServices.clearAccountDB();
+ userServices.clearUserDB();
+ User user1 = new User("Jim", "Brown", "goolybib", 98, 12343);
+ userServices.saveUserToDB(user1);
+ Account account1 = new Checking(1532.34, 23, 1232123, Account.Status.valueOf("OPEN"), Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ accountServices.attemptAccountWithdrawal(account1, 32.34);
+ Account retrieved = accountServices.getAccountByInfo(accountServices.getAccountInfoByID(1232123));
+ double actual = retrieved.getBalance();
+ double expected = 1500.00;
+
+ Assert.assertEquals(actual, expected, .01);
+
+
+ }
+
+ @Test(expected = ClosedAccountException.class)
+ public void closedAccountDepositTest() throws FrozenAccountException, ClosedAccountException, InsufficientFundsException {
+ accountServices.clearAccountDB();
+ userServices.clearUserDB();
+ User user1 = new User("Jim", "Brown", "goolybib", 98, 12343);
+ userServices.saveUserToDB(user1);
+ Account account1 = new Checking(1532.34, 23, 1232123, Account.Status.valueOf("CLOSED"), Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ accountServices.accountDeposit(account1, 50.00);
+ double actual = account1.getBalance();
+ double expected = 1582.34;
+ Assert.assertEquals(actual, expected);
+ }
+
+ @Test
+ public void accountDepositTest() throws FrozenAccountException, ClosedAccountException {
+ accountServices.clearAccountDB();
+ userServices.clearUserDB();
+ User user1 = new User("Jim", "Brown", "goolybib", 98, 12343);
+ userServices.saveUserToDB(user1);
+ Account account1 = new Checking(1532.34, 23, 1232123, Account.Status.valueOf("OPEN"), Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ accountServices.accountDeposit(account1, 50.00);
+ double actual = account1.getBalance();
+ double expected = 1582.34;
+ Assert.assertEquals(expected, actual, 0);
+ }
+
+ @Test
+ public void closeAccountTest() throws BalanceRemainingException, FrozenAccountException, ClosedAccountException {
+ accountServices.clearAccountDB();
+ userServices.clearUserDB();
+ User user1 = new User("Jim", "Brown", "goolybib", 98, 12343);
+ userServices.saveUserToDB(user1);
+ Account account1 = new Checking(0.00, 23, 1232123, Account.Status.valueOf("OPEN"), Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ accountServices.closeAccount(account1);
+ Account.Status actual = account1.getAcctStatus();
+ Account.Status expected = Account.Status.CLOSED;
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test(expected = BalanceRemainingException.class)
+ public void closeAccountWithBalanceTest() throws BalanceRemainingException, FrozenAccountException, ClosedAccountException {
+ accountServices.clearAccountDB();
+ userServices.clearUserDB();
+ User user1 = new User("Jim", "Brown", "goolybib", 98, 12343);
+ userServices.saveUserToDB(user1);
+ Account account1 = new Checking(10.00, 23, 1232123, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ accountServices.closeAccount(account1);
+ }
+
+ @Test(expected = FrozenAccountException.class)
+ public void closeFrozenAccountTest() throws BalanceRemainingException, FrozenAccountException, ClosedAccountException {
+ accountServices.clearAccountDB();
+ userServices.clearUserDB();
+ User user1 = new User("Jim", "Brown", "goolybib", 98, 12343);
+ userServices.saveUserToDB(user1);
+ Account account1 = new Checking(0.00, 23, 1232123, Account.Status.OFAC, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ accountServices.closeAccount(account1);
+ }
+
+ @Test(expected = ClosedAccountException.class)
+ public void alreadyClosedAccountTest() throws BalanceRemainingException, FrozenAccountException, ClosedAccountException {
+ accountServices.clearAccountDB();
+ userServices.clearUserDB();
+ User user1 = new User("Jim", "Brown", "goolybib", 98, 12343);
+ userServices.saveUserToDB(user1);
+ Account account1 = new Checking(0.00, 23, 1232123, Account.Status.CLOSED, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ accountServices.closeAccount(account1);
+ }
+
+ @Test
+ public void getAccountDBLengthTest() {
+ accountServices.clearAccountDB();
+
+ Account account1 = new Checking(1532.34, 23, 1232123, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ Account account2 = new Savings(120.43, 12, 749, 0.01, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account2);
+ Account account3 = new Investment(234023.23, 42, 48, 0.06, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account3);
+ Account account4 = new Checking(1532.34, 42, 5423, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account4);
+ int actual = accountServices.getAccountDBLength();
+ int expected = 4;
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void getNewRateTest() {
+ Random random = new Random();
+ accountServices.clearAccountDB();
+ Savings account2 = new Savings(120.43, 12, 749, 0.01, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account2);
+ Double actual = account2.getInterestRate();
+ Double expected = accountServices.getNewRate(random, account2);
+ Assert.assertEquals(expected, actual, .1);
+ }
+
+ @Test
+ public void setNewRateTest() {
+ accountServices.clearAccountDB();
+ Savings account2 = new Savings(120.43, 12, 749, 0.01, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account2);
+ account2.setInterestRate(0.03);
+ Double actual = account2.getInterestRate();
+ Double expected = 0.03;
+ Assert.assertEquals(expected,actual, 0);
+ }
+
+ @Test
+ public void calcInterestTest(){
+ accountServices.clearAccountDB();
+ Savings account2 = new Savings(200.00, 12, 749, 0.01, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account2);
+ accountServices.calcInterest(account2);
+ Double actual = account2.getBalance();
+ Double expected = 200.02;
+ Assert.assertEquals(expected, actual, 0);
+ }
+
+ @Test
+ public void setInterestRateTest() {
+ Savings account2 = new Savings(200.00, 12, 749, 0.01, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account2);
+
+ accountServices.setNewInterestRate(account2, .05);
+ double expected = .05;
+ double actual = Double.parseDouble(accountServices.getAccountInfoByID(749)[4]);
+ Assert.assertEquals(expected,actual, .001);
+ }
+
+ @Test
+ public void setInterestRateTest2() {
+ Savings account2 = new Savings(200.00, 12, 749, 0.01, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account2);
+
+ accountServices.setNewInterestRate(account2, .05);
+ atm.getTransactionServices().getTransactionsForAccount(account2);
+ String expected = "Interest rate changed to 00.05";
+ String actual = atm.getTransactionServices().getTransactionInfoByRow(0)[4];
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void applyInterestTest() {
+
+ User user1 = new User("Jim", "Brown", "goolybib", 98, 12343);
+ userServices.saveUserToDB(user1);
+ atm.setCurrentUser(user1);
+
+ Account account1 = new Checking(1500.00, 98, 1232123, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ Account account2 = new Savings(10000.00, 98, 749, 0.01, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account2);
+ Account account3 = new Savings(234023.23, 4, 48, 0.06, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account3);
+ Account account4 = new Savings(1000.00, 98, 5423, 0.05,Account.Status.OPEN);
+ accountServices.saveAccountToDB(account4);
+ Account account5 = new Savings(2000.00, 98, 543, 0.05,Account.Status.CLOSED);
+ accountServices.saveAccountToDB(account5);
+ Account account6 = new Savings(2000.00, 98, 53, 0.05,Account.Status.OFAC);
+ accountServices.saveAccountToDB(account6);
+
+ accountServices.applyInterest();
+
+ ArrayList accts = accountServices.getAccountsForUser(user1);
+ double expected = 10001.00;
+ double actual = accts.get(1).getBalance();
+ Assert.assertEquals(expected, actual, .01);
+
+ expected = 1000.5;
+ actual = accts.get(2).getBalance();
+ Assert.assertEquals(expected, actual, .01);
+
+ expected = 1500.0;
+ actual = accts.get(0).getBalance();
+ Assert.assertEquals(expected, actual, .01);
+
+ expected = 2000.0;
+ actual = accts.get(3).getBalance();
+ Assert.assertEquals(expected, actual, .01);
+
+ expected = 2000.0;
+ actual = accts.get(4).getBalance();
+ Assert.assertEquals(expected, actual, .01);
+ }
+
+ @Test
+ public void interestRateChangeTest() {
+
+ User user1 = new User("Jim", "Brown", "goolybib", 98, 12343);
+ userServices.saveUserToDB(user1);
+ atm.setCurrentUser(user1);
+
+ Account account1 = new Checking(1500.00, 98, 1232123, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ Account account2 = new Savings(10000.00, 98, 749, 0.01, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account2);
+ Account account3 = new Savings(234023.23, 4, 48, 0.06, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account3);
+ Account account4 = new Savings(1000.00, 98, 5423, 0.05,Account.Status.OPEN);
+ accountServices.saveAccountToDB(account4);
+ Account account5 = new Savings(2000.00, 98, 543, 0.05,Account.Status.CLOSED);
+ accountServices.saveAccountToDB(account5);
+ Account account6 = new Savings(2000.00, 98, 53, 0.05,Account.Status.OFAC);
+ accountServices.saveAccountToDB(account6);
+
+ double expected;
+ double actual;
+ for (int i = 0; i < 50; i++) {
+ accountServices.interestRateChange();
+
+ ArrayList accts = accountServices.getAccountsForUser(user1);
+ expected = 0.01;
+ actual = ((Savings) accts.get(1)).getInterestRate();
+ Assert.assertTrue(actual >= 0.0);
+ Assert.assertTrue(actual <= 0.07);
+ ((Savings) accts.get(1)).setInterestRate(.01);
+ accountServices.saveAccountToDB(accts.get(1));
+
+ expected = .05;
+ actual = ((Savings) accts.get(2)).getInterestRate();
+ Assert.assertTrue(actual >= 0.0);
+ Assert.assertTrue(actual <= 0.11);
+ ((Savings) accts.get(2)).setInterestRate(.05);
+ accountServices.saveAccountToDB(accts.get(2));
+
+ expected = .05;
+ actual = ((Savings) accts.get(3)).getInterestRate();
+ Assert.assertEquals(expected, actual, .01);
+
+ expected = .05;
+ actual = ((Savings) accts.get(4)).getInterestRate();
+ Assert.assertEquals(expected, actual, .01);
+
+ expected = 2000.0;
+ actual = accts.get(4).getBalance();
+ Assert.assertEquals(expected, actual, .01);
+ }
+ }
+
+ @Test
+ public void deleteAccountFromDBTest() {
+ Account account1 = new Checking(1500.00, 98, 1232123, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ Account account2 = new Savings(10000.00, 98, 749, 0.01, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account2);
+ Account account3 = new Savings(234023.23, 4, 48, 0.06, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account3);
+
+ Assert.assertEquals(3,accountServices.getAccountDBLength());
+ accountServices.deleteAccountFromDB(account2);
+ Assert.assertEquals("48",accountServices.getAccountInfoByRow(1)[0]);
+ Assert.assertEquals(2,accountServices.getAccountDBLength());
+ accountServices.deleteAccountFromDB(account1);
+ Assert.assertEquals("48",accountServices.getAccountInfoByRow(0)[0]);
+ Assert.assertEquals(1,accountServices.getAccountDBLength());
+ }
+
+ @Test
+ public void createCheckingTest() {
+ User user1 = new User("Jim", "Brown", "goolybib", 98, 12343);
+ userServices.saveUserToDB(user1);
+ atm.setCurrentUser(user1);
+
+ accountServices.createCheckingAccount(1000.00, user1);
+
+ ArrayList accounts = accountServices.getAccountsForUser(user1);
+ Assert.assertEquals(1000.0, accounts.get(0).getBalance(), .01);
+ Assert.assertTrue(accounts.get(0) instanceof Checking);
+
+ ArrayList trans = atm.getTransactionServices().getTransactionsForAccount(accounts.get(0));
+ Assert.assertEquals(1,trans.size());
+ }
+
+ @Test
+ public void createSavingsTest() {
+ User user1 = new User("Jim", "Brown", "goolybib", 98, 12343);
+ userServices.saveUserToDB(user1);
+ atm.setCurrentUser(user1);
+
+ accountServices.createSavingsAccount(1000.00, user1);
+
+ ArrayList accounts = accountServices.getAccountsForUser(user1);
+ Assert.assertEquals(1000.0, accounts.get(0).getBalance(), .01);
+ Assert.assertEquals(.02, ((Savings) accounts.get(0)).getInterestRate(), .01);
+ Assert.assertTrue(accounts.get(0) instanceof Savings);
+
+ ArrayList trans = atm.getTransactionServices().getTransactionsForAccount(accounts.get(0));
+ Assert.assertEquals(1,trans.size());
+ }
+
+ @Test
+ public void createInvestmentTest() {
+ User user1 = new User("Jim", "Brown", "goolybib", 98, 12343);
+ userServices.saveUserToDB(user1);
+ atm.setCurrentUser(user1);
+
+ accountServices.createInvestmentAccount(1000.00, user1,4);
+
+ ArrayList accounts = accountServices.getAccountsForUser(user1);
+ Assert.assertEquals(1000.0, accounts.get(0).getBalance(), .01);
+ Assert.assertEquals(.04, ((Investment)accounts.get(0)).getRisk(), .01);
+ Assert.assertTrue(accounts.get(0) instanceof Investment);
+
+ ArrayList trans = atm.getTransactionServices().getTransactionsForAccount(accounts.get(0));
+ Assert.assertEquals(1,trans.size());
+ }
+
+ @Test
+ public void investmentReturnsTest() {
+
+ User user1 = new User("Jim", "Brown", "goolybib", 98, 12343);
+ userServices.saveUserToDB(user1);
+ atm.setCurrentUser(user1);
+
+ Account account1 = new Checking(1500.00, 98, 1232123, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ Account account2 = new Investment(10000.00, 98, 749, 0.01, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account2);
+ Account account3 = new Investment(234023.23, 4, 48, 0.06, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account3);
+ Account account4 = new Investment(1000.00, 98, 5423, 0.05,Account.Status.OPEN);
+ accountServices.saveAccountToDB(account4);
+ Account account5 = new Investment(2000.00, 98, 543, 0.05,Account.Status.CLOSED);
+ accountServices.saveAccountToDB(account5);
+ Account account6 = new Investment(2000.00, 98, 53, 0.05,Account.Status.OFAC);
+ accountServices.saveAccountToDB(account6);
+
+ double expected;
+ double actual;
+ for (int i = 0; i < 50; i++) {
+ accountServices.applyReturns();
+
+ ArrayList accts = accountServices.getAccountsForUser(user1);
+ expected = account2.getBalance();
+ Assert.assertNotEquals(expected, accts.get(1).getBalance());
+ accts.get(1).setBalance(10000.0);
+ accountServices.saveAccountToDB(accts.get(1));
+
+ expected = account5.getBalance();
+ Assert.assertEquals(expected, accts.get(3).getBalance(), .01);
+ accts.get(3).setBalance(2000.0);
+ accountServices.saveAccountToDB(accts.get(3));
+
+ expected = account6.getBalance();
+ Assert.assertEquals(expected, accts.get(4).getBalance(), .01);
+ accts.get(4).setBalance(2000.0);
+ accountServices.saveAccountToDB(accts.get(4));
+ }
+
+
+ }
+}
+
+
+
+
+ // @Test
+// public void clearAccountDB() {
+// ATM.DB accountDB = null;
+// try {
+// accountDB = new ATM.DB("accounts.csv", 5);
+// } catch (IOException e) {
+// e.printStackTrace();
+// }
+// accountDB.clear();
+// }
diff --git a/src/test/java/ATM/services/TransactionServicesTest.java b/src/test/java/ATM/services/TransactionServicesTest.java
new file mode 100644
index 0000000..61301f4
--- /dev/null
+++ b/src/test/java/ATM/services/TransactionServicesTest.java
@@ -0,0 +1,279 @@
+package ATM.services;
+
+import ATM.ATM;
+import ATM.DB;
+import ATM.Transaction;
+import ATM.User;
+
+import ATM.accounts.Account;
+import ATM.accounts.Checking;
+import ATM.accounts.Investment;
+import ATM.accounts.Savings;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Date;
+
+public class TransactionServicesTest {
+
+ private ATM atm;
+ private TransactionServices transactionsServices;
+ UserServices userServices;
+ AccountServices accountServices;
+
+ @Before
+ public void setUp() throws Exception {
+ atm = new ATM("testuserDB.csv", "testaccountDB.csv", "testtransactionDB.csv");
+ transactionsServices = atm.getTransactionServices();
+ userServices = atm.getUserServices();
+ accountServices = atm.getAccountServices();
+ transactionsServices.linkServices();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ atm.getUserDB().clear();
+ atm.getAccountDB().clear();
+ atm.getTransactionDB().clear();
+ }
+
+ @Test
+ public void getTransactionDB() {
+ DB foundDB = atm.getTransactionDB();
+ String fileName = foundDB.getFileName();
+ Assert.assertEquals("testtransactionDB.csv", fileName);
+ }
+
+ @Test
+ public void savePendingTransactionsTest() {
+ transactionsServices.clearTransactionsDB();
+ Assert.assertEquals((int) 0, (int) transactionsServices.getTransactionDB().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);
+
+ transactionsServices.savePendingTransactionsToDB(pendingTransactions);
+
+ Assert.assertEquals((int) 2, (int) transactionsServices.getTransactionDB().length());
+
+ transactionsServices.clearTransactionsDB();
+ Assert.assertEquals((int) 0, (int) transactionsServices.getTransactionDB().length());
+
+ }
+
+ @Test
+ public void saveTransactionTest() {
+ transactionsServices.clearTransactionsDB();
+ Assert.assertEquals((int) 0, (int) transactionsServices.getTransactionDB().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);
+
+ transactionsServices.saveTransactionToDB(trans1);
+ transactionsServices.saveTransactionToDB(trans2);
+
+ Assert.assertEquals((int) 2, (int) transactionsServices.getTransactionDB().length());
+ }
+
+ @Test
+ public void getTransactionRowsByUserTest() {
+ transactionsServices.clearTransactionsDB();
+ userServices.clearUserDB();
+ Assert.assertEquals((int) 0, (int) transactionsServices.getTransactionDB().length());
+
+ User user1 = new User("Jim", "Brown", "goolybib", 12, 12343);
+ userServices.saveUserToDB(user1);
+ User user2 = new User("Ji123m", "Bro23wn", "gool321ybib", 35, 1234313);
+ userServices.saveUserToDB(user2);
+ User user3 = new User("Jane", "Himne", "gasdsdool321ybib", 41, 313);
+ userServices.saveUserToDB(user3);
+
+ Account account1 = new Checking(1532.34, 23, 1232123, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ Account account2 = new Savings(10000.00, 12, 749, 0.01, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account2);
+ Account account3 = new Investment(234023.23, 41, 41, 0.06, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account3);
+
+ Transaction trans1 = new Transaction(123.42, new Date(2014, 1, 6, 11, 23, 40), 123123, "Opened account", true);
+ transactionsServices.saveTransactionToDB(trans1);
+ Transaction trans2 = new Transaction(-23.57, new Date(2015, 2, 7, 10, 23, 3), 12, "Withdrawal", false);
+ transactionsServices.saveTransactionToDB(trans2);
+ Transaction trans3 = new Transaction(200.42, new Date(2014, 1, 8, 5, 20, 40), 35, "Opened account", true);
+ transactionsServices.saveTransactionToDB(trans3);
+ Transaction trans4 = new Transaction(-40.57, new Date(2015, 2, 9, 6, 39, 3), 41, "Withdrawal", false);
+ transactionsServices.saveTransactionToDB(trans4);
+ Transaction trans5 = new Transaction(-30.57, new Date(2015, 2, 9, 7, 40, 13), 41, "Withdrawal", false);
+ transactionsServices.saveTransactionToDB(trans5);
+
+ int[] actual = transactionsServices.getTransactionRowsByUser(user3);
+ int[] expected = new int[]{3, 4};
+ Assert.assertArrayEquals(expected, actual);
+ }
+
+
+ @Test
+ public void getTransactionRowsByAccountTest() {
+ transactionsServices.clearTransactionsDB();
+ userServices.clearUserDB();
+ Assert.assertEquals((int) 0, (int) transactionsServices.getTransactionDB().length());
+
+ User user1 = new User("Jim", "Brown", "goolybib", 12, 12343);
+ userServices.saveUserToDB(user1);
+ User user2 = new User("Ji123m", "Bro23wn", "gool321ybib", 35, 1234313);
+ userServices.saveUserToDB(user2);
+ User user3 = new User("Jane", "Himne", "gasdsdool321ybib", 41, 313);
+ userServices.saveUserToDB(user3);
+
+ Account account1 = new Checking(1532.34, 23, 1232123, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ Account account2 = new Savings(10000.00, 12, 749, 0.01, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account2);
+ Account account3 = new Investment(234023.23, 41, 41, 0.06, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account3);
+
+ Transaction trans1 = new Transaction(123.42, new Date(2014, 1, 6, 11, 23, 40), 123123, "Opened account", true);
+ transactionsServices.saveTransactionToDB(trans1);
+ Transaction trans2 = new Transaction(-23.57, new Date(2015, 2, 7, 10, 23, 3), 12, "Withdrawal", false);
+ transactionsServices.saveTransactionToDB(trans2);
+ Transaction trans3 = new Transaction(200.42, new Date(2014, 1, 8, 5, 20, 40), 35, "Opened account", true);
+ transactionsServices.saveTransactionToDB(trans3);
+ Transaction trans4 = new Transaction(-40.57, new Date(2015, 2, 9, 6, 39, 3), 41, "Withdrawal", false);
+ transactionsServices.saveTransactionToDB(trans4);
+ Transaction trans5 = new Transaction(-30.57, new Date(2015, 2, 9, 7, 40, 13), 41, "Withdrawal", false);
+ transactionsServices.saveTransactionToDB(trans5);
+
+ int[] actual = transactionsServices.getTransactionRowsByAccount(account1);
+ int[] expected = new int[0];
+
+ Assert.assertArrayEquals(expected, actual);
+
+ }
+
+ @Test
+ public void getTransactionInfoByRowTest() {
+ transactionsServices.clearTransactionsDB();
+ userServices.clearUserDB();
+ Assert.assertEquals((int) 0, (int) transactionsServices.getTransactionDB().length());
+
+ User user1 = new User("Jim", "Brown", "goolybib", 12, 12343);
+ userServices.saveUserToDB(user1);
+ User user2 = new User("Ji123m", "Bro23wn", "gool321ybib", 35, 1234313);
+ userServices.saveUserToDB(user2);
+ User user3 = new User("Jane", "Himne", "gasdsdool321ybib", 41, 313);
+ userServices.saveUserToDB(user3);
+
+ Account account1 = new Checking(1532.34, 23, 1232123, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ Account account2 = new Savings(10000.00, 12, 749, 0.01, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account2);
+ Account account3 = new Investment(234023.23, 41, 41, 0.06, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account3);
+
+ Transaction trans1 = new Transaction(123.42, new Date(2014, 1, 6, 11, 23, 40), 123123, "Opened account", true);
+ transactionsServices.saveTransactionToDB(trans1);
+ Transaction trans2 = new Transaction(-23.57, new Date(2015, 2, 7, 10, 23, 3), 12, "Withdrawal", false);
+ transactionsServices.saveTransactionToDB(trans2);
+ Transaction trans3 = new Transaction(200.42, new Date(2014, 1, 8, 5, 20, 40), 35, "Opened account", true);
+ transactionsServices.saveTransactionToDB(trans3);
+ Transaction trans4 = new Transaction(-40.57, new Date(2015, 2, 9, 6, 39, 3), 41, "Withdrawal", false);
+ transactionsServices.saveTransactionToDB(trans4);
+ Transaction trans5 = new Transaction(-30.57, new Date(2015, 2, 9, 7, 40, 13), 41, "Withdrawal", false);
+ transactionsServices.saveTransactionToDB(trans5);
+
+ String[] expected = transactionsServices.getTransactionInfoByRow(0);
+ String[] actual = new String[]{"credit", "123123", "123.42", "Fri Feb 06 11:23:40 EST 3914", "Opened account"};
+
+ Assert.assertArrayEquals(expected, actual);
+
+ }
+
+ @Test
+ public void getTransactionByUserTest() {
+ transactionsServices.clearTransactionsDB();
+ userServices.clearUserDB();
+ Assert.assertEquals((int) 0, (int) transactionsServices.getTransactionDB().length());
+
+ User user1 = new User("Jim", "Brown", "goolybib", 35, 12343);
+ userServices.saveUserToDB(user1);
+ User user2 = new User("Ji123m", "Bro23wn", "gool321ybib", 12, 1234313);
+ userServices.saveUserToDB(user2);
+ User user3 = new User("Jane", "Himne", "gasdsdool321ybib", 41, 313);
+ userServices.saveUserToDB(user3);
+
+ Account account1 = new Checking(1532.34, 23, 1232123, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ Account account2 = new Savings(10000.00, 12, 749, 0.01, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account2);
+ Account account3 = new Investment(234023.23, 41, 41, 0.06, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account3);
+ Account account4 = new Savings(10000.00, 12, 750, 0.01, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account4);
+
+ Transaction trans1 = new Transaction(123.42, new Date(2014, 1, 6, 11, 23, 40), 123123, "Opened account", true);
+ transactionsServices.saveTransactionToDB(trans1);
+ Transaction trans2 = new Transaction(-23.57, new Date(2015, 2, 7, 10, 23, 3), 749, "Withdrawal", false);
+ transactionsServices.saveTransactionToDB(trans2);
+ Transaction trans3 = new Transaction(200.42, new Date(2014, 1, 8, 5, 20, 40), 35, "Opened account", true);
+ transactionsServices.saveTransactionToDB(trans3);
+ Transaction trans4 = new Transaction(-40.57, new Date(2015, 2, 9, 6, 39, 3), 750, "Withdrawal", false);
+ transactionsServices.saveTransactionToDB(trans4);
+ Transaction trans5 = new Transaction(-30.57, new Date(2015, 2, 9, 7, 40, 13), 749, "Withdrawal", false);
+ transactionsServices.saveTransactionToDB(trans5);
+
+ ArrayList expected = new ArrayList(Arrays.asList(trans2,trans4,trans5));
+ ArrayList actual = transactionsServices.getTransactionsForUser(user2);
+
+ for (int i = 0; i < expected.size(); i++) {
+ Assert.assertArrayEquals(expected.get(i).toStringArray(), actual.get(i).toStringArray());
+ }
+ }
+
+ @Test
+ public void getTransactionForAccountTest() {
+ transactionsServices.clearTransactionsDB();
+ userServices.clearUserDB();
+ Assert.assertEquals((int) 0, (int) transactionsServices.getTransactionDB().length());
+
+ User user1 = new User("Jim", "Brown", "goolybib", 35, 12343);
+ userServices.saveUserToDB(user1);
+ User user2 = new User("Ji123m", "Bro23wn", "gool321ybib", 12, 1234313);
+ userServices.saveUserToDB(user2);
+ User user3 = new User("Jane", "Himne", "gasdsdool321ybib", 41, 313);
+ userServices.saveUserToDB(user3);
+
+ Account account1 = new Checking(1532.34, 23, 1232123, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ Account account2 = new Savings(10000.00, 12, 749, 0.01, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account2);
+ Account account3 = new Investment(234023.23, 41, 41, 0.06, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account3);
+ Account account4 = new Savings(10000.00, 12, 750, 0.01, Account.Status.OPEN);
+ accountServices.saveAccountToDB(account4);
+
+ Transaction trans1 = new Transaction(123.42, new Date(2014, 1, 6, 11, 23, 40), 123123, "Opened account", true);
+ transactionsServices.saveTransactionToDB(trans1);
+ Transaction trans2 = new Transaction(-23.57, new Date(2015, 2, 7, 10, 23, 3), 749, "Withdrawal", false);
+ transactionsServices.saveTransactionToDB(trans2);
+ Transaction trans3 = new Transaction(200.42, new Date(2014, 1, 8, 5, 20, 40), 35, "Opened account", true);
+ transactionsServices.saveTransactionToDB(trans3);
+ Transaction trans4 = new Transaction(-40.57, new Date(2015, 2, 9, 6, 39, 3), 750, "Withdrawal", false);
+ transactionsServices.saveTransactionToDB(trans4);
+ Transaction trans5 = new Transaction(-30.57, new Date(2015, 2, 9, 7, 40, 13), 749, "Withdrawal", false);
+ transactionsServices.saveTransactionToDB(trans5);
+
+ ArrayList expected = new ArrayList(Arrays.asList(trans2,trans5));
+ ArrayList actual = transactionsServices.getTransactionsForAccount(account2);
+
+ for (int i = 0; i < expected.size(); i++) {
+ Assert.assertArrayEquals(expected.get(i).toStringArray(), actual.get(i).toStringArray());
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/ATM/services/TransferServicesTest.java b/src/test/java/ATM/services/TransferServicesTest.java
new file mode 100644
index 0000000..1c1aec3
--- /dev/null
+++ b/src/test/java/ATM/services/TransferServicesTest.java
@@ -0,0 +1,151 @@
+package ATM.services;
+
+import ATM.ATM;
+import ATM.Exceptions.ClosedAccountException;
+import ATM.Exceptions.FrozenAccountException;
+import ATM.Exceptions.InsufficientFundsException;
+import ATM.accounts.Account;
+import ATM.accounts.Checking;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+public class TransferServicesTest {
+
+ private ATM atm;
+ private AccountServices accountServices;
+ private TransactionServices transactionServices;
+
+ @Before
+ public void setUp() throws Exception {
+ atm = new ATM("testuserDB.csv", "testaccountDB.csv", "testtransactionDB.csv");
+ accountServices = atm.getAccountServices();
+ transactionServices = atm.getTransactionServices();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ atm.getUserDB().clear();
+ atm.getAccountDB().clear();
+ atm.getTransactionDB().clear();
+ }
+
+ @Test(expected = ClosedAccountException.class)
+ public void accountTransferExceptionTest1() throws FrozenAccountException, ClosedAccountException, InsufficientFundsException {
+ accountServices.clearAccountDB();
+
+ Account account1 = new Checking(500.00,23,1232123, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ Account account2 = new Checking(2000.00,23,1232123, Account.Status.CLOSED, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+
+ TransferServices transferServices = new TransferServices(atm, account1);
+ transferServices.executeTransfer(account1, account2, 200.00);
+ }
+
+ @Test(expected = FrozenAccountException.class)
+ public void accountTransferExceptionTest2() throws FrozenAccountException, ClosedAccountException, InsufficientFundsException {
+ accountServices.clearAccountDB();
+
+ Account account1 = new Checking(1532.34,23,1232123, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ Account account2 = new Checking(1532.34,23,1232123, Account.Status.OFAC, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+
+ TransferServices transferServices = new TransferServices(atm, account1);
+ transferServices.executeTransfer(account1, account2, 1203.00);
+ }
+
+ @Test(expected = InsufficientFundsException.class)
+ public void accountTransferExceptionTest3() throws FrozenAccountException, ClosedAccountException, InsufficientFundsException {
+ accountServices.clearAccountDB();
+
+ Account account1 = new Checking(1532.34,23,1232123, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ Account account2 = new Checking(1532.34,23,1232123, Account.Status.CLOSED, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+
+ TransferServices transferServices = new TransferServices(atm, account1);
+ transferServices.executeTransfer(account1, account2, 2203.00);
+ }
+
+ @Test
+ public void accountTransferTest1() throws FrozenAccountException, ClosedAccountException, InsufficientFundsException {
+ accountServices.clearAccountDB();
+
+ Account account1 = new Checking(500.00,23,1232123, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ Account account2 = new Checking(2000.00,23,1232123, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+
+ TransferServices transferServices = new TransferServices(atm, account1);
+ transferServices.executeTransfer(account1, account2, 200.00);
+
+ Assert.assertEquals(300.00, account1.getBalance(), .01);
+ Assert.assertEquals(2200.00, account2.getBalance(), .01);
+ }
+
+ @Test
+ public void accountTransferTest2() throws FrozenAccountException, ClosedAccountException, InsufficientFundsException {
+ accountServices.clearAccountDB();
+
+ Account account1 = new Checking(500.00,23,1232123, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ Account account2 = new Checking(2000.00,23,1232123, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+
+ TransferServices transferServices = new TransferServices(atm, account1);
+ transferServices.executeTransfer(account1, account2, 500.00);
+
+ Assert.assertEquals(0.00, account1.getBalance(), .01);
+ Assert.assertEquals(2500.00, account2.getBalance(), .01);
+ }
+
+ @Test
+ public void accountTransferTest3() throws FrozenAccountException, ClosedAccountException, InsufficientFundsException {
+ accountServices.clearAccountDB();
+
+ Account account1 = new Checking(500.00,23,1232123, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ Account account2 = new Checking(2000.00,23,1232124, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+
+ Assert.assertEquals(0,transactionServices.getTransactionRowsByAccount(account1).length);
+ Assert.assertEquals(0,transactionServices.getTransactionRowsByAccount(account2).length);
+
+ TransferServices transferServices = new TransferServices(atm, account1);
+ transferServices.executeTransfer(account1, account2, 200.00);
+
+ Assert.assertEquals(300.00, account1.getBalance(), .01);
+ Assert.assertEquals(2200.00, account2.getBalance(), .01);
+ Assert.assertEquals(1,transactionServices.getTransactionRowsByAccount(account1).length);
+ Assert.assertEquals(1,transactionServices.getTransactionRowsByAccount(account2).length);
+
+ }
+
+ @Test
+ public void accountTransferTest4() throws FrozenAccountException, ClosedAccountException, InsufficientFundsException {
+ accountServices.clearAccountDB();
+
+ Account account1 = new Checking(500.00,23,1232123, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+ Account account2 = new Checking(2000.00,23,1232124, Account.Status.OPEN, Checking.Overdraft.OFF);
+ accountServices.saveAccountToDB(account1);
+
+ TransferServices transferServices = new TransferServices(atm, account1);
+ transferServices.executeTransfer(account1, account2, 200.00);
+
+ String[] array1 = transactionServices.getTransactionsForAccount(account1).get(0).toStringArray();
+ Assert.assertEquals("debit",array1[0]);
+ Assert.assertEquals("1232123",array1[1]);
+ Assert.assertEquals("-200.00",array1[2]);
+ Assert.assertEquals("Transfer to account 1232124",array1[4]);
+ String[] array2 = transactionServices.getTransactionsForAccount(account2).get(0).toStringArray();
+ Assert.assertEquals("credit",array2[0]);
+ Assert.assertEquals("1232124",array2[1]);
+ Assert.assertEquals("200.00",array2[2]);
+ Assert.assertEquals("Transfer from account 1232123",array2[4]);
+ }
+
+}
diff --git a/src/test/java/ATM/services/UserServicesTest.java b/src/test/java/ATM/services/UserServicesTest.java
new file mode 100644
index 0000000..2d7be77
--- /dev/null
+++ b/src/test/java/ATM/services/UserServicesTest.java
@@ -0,0 +1,286 @@
+package ATM.services;
+
+import ATM.ATM;
+import ATM.DB;
+import ATM.User;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+
+public class UserServicesTest {
+
+ private ATM atm;
+ private UserServices userServices;
+
+ @Before
+ public void setUp() throws Exception {
+ atm = new ATM("testuserDB.csv", "testaccountDB.csv", "testtransactionDB.csv");
+ userServices = atm.getUserServices();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ userServices.clearUserDB();
+ }
+
+ @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 getMaxUserNumber() {
+ userServices.clearUserDB();
+
+ int actual = userServices.getMaxUserNumber();
+ int expected = 0;
+
+ Assert.assertEquals(actual, expected);
+
+ User user1 = new User("Jim", "Brown", "goolybib", 12, 12343);
+ userServices.saveUserToDB(user1);
+ User user2 = new User("Ji123m", "Bro23wn", "gool321ybib", 122, 1234313);
+ userServices.saveUserToDB(user2);
+ User user3 = new User("Jane", "Himne", "gasdsdool321ybib", 32, 313);
+ userServices.saveUserToDB(user3);
+
+ actual = userServices.getMaxUserNumber();
+ expected = 122;
+
+ Assert.assertEquals(actual, expected);
+
+ User user4 = new User("Jane", "Himne", "gasdsdool321ybib", 29, 313);
+ userServices.saveUserToDB(user4);
+
+ actual = userServices.getMaxUserNumber();
+ expected = 122;
+
+ Assert.assertEquals(actual, expected);
+
+ User user5 = new User("Jane", "Himne", "gasdsdool321ybib", 199, 313);
+ userServices.saveUserToDB(user5);
+
+ actual = userServices.getMaxUserNumber();
+ expected = 199;
+
+ Assert.assertEquals(actual, expected);
+ }
+
+
+ @Test
+ public void getUserRowByID() {
+
+ userServices.clearUserDB();
+
+ User user1 = new User("Jim", "Brown", "goolybib", 12, 12343);
+ userServices.saveUserToDB(user1);
+ User user2 = new User("Ji123m", "Bro23wn", "gool321ybib", 122, 1234313);
+ userServices.saveUserToDB(user2);
+ User user3 = new User("Jane", "Himne", "gasdsdool321ybib", 32, 313);
+ userServices.saveUserToDB(user3);
+
+ int actual = userServices.getUserRowByID(122);
+ int expected = 1;
+
+ Assert.assertEquals(actual, expected);
+
+ actual = userServices.getUserRowByID(12);
+ expected = 0;
+
+ Assert.assertEquals(actual, expected);
+
+ actual = userServices.getUserRowByID(32);
+ expected = 2;
+
+ Assert.assertEquals(actual, expected);
+
+ actual = userServices.getUserRowByID(323232);
+ expected = -1;
+
+ Assert.assertEquals(actual, expected);
+ }
+
+
+ @Test
+ public void getUserInfoByID() {
+
+ userServices.clearUserDB();
+
+ User user1 = new User("Jim", "Brown", "goolybib", 12, 12343);
+ userServices.saveUserToDB(user1);
+ User user2 = new User("Ji123m", "Bro23wn", "gool321ybib", 122, 1234313);
+ userServices.saveUserToDB(user2);
+ User user3 = new User("Jane", "Himne", "gasdsdool321ybib", 32, 313);
+ userServices.saveUserToDB(user3);
+
+ String[] actual = userServices.getUserInfoByID(122);
+ String[] expected = user2.toStringArray();
+
+ Assert.assertEquals(actual, expected);
+ }
+
+
+ @Test
+ public void getUserInfoByCardNum() {
+
+ userServices.clearUserDB();
+ User user1 = new User("Jim", "Brown", "goolybib", 12, 12343);
+ userServices.saveUserToDB(user1);
+ User user2 = new User("Ji123m", "Bro23wn", "gool321ybib", 122, 1234313);
+ userServices.saveUserToDB(user2);
+ User user3 = new User("Jane", "Himne", "gasdsdool321ybib", 32, 313);
+ userServices.saveUserToDB(user3);
+
+ String[] actual = userServices.getUserInfoByCardNum(1234313);
+ String[] expected = user2.toStringArray();
+
+ Assert.assertEquals(actual, expected);
+
+ actual = userServices.getUserInfoByCardNum(313);
+ expected = user3.toStringArray();
+
+ Assert.assertEquals(actual, expected);
+ }
+
+
+ @Test
+ public void saveUserToDB() {
+
+ userServices.clearUserDB();
+
+ User user1 = new User("Jim", "Brown", "goolybib", 12, 12343);
+ userServices.saveUserToDB(user1);
+ User user2 = new User("Ji123m", "Bro23wn", "gool321ybib", 122, 1234313);
+ userServices.saveUserToDB(user2);
+ User user3 = new User("Jane", "Himne", "gasdsdool321ybib", 32, 313);
+ userServices.saveUserToDB(user3);
+
+ String[] actual = userServices.getUserInfoByID(122);
+ String[] expected = user2.toStringArray();
+
+ Assert.assertEquals(actual, expected);
+
+ actual = userServices.getUserInfoByID(12);
+ expected = user1.toStringArray();
+
+ Assert.assertEquals(actual, expected);
+
+ int actual2 = userServices.getUserDBLength();
+ int expected2 = 3;
+
+ Assert.assertEquals(actual, expected);
+
+ User user4 = new User("Ji123m", "Bro23wn", "gool321ysdasdbib", 12, 1234313);
+ userServices.saveUserToDB(user4);
+
+ actual2 = userServices.getUserDBLength();
+ expected2 = 3;
+
+ Assert.assertEquals(actual, expected);
+
+ actual = userServices.getUserInfoByID(12);
+ expected = user4.toStringArray();
+
+ Assert.assertEquals(actual, expected);
+
+ expected = user1.toStringArray();
+
+ Assert.assertNotEquals(actual, expected);
+ }
+
+ @Test
+ public void getUserDBLengthTest() {
+ userServices.clearUserDB();
+ }
+
+ @Test
+ public void genCardNumTest() {
+
+ System.out.println("Testing Card Number Gen");
+
+ for (int i = 0; i < 10; i++) {
+ Integer cardNum = userServices.genCardNum();
+
+ System.out.println(cardNum);
+ Assert.assertEquals(8, cardNum.toString().length());
+ }
+ }
+
+ @Test
+ public void genUserIDTest(){
+ int currentHighestUserID = userServices.getMaxUserNumber();
+ Integer expectedUserID = currentHighestUserID +1;
+ Assert.assertEquals(expectedUserID,userServices.genUserID());
+ }
+
+
+ @Test
+ public void createNewUserTest(){
+ String testFirstName = "John";
+ String testLastName = "Doe";
+ String testPassword = "password";
+ Integer testUserID = 9999;
+ Integer testUserCardNum = 99999999;
+ atm.setCurrentUser(userServices.createNewUser(testFirstName, testLastName, testPassword));
+
+ Assert.assertEquals("password", atm.getCurrentUser().getPassword());
+ User user1 = new User("Jim", "Brown", "goolybib", 12, 12343);
+ userServices.saveUserToDB(user1);
+ User user2 = new User("Ji123m", "Bro23wn", "gool321ybib", 122, 1234313);
+ userServices.saveUserToDB(user2);
+ User user3 = new User("Jane", "Himne", "gasdsdool321ybib", 32, 313);
+ userServices.saveUserToDB(user3);
+
+ int actual = userServices.getUserDBLength();
+ int expected = 4;
+
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void nameChangeTest1() {
+ User user1 = new User("Jim", "Brown", "goolybib", 12, 12343);
+ userServices.saveUserToDB(user1);
+ boolean actual = userServices.changeName(user1, "", "Halpert");
+ Assert.assertFalse(actual);
+ }
+
+ @Test
+ public void nameChangeTest2() {
+ User user1 = new User("Jim", "Brown", "goolybib", 12, 12343);
+ userServices.saveUserToDB(user1);
+ boolean actual = userServices.changeName(user1, "Jim", "");
+ Assert.assertFalse(actual);
+ }
+
+ @Test
+ public void nameChangeTest3() {
+ User user1 = new User("Jim", "Brown", "goolybib", 12, 12343);
+ userServices.saveUserToDB(user1);
+ boolean actual = userServices.changeName(user1, "Jim", "Halpert");
+ Assert.assertTrue(actual);
+ }
+
+ @Test
+ public void nameChangeTest4() {
+ User user1 = new User("Jim", "Brown", "goolybib", 12, 12343);
+ userServices.saveUserToDB(user1);
+ boolean actual = userServices.changeName(user1, "Jim", "Halpert");
+ String[] info = userServices.getUserInfoByID(12);
+ Assert.assertEquals("Halpert",info[1]);
+ }
+}
+