diff --git a/UML Bank Accounts ATM 17 1023.pdf b/UML Bank Accounts ATM 17 1023.pdf new file mode 100644 index 0000000..eae398c Binary files /dev/null and b/UML Bank Accounts ATM 17 1023.pdf differ diff --git a/pom.xml b/pom.xml index 9901415..9404b5c 100644 --- a/pom.xml +++ b/pom.xml @@ -8,5 +8,15 @@ project-2-atm 1.0-SNAPSHOT + + + + junit + junit + 4.12 + + + + \ No newline at end of file diff --git a/src/main/java/ATMInterface.java b/src/main/java/ATMInterface.java new file mode 100644 index 0000000..6de75f3 --- /dev/null +++ b/src/main/java/ATMInterface.java @@ -0,0 +1,545 @@ +import java.util.Scanner; + +public class ATMInterface { + private User currentUser; + private Account currentAccount; + + public void start() { + UserWarehouse.generateMyUsers(); + displayWelcome(); + } + + private void displayWelcome() { + System.out.println("Welcome to the ATM"); + newUserOrLogin(); + } + + private void newUserOrLogin() { + System.out.println("New user or login?"); + System.out.println("1: New User"); + System.out.println("2: Login"); + System.out.println("3: Exit"); + int userInput = -1; + try { + userInput = getUserInputInt(); + } catch (Exception e) { + printInvalidInputMessage(); + newUserOrLogin(); + } + System.out.println(); + switch (userInput) { + case 1: + newUser(); + break; + case 2: + login(); + break; + case 3: + System.out.println("Goodbye"); + System.exit(0); + break; + default: + printInvalidInputMessage(); + newUserOrLogin(); + break; + } + + } + + private void newUser() { + String password = testPasswordRules(); + User newUser = new User(password); + UserWarehouse.addUser(newUser); + currentUser = newUser; + System.out.printf("Verified User %d is now logged in\n", newUser.getUserID()); + System.out.println(); + displayAvailableOptions(); + } + + private String testPasswordRules() { + boolean isPasswordValid = false; + String password = ""; + while(!isPasswordValid) { + System.out.println("Enter your preferred password: (must be at least 6 characters)"); + try { + password = getUserInputString(); + } catch (Exception e) { + printInvalidInputMessage(); + } + System.out.println(); + if(password.length() < 6) { + printInvalidInputMessage(); + } + else { + isPasswordValid = true; + } + } + return password; + } + + private void login() { + int userID = loginEnterUserID(); + User userAtLogin = UserWarehouse.returnUserFromID(userID); + if(userAtLogin != null) { + boolean loggedInCorrectly = false; + while (!loggedInCorrectly) { + String inputPassword = loginEnterPassword(); + if (userAtLogin.isCorrectPassword(inputPassword)) { + System.out.printf("Verified User %d is now logged in\n", userID); + System.out.println(); + loggedInCorrectly = true; + currentUser = userAtLogin; + displayAvailableOptions(); + } else { + System.out.println("Sorry, password is incorrect, try again"); + System.out.println(); + } + } + } + else { + System.out.println("User does not exist"); + System.out.println(); + newUserOrLogin(); + } + } + + private int loginEnterUserID() { + System.out.println("Login screen"); + System.out.println("Enter user ID"); + int userID = -1; + try { + userID = getUserInputInt(); + } catch (Exception e) { + printInvalidInputMessage(); + login(); + } + System.out.println(); + return userID; + } + + private String loginEnterPassword() { + System.out.println("Enter password"); + String inputPassword = ""; + try { + inputPassword = getUserInputString(); + } catch (Exception e) { + printInvalidInputMessage(); + login(); + } + System.out.println(); + return inputPassword; + } + + + + private void displayAvailableOptions() { + System.out.println("Available Options:"); + System.out.println("1: Select Account"); + System.out.println("2: Create Account"); + System.out.println("3: Logout"); + int userInput = -1; + try { + userInput = getUserInputInt(); + } catch (Exception e) { + printInvalidInputMessage(); + displayAvailableOptions(); + } + System.out.println(); + switch (userInput) { + case 1: + displayAvailableAccounts(); + break; + case 2: + createAccount(); + break; + case 3: + logout(); + break; + default: + printInvalidInputMessage(); + displayAvailableOptions(); + break; + } + } + + private void displayAvailableAccounts() { + if(currentUser.getAccounts().size() == 0) { + System.out.println("You have no accounts, would you like to create one?"); + System.out.println("1: Create Account"); + System.out.println("2: Logout"); + int userInput = 1; + try { + userInput = getUserInputInt(); + } catch (Exception e) { + printInvalidInputMessage(); + displayAvailableAccounts(); + } + System.out.println(); + switch (userInput) { + case 1: + createAccount(); + break; + case 2: + logout(); + break; + default: + printInvalidInputMessage(); + displayAvailableAccounts(); + break; + } + } + else { + System.out.println("Your available accounts are below, please select the account"); + int index = 1; + for(Account account: currentUser.getAccounts()) { + System.out.println(index + ": " + account.getAccountType() + " Available balance: " + + String.format("$%.2f",account.getBalance()) + " Account number: " + account.getAccountNumber()); + index++; + } + int userInput = 1; + try { + userInput = getUserInputInt(); + } catch (Exception e) { + printInvalidInputMessage(); + displayAvailableAccounts(); + } + System.out.println(); + Account chosenAccount = currentUser.getAccounts().get(userInput - 1); + if(chosenAccount != null) { + currentAccount = chosenAccount; + displaySelectedAccountOptions(); + } + } + } + + private void createAccount() { + AccountType typeOfAccount = selectTypeOfAccount(); + Account newAccount = new Checking(); // default + + boolean hasInitialBalance = hasInitialBalance(); + double initialBalance = 0; + if(hasInitialBalance) { + initialBalance = setInitialBalance(); + } + + switch (typeOfAccount) { + case CHECKING: + newAccount = new Checking(initialBalance); + break; + case SAVINGS: + newAccount = new Savings(initialBalance); + break; + case INVESTMENT: + newAccount = new Investment(initialBalance); + break; + default: + printInvalidInputMessage(); + createAccount(); + break; + } + currentUser.addAccount(newAccount); + currentAccount = newAccount; + System.out.println("Your new account is: " + currentAccount.getAccountType() + " Available balance: " + + String.format("$%.2f",currentAccount.getBalance()) + " Account number: " + + currentAccount.getAccountNumber()); + System.out.println(); + displaySelectedAccountOptions(); + } + + private AccountType selectTypeOfAccount() { + int userInput = -1; + AccountType typeOfAccount = AccountType.CHECKING; + while (userInput < 1 || userInput > 3) { + System.out.println("What type of account would you like to create?"); + System.out.println("1: " + AccountType.CHECKING); + System.out.println("2: " + AccountType.SAVINGS); + System.out.println("3: " + AccountType.INVESTMENT); + + try { + userInput = getUserInputInt(); + } catch (Exception e) { + printInvalidInputMessage(); + createAccount(); + } + System.out.println(); + + switch (userInput) { + case 1: + typeOfAccount = AccountType.CHECKING; + break; + case 2: + typeOfAccount = AccountType.SAVINGS; + break; + case 3: + typeOfAccount = AccountType.INVESTMENT; + break; + default: + printInvalidInputMessage(); + break; + } + } + return typeOfAccount; + } + + private boolean hasInitialBalance() { + boolean providedInitialBalance = false; + int userInputBalance = -1; + while(userInputBalance < 1 || userInputBalance > 2) { + System.out.println("Provide initial balance?"); + System.out.println("1: Yes"); + System.out.println("2: No"); + try { + userInputBalance = getUserInputInt(); + } catch (Exception e) { + printInvalidInputMessage(); + createAccount(); + } + System.out.println(); + + switch (userInputBalance) { + case 1: + providedInitialBalance = true; + break; + case 2: + providedInitialBalance = false; + break; + default: + printInvalidInputMessage(); + break; + } + } + return providedInitialBalance; + } + + private double setInitialBalance() { + double initialBalance = 0; + System.out.println("Deposit initial balance (max $25,000)"); + System.out.print("$"); + try { + initialBalance = Main.truncateToTwoDecimalPlaces(getUserInputDouble()); + } catch (Exception e) { + printInvalidInputMessage(); + createAccount(); + } + System.out.println(); + return initialBalance; + } + + private void logout() { + System.out.println("You are now logged out"); + System.out.println(); + displayWelcome(); + } + + private void displaySelectedAccountOptions() { + System.out.println("Available Options:"); + System.out.println("1: Get Available Balance"); + System.out.println("2: Deposit Money Into Account"); + System.out.println("3: Withdraw Money From Account"); + System.out.println("4: Transfer Money to Another Account"); + System.out.println("5: View Transaction History"); + System.out.println("6: Close Account"); + System.out.println("7: Back to Select or Create Account"); + System.out.println("8: Logout"); + int userInput = 1; + try { + userInput = getUserInputInt(); + } catch (Exception e) { + printInvalidInputMessage(); + displaySelectedAccountOptions(); + } + System.out.println(); + if(userInput < 7) { + System.out.println(currentAccount.getAccountType() + " Available balance: " + + String.format("$%.2f", currentAccount.getBalance()) + " Account number: " + + currentAccount.getAccountNumber()); + System.out.println(); + } + switch (userInput) { + case 1: + displayBalance(); + break; + case 2: + depositToAccount(); + break; + case 3: + withdrawFromAccount(); + break; + case 4: + transferToOtherAccount(); + break; + case 5: + printTransactionHistory(); + break; + case 6: + closeCurrentAccount(); + break; + case 7: + displayAvailableOptions(); + break; + case 8: + logout(); + break; + default: + printInvalidInputMessage(); + displaySelectedAccountOptions(); + break; + } + } + + private void displayBalance() { + System.out.println("The current balance of this account is " + String.format("$%.2f",currentAccount.getBalance())); + System.out.println(); + displaySelectedAccountOptions(); + } + + private void depositToAccount() { + System.out.println("How much would you like to deposit? (Max $25,000)"); + System.out.print("$"); + double userInput = 0; + try { + userInput = getUserInputDouble(); + } catch (Exception e) { + printInvalidInputMessage(); + depositToAccount(); + } + System.out.println(); + boolean depositedCorrectly = currentAccount.deposit(userInput); + if(depositedCorrectly) { + System.out.println("Thank you for your deposit"); + } + else { + System.out.println("Sorry, unable to deposit"); + } + System.out.println(); + displaySelectedAccountOptions(); + } + + private void withdrawFromAccount() { + System.out.println("How much would you like to withdraw? (Max $25,000)"); + System.out.print("$"); + double userInput = 0; + try { + userInput = getUserInputDouble(); + } catch (Exception e) { + printInvalidInputMessage(); + withdrawFromAccount(); + } + System.out.println(); + boolean withdrewCorrectly = currentAccount.withdraw(userInput); + if(withdrewCorrectly) { + System.out.println("Thank you for your withdrawal"); + } + else { + System.out.println("Sorry, unable to withdraw"); + } + System.out.println(); + displaySelectedAccountOptions(); + + } + + private void transferToOtherAccount() { + if(currentUser.getAccounts().size() > 1) { + Account chosenAccount = selectAvailableTransferAccount(); + System.out.println("How much would you like to transfer to the account?"); + System.out.print("$"); + double userInputAmount = 0; + try { + userInputAmount = getUserInputDouble(); + } catch (Exception e) { + printInvalidInputMessage(); + transferToOtherAccount(); + } + System.out.println(); + boolean transferredCorrectly = currentAccount.transferTo(chosenAccount, userInputAmount); + if(transferredCorrectly) { + System.out.println("Thank you for your transfer"); + } + else { + System.out.println("Sorry, unable to transfer"); + } + } + else { + System.out.println("Sorry, no available accounts to transfer to"); + } + System.out.println(); + displaySelectedAccountOptions(); + + } + + private Account selectAvailableTransferAccount() { + System.out.println("Your available accounts to transfer to are below, please select the account"); + int index = 1; + int currentAccountIndex = 1; + for (Account account : currentUser.getAccounts()) { + if (!account.equals(currentAccount)) { + System.out.println(index + ": " + account.getAccountType() + " Available balance: " + + String.format("$%.2f", account.getBalance()) + " Account number: " + account.getAccountNumber()); + index++; + } + else { + currentAccountIndex = index; + } + } + int userInput = 1; + try { + userInput = getUserInputInt(); + } catch (Exception e) { + printInvalidInputMessage(); + transferToOtherAccount(); + } + System.out.println(); + Account chosenAccount; + if(userInput < currentAccountIndex) { + chosenAccount = currentUser.getAccounts().get(userInput - 1); + } + else { + chosenAccount = currentUser.getAccounts().get(userInput); + } + + return chosenAccount; + } + + private void printTransactionHistory() { + System.out.println(currentAccount.formatTransactionHistory()); + displaySelectedAccountOptions(); + } + + private void closeCurrentAccount() { + boolean closedCorrectly = currentUser.closeAccount(currentAccount); + if(closedCorrectly) { + System.out.println("Account successfully closed"); + System.out.println(); + displayAvailableOptions(); + } + else { + System.out.println("Sorry, unable to close account"); + System.out.println(); + displaySelectedAccountOptions(); + } + } + + private int getUserInputInt() { + Scanner scan = new Scanner(System.in); + int choice = scan.nextInt(); + return choice; + } + + private double getUserInputDouble() { + Scanner scan = new Scanner(System.in); + double input = scan.nextDouble(); + return input; + } + + private String getUserInputString() { + Scanner scan = new Scanner(System.in); + String input = scan.nextLine(); + return input; + } + + private static void printInvalidInputMessage() { + System.out.println(); + System.out.println("Invalid input"); + System.out.println(); + } +} diff --git a/src/main/java/Account.java b/src/main/java/Account.java new file mode 100644 index 0000000..f6f4895 --- /dev/null +++ b/src/main/java/Account.java @@ -0,0 +1,106 @@ +import java.util.ArrayList; +import java.util.Random; + +public class Account { + private int accountNumber; + private AccountType accountType; + private double balance; + private ArrayList transactionHistory = new ArrayList(); + + public Account() { + this(AccountType.CHECKING); + } + + public Account(AccountType type) { + this(type, 0); + } + + public Account(AccountType type, double initialBalance) { + Random rand = new Random(); + accountNumber = rand.nextInt(1000000); + accountType = type; + if(initialBalance < 0) { + balance = 0; + } + else if(initialBalance > 25000) { + balance = 25000; + } + else { + balance = Main.truncateToTwoDecimalPlaces(initialBalance); + } + } + + public boolean deposit(double amount) { + if(amount > 25000) { + amount = 25000; + } + else { + amount = Main.truncateToTwoDecimalPlaces(amount); + } + if(amount > 0) { + balance += amount; + transactionHistory.add(String.format("Deposited $%.2f, Account balance $%.2f", amount, balance)); + return true; + } + else { + return false; + } + } + + public boolean withdraw(double amount) { + if(amount > 25000) { + amount = 25000; + } + else { + amount = Main.truncateToTwoDecimalPlaces(amount); + } + if(amount > 0) { + if(balance >= amount) { + balance -= amount; + transactionHistory.add(String.format("Withdrew $%.2f, Account balance $%.2f", amount, balance)); + return true; + } + } + + return false; + } + + public boolean transferTo(Account otherAccount, double amount) { + if(amount > 25000) { + amount = 25000; + } + else { + amount = Main.truncateToTwoDecimalPlaces(amount); + } + boolean validWithdraw = this.withdraw(amount); + if(validWithdraw) { + boolean depositWorked = otherAccount.deposit(amount); + return depositWorked; + } + return false; + } + + public String formatTransactionHistory() { + String output = ""; + for(String transaction: transactionHistory) { + output += transaction + "\n"; + } + return output; + } + + public double getBalance() { + return balance; + } + + public ArrayList getTransactionHistory() { + return transactionHistory; + } + + public AccountType getAccountType() { + return accountType; + } + + public int getAccountNumber() { + return accountNumber; + } +} diff --git a/src/main/java/AccountType.java b/src/main/java/AccountType.java new file mode 100644 index 0000000..659968f --- /dev/null +++ b/src/main/java/AccountType.java @@ -0,0 +1,5 @@ +public enum AccountType { + CHECKING, + SAVINGS, + INVESTMENT +} diff --git a/src/main/java/Checking.java b/src/main/java/Checking.java new file mode 100644 index 0000000..7221eb8 --- /dev/null +++ b/src/main/java/Checking.java @@ -0,0 +1,10 @@ +public class Checking extends Account { + + public Checking() { + this(0); + } + + public Checking(double initialBalance) { + super(AccountType.CHECKING, initialBalance); + } +} diff --git a/src/main/java/Investment.java b/src/main/java/Investment.java new file mode 100644 index 0000000..9cd5df0 --- /dev/null +++ b/src/main/java/Investment.java @@ -0,0 +1,10 @@ +public class Investment extends Account { + + public Investment() { + this(0); + } + + public Investment(double initialBalance) { + super(AccountType.INVESTMENT, initialBalance); + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 05e41a9..35ad599 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -4,6 +4,12 @@ public class Main { public static void main(String[] args){ + ATMInterface atmInterface = new ATMInterface(); + atmInterface.start(); } + + public static double truncateToTwoDecimalPlaces(double doubleThatNeedsTruncating) { + return Math.floor(doubleThatNeedsTruncating * 100) / 100; + } } diff --git a/src/main/java/Savings.java b/src/main/java/Savings.java new file mode 100644 index 0000000..ba2b4b3 --- /dev/null +++ b/src/main/java/Savings.java @@ -0,0 +1,10 @@ +public class Savings extends Account { + + public Savings() { + this(0); + } + + public Savings(double initialBalance) { + super(AccountType.SAVINGS, initialBalance); + } +} diff --git a/src/main/java/User.java b/src/main/java/User.java new file mode 100644 index 0000000..be7e886 --- /dev/null +++ b/src/main/java/User.java @@ -0,0 +1,41 @@ +import java.util.ArrayList; + +public class User { + private int userID; + private String password; + private ArrayList accounts = new ArrayList(3); + + private static int nextID = 1; + + public User(String aPassword) { + userID = nextID; + nextID++; + password = aPassword; + } + + + public int getUserID() { + return userID; + } + + public boolean isCorrectPassword(String input) { + return password.equals(input); + } + + public ArrayList getAccounts() { + return accounts; + } + + public void addAccount(Account newAccount) { + accounts.add(newAccount); + } + + public boolean closeAccount(Account accountToRemove) { + if(accountToRemove.getBalance() > 0) { + return false; + } + else { + return accounts.remove(accountToRemove); + } + } +} diff --git a/src/main/java/UserWarehouse.java b/src/main/java/UserWarehouse.java new file mode 100644 index 0000000..35066c2 --- /dev/null +++ b/src/main/java/UserWarehouse.java @@ -0,0 +1,71 @@ +import java.util.ArrayList; +import java.util.Random; +import java.util.Scanner; + +public class UserWarehouse { + private static ArrayList allUsers = new ArrayList(128); + + + public static void addUser(User newUser) { + allUsers.add(newUser); + } + + public static User returnUserFromID(int userID) { + for (User user : allUsers) { + if (user.getUserID() == userID) { + return user; + } + } + return null; + } + + public static ArrayList getAllUsers() { + return allUsers; + } + + public static void generateMyUsers() { + String passwords = "abc123\n" // userID 1 + + "dogdog\n" // userID 2 + + "catcat\n" // userID 3 + + "password\n"; // userID 4 + + Scanner scan = new Scanner(passwords); + Random rand = new Random(); + + while (scan.hasNextLine()) { + String tempPassword = scan.nextLine(); + User tempUser = new User(tempPassword); + int numAccounts = rand.nextInt(3) + 1; + for(int i = 1; i <= numAccounts; i++) { + int accountTypeGenerator = rand.nextInt(3); + AccountType generatedType; + switch (accountTypeGenerator) { + case 0: + generatedType = AccountType.CHECKING; + break; + case 1: + generatedType = AccountType.SAVINGS; + break; + case 2: + generatedType = AccountType.INVESTMENT; + break; + default: + generatedType = AccountType.CHECKING; + } + double tempBalance = Main.truncateToTwoDecimalPlaces(rand.nextDouble() * 1000); + switch (generatedType) { + case CHECKING: + tempUser.addAccount(new Checking(tempBalance)); + break; + case SAVINGS: + tempUser.addAccount(new Savings(tempBalance)); + break; + case INVESTMENT: + tempUser.addAccount(new Investment(tempBalance)); + break; + } + } + addUser(tempUser); + } + } +} diff --git a/src/test/java/AccountTest.java b/src/test/java/AccountTest.java new file mode 100644 index 0000000..6ed0dd1 --- /dev/null +++ b/src/test/java/AccountTest.java @@ -0,0 +1,183 @@ +import org.junit.Assert; +import org.junit.Test; + +public class AccountTest { + @Test + public void depositTest1() { + //: Given + Account testAccount = new Account(); + double expected = 50.25; + + //: When + testAccount.deposit(50.25); + double actual = testAccount.getBalance(); + + //: Then + Assert.assertEquals(expected,actual,0); + } + + @Test + public void depositTest2() { + //: Given + Account testAccount = new Account(); + String expected = "Deposited $50.25, Account balance $50.25"; + + //: When + testAccount.deposit(50.25); + String actual = testAccount.getTransactionHistory().get(testAccount.getTransactionHistory().size()-1); + + //: Then + Assert.assertEquals(expected,actual); + } + + @Test + public void depositTest3() { + //: Given + Account testAccount = new Account(); + double expected = 0; + + //: When + testAccount.deposit(0); + double actual = testAccount.getBalance(); + + //: Then + Assert.assertEquals(expected,actual,0); + } + + @Test + public void depositTest4() { + //: Given + Account testAccount = new Account(); + String expected = "Deposited $8.00, Account balance $8.00"; + + //: When + testAccount.deposit(8); + String actual = testAccount.getTransactionHistory().get(testAccount.getTransactionHistory().size()-1); + + //: Then + Assert.assertEquals(expected,actual); + } + + @Test + public void depositTest5() { + //: Given + Account testAccount = new Account(); + boolean expected = false; + + //: When + boolean actual = testAccount.deposit(-10); + + //: Then + Assert.assertEquals(expected,actual); + } + + @Test + public void withdrawTest1() { + //: Given + Account testAccount = new Account(); + double expected = 145; + + //: When + testAccount.deposit(200); + testAccount.withdraw(55); + double actual = testAccount.getBalance(); + + //: Then + Assert.assertEquals(expected,actual,0); + } + + @Test + public void withdrawTest2() { + //: Given + Account testAccount = new Account(); + String expected = "Withdrew $99.99, Account balance $0.01"; + + //: When + testAccount.deposit(100); + testAccount.withdraw(99.99); + String actual = testAccount.getTransactionHistory().get(testAccount.getTransactionHistory().size()-1); + + //: Then + Assert.assertEquals(expected,actual); + } + + @Test + public void withdrawTest3() { + //: Given + Account testAccount = new Account(); + boolean expected = false; + + //: When + testAccount.deposit(100); + boolean actual = testAccount.withdraw(110); + + //: Then + Assert.assertEquals(expected,actual); + } + + @Test + public void withdrawTest4() { + //: Given + Account testAccount = new Account(); + boolean expected = true; + + //: When + testAccount.deposit(100); + boolean actual = testAccount.withdraw(100); + + //: Then + Assert.assertEquals(expected,actual); + } + + @Test + public void transferToTest1() { + //: Given + Account testAccount1 = new Account(); + Account testAccount2 = new Account(); + double expected = 100; + + //: When + testAccount1.deposit(200); + testAccount2.deposit(400); + testAccount1.transferTo(testAccount2, 100); + double actual = testAccount1.getBalance(); + + //: Then + Assert.assertEquals(expected,actual,0); + } + + @Test + public void transferToTest2() { + //: Given + Account testAccount1 = new Account(); + Account testAccount2 = new Account(); + String expected = "Deposited $8.00, Account balance $408.00"; + + //: When + testAccount1.deposit(200); + testAccount2.deposit(400); + testAccount1.transferTo(testAccount2, 8); + String actual = testAccount2.getTransactionHistory().get(testAccount2.getTransactionHistory().size()-1); + + //: Then + Assert.assertEquals(expected,actual); + } + + @Test + public void formatTransactionHistoryTest() { + //: Given + Account testAccount = new Account(); + String expected = "Deposited $100.00, Account balance $100.00\nWithdrew $50.00, Account balance $50.00\n" + + "Deposited $300.33, Account balance $350.33\nWithdrew $106.66, Account balance $243.67\n"; + + //: When + testAccount.deposit(100); + testAccount.withdraw(50); + testAccount.deposit(300.33); + testAccount.withdraw(106.66); + String actual = testAccount.formatTransactionHistory(); + + //: Then + Assert.assertEquals(expected,actual); + } +} diff --git a/src/test/java/MainTest.java b/src/test/java/MainTest.java new file mode 100644 index 0000000..e5ee229 --- /dev/null +++ b/src/test/java/MainTest.java @@ -0,0 +1,16 @@ +import org.junit.Assert; +import org.junit.Test; + +public class MainTest { + @Test + public void truncateToTwoDecimalPlacesTest() { + //: Given + double expected = 100.44; + + //: When + double actual = Main.truncateToTwoDecimalPlaces(100.44321); + + //: Then + Assert.assertEquals(expected, actual, 0); + } +} diff --git a/src/test/java/UserTest.java b/src/test/java/UserTest.java new file mode 100644 index 0000000..e17a0e6 --- /dev/null +++ b/src/test/java/UserTest.java @@ -0,0 +1,77 @@ +import org.junit.Assert; +import org.junit.Test; + +public class UserTest { + + @Test + public void isCorrectPasswordTest1() { + //: Given + User testUser = new User("a"); + boolean expected = false; + + //: When + boolean actual = testUser.isCorrectPassword("b"); + + //: Then + Assert.assertEquals(expected, actual); + } + + @Test + public void isCorrectPasswordTest2() { + //: Given + User testUser = new User("a"); + boolean expected = true; + + //: When + boolean actual = testUser.isCorrectPassword("a"); + + //: Then + Assert.assertEquals(expected, actual); + } + + @Test + public void addAccountTest() { + //: Given + User testUser = new User("a"); + Account testAccount = new Account(); + Account expected = testAccount; + + //: When + testUser.addAccount(testAccount); + Account actual = testUser.getAccounts().get(testUser.getAccounts().size()-1); + + //: Then + Assert.assertEquals(expected, actual); + } + + @Test + public void closeAccountTest1() { + //: Given + User testUser = new User("a"); + Account testAccount = new Account(); + testUser.addAccount(testAccount); + boolean expected = false; + + //: When + testAccount.deposit(100); + boolean actual = testUser.closeAccount(testAccount); + + //: Then + Assert.assertEquals(expected, actual); + } + + @Test + public void closeAccountTest2() { + //: Given + User testUser = new User("a"); + Account testAccount = new Account(); + testUser.addAccount(testAccount); + boolean expected = true; + + //: When + boolean actual = testUser.closeAccount(testAccount); + + //: Then + Assert.assertEquals(expected, actual); + } +} diff --git a/src/test/java/UserWarehouseTest.java b/src/test/java/UserWarehouseTest.java new file mode 100644 index 0000000..2000179 --- /dev/null +++ b/src/test/java/UserWarehouseTest.java @@ -0,0 +1,65 @@ +import org.junit.Assert; +import org.junit.Test; + +public class UserWarehouseTest { + + @Test + public void addUserTest() { + //: Given + UserWarehouse allUsers = new UserWarehouse(); + User testUser = new User("a"); + User expected = testUser; + + //: When + allUsers.addUser(testUser); + User actual = UserWarehouse.getAllUsers().get(UserWarehouse.getAllUsers().size()-1); + + //: Then + Assert.assertEquals(expected, actual); + } + + @Test + public void returnUserFromIDTest() { + //: Given + UserWarehouse allUsers = new UserWarehouse(); + int expected = 3; + + //: When + allUsers.addUser(new User("a")); + allUsers.addUser(new User("a")); + allUsers.addUser(new User("a")); + allUsers.addUser(new User("a")); + int actual = UserWarehouse.returnUserFromID(3).getUserID(); + + //: Then + Assert.assertEquals(expected, actual); + } + + @Test + public void generateMyUsersTest1() { + //: Given + UserWarehouse allUsers = new UserWarehouse(); + int expected = 4; + + //: When + UserWarehouse.generateMyUsers(); + int actual = UserWarehouse.getAllUsers().size(); + + //: Then + Assert.assertEquals(expected, actual); + } + + @Test + public void generateMyUsersTest2() { + //: Given + UserWarehouse allUsers = new UserWarehouse(); + boolean expected = true; + + //: When + UserWarehouse.generateMyUsers(); + boolean actual = UserWarehouse.returnUserFromID(3).isCorrectPassword("cat"); + + //: Then + Assert.assertEquals(expected, actual); + } +}