diff --git a/lib/hamcrest-core-1.3.jar b/lib/hamcrest-core-1.3.jar new file mode 100644 index 0000000..9d5fe16 Binary files /dev/null and b/lib/hamcrest-core-1.3.jar differ diff --git a/lib/junit-4.12.jar b/lib/junit-4.12.jar new file mode 100644 index 0000000..3a7fc26 Binary files /dev/null and b/lib/junit-4.12.jar differ diff --git a/pom.xml b/pom.xml index 9901415..775ae30 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,54 @@ io.zipcoder project-2-atm 1.0-SNAPSHOT - - + + + + org.apache.maven.plugins + maven-compiler-plugin + + 7 + 7 + + + + + + + junit + junit + 4.12 + test + + + junit + junit + 4.12 + test + + + junit + junit + 4.12 + test + + + org.junit.jupiter + junit-jupiter + RELEASE + compile + + + org.junit.jupiter + junit-jupiter + RELEASE + compile + + + junit + junit + 4.13.2 + compile + + \ No newline at end of file diff --git a/src/main/java/Account.java b/src/main/java/Account.java new file mode 100644 index 0000000..73fa491 --- /dev/null +++ b/src/main/java/Account.java @@ -0,0 +1,90 @@ +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.ArrayList; +import java.util.List; + +public class Account { + + Double balance; + String accountType= "Checking"; + //Might use this for something later. + //Maybe an added modifier in case of multiple accounts of same type + String IDnum; + List transHistory=new ArrayList<>(); + + + //A new instance of account would need starting funds and account type + public Account(Double startingBalance, String typeOfAccount){ + if(startingBalance>=0) balance=startingBalance; + else {balance=0.0; System.out.println("Can't open with a negative balance.");} + accountType=typeOfAccount; + } + + //the version of the constructor the tests use. + //The console should probably use the other one. + //If this constructor IS used, account defaults to a checking account. + public Account(Double startingBalance){ + if(startingBalance>=0) balance=startingBalance; + else {balance=0.0; System.out.println("Can't open with a negative balance.");} + } + + public Double getBalance(){ + BigDecimal bd = new BigDecimal(balance).setScale(2, RoundingMode.HALF_UP); + balance = bd.doubleValue(); + return balance; + } + + public String getAccountType(){ + return accountType; + } + + + + //Possible area to expand on: perhaps withdrawing works differently pending account type? + //prevents overdrawing. uses return statement for either + //A balance or to be used in the transfer method. + public Double withdraw(Double amount){ + if(amount>balance){ + System.out.println("Insufficient funds for withdrawal."); + return 0.0; + }else if(amount<0.0){ + System.out.println("Cannot withdraw negative amount."); + return 0.0; + }else{balance-=amount; + transHistory.add(-amount); + return amount;} + } + + //cannot deposit a negative amount + public void deposit(Double amount){ + if (amount>=0.0) {balance+=amount; + transHistory.add(amount);} + if (amount<0.0) System.out.println("You can't deposit what you don't have."); + } + + + + public void transfer(Account gettingMoney, Double amount){ + + gettingMoney.deposit(this.withdraw(amount)); + } + + public void showTransactionHistory(){ + for(Double i:transHistory){ + if(i>0.0){ + System.out.println("Deposit : "+i); + }else if(i<0.0){ + System.out.println("Withdraw: "+i);} + } + } + + + //Returns true if the account can be closed. False if not. + public Boolean closeAccount(){ + if (balance==0.0){return true;} + else return false; + } + + + +} diff --git a/src/main/java/AsciiArtGenerator.java b/src/main/java/AsciiArtGenerator.java new file mode 100644 index 0000000..df43e81 --- /dev/null +++ b/src/main/java/AsciiArtGenerator.java @@ -0,0 +1,60 @@ +import java.awt.*; +import java.awt.image.BufferedImage; +import java.awt.image.RenderedImage; + +public class AsciiArtGenerator { + + //:D + public void introMessage() throws InterruptedException { + int width = 100; + int height = 21; + + BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + Graphics g = image.getGraphics(); + g.setFont(new Font("SansSerif", Font.BOLD,20)); + + Graphics2D g2 = (Graphics2D) g; + g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); + g2.drawString("$ ATM $", 5, 18); + + for(int y = 0; y< height; y++) { + StringBuilder sb = new StringBuilder(); + + Thread.sleep(80); + + for(int x = 0; x < width; x++) { + sb.append(image.getRGB(x, y) == -16777216 ? "$" : " "); + } + System.out.println(sb); + + } + System.out.println("\n"); + } +// +// public void intruderMessage() throws InterruptedException { +// int width = 111; +// int height = 21; +// +// BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); +// Graphics g = image.getGraphics(); +// g.setFont(new Font("SansSerif", Font.BOLD,20)); +// +// Graphics2D g2 = (Graphics2D) g; +// g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); +// g2.drawString("Intruder", 5, 18); +// +// for(int y = 0; y< height; y++) { +// StringBuilder sb = new StringBuilder(); +// +// Thread.sleep(80); +// +// for(int x = 0; x < width; x++) { +// sb.append(image.getRGB(x, y) == -16777216 ? "$" : " "); +// } +// System.out.println(sb); +// +// } +// Thread.sleep(200); +// System.out.println("\n"); +// } +} diff --git a/src/main/java/AtmEngine.java b/src/main/java/AtmEngine.java new file mode 100644 index 0000000..a460210 --- /dev/null +++ b/src/main/java/AtmEngine.java @@ -0,0 +1,81 @@ +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class AtmEngine { + + Map> bank=new HashMap>(); + + //No need for parameters right now, as far as I can tell. + public AtmEngine(){} + + + public void addNewAccount(String passcode, Account newAccount){ + List tempList = bank.get(passcode); + if(tempList == null) { + tempList = new ArrayList<>(); + bank.put(passcode, tempList); + } + tempList.add(newAccount); + } + + //Method to close an account + public void closeAccount(String password, int whichOne){ + ListcurrentUserAccounts= bank.get(password); + if(currentUserAccounts.get(whichOne).closeAccount()){ + currentUserAccounts.remove(whichOne); + } + } + + + //Method to show list of available accounts under a passcode + public void printAllAccounts(String password){ + ListcurrentUserAccounts= bank.get(password); + for(int i=0; i"+currentUserAccounts.get(i).getBalance()); + } + } + + + //Method to transfer funds between existing accounts + public void transfer(String password, Integer accountGiving, Integer accountReceiving, Double cash){ + ListcurrentUserAccounts= bank.get(password); + currentUserAccounts.get(accountGiving).transfer(currentUserAccounts.get(accountReceiving),cash); + } + + + //Method to withdraw from a chosen account + public void withdrawCash(String password, Integer accountUsed, Double cash){ + ListcurrentUserAccounts= bank.get(password); + currentUserAccounts.get(accountUsed).withdraw(cash); + } + + //Method to withdraw money from a chosen account. + public void depositCash(String password, Integer accountUsed, Double cash){ + ListcurrentUserAccounts= bank.get(password); + currentUserAccounts.get(accountUsed).deposit(cash); + } + + public void showAccountTransactions(String password, Integer accountUsed){ + ListcurrentUserAccounts=bank.get(password); + System.out.println("Transaction history for account #: "+(accountUsed+1)); + currentUserAccounts.get(accountUsed).showTransactionHistory(); + } + + //Method to ensure given password exists in database. + public Boolean userExists(String password){ + for (String i : bank.keySet()) { + if(i.equals(password)){return true;} + } + return false; + } + + public Integer sizeOfAccountList(String password){ + ListcurrentUserAccounts= bank.get(password); + return currentUserAccounts.size(); + } + + +} diff --git a/src/main/java/BreakCheck.java b/src/main/java/BreakCheck.java new file mode 100644 index 0000000..a271597 --- /dev/null +++ b/src/main/java/BreakCheck.java @@ -0,0 +1,93 @@ +import javax.swing.*; +import java.sql.SQLOutput; +import java.util.Random; +import java.util.Scanner; + +public class BreakCheck { + + //Ensures user chooses from options provided + public String confirmIsNumber(){ + String newString = ""; + boolean redo = true; + while(redo) { + newString = Console.getStringInput("1 - Checking, 2 - Savings, 3 - Investing, 4 - Exit"); + switch(newString){ + case "1": + redo = false; + return "Checking"; + case "2": + redo = false; + return "Savings"; + case "3": + redo = false; + return "Investing"; + case "4": + System.out.println("Thank you for choosing a real financial institution"); + System.exit(0); + default: System.out.println("Please choose from the menu!"); + } + } + return null; + } + + //Checks that amount entered is a double + public Double isDouble(){ + + Double result = 0.0; + boolean check = true; + do { + String newString = Console.getStringInput("\r"); + try { + result = Double.parseDouble(newString); + check = false; + } catch (NumberFormatException e) { + System.out.println("Please enter valid amount!"); + } + } while(check); + return result; + } + + public Integer isInteger(){ + Integer result = 0; + boolean check = true; + do { + String newString = Console.getStringInput("\r"); + try { + result = Integer.parseInt(newString); + check = false; + } catch (NumberFormatException e) { + System.out.println("Please enter valid amount!"); + } + } while(check); + return result; + } + + //Generates pin for user + public String pinGenerator(){ + String newPin = ""; + Random randomInt = new Random(); + for(int i =8; i > 0; i--) { + newPin += String.valueOf(randomInt.nextInt(10)); + } + + return newPin; + } + + public Integer isPIN(){ + + Integer result = 0; + boolean check = true; + do { + String newString = Console.getStringInput("\r"); + try { + result = Integer.parseInt(newString); + check = false; + } catch (NumberFormatException e) { + System.out.println("Please enter valid PIN"); + } + } while(check); + return result; + } + + +} diff --git a/src/main/java/Console.java b/src/main/java/Console.java new file mode 100644 index 0000000..a5ff07a --- /dev/null +++ b/src/main/java/Console.java @@ -0,0 +1,124 @@ +import java.util.Scanner; + +public class Console { + Main main = new Main(); + BreakCheck breakCheck = new BreakCheck(); + Scanner userInput = new Scanner(System.in); + + public void greetingMessage() throws InterruptedException { + AsciiArtGenerator asciiGet = new AsciiArtGenerator(); + asciiGet.introMessage(); + //System.out.println("\n"); + System.out.println("Welcome to a real financial institution! \n"); + } + + // We will need a prompt to enter a PIN before anything else. + // link to account object associated with pin in HashMap + + public static String getStringInput(String prompt) { + Scanner scanner = new Scanner(System.in); + System.out.println(prompt); + String userInput = scanner.nextLine(); + return userInput; + } + + public String customerPortal(/*String prompt*/) { + System.out.println("Are you already an account holder at real financial institution?"); + System.out.println("1 - New Customer, 2 - Existing Customer, 3 - Exit"); + //getStringInput(prompt); + String customerStatus = ""; + boolean invalidInput = true; + while (invalidInput) { + customerStatus = userInput.nextLine(); + switch (customerStatus) { + case "1": { + main.newCustomer(); + invalidInput = false; + break; + } + case "2": { + main.returningCustomer(); + invalidInput = false; + break; + } + case "3": { + System.out.println("Thank you for choosing a real financial institution."); + System.exit(0); + } + default: { + System.out.println("Please choose from the menu."); + break; + } + } + } + return customerStatus; + } + + + //Simple user Interface + public String chooseAccount() { + String accountType = breakCheck.confirmIsNumber(); + System.out.println("You have chosen " + accountType); + return accountType; + } +// this will expand to include transfer and other method calls + public void accountActions() { + System.out.println("1 - Account Balance(s) 2 - Deposit, 3 - Withdraw, 4 - Transfer, 5 - Choose Account, " + + "\n6 - Add Account, 7 - Close an Account, 8 - Transaction History\n" + + " 9 - Return to Main Menu, 10 - Exit"); + + String accountAction = ""; + boolean invalidInput = true; + while (invalidInput) { + accountAction = userInput.nextLine(); + switch (accountAction) { + case "1": { + main.getAccountSummary(); + break; + } + case "2": { + main.depositMade(); + break; + } + case "3": { + main.withdrawMade(); + + break; + } + case "4": { + main.transferMade(); + break; + } + case "5": { + main.chooseAccount(); + break; + } + case "6": { + main.additionalAccount(); + break; + } + case "7": { + main.closeAccount(); + break; + } + case "8": { + main.getTransactionHistory(); + break; + } + case "9": { + customerPortal(); + break; + } + case "10": { + System.out.println("Thank you for choosing real financial institution."); + System.exit(0); + } + default: { + System.out.println("Please choose from the menu."); + break; + } + } + accountActions(); + } + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 05e41a9..f4ed735 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,9 +1,136 @@ +import java.util.Scanner; + /** * Created by iyasuwatts on 10/17/17. */ public class Main { + AtmEngine atmEngine = new AtmEngine(); + BreakCheck newCheck = new BreakCheck(); + Scanner userInput = new Scanner(System.in); + String pin = ""; + Integer currentAccount = 0; + + public static void main(String[] args) throws InterruptedException { + // build instance of AtmEngine and call that in the + // instance in the console methods + Console consoleOut = new Console(); + consoleOut.greetingMessage(); + consoleOut.customerPortal(); + //consoleOut.chooseAccount(); + consoleOut.accountActions(); + } + + public void newCustomer() { + + System.out.println("What type of account would you like to open?"); + String accountType = newCheck.confirmIsNumber(); + System.out.println("What is your initial deposit?"); + Double initialDeposit = newCheck.isDouble(); + pin = newCheck.pinGenerator(); + if(atmEngine.userExists(pin)){ + pin = newCheck.pinGenerator(); + } + System.out.println("Your new pin is: " + pin); + Account account = new Account(initialDeposit,accountType); + atmEngine.addNewAccount(pin, account); + atmEngine.printAllAccounts(pin); + currentAccount = 0; + } + + public void getAccountSummary(){ + atmEngine.printAllAccounts(pin); + } + + public void depositMade() { + System.out.print("Amount to be deposited: "); + Double amount = newCheck.isDouble(); + atmEngine.depositCash(this.pin, currentAccount, amount); + atmEngine.printAllAccounts(pin); + } + + public void withdrawMade() { + System.out.print("Amount to be withdrawn: "); + Double amount = newCheck.isDouble(); + atmEngine.withdrawCash(pin, currentAccount, amount); + atmEngine.printAllAccounts(pin); + } + + public void transferMade() { + System.out.println("Pull funds from?"); + Integer accountFrom = newCheck.isInteger(); + while (accountFrom > atmEngine.sizeOfAccountList(pin)) { + System.out.println("Sorry that account is not in our system. Try again:"); + accountFrom = newCheck.isInteger(); + } + + System.out.println("Move funds to?"); + Integer accountTo = newCheck.isInteger(); + while (accountTo > atmEngine.sizeOfAccountList(pin)) { + System.out.println("Sorry that account is not in our system. Try again:"); + accountTo = newCheck.isInteger(); + } + + System.out.println("Amount to be transferred?"); + Double amount = newCheck.isDouble(); + + atmEngine.transfer(this.pin, accountFrom - 1, accountTo - 1, amount); + atmEngine.printAllAccounts(pin); + } + + public void chooseAccount() { + atmEngine.printAllAccounts(pin); + System.out.println("Choose an account:"); + Integer accountToAccess = newCheck.isInteger();; + + while (accountToAccess > atmEngine.sizeOfAccountList(pin)) { + System.out.println("Sorry that account is not in our system. Try again:"); + accountToAccess = newCheck.isInteger(); + } + currentAccount = accountToAccess - 1; + } + + public void additionalAccount() { + System.out.println("What type of account would you like to add?"); + String additionalAccountType = newCheck.confirmIsNumber();; + System.out.println("What is your initial deposit?"); + Double initialDeposit = newCheck.isDouble(); + Account account = new Account(initialDeposit,additionalAccountType); + atmEngine.addNewAccount(pin, account); + atmEngine.printAllAccounts(pin); + } + + public void closeAccount() { + atmEngine.printAllAccounts(pin); + System.out.println("Accounts must be empty to close. Make transfers first if necessary."); + System.out.println("Which account would you like to close?"); + Integer accountToClose = newCheck.isInteger();; + atmEngine.closeAccount(pin,accountToClose - 1); + + + } + + public void returningCustomer() { + System.out.println("Please enter your PIN: "); + String returningPin = ""; + returningPin = userInput.nextLine(); + int counter = 5; + while (!atmEngine.userExists(returningPin) && counter > 0) { + System.out.println("Sorry that PIN is not in our system."); + returningPin = userInput.nextLine(); + counter--; + } + if (counter == 0) { + + System.out.println("Suspicious activity. ATM powering down."); + System.exit(0); + } + pin = returningPin; + } + + public void getTransactionHistory() { + System.out.println("Select one of your accounts."); + Integer accountSelection = newCheck.isInteger(); - public static void main(String[] args){ - + atmEngine.showAccountTransactions(pin, accountSelection-1); } } diff --git a/src/main/test/AccountTest.java b/src/main/test/AccountTest.java index cbea4ad..462d94e 100644 --- a/src/main/test/AccountTest.java +++ b/src/main/test/AccountTest.java @@ -9,56 +9,56 @@ // Test the expected Account class from ATM. public class AccountTest { - @Test + /*@Test public void testA0() { Account a = new Account(0.0); - assertEquals(0.0, a.balance(), 0.0001); + Assert.assertEquals(0.0, a.balance(), 0.0001); } @Test public void testA00() { Account a = new Account(10.0); - assertEquals(10.0, a.balance(), 0.0001); + Assert.assertEquals(10.0, a.balance(), 0.0001); } @Test public void testA01() { Account a = new Account(0.0); - assertEquals(true, a.closeAccount()); + Assert.assertEquals(true, a.closeAccount()); } @Test public void testA02() { Account a = new Account(10.0); - assertEquals(false, a.closeAccount()); + Assert.assertEquals(false, a.closeAccount()); } @Test public void testA1() { Account a = new Account(0.0); a.deposit(100.0); - assertEquals(100.0, a.balance(), 0.0001); + Assert.assertEquals(100.0, a.balance(), 0.0001); } @Test public void testA2() { Account a = new Account(10.0); a.deposit(100.0); - assertEquals(110.0, a.balance(), 0.0001); + Assert.assertEquals(110.0, a.balance(), 0.0001); } @Test public void testA3() { Account a = new Account(200.0); Double actual = a.withdraw(100.0); - assertEquals(100.0, actual, 0.0001); + Assert.assertEquals(100.0, actual, 0.0001); } @Test public void testA4() { Account a = new Account(0.0); Double actual = a.withdraw(1.0); - assertEquals(0.0, actual, 0.0001); + Assert.assertEquals(0.0, actual, 0.0001); } @Test @@ -67,7 +67,7 @@ public void testA5() { Account b = new Account(0.0); a.transfer(b, 10.0); assertEquals(0.0, a.balance(), 0.0001); - assertEquals(10.0, b.balance(), 0.0001); + Assert.assertEquals(10.0, b.balance(), 0.0001); } @Test @@ -75,9 +75,9 @@ public void testA6() { Account a = new Account(10.0); Account b = new Account(0.0); a.transfer(b, 100.0); // nothing should happen - assertEquals(10.0, a.balance(), 0.0001); - assertEquals(0.0, b.balance(), 0.0001); - } + Assert.assertEquals(10.0, a.balance(), 0.0001); + Assert.assertEquals(0.0, b.balance(), 0.0001); + }*/ } diff --git a/src/main/test/BreakCheckTest.java b/src/main/test/BreakCheckTest.java new file mode 100644 index 0000000..9831bbc --- /dev/null +++ b/src/main/test/BreakCheckTest.java @@ -0,0 +1,123 @@ +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.StringWriter; + +//@RunWith(Parameterized.class) +public class BreakCheckTest { + + @Test + public void pinGeneratorTest1(){ + //Given + BreakCheck newCheck = new BreakCheck(); + //Action + String newPin = newCheck.pinGenerator(); + //Expected + int expected = 8; + //Actual + int actual = newPin.length(); + System.out.println(newPin); + Assert.assertEquals(expected,actual); + } + @Test + public void pinGeneratorTest2(){ + //Given + BreakCheck newCheck = new BreakCheck(); + //Action + String newPin = newCheck.pinGenerator(); + //Expected + int expected = 8; + //Actual + int actual = newPin.length(); + System.out.println(newPin); + Assert.assertEquals(expected,actual); + } + @Test + public void pinGeneratorTest3(){ + //Given + BreakCheck newCheck = new BreakCheck(); + //Action + String newPin = newCheck.pinGenerator(); + //Expected + int expected = 8; + //Actual + int actual = newPin.length(); + System.out.println(newPin); + Assert.assertEquals(expected,actual); + } + + + @Test + public void isDouble(){ + // Given, simulated user input + BreakCheck newCheck = new BreakCheck(); + InputStream sysInBackup = System.in; // backup System.in to restore it later + ByteArrayInputStream in = new ByteArrayInputStream("100".getBytes()); + System.setIn(in); + // Expected + Double expected = 100.0; + // Actual + Double actual = newCheck.isDouble(); + // Assert + System.out.println(actual); + System.setIn(sysInBackup); + Assert.assertEquals(expected,actual); + } + + + @Test + public void confirmIsNumberOneTest(){ + // Given, simulated user input + BreakCheck newCheck = new BreakCheck(); + InputStream sysInBackup = System.in; // backup System.in to restore it later + ByteArrayInputStream in = new ByteArrayInputStream("1".getBytes()); + System.setIn(in); + // Expected + String expected = "Checking"; + // Actual + String actual = newCheck.confirmIsNumber(); + // Assert + System.out.println(actual); + System.setIn(sysInBackup); + Assert.assertEquals(expected,actual); + } + @Test + public void confirmIsNumberTwoTest(){ + // Given, simulated user input + BreakCheck newCheck = new BreakCheck(); + InputStream sysInBackup = System.in; // backup System.in to restore it later + ByteArrayInputStream in = new ByteArrayInputStream("2".getBytes()); + System.setIn(in); + // Expected + String expected = "Savings"; + // Actual + String actual = newCheck.confirmIsNumber(); + // Assert + System.out.println(actual); + System.setIn(sysInBackup); + Assert.assertEquals(expected,actual); + } + @Test + public void confirmIsNumberThreeTest(){ + // Given, simulated user input + BreakCheck newCheck = new BreakCheck(); + InputStream sysInBackup = System.in; // backup System.in to restore it later + ByteArrayInputStream in = new ByteArrayInputStream("3".getBytes()); + System.setIn(in); + // Expected + String expected = "Investing"; + // Actual + String actual = newCheck.confirmIsNumber(); + // Assert + System.out.println(actual); + System.setIn(sysInBackup); + Assert.assertEquals(expected,actual); + } + + + +} diff --git a/src/main/test/ConsoleTest.java b/src/main/test/ConsoleTest.java new file mode 100644 index 0000000..9e4c376 --- /dev/null +++ b/src/main/test/ConsoleTest.java @@ -0,0 +1,28 @@ +import org.junit.Assert; +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.util.Scanner; + +public class ConsoleTest{ + + @Test + public void getStringInputTest(){ + // Given, simulated user input + Console console = new Console(); + InputStream sysInBackup = System.in; // backup System.in to restore it later + ByteArrayInputStream in = new ByteArrayInputStream("Hello!".getBytes()); + System.setIn(in); + // Expected + String expected = "Hello!"; + // Actual + String actual = console.getStringInput(expected); + // Assert + System.out.println(actual); + System.setIn(sysInBackup); + Assert.assertEquals(expected,actual); + } + + +} \ No newline at end of file diff --git a/src/main/test/TestAccount.java b/src/main/test/TestAccount.java new file mode 100644 index 0000000..32d33bf --- /dev/null +++ b/src/main/test/TestAccount.java @@ -0,0 +1,144 @@ +import org.junit.Assert; +import org.junit.jupiter.api.Test; + +/* +Test checklist: +Test both constructors +Check balance +Test withdrawal (Both with enough and not enough money in account, withdrawing negative amount...) +Test deposit (make sure positive) + +*/ + +public class TestAccount { + + + @Test + public void constructorTest1() { + Account a = new Account(0.0); + Assert.assertEquals(0.0, a.getBalance(), 0.0001); + } + + @Test + public void constructorTest2() { + Account a = new Account(10.0); + Assert.assertEquals(10.0, a.getBalance(), 0.0001); + } + + @Test + public void constructorTest3() { + Account a = new Account(-5.0, "Investment"); + Assert.assertEquals(0.0, a.getBalance(), 0.0001); + } + + @Test + public void constructorTest4() { + String expected="Investment"; + Account a = new Account(0.0, "Investment"); + Assert.assertEquals(expected, a.getAccountType()); + } + + + + @Test + public void closeAccountTest1() { + Account a = new Account(0.0); + Assert.assertEquals(true, a.closeAccount()); + } + + @Test + public void closeAccountTest2() { + Account a = new Account(10.0); + Assert.assertEquals(false, a.closeAccount()); + } + + @Test + public void depositTest() { + Account a = new Account(0.0); + a.deposit(100.0); + Assert.assertEquals(100.0, a.getBalance(), 0.0001); + } + + @Test + public void depositTest2() { + Account a = new Account(10.0); + a.deposit(100.0); + Assert.assertEquals(110.0, a.getBalance(), 0.0001); + } + + @Test + public void depositTest3() { + Account a = new Account(10.0); + a.deposit(-4.0); + Assert.assertEquals(10.0, a.getBalance(), 0.0001); + } + + + @Test + public void withdrawTest1() { + Account a = new Account(200.0); + Double actual = a.withdraw(100.0); + Assert.assertEquals(100.0, actual, 0.0001); + } + + @Test + public void withdrawTest2() { + Account a = new Account(0.0); + Double actual = a.withdraw(1.0); + Assert.assertEquals(0.0, actual, 0.0001); + } + + @Test + public void withdrawTest3() { + Account a = new Account(0.0); + Double actual = a.withdraw(-100.0); + Assert.assertEquals(0.0, actual, 0.0001); + } + + @Test + public void transferTest() { + Account a = new Account(10.0); + Account b = new Account(0.0); + a.transfer(b, 10.0); + Assert.assertEquals(0.0, a.getBalance(), 0.0001); + Assert.assertEquals(10.0, b.getBalance(), 0.0001); + } + + @Test + public void transferTest2() { + Account a = new Account(10.0); + Account b = new Account(0.0); + a.transfer(b, 100.0); // nothing should happen + Assert.assertEquals(10.0, a.getBalance(), 0.0001); + Assert.assertEquals(0.0, b.getBalance(), 0.0001); + } + + @Test + public void historyTest() { + Account a = new Account(10.0); + a.deposit(33.33); + a.showTransactionHistory(); + } + + @Test + public void historyTest2() { + Account a = new Account(57.66); + a.withdraw(33.32); + a.showTransactionHistory(); + System.out.println(a.getBalance()); + } + + @Test + public void historyTest3() { + Account a = new Account(57.66); + a.withdraw(33.32); + a.deposit(2.0); + a.deposit(8.9); + a.withdraw(100.0); + a.deposit(1.1); + a.withdraw(5.6); + a.showTransactionHistory(); + System.out.println(a.getBalance()); + } + +} diff --git a/src/main/test/testAtmEngine.java b/src/main/test/testAtmEngine.java new file mode 100644 index 0000000..4efa0c2 --- /dev/null +++ b/src/main/test/testAtmEngine.java @@ -0,0 +1,258 @@ +import org.junit.Assert; +import org.junit.jupiter.api.Test; + +/* +Test checklist: +Test both constructors +Check balance +Test withdrawal (Both with enough and not enough money in account, withdrawing negative amount...) +Test deposit (make sure positive) + +*/ + +public class testAtmEngine { + @Test + public void engineConstructor(){ + AtmEngine bank = new AtmEngine(); + Assert.assertTrue(bank instanceof AtmEngine); + } + + @Test + public void addNewUser(){ + AtmEngine badBank = new AtmEngine(); + Account a=new Account(-9.0); + Account b=new Account(10.0); + Account c=new Account(10.0); + Account d=new Account(34.0); + String userPin="8675309"; + String otherUser="Dude it's just a test."; + badBank.addNewAccount(userPin,a); + badBank.addNewAccount(userPin,b); + badBank.addNewAccount(otherUser,c); + badBank.addNewAccount(otherUser,d); + badBank.printAllAccounts(otherUser); + badBank.printAllAccounts(userPin); + } + + @Test + public void userExistsTest1(){ + AtmEngine badBank = new AtmEngine(); + Account a=new Account(-9.0); + Account b=new Account(10.0); + Account c=new Account(10.0); + Account d=new Account(34.0); + String userPin="8675309"; + String otherUser="12341234"; + badBank.addNewAccount(userPin,a); + badBank.addNewAccount(otherUser,c); + Assert.assertTrue(badBank.userExists(userPin)); + + } + + @Test + public void userExistsTest2(){ + AtmEngine badBank = new AtmEngine(); + Account a=new Account(-9.0); + Account b=new Account(10.0); + Account c=new Account(10.0); + Account d=new Account(34.0); + String userPin="8675309"; + String otherUser="12341234"; + String anotherOne="88888888"; + badBank.addNewAccount(userPin,a); + badBank.addNewAccount(otherUser,c); + badBank.addNewAccount(otherUser,b); + badBank.addNewAccount(anotherOne,d); + Assert.assertTrue(badBank.userExists(otherUser)); + } + + @Test + public void userExistsTest3(){ + AtmEngine badBank = new AtmEngine(); + Account a=new Account(-9.0); + Account b=new Account(10.0); + Account c=new Account(10.0); + Account d=new Account(34.0); + String userPin="8675309"; + String otherUser="12341234"; + badBank.addNewAccount(userPin,a); + badBank.addNewAccount(otherUser,c); + Assert.assertFalse(badBank.userExists("NotAPassword")); + } + + @Test + public void addNewAccount(){ + AtmEngine badBank = new AtmEngine(); + Account a=new Account(10.0); + Account b=new Account(10.0); + String userPin="8675309"; + badBank.addNewAccount(userPin,a); + badBank.addNewAccount(userPin,b); + badBank.printAllAccounts(userPin); + } + + @Test + public void removeTest(){ + AtmEngine badBank = new AtmEngine(); + Account a=new Account(10.0, "Investment"); + Account b=new Account(0.0, "Whatever"); + Account c= new Account(33.9,"PleaseJustWork"); + String userPin="8675309"; + badBank.addNewAccount(userPin,a); + badBank.addNewAccount(userPin,b); + badBank.addNewAccount(userPin,c); + badBank.closeAccount(userPin,1); + badBank.printAllAccounts(userPin); + } + + @Test + public void removeTest2(){ + AtmEngine badBank = new AtmEngine(); + Account a=new Account(10.0, "Investment"); + Account b=new Account(0.0, "Whatever"); + Account c= new Account(33.9,"PleaseJustWork"); + String userPin="8675309"; + badBank.addNewAccount(userPin,a); + badBank.addNewAccount(userPin,b); + badBank.addNewAccount(userPin,c); + badBank.closeAccount(userPin,0); + badBank.printAllAccounts(userPin); + } + + @Test + public void depositAtmTest(){ + AtmEngine badBank = new AtmEngine(); + Account a=new Account(10.0, "Investment"); + Account b=new Account(0.0, "Whatever"); + Account c= new Account(33.9,"PleaseJustWork"); + String userPin="8675309"; + badBank.addNewAccount(userPin,a); + badBank.addNewAccount(userPin,b); + badBank.addNewAccount(userPin,c); + badBank.depositCash(userPin,0,44.9); + badBank.depositCash(userPin,2,-21.0); + badBank.printAllAccounts(userPin); + } + + @Test + public void depositAtmTest2(){ + AtmEngine badBank = new AtmEngine(); + Account a=new Account(16.66, "Investment"); + Account b=new Account(0.0, "Whatever"); + Account c= new Account(33.9,"PleaseJustWork"); + String userPin="8675309"; + badBank.addNewAccount(userPin,a); + badBank.addNewAccount(userPin,b); + badBank.addNewAccount(userPin,c); + badBank.depositCash(userPin,0,44.93); + badBank.depositCash(userPin,0,3.32); + badBank.printAllAccounts(userPin); + } + + @Test + public void withdrawAtmTest(){ + AtmEngine badBank = new AtmEngine(); + Account a=new Account(10.0, "Investment"); + Account b=new Account(0.0, "Whatever"); + Account c= new Account(33.9,"PleaseJustWork"); + String userPin="8675309"; + badBank.addNewAccount(userPin,a); + badBank.addNewAccount(userPin,b); + badBank.addNewAccount(userPin,c); + badBank.withdrawCash(userPin,0,5.5); + badBank.withdrawCash(userPin,1,44.9); + badBank.withdrawCash(userPin,2,-21.0); + badBank.printAllAccounts(userPin); + } + + @Test + public void transferAtmTest(){ + AtmEngine badBank = new AtmEngine(); + Account a=new Account(10.0, "Investment"); + Account b=new Account(0.0, "Whatever"); + Account c= new Account(33.9,"PleaseJustWork"); + String userPin="8675309"; + badBank.addNewAccount(userPin,a); + badBank.addNewAccount(userPin,b); + badBank.addNewAccount(userPin,c); + badBank.transfer(userPin,0,2,9.21); + badBank.printAllAccounts(userPin); + } + + @Test + public void transferAtmTest2(){ + AtmEngine badBank = new AtmEngine(); + Account a=new Account(10.0, "Investment"); + Account b=new Account(0.0, "Whatever"); + Account c= new Account(33.9,"PleaseJustWork"); + String userPin="8675309"; + badBank.addNewAccount(userPin,a); + badBank.addNewAccount(userPin,b); + badBank.addNewAccount(userPin,c); + badBank.transfer(userPin,1,0,9.21); + badBank.printAllAccounts(userPin); + } + + @Test + public void transferAtmTest3(){ + AtmEngine badBank = new AtmEngine(); + Account a=new Account(10.0, "Investment"); + Account b=new Account(0.0, "Whatever"); + Account c= new Account(33.9,"PleaseJustWork"); + String userPin="8675309"; + badBank.addNewAccount(userPin,a); + badBank.addNewAccount(userPin,b); + badBank.addNewAccount(userPin,c); + badBank.transfer(userPin,2,0,-12.1); + badBank.printAllAccounts(userPin); + } + + @Test + public void transferAtmTest4(){ + AtmEngine badBank = new AtmEngine(); + Account a=new Account(10.0, "Investment"); + Account b=new Account(0.0, "Whatever"); + Account c= new Account(33.9,"PleaseJustWork"); + String userPin="8675309"; + badBank.addNewAccount(userPin,a); + badBank.addNewAccount(userPin,b); + badBank.addNewAccount(userPin,c); + badBank.transfer(userPin,0,1,15.5); + badBank.printAllAccounts(userPin); + } + + @Test + public void historyTest1(){ + AtmEngine badBank = new AtmEngine(); + Account a=new Account(10.0, "Investment"); + Account b=new Account(0.0, "Whatever"); + Account c= new Account(33.9,"PleaseJustWork"); + String userPin="8675309"; + String otherUser="88888888"; + badBank.addNewAccount(userPin,a); + badBank.addNewAccount(userPin,b); + badBank.addNewAccount(userPin,c); + badBank.depositCash(userPin,1,30.0); + badBank.depositCash(userPin,2,9.0); + badBank.depositCash(userPin,0,9.0); + badBank.showAccountTransactions(userPin,1); + } + + @Test + public void historyTest2(){ + AtmEngine badBank = new AtmEngine(); + Account a=new Account(10.0, "Investment"); + Account b=new Account(0.0, "Whatever"); + Account c= new Account(33.9,"PleaseJustWork"); + String userPin="8675309"; + String otherUser="88888888"; + badBank.addNewAccount(userPin,a); + badBank.addNewAccount(userPin,b); + badBank.addNewAccount(userPin,c); + badBank.depositCash(userPin,2,30.44); + badBank.withdrawCash(userPin,2,60.12); + badBank.withdrawCash(userPin,2,190.99); + badBank.depositCash(userPin,2,9.16); + badBank.showAccountTransactions(userPin,2); + } +}