diff --git a/ATM_Lab.png b/ATM_Lab.png new file mode 100644 index 0000000..0a9811c Binary files /dev/null and b/ATM_Lab.png differ diff --git a/Reusing_Classes_1.md b/Reusing_Classes_1.md new file mode 100755 index 0000000..658b615 --- /dev/null +++ b/Reusing_Classes_1.md @@ -0,0 +1,24 @@ +# Rotate Array + +## Objectives + +1. To demonstrate your understanding of objects and functions +2. To demonstrate your understanding of controlling execution +3. To demonstrate your understanding of access control +4. To demonstrate your understanding of reusing classes + + +## Overview + +Create a class that subclasses the built-in List class. Write a function that rotates a list by k elements. For example [1,2,3,4,5,6] rotated by two becomes [3,4,5,6,1,2]. Try solving this without creating a copy of the list. How many swap or move operations do you need? + + +## Unit Test + +No Unit Test + +## Instructions + +1. In your main class, initialize your custom List with values +2. In your main class call the method that rotates your array +3. Print original and rotated List values to screen \ No newline at end of file diff --git a/Reusing_Classes_2.md b/Reusing_Classes_2.md new file mode 100755 index 0000000..ba3aa0f --- /dev/null +++ b/Reusing_Classes_2.md @@ -0,0 +1,25 @@ +# Rotate Array + +## Humans and Superhumans + +## Objectives + +1. To demonstrate your understanding of objects and functions +2. To demonstrate your understanding of controlling execution +3. To demonstrate your understanding of access control +4. To demonstrate your understanding of reusing classes + + +## Overview + +Create a 'Human' class that has fields for: name, age, gender, occupation, and address. Also create methods for retreiving and outputing this data to screen.Then create a SuperHuman class that subclasses the first with fields for good or bad, hero name, super ability. As before, create methods for retrieving field data and printing to screen. + +## Unit Test + +No Unit Test + +## Instructions + +1. In your main class initialize a few human and superhuman instances +2. Demonstrate calling methods inherited from Human on your SuperHuman instances +3. Print all output to screen \ No newline at end of file diff --git a/Reusing_Classes_3.md b/Reusing_Classes_3.md new file mode 100755 index 0000000..9152922 --- /dev/null +++ b/Reusing_Classes_3.md @@ -0,0 +1,26 @@ +# Class Manager + +## Product Inventory Project + +## Objectives + +1. To demonstrate your understanding of objects and functions +2. To demonstrate your understanding of controlling execution +3. To demonstrate your understanding of access control +4. To demonstrate your understanding of reusing classes + + +## Overview + +Create an application which manages an inventory of products. Create a product class which has a price, id, and quantity on hand. Then create an inventory class which keeps track of various products and can sum up the inventory value. + +## Unit Test + +UML is required +Unit test in place before proceeding with code + +## Instructions + +1. In your main class initialize your manager and populate your inventory +2. Demonstrate calling methods on your manager +3. Print all output to screen \ No newline at end of file diff --git a/Reusing_Classes_4.md b/Reusing_Classes_4.md new file mode 100755 index 0000000..797a3cc --- /dev/null +++ b/Reusing_Classes_4.md @@ -0,0 +1,26 @@ +# Abstract Class + +## Bank Account Manager + +## Objectives + +1. To demonstrate your understanding of objects and functions +2. To demonstrate your understanding of controlling execution +3. To demonstrate your understanding of access control +4. To demonstrate your understanding of reusing classes + + +## Overview + +Create a class called “Account” which will be an abstract class for three other classes called “CheckingAccount”, “SavingsAccount” and “BusinessAccount”. Manage credits and debits from these accounts through an ATM style program. + +## Unit Test + +UML is required +Unit test in place before proceeding with code + +## Instructions + +1. In your main class initialize your ATM +2. Demonstrate calling methods on your ATM with creating, deleting and managing account data +3. Print all output to screen \ No newline at end of file diff --git a/Reusing_Classes_Class_Manager.png b/Reusing_Classes_Class_Manager.png new file mode 100644 index 0000000..124c006 Binary files /dev/null and b/Reusing_Classes_Class_Manager.png differ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..3d142a3 --- /dev/null +++ b/pom.xml @@ -0,0 +1,34 @@ + + + 4.0.0 + + squier.john.reusingClasses + reusingClassesLab + 1.0-SNAPSHOT + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + + + + junit + junit + 4.12 + + + + \ No newline at end of file diff --git a/src/main/java/squier/john/reusingClasses/ATM/ATM.java b/src/main/java/squier/john/reusingClasses/ATM/ATM.java new file mode 100644 index 0000000..3bb9859 --- /dev/null +++ b/src/main/java/squier/john/reusingClasses/ATM/ATM.java @@ -0,0 +1,57 @@ +package squier.john.reusingClasses.ATM; + +import java.util.ArrayList; + +/** + * Created by johnsquier on 1/18/17. + */ +public class ATM { + + private ArrayList accounts; + + public ATM() { + accounts = new ArrayList(); + } + + public ATM(ArrayList accounts) { + this.accounts = accounts; + } + + public ArrayList getAccounts() { return accounts; } + + public String displayAccountNamesAndBalances() { + StringBuilder stringBuilder = new StringBuilder(500); + + for ( ATM.Account a : accounts ) { + stringBuilder.append("Name: "); + stringBuilder.append(a.getAccountHoldersName()); + stringBuilder.append(" Balance: "); + stringBuilder.append(a.getBalance() + "\n"); + } + + return stringBuilder.toString(); + } + + public void addAccountToAccounts(ATM.Account account) { + accounts.add(account); + return; + } + + public void removeAccountWithNameAndBalance(String accountHoldersName, Double balance) { + ATM.Account toRemove = null; + + for ( ATM.Account a : accounts ) { + if ( a.getAccountHoldersName().equalsIgnoreCase(accountHoldersName) + && a.getBalance().equals(balance) ) { + toRemove = a; + break; + // can we have multiple accts with the same name and balance? + // probably... + } + } + + if ( !toRemove.equals(null) ) { + accounts.remove(toRemove); + } + } +} diff --git a/src/main/java/squier/john/reusingClasses/ATM/ATMMain.java b/src/main/java/squier/john/reusingClasses/ATM/ATMMain.java new file mode 100644 index 0000000..c20afdd --- /dev/null +++ b/src/main/java/squier/john/reusingClasses/ATM/ATMMain.java @@ -0,0 +1,59 @@ +package squier.john.reusingClasses.ATM; + +import java.util.ArrayList; + +/** + * Created by johnsquier on 1/18/17. + */ +public class ATMMain { + public static void main(String[] args) { + + // Create some accounts + ATM.Account savings = new SavingsAccount(BankAccountType.SAVINGS, 100.0, "Alice", 1.0, + BankAccountStatus.OPEN, OverdraftProtection.ENABLED); + + ATM.Account checking = new CheckingAccount(BankAccountType.CHECKING, 50.0, "Bob", 0.0, + BankAccountStatus.OPEN, OverdraftProtection.ENABLED); + + ATM.Account investment = new InvestmentAccount(BankAccountType.INVESTMENT, 10000.0, "Eve", 10.0, + BankAccountStatus.OPEN, OverdraftProtection.ENABLED); + + // Put em in an array list + ArrayList accounts = new ArrayList<>(); + accounts.add(savings); + accounts.add(checking); + accounts.add(investment); + + // Create the atm with the accounts + ATM atm = new ATM(accounts); + + // Loop through and display everyone's balance and name + System.out.println(atm.displayAccountNamesAndBalances()); + + // Add a new account to the atm + ATM.Account newAccount = new InvestmentAccount(BankAccountType.INVESTMENT, 0.0, "Steve", 10.0, + BankAccountStatus.OPEN, OverdraftProtection.ENABLED); + + atm.addAccountToAccounts(newAccount); + + // Display the new accounts + System.out.println(atm.displayAccountNamesAndBalances()); + + // Delete an account + System.out.println("Removing steve"); + atm.removeAccountWithNameAndBalance("Steve", 0.0); + + // And print again + System.out.println(atm.displayAccountNamesAndBalances()); + + // Delete another account + System.out.println("Removing Bob"); + atm.removeAccountWithNameAndBalance("Bob", 50.0); + System.out.println(atm.displayAccountNamesAndBalances()); + + // Change Alice's balance + System.out.println("Change Alice's balance"); + atm.getAccounts().get(0).updateBalanceWithCreditOrDebit(-100.0); + System.out.println(atm.displayAccountNamesAndBalances()); + } +} diff --git a/src/main/java/squier/john/reusingClasses/ATM/Account.java b/src/main/java/squier/john/reusingClasses/ATM/Account.java new file mode 100644 index 0000000..6941fcb --- /dev/null +++ b/src/main/java/squier/john/reusingClasses/ATM/Account.java @@ -0,0 +1,201 @@ +package squier.john.reusingClasses.ATM; + +import java.util.ArrayList; + +/** + * Created by johnsquier on 1/18/17. + */ +public abstract class Account { + + private BankAccountType accountType; + private int accountNumber; + private static int nextAccountNumber = 1; + private double balance; + private String accountHoldersName; + private double interestRate; + private BankAccountStatus accountStatus; + private OverdraftProtection overdraftProtection; + private ArrayList transactionRecord; + + + public Account(BankAccountType accountType, double balance, String accountHoldersName, double interestRate, + BankAccountStatus accountStatus, OverdraftProtection overdraftProtection) { + this.accountType = accountType; + accountNumber = nextAccountNumber++; + this.balance = balance; + this.accountHoldersName = accountHoldersName; + this.interestRate = interestRate; + this.accountStatus = accountStatus; + this.overdraftProtection = overdraftProtection; + transactionRecord = new ArrayList(); + } + + public Double getBalance() { + if (accountStatus.equals(BankAccountStatus.OFAC_FROZEN)) { + return null; + } else { + return balance; + } + } + + public String getAccountHoldersName() { + return accountHoldersName; + } + + public void setAccountHoldersName(String newName) { + if (accountStatus.equals(BankAccountStatus.OPEN)) { + accountHoldersName = newName; + + transactionRecord.add(new BankAccountTransaction(TransactionType.NAME_CHANGE, 0.0, + accountStatus, this.accountHoldersName)); + + return; + } + } + + public BankAccountStatus getAccountStatus() { + return accountStatus; + } + + public void setAccountStatus(BankAccountStatus accountStatus) { + if (isAccountOpenOrFrozen()) { + // check balance if trying to close + if (isNewAccountStatusClose(accountStatus)) { + if (this.balance == 0.0) { + this.accountStatus = accountStatus; + + transactionRecord.add(new BankAccountTransaction(TransactionType.STATUS_CHANGE, 0.0, + this.accountStatus, accountHoldersName)); + } + } else { + this.accountStatus = accountStatus; + + transactionRecord.add(new BankAccountTransaction(TransactionType.STATUS_CHANGE, 0.0, + this.accountStatus, accountHoldersName)); + } + } + + return; + } + + private boolean isAccountOpenOrFrozen() { + return !accountStatus.equals(BankAccountStatus.CLOSED); + } + + private boolean isNewAccountStatusClose(BankAccountStatus newStatus) { + return newStatus.equals(BankAccountStatus.CLOSED); + } + + public ApprovalStatus updateBalanceWithCreditOrDebit(double amount) { + + if (accountStatus.equals(BankAccountStatus.OPEN)) { + + if (overdraftProtection.equals(OverdraftProtection.ENABLED)) { + if (isTransactionAnOverDraft(amount)) { + return ApprovalStatus.NOT_APPROVED; + } + } + + if (amount > 0.0) { + return credit(amount); + } else if (amount < 0.0) { + return debit(amount); + } else { + return ApprovalStatus.ZERO_TRANSACTION; + } + } else { + return ApprovalStatus.CANT_COMPLETE_DUE_TO_ACCT_STATUS; + } + } + + private ApprovalStatus credit(double amountToCredit) { + Double previousBalance = getBalance(); + balance += amountToCredit; + + // check if balance updated + if ((previousBalance + amountToCredit) == getBalance()) { + // create new transaction record and add to list + transactionRecord.add(new BankAccountTransaction(TransactionType.DEPOSIT, + amountToCredit, accountStatus, accountHoldersName)); + return ApprovalStatus.APPROVED; + } else { + return ApprovalStatus.NOT_APPROVED; + } + } + + private ApprovalStatus debit(double amountToDebit) { + Double previousBalance = getBalance(); + balance += amountToDebit; + + // amountToDebit is negative + if ((previousBalance + amountToDebit) == getBalance()) { + // create new transaction record + transactionRecord.add(new BankAccountTransaction(TransactionType.WITHDRAWL, amountToDebit, + accountStatus, accountHoldersName)); + return ApprovalStatus.APPROVED; + } else { + return ApprovalStatus.NOT_APPROVED; + } + } + + + private boolean isTransactionAnOverDraft(double amount) { + return (Math.abs(amount) > this.getBalance()); + } + + // refactor this and transferBalanceTo, gotta be some reapeat code + public ApprovalStatus transferBalanceFrom(Account transferFrom, double amountToTransfer) { + if (bothAccountsHaveSameOwner(transferFrom, this)) { + + if (doesAccountHaveSufficientBalance(transferFrom, amountToTransfer)) { + ApprovalStatus debitApproval = transferFrom.updateBalanceWithCreditOrDebit(-amountToTransfer); + ApprovalStatus creditApproval = this.updateBalanceWithCreditOrDebit(amountToTransfer); + + transactionRecord.add(new BankAccountTransaction(TransactionType.TRANSFER, amountToTransfer, + accountStatus, accountHoldersName)); + + // pull out into check approvals method + if ((debitApproval.equals(ApprovalStatus.APPROVED)) + && creditApproval.equals(ApprovalStatus.APPROVED)) { + return ApprovalStatus.APPROVED; + } + } + } + + return ApprovalStatus.NOT_APPROVED; + } + + public ApprovalStatus transferBalanceTo(Account transferTo, double amountToTransfer) { + // check if both accts have the same owener + if (bothAccountsHaveSameOwner(this, transferTo)) { + + // check if from has enough money + if (doesAccountHaveSufficientBalance(this, amountToTransfer)) { + + // do the transfer + ApprovalStatus debitApproval = this.updateBalanceWithCreditOrDebit(-amountToTransfer); + ApprovalStatus creditApproval = transferTo.updateBalanceWithCreditOrDebit(amountToTransfer); + + transactionRecord.add(new BankAccountTransaction(TransactionType.TRANSFER, amountToTransfer, + accountStatus, accountHoldersName)); + + + if ((debitApproval.equals(ApprovalStatus.APPROVED)) + && creditApproval.equals(ApprovalStatus.APPROVED)) { + return ApprovalStatus.APPROVED; + } + } + } + + return ApprovalStatus.NOT_APPROVED; + } + + private boolean bothAccountsHaveSameOwner(Account transferFrom, Account transferTo) { + return transferFrom.getAccountHoldersName().equals(transferTo.accountHoldersName); + } + + private boolean doesAccountHaveSufficientBalance(Account acct, double amount) { + return acct.getBalance() >= amount; + } +} + diff --git a/src/main/java/squier/john/reusingClasses/ATM/ApprovalStatus.java b/src/main/java/squier/john/reusingClasses/ATM/ApprovalStatus.java new file mode 100644 index 0000000..858b829 --- /dev/null +++ b/src/main/java/squier/john/reusingClasses/ATM/ApprovalStatus.java @@ -0,0 +1,6 @@ +package squier.john.reusingClasses.ATM; + +/** + * Created by johnsquier on 1/18/17. + */ +public enum ApprovalStatus {APPROVED, NOT_APPROVED, ZERO_TRANSACTION, CANT_COMPLETE_DUE_TO_ACCT_STATUS} diff --git a/src/main/java/squier/john/reusingClasses/ATM/BankAccountStatus.java b/src/main/java/squier/john/reusingClasses/ATM/BankAccountStatus.java new file mode 100644 index 0000000..84950e1 --- /dev/null +++ b/src/main/java/squier/john/reusingClasses/ATM/BankAccountStatus.java @@ -0,0 +1,6 @@ +package squier.john.reusingClasses.ATM; + +/** + * Created by johnsquier on 1/18/17. + */ +public enum BankAccountStatus {OPEN, CLOSED, OFAC_FROZEN} diff --git a/src/main/java/squier/john/reusingClasses/ATM/BankAccountTransaction.java b/src/main/java/squier/john/reusingClasses/ATM/BankAccountTransaction.java new file mode 100644 index 0000000..57a46dd --- /dev/null +++ b/src/main/java/squier/john/reusingClasses/ATM/BankAccountTransaction.java @@ -0,0 +1,24 @@ +package squier.john.reusingClasses.ATM; + +/** + * Created by johnsquier on 1/18/17. + */ +public class BankAccountTransaction { + private TransactionType transactionType; + private Double transactionAmount; + private BankAccountStatus newStatus; + private String newName; + + public BankAccountTransaction(TransactionType transactionType, double transactionAmount, + BankAccountStatus newStatus, String newName) { + this.transactionType = transactionType; + this.transactionAmount = transactionAmount; + this.newStatus = newStatus; + this.newName = newName; + } + + public TransactionType getTransactionType() { return transactionType; } + public Double getTransactionAmount() { return transactionAmount; } + public BankAccountStatus getBankAccountStatus() { return newStatus; } + public String getName() { return newName; } +} diff --git a/src/main/java/squier/john/reusingClasses/ATM/BankAccountType.java b/src/main/java/squier/john/reusingClasses/ATM/BankAccountType.java new file mode 100644 index 0000000..b62f5e2 --- /dev/null +++ b/src/main/java/squier/john/reusingClasses/ATM/BankAccountType.java @@ -0,0 +1,6 @@ +package squier.john.reusingClasses.ATM; + +/** + * Created by johnsquier on 1/18/17. + */ +public enum BankAccountType {CHECKING, SAVINGS, INVESTMENT} diff --git a/src/main/java/squier/john/reusingClasses/ATM/CheckingAccount.java b/src/main/java/squier/john/reusingClasses/ATM/CheckingAccount.java new file mode 100644 index 0000000..c8bdf1e --- /dev/null +++ b/src/main/java/squier/john/reusingClasses/ATM/CheckingAccount.java @@ -0,0 +1,13 @@ +package squier.john.reusingClasses.ATM; + +/** + * Created by johnsquier on 1/18/17. + */ +public class CheckingAccount extends ATM.Account { + + public CheckingAccount(BankAccountType accountType, double balance, String accountHoldersName, double interestRate, + BankAccountStatus accountStatus, OverdraftProtection overdraftProtection) { + + super(accountType, balance, accountHoldersName, interestRate, accountStatus, overdraftProtection); + } +} diff --git a/src/main/java/squier/john/reusingClasses/ATM/InvestmentAccount.java b/src/main/java/squier/john/reusingClasses/ATM/InvestmentAccount.java new file mode 100644 index 0000000..577f1ac --- /dev/null +++ b/src/main/java/squier/john/reusingClasses/ATM/InvestmentAccount.java @@ -0,0 +1,13 @@ +package squier.john.reusingClasses.ATM; + +/** + * Created by johnsquier on 1/18/17. + */ +public class InvestmentAccount extends ATM.Account { + + public InvestmentAccount(BankAccountType accountType, double balance, String accountHoldersName, double interestRate, + BankAccountStatus accountStatus, OverdraftProtection overdraftProtection) { + + super(accountType, balance, accountHoldersName, interestRate, accountStatus, overdraftProtection); + } +} diff --git a/src/main/java/squier/john/reusingClasses/ATM/OverdraftProtection.java b/src/main/java/squier/john/reusingClasses/ATM/OverdraftProtection.java new file mode 100644 index 0000000..56804c3 --- /dev/null +++ b/src/main/java/squier/john/reusingClasses/ATM/OverdraftProtection.java @@ -0,0 +1,6 @@ +package squier.john.reusingClasses.ATM; + +/** + * Created by johnsquier on 1/18/17. + */ +public enum OverdraftProtection {ENABLED, DISABLED, AUTOMATIC_ACCT_TRANSFER} diff --git a/src/main/java/squier/john/reusingClasses/ATM/SavingsAccount.java b/src/main/java/squier/john/reusingClasses/ATM/SavingsAccount.java new file mode 100644 index 0000000..62ad4ca --- /dev/null +++ b/src/main/java/squier/john/reusingClasses/ATM/SavingsAccount.java @@ -0,0 +1,18 @@ +package squier.john.reusingClasses.ATM; + +import squier.john.reusingClasses.ATM.ATM; +import squier.john.reusingClasses.ATM.BankAccountStatus; +import squier.john.reusingClasses.ATM.BankAccountType; +import squier.john.reusingClasses.ATM.OverdraftProtection; + +/** + * Created by johnsquier on 1/18/17. + */ +public class SavingsAccount extends ATM.Account { + + public SavingsAccount(BankAccountType accountType, double balance, String accountHoldersName, double interestRate, + BankAccountStatus accountStatus, OverdraftProtection overdraftProtection) { + + super(accountType, balance, accountHoldersName, interestRate, accountStatus, overdraftProtection); + } +} diff --git a/src/main/java/squier/john/reusingClasses/ATM/TransactionType.java b/src/main/java/squier/john/reusingClasses/ATM/TransactionType.java new file mode 100644 index 0000000..2d160ae --- /dev/null +++ b/src/main/java/squier/john/reusingClasses/ATM/TransactionType.java @@ -0,0 +1,6 @@ +package squier.john.reusingClasses.ATM; + +/** + * Created by johnsquier on 1/18/17. + */ +public enum TransactionType {WITHDRAWL, DEPOSIT, TRANSFER, STATUS_CHANGE, NAME_CHANGE, INTEREST_RATE_CHANGE} diff --git a/src/main/java/squier/john/reusingClasses/Main.java b/src/main/java/squier/john/reusingClasses/Main.java new file mode 100644 index 0000000..99adb16 --- /dev/null +++ b/src/main/java/squier/john/reusingClasses/Main.java @@ -0,0 +1,63 @@ +package squier.john.reusingClasses; + +import squier.john.reusingClasses.humans.Genders; +import squier.john.reusingClasses.humans.Human; +import squier.john.reusingClasses.humans.SuperHuman; +import squier.john.reusingClasses.inventory.Inventory; +import squier.john.reusingClasses.inventory.Product; +import squier.john.reusingClasses.rotatableList.RotatableList; + +/** + * Created by johnsquier on 1/18/17. + */ +public class Main { + public static void main(String[] args) { + + // Demonstrate RotatableList + RotatableList rotatableList = new RotatableList(); + rotatableList.runRotateDemo(); + + // Print newline + System.out.println(); + + // Demonstrate Human and Superhuman functionality + Human human = new Human(); + System.out.println("Default Human Info:"); + human.printInformation(); + System.out.println(); + + Human human2 = new Human("John", 29, Genders.MALE, "Unemployed", "7 Cartier"); + System.out.println("Human Info:"); + human2.printInformation(); + System.out.println(); + + SuperHuman superHuman = new SuperHuman(); + System.out.println("Default Superhuman Info:"); + superHuman.printInformation(); + System.out.println(); + + SuperHuman superHuman2 = new SuperHuman("John", 29, Genders.MALE, "Unemployed", "7 Cartier", + true, "SuperJohn", "Incredible Typing Speed!"); + System.out.println("SuperHuman Info:"); + superHuman2.printInformation(); + System.out.println(); + + // Demonstrate inventory and product functionality + Inventory inventory = new Inventory(); + Product product1 = new Product(1.00, "1", 1); + Product product2 = new Product(2.00, "2", 2); + System.out.printf("\nTotal Inventory value when empty: %.2f\n", + inventory.calculateInventoryValue()); + + // Now add a few products + inventory.addProductToInventory(product1); + inventory.addProductToInventory(product2); + System.out.printf("After adding a few prodcuts: %.2f\n", + inventory.calculateInventoryValue()); + + // And add one more + inventory.addProductToInventory(new Product(3.00, "3", 3)); + System.out.printf("After adding another product: %.2f\n", + inventory.calculateInventoryValue()); + } +} diff --git a/src/main/java/squier/john/reusingClasses/humans/Genders.java b/src/main/java/squier/john/reusingClasses/humans/Genders.java new file mode 100644 index 0000000..db3048b --- /dev/null +++ b/src/main/java/squier/john/reusingClasses/humans/Genders.java @@ -0,0 +1,7 @@ +package squier.john.reusingClasses.humans; + +/** + * Created by johnsquier on 1/18/17. + */ +// extensibile +public enum Genders {MALE, FEMALE} diff --git a/src/main/java/squier/john/reusingClasses/humans/Human.java b/src/main/java/squier/john/reusingClasses/humans/Human.java new file mode 100644 index 0000000..82cf980 --- /dev/null +++ b/src/main/java/squier/john/reusingClasses/humans/Human.java @@ -0,0 +1,76 @@ +package squier.john.reusingClasses.humans; + +import com.sun.tools.javac.jvm.Gen; + +/** + * Created by johnsquier on 1/18/17. + */ +public class Human { + + private String name; + private Integer age; + private Genders gender; + private String occupation; + private String address; + + public Human() { + name = "default"; + age = 0; + gender = Genders.MALE; + occupation = "default"; + address = "default"; + } + + public Human(String name, Integer age, Genders gender, String occupation, String address) { + this.name = name; + this.age = age; + this.gender = gender; + this.occupation = occupation; + this.address = address; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + public Genders getGender() { + return gender; + } + + public void setGender(Genders gender) { + this.gender = gender; + } + + public String getOccupation() { + return occupation; + } + + public void setOccupation(String occupation) { + this.occupation = occupation; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public void printInformation() { + System.out.printf("Name: %s\nAge: %d\nGender: %s\nOccupation: %s\nAddress: %s\n", + name, age, gender.toString(), occupation, address); + } +} diff --git a/src/main/java/squier/john/reusingClasses/humans/SuperHuman.java b/src/main/java/squier/john/reusingClasses/humans/SuperHuman.java new file mode 100644 index 0000000..58cf35b --- /dev/null +++ b/src/main/java/squier/john/reusingClasses/humans/SuperHuman.java @@ -0,0 +1,41 @@ +package squier.john.reusingClasses.humans; + +/** + * Created by johnsquier on 1/18/17. + */ +public class SuperHuman extends Human { + Boolean isGood; + String heroName; + String superAbility; + + public SuperHuman() { + super(); + isGood = true; + heroName = "default"; + superAbility = "default"; + } + + public SuperHuman(String name, Integer age, Genders gender, String occupation, String address, + Boolean isGood, String heroName, String superAbility) { + super(name, age, gender, occupation, address); + this.isGood = isGood; + this.heroName = heroName; + this.superAbility = superAbility; + } + + public Boolean getIsGood() { return isGood; } + public void setIsGood(Boolean isGood) { this.isGood = isGood; } + + public String getHeroName() { return heroName; } + public void setHeroName(String heroName) { this.heroName = heroName; } + + public String getSuperAbility() { return superAbility; } + public void setSuperAbility(String superAbility) { this.superAbility = superAbility; } + + @Override + public void printInformation() { + super.printInformation(); + System.out.printf("isGood: %s\nheroName: %s\nsuperAbility: %s\n", + isGood.toString(), heroName, superAbility); + } +} diff --git a/src/main/java/squier/john/reusingClasses/inventory/Inventory.java b/src/main/java/squier/john/reusingClasses/inventory/Inventory.java new file mode 100644 index 0000000..94727ff --- /dev/null +++ b/src/main/java/squier/john/reusingClasses/inventory/Inventory.java @@ -0,0 +1,37 @@ +package squier.john.reusingClasses.inventory; + +import java.util.ArrayList; + +/** + * Created by johnsquier on 1/18/17. + */ +public class Inventory { + + private ArrayList products; + + public Inventory() { + products = new ArrayList(); + } + + public Inventory(ArrayList products) { + this.products = products; + } + + public ArrayList getProductsList() { + return products; + } + + public void addProductToInventory(Product product) { + products.add(product); + } + + public Double calculateInventoryValue() { + Double totalInventoryValue = 0.0; + + for ( Product p : products) { + totalInventoryValue += (p.getPrice() * p.getQuantityOnHand()); + } + + return totalInventoryValue; + } +} diff --git a/src/main/java/squier/john/reusingClasses/inventory/Product.java b/src/main/java/squier/john/reusingClasses/inventory/Product.java new file mode 100644 index 0000000..6d86920 --- /dev/null +++ b/src/main/java/squier/john/reusingClasses/inventory/Product.java @@ -0,0 +1,40 @@ +package squier.john.reusingClasses.inventory; + +/** + * Created by johnsquier on 1/18/17. + */ +public class Product { + private Double price; + private String productID; + private Integer quantityOnHand; + + public Product(Double price, String productID, Integer quantityOnHand) { + this.price = price; + this.productID = productID; + this.quantityOnHand = quantityOnHand; + } + + public Double getPrice() { + return price; + } + + public void setPrice(Double price) { + this.price = price; + } + + public String getProductID() { + return productID; + } + + public void setProductID(String productID) { + this.productID = productID; + } + + public Integer getQuantityOnHand() { + return quantityOnHand; + } + + public void setQuantityOnHand(Integer quantityOnHand) { + this.quantityOnHand = quantityOnHand; + } +} diff --git a/src/main/java/squier/john/reusingClasses/rotatableList/RotatableList.java b/src/main/java/squier/john/reusingClasses/rotatableList/RotatableList.java new file mode 100644 index 0000000..035f0d8 --- /dev/null +++ b/src/main/java/squier/john/reusingClasses/rotatableList/RotatableList.java @@ -0,0 +1,61 @@ +package squier.john.reusingClasses.rotatableList; + +import java.util.*; + +/** + * Created by johnsquier on 1/18/17. + * Learn about generics so I can avoid downcasting when extracting from the RotatableList + */ +public class RotatableList extends ArrayList { + + public RotatableList rotateList(int shift) { + RotatableList newList = new RotatableList(); + + for ( int i = 0; i < this.size(); i++ ) { + newList.add(this.get((i + shift) % this.size())); + } + + return newList; + } + + public void createAndInitializeList() { + this.add(1); + this.add(2); + this.add(3); + this.add(4); + this.add(5); + this.add(6); + } + + private void printOutList() { + System.out.print("["); + + // pretty print + for ( int i = 0; i < this.size(); i++ ) { + // handle case where i is the last element + if ( i == this.size() - 1 ) { + System.out.printf("%d", this.get(i)); + } + else { + System.out.printf("%d, ", this.get(i)); + } + } + + System.out.print("]\n"); + + } + + public void runRotateDemo() { + RotatableList list = new RotatableList(); + list.createAndInitializeList(); + + System.out.printf("Original List: "); + list.printOutList(); + System.out.printf("Shift = %d\n", 3); + + list = list.rotateList(3); + + System.out.printf("Rotated List: "); + list.printOutList(); + } +} diff --git a/src/test/java/squier/john/reusingClasses/ATMTest.java b/src/test/java/squier/john/reusingClasses/ATMTest.java new file mode 100644 index 0000000..ad9a559 --- /dev/null +++ b/src/test/java/squier/john/reusingClasses/ATMTest.java @@ -0,0 +1,74 @@ +package squier.john.reusingClasses; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import squier.john.reusingClasses.ATM.*; + +import java.util.ArrayList; + +/** + * Created by johnsquier on 1/18/17. + */ +public class ATMTest { + + ATM atm; + ArrayList accounts; + + @Before + public void setup() { + accounts = new ArrayList(); + + ATM.Account account1 = new SavingsAccount( + BankAccountType.SAVINGS, 100.0, "John", 10.0, + BankAccountStatus.OPEN, OverdraftProtection.ENABLED); + + ATM.Account account2 = new CheckingAccount(BankAccountType.CHECKING, 50.0, "Alice", 0.0, + BankAccountStatus.OPEN, OverdraftProtection.DISABLED); + + + ATM.Account account3 = new InvestmentAccount(BankAccountType.INVESTMENT, 10000.0, "Bob", 15.0, + BankAccountStatus.OPEN, OverdraftProtection.ENABLED); + + accounts.add(account1); + accounts.add(account2); + accounts.add(account3); + + atm = new ATM(accounts); + } + + @Test + public void getAccountsTest() { + ArrayList expected = accounts; + ArrayList actual = atm.getAccounts(); + Assert.assertEquals(expected, actual); + } + + @Test + public void displayAccountNamesAndBalancesTest() { + String expected = "Name: John Balance: 100.0\n" + + "Name: Alice Balance: 50.0\n" + + "Name: Bob Balance: 10000.0\n"; + String actual = atm.displayAccountNamesAndBalances(); + Assert.assertEquals(expected, actual); + } + + @Test + public void addAccountToAccountsTest() { + ATM.Account expected = new SavingsAccount(BankAccountType.SAVINGS, 0.0, "John", 0.0, + BankAccountStatus.OPEN, OverdraftProtection.ENABLED); + atm.addAccountToAccounts(expected); + ATM.Account actual = atm.getAccounts().get(3); + Assert.assertEquals(expected, actual); + } + + @Test + public void removeAccountWithNameAndBalanceTest() { + // need to implement .equals in Account I believe + ArrayList expected = accounts; + + atm.removeAccountWithNameAndBalance("John", 0.0); + + ArrayList actual = atm.getAccounts(); + } +} diff --git a/src/test/java/squier/john/reusingClasses/CheckingAccountTest.java b/src/test/java/squier/john/reusingClasses/CheckingAccountTest.java new file mode 100644 index 0000000..bcfd424 --- /dev/null +++ b/src/test/java/squier/john/reusingClasses/CheckingAccountTest.java @@ -0,0 +1,316 @@ +package squier.john.reusingClasses; + +import org.junit.Assert; +import org.junit.Test; +import squier.john.reusingClasses.ATM.*; + +/** + * Created by johnsquier on 1/18/17. + */ +public class CheckingAccountTest { + ATM.Account bankAccount; + double delta = 0.00001; + + @Test + public void getBalanceAccountUnfrozenTest() { + bankAccount = new CheckingAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + Double expected = 100.0; + Double actual = bankAccount.getBalance(); + Assert.assertEquals(expected, actual, delta); + } + + @Test + public void getBalanceAccountFrozenTest() { + bankAccount = new CheckingAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OFAC_FROZEN, + OverdraftProtection.ENABLED); + + // using null to indicate frozen acct status + Double actual = bankAccount.getBalance(); + Assert.assertNull(actual); + } + + @Test + public void getAccountHoldersNameTest() { + bankAccount = new CheckingAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OFAC_FROZEN, + OverdraftProtection.ENABLED); + + String expected = "John"; + String actual = bankAccount.getAccountHoldersName(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setAccountHoldersNameOpenTest() { + bankAccount = new CheckingAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + String expected = "Bob"; + bankAccount.setAccountHoldersName("Bob"); + String actual = bankAccount.getAccountHoldersName(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setAccountHoldersNameClosedTest() { + bankAccount = new CheckingAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.CLOSED, + OverdraftProtection.ENABLED); + + String expected = "John"; + bankAccount.setAccountHoldersName("Bob"); + String actual = bankAccount.getAccountHoldersName(); + Assert.assertEquals(expected, actual); + } + + @Test + public void updateBalanceAccountClosedTest() { + bankAccount = new CheckingAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.CLOSED, + OverdraftProtection.ENABLED); + + Double expected = 100.0; + bankAccount.updateBalanceWithCreditOrDebit(5.0); + Double actual = bankAccount.getBalance(); + Assert.assertEquals(expected, actual, delta); + } + + @Test + public void updateBalancePositiveTest() { + bankAccount = new CheckingAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + Double expected = 200.0; + bankAccount.updateBalanceWithCreditOrDebit(100.0); + Double actual = bankAccount.getBalance(); + Assert.assertEquals(expected, actual, delta); + } + + @Test + public void updateBalanceNegativeTest() { + bankAccount = new CheckingAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + Double expected = 50.0; + bankAccount.updateBalanceWithCreditOrDebit(-50.0); + Double actual = bankAccount.getBalance(); + Assert.assertEquals(expected, actual, delta); + } + + @Test + public void updateBalanceZeroTest() { + bankAccount = new CheckingAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + ATM.ApprovalStatus expected = ATM.ApprovalStatus.ZERO_TRANSACTION; + ATM.ApprovalStatus actual = bankAccount.updateBalanceWithCreditOrDebit(0.0); + Assert.assertEquals(expected, actual); + } + + @Test + public void transferBalanceFromTestBothEndAboveZeroTest() { + CheckingAccount transferFrom = new CheckingAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + CheckingAccount transferTo = new CheckingAccount(BankAccountType.CHECKING, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + transferTo.transferBalanceFrom(transferFrom, 50.0); + + Double[] expected = {150.0, 50.0}; + Double[] actual = {transferTo.getBalance(), transferFrom.getBalance()}; + Assert.assertArrayEquals(expected, actual); + } + + @Test + public void transferBalanceToTestBothEndAboveZeroTest() { + CheckingAccount transferFrom = new CheckingAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + CheckingAccount transferTo = new CheckingAccount(BankAccountType.CHECKING, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + transferFrom.transferBalanceTo(transferTo, 50.0); + + Double[] expected = {150.0, 50.0}; + Double[] actual = {transferTo.getBalance(), transferFrom.getBalance()}; + Assert.assertArrayEquals(expected, actual); + } + + @Test + public void transferBalanceFromTestNotEnoughInFromTest() { + CheckingAccount transferFrom = new CheckingAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + CheckingAccount transferTo = new CheckingAccount(BankAccountType.CHECKING, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + transferTo.transferBalanceFrom(transferFrom, 500.0); + + Double[] expected = {100.0, 100.0}; + Double[] actual = {transferTo.getBalance(), transferFrom.getBalance()}; + Assert.assertArrayEquals(expected, actual); + } + + @Test + public void transferBalanceToTestNotEnoughInFromTest() { + CheckingAccount transferFrom = new CheckingAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + CheckingAccount transferTo = new CheckingAccount(BankAccountType.CHECKING, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + transferFrom.transferBalanceTo(transferTo, 500.0); + + Double[] expected = {100.0, 100.0}; + Double[] actual = {transferTo.getBalance(), transferFrom.getBalance()}; + Assert.assertArrayEquals(expected, actual); + } + + @Test + public void overdrawWithProtectionOnTest() { + bankAccount = new CheckingAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + Double expected = 100.0; + bankAccount.updateBalanceWithCreditOrDebit(-500.0); + Double actual = bankAccount.getBalance(); + + Assert.assertEquals(expected, actual); + } + + @Test + public void overdrawWithProtectOffTest() { + bankAccount = new CheckingAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.DISABLED); + + Double expected = -400.0; + bankAccount.updateBalanceWithCreditOrDebit(-500.0); + Double actual = bankAccount.getBalance(); + + Assert.assertEquals(expected, actual); + } + + @Test + public void getBankAccountStatusTest() { + bankAccount = new CheckingAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + BankAccountStatus expected = BankAccountStatus.OPEN; + BankAccountStatus actual = bankAccount.getAccountStatus(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setBankAccountStatusClosedFromOpen() { + bankAccount = new CheckingAccount(BankAccountType.SAVINGS, 0.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + BankAccountStatus expected = BankAccountStatus.CLOSED; + bankAccount.setAccountStatus(BankAccountStatus.CLOSED); + BankAccountStatus actual = bankAccount.getAccountStatus(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setBankAccountStatusClosedFromFrozen() { + bankAccount = new CheckingAccount(BankAccountType.SAVINGS, 0.0, + "John", 10.0, BankAccountStatus.OFAC_FROZEN, + OverdraftProtection.ENABLED); + + BankAccountStatus expected = BankAccountStatus.CLOSED; + bankAccount.setAccountStatus(BankAccountStatus.CLOSED); + BankAccountStatus actual = bankAccount.getAccountStatus(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setBankAccountStatusOpenFromFrozen() { + bankAccount = new CheckingAccount(BankAccountType.SAVINGS, 0.0, + "John", 10.0, BankAccountStatus.OFAC_FROZEN, + OverdraftProtection.ENABLED); + + BankAccountStatus expected = BankAccountStatus.OPEN; + bankAccount.setAccountStatus(BankAccountStatus.OPEN); + BankAccountStatus actual = bankAccount.getAccountStatus(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setBankAccountStatusOpenFromClosed() { + // needs to fail + bankAccount = new CheckingAccount(BankAccountType.SAVINGS, 0.0, + "John", 10.0, BankAccountStatus.CLOSED, + OverdraftProtection.ENABLED); + + BankAccountStatus expected = BankAccountStatus.CLOSED; + bankAccount.setAccountStatus(BankAccountStatus.OPEN); + BankAccountStatus actual = bankAccount.getAccountStatus(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setBankAccountStatusFrozenFromOpen() { + bankAccount = new CheckingAccount(BankAccountType.SAVINGS, 0.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + BankAccountStatus expected = BankAccountStatus.OFAC_FROZEN; + bankAccount.setAccountStatus(BankAccountStatus.OFAC_FROZEN); + BankAccountStatus actual = bankAccount.getAccountStatus(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setBankAccountStatusFrozenFromClosed() { + bankAccount = new CheckingAccount(BankAccountType.SAVINGS, 0.0, + "John", 10.0, BankAccountStatus.CLOSED, + OverdraftProtection.ENABLED); + + BankAccountStatus expected = BankAccountStatus.CLOSED; + bankAccount.setAccountStatus(BankAccountStatus.OFAC_FROZEN); + BankAccountStatus actual = bankAccount.getAccountStatus(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setBankAccountStatusClosedWithZeroBalance() { + bankAccount = new CheckingAccount(BankAccountType.SAVINGS, 0.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + BankAccountStatus expected = BankAccountStatus.CLOSED; + bankAccount.setAccountStatus(BankAccountStatus.CLOSED); + BankAccountStatus actual = bankAccount.getAccountStatus(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setBankAccountStatusClosedWithNonZeroBalance() { + bankAccount = new CheckingAccount(BankAccountType.SAVINGS, 10.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + BankAccountStatus expected = BankAccountStatus.OPEN; + bankAccount.setAccountStatus(BankAccountStatus.CLOSED); + BankAccountStatus actual = bankAccount.getAccountStatus(); + Assert.assertEquals(expected, actual); + } +} + diff --git a/src/test/java/squier/john/reusingClasses/InventoryTest.java b/src/test/java/squier/john/reusingClasses/InventoryTest.java new file mode 100644 index 0000000..e8220e2 --- /dev/null +++ b/src/test/java/squier/john/reusingClasses/InventoryTest.java @@ -0,0 +1,66 @@ +package squier.john.reusingClasses; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import squier.john.reusingClasses.inventory.Inventory; +import squier.john.reusingClasses.inventory.Product; + +import java.util.ArrayList; + +/** + * Created by johnsquier on 1/18/17. + */ +public class InventoryTest { + + Inventory inventory; + Product product1, product2; + ArrayList productsList; + + @Before + public void setup() { + product1 = new Product(1.00, "1", 1); + product2 = new Product(2.00, "2", 2); + + productsList = new ArrayList(); + productsList.add(product1); + productsList.add(product2); + + inventory = new Inventory(productsList); + } + + @Test + public void getProductsListTest() { + productsList.add(product1); + productsList.add(product2); + + ArrayList expected = productsList; + ArrayList actual = inventory.getProductsList(); + Assert.assertEquals(expected, actual); + } + + @Test + public void addProductToInventoryTest() { + inventory.addProductToInventory(product1); + inventory.addProductToInventory(product2); + + ArrayList expected = productsList; + ArrayList actual = inventory.getProductsList(); + Assert.assertEquals(expected, actual); + } + + @Test + public void calculateInventoryValueEmptyTest() { + Inventory inventory2 = new Inventory(); + Double expected = 0.00; + Double actual = inventory2.calculateInventoryValue(); + Assert.assertEquals(expected, actual); + } + + @Test + public void calculateInventoryValueNonEmptyTest() { + Double expected = 5.00; + Double actual = inventory.calculateInventoryValue(); + Assert.assertEquals(expected, actual); + } +} diff --git a/src/test/java/squier/john/reusingClasses/InvestmentAccountTest.java b/src/test/java/squier/john/reusingClasses/InvestmentAccountTest.java new file mode 100644 index 0000000..442fa5b --- /dev/null +++ b/src/test/java/squier/john/reusingClasses/InvestmentAccountTest.java @@ -0,0 +1,316 @@ +package squier.john.reusingClasses; + +import org.junit.Assert; +import org.junit.Test; +import squier.john.reusingClasses.ATM.*; + +/** + * Created by johnsquier on 1/18/17. + */ +public class InvestmentAccountTest { + ATM.Account bankAccount; + double delta = 0.00001; + + @Test + public void getBalanceAccountUnfrozenTest() { + bankAccount = new InvestmentAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + Double expected = 100.0; + Double actual = bankAccount.getBalance(); + Assert.assertEquals(expected, actual, delta); + } + + @Test + public void getBalanceAccountFrozenTest() { + bankAccount = new InvestmentAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OFAC_FROZEN, + OverdraftProtection.ENABLED); + + // using null to indicate frozen acct status + Double actual = bankAccount.getBalance(); + Assert.assertNull(actual); + } + + @Test + public void getAccountHoldersNameTest() { + bankAccount = new InvestmentAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OFAC_FROZEN, + OverdraftProtection.ENABLED); + + String expected = "John"; + String actual = bankAccount.getAccountHoldersName(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setAccountHoldersNameOpenTest() { + bankAccount = new InvestmentAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + String expected = "Bob"; + bankAccount.setAccountHoldersName("Bob"); + String actual = bankAccount.getAccountHoldersName(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setAccountHoldersNameClosedTest() { + bankAccount = new InvestmentAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.CLOSED, + OverdraftProtection.ENABLED); + + String expected = "John"; + bankAccount.setAccountHoldersName("Bob"); + String actual = bankAccount.getAccountHoldersName(); + Assert.assertEquals(expected, actual); + } + + @Test + public void updateBalanceAccountClosedTest() { + bankAccount = new InvestmentAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.CLOSED, + OverdraftProtection.ENABLED); + + Double expected = 100.0; + bankAccount.updateBalanceWithCreditOrDebit(5.0); + Double actual = bankAccount.getBalance(); + Assert.assertEquals(expected, actual, delta); + } + + @Test + public void updateBalancePositiveTest() { + bankAccount = new InvestmentAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + Double expected = 200.0; + bankAccount.updateBalanceWithCreditOrDebit(100.0); + Double actual = bankAccount.getBalance(); + Assert.assertEquals(expected, actual, delta); + } + + @Test + public void updateBalanceNegativeTest() { + bankAccount = new InvestmentAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + Double expected = 50.0; + bankAccount.updateBalanceWithCreditOrDebit(-50.0); + Double actual = bankAccount.getBalance(); + Assert.assertEquals(expected, actual, delta); + } + + @Test + public void updateBalanceZeroTest() { + bankAccount = new InvestmentAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + ATM.ApprovalStatus expected = ATM.ApprovalStatus.ZERO_TRANSACTION; + ATM.ApprovalStatus actual = bankAccount.updateBalanceWithCreditOrDebit(0.0); + Assert.assertEquals(expected, actual); + } + + @Test + public void transferBalanceFromTestBothEndAboveZeroTest() { + InvestmentAccount transferFrom = new InvestmentAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + InvestmentAccount transferTo = new InvestmentAccount(BankAccountType.CHECKING, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + transferTo.transferBalanceFrom(transferFrom, 50.0); + + Double[] expected = {150.0, 50.0}; + Double[] actual = {transferTo.getBalance(), transferFrom.getBalance()}; + Assert.assertArrayEquals(expected, actual); + } + + @Test + public void transferBalanceToTestBothEndAboveZeroTest() { + InvestmentAccount transferFrom = new InvestmentAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + InvestmentAccount transferTo = new InvestmentAccount(BankAccountType.CHECKING, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + transferFrom.transferBalanceTo(transferTo, 50.0); + + Double[] expected = {150.0, 50.0}; + Double[] actual = {transferTo.getBalance(), transferFrom.getBalance()}; + Assert.assertArrayEquals(expected, actual); + } + + @Test + public void transferBalanceFromTestNotEnoughInFromTest() { + InvestmentAccount transferFrom = new InvestmentAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + InvestmentAccount transferTo = new InvestmentAccount(BankAccountType.CHECKING, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + transferTo.transferBalanceFrom(transferFrom, 500.0); + + Double[] expected = {100.0, 100.0}; + Double[] actual = {transferTo.getBalance(), transferFrom.getBalance()}; + Assert.assertArrayEquals(expected, actual); + } + + @Test + public void transferBalanceToTestNotEnoughInFromTest() { + InvestmentAccount transferFrom = new InvestmentAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + InvestmentAccount transferTo = new InvestmentAccount(BankAccountType.CHECKING, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + transferFrom.transferBalanceTo(transferTo, 500.0); + + Double[] expected = {100.0, 100.0}; + Double[] actual = {transferTo.getBalance(), transferFrom.getBalance()}; + Assert.assertArrayEquals(expected, actual); + } + + @Test + public void overdrawWithProtectionOnTest() { + bankAccount = new InvestmentAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + Double expected = 100.0; + bankAccount.updateBalanceWithCreditOrDebit(-500.0); + Double actual = bankAccount.getBalance(); + + Assert.assertEquals(expected, actual); + } + + @Test + public void overdrawWithProtectOffTest() { + bankAccount = new InvestmentAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.DISABLED); + + Double expected = -400.0; + bankAccount.updateBalanceWithCreditOrDebit(-500.0); + Double actual = bankAccount.getBalance(); + + Assert.assertEquals(expected, actual); + } + + @Test + public void getBankAccountStatusTest() { + bankAccount = new InvestmentAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + BankAccountStatus expected = BankAccountStatus.OPEN; + BankAccountStatus actual = bankAccount.getAccountStatus(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setBankAccountStatusClosedFromOpen() { + bankAccount = new InvestmentAccount(BankAccountType.SAVINGS, 0.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + BankAccountStatus expected = BankAccountStatus.CLOSED; + bankAccount.setAccountStatus(BankAccountStatus.CLOSED); + BankAccountStatus actual = bankAccount.getAccountStatus(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setBankAccountStatusClosedFromFrozen() { + bankAccount = new InvestmentAccount(BankAccountType.SAVINGS, 0.0, + "John", 10.0, BankAccountStatus.OFAC_FROZEN, + OverdraftProtection.ENABLED); + + BankAccountStatus expected = BankAccountStatus.CLOSED; + bankAccount.setAccountStatus(BankAccountStatus.CLOSED); + BankAccountStatus actual = bankAccount.getAccountStatus(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setBankAccountStatusOpenFromFrozen() { + bankAccount = new InvestmentAccount(BankAccountType.SAVINGS, 0.0, + "John", 10.0, BankAccountStatus.OFAC_FROZEN, + OverdraftProtection.ENABLED); + + BankAccountStatus expected = BankAccountStatus.OPEN; + bankAccount.setAccountStatus(BankAccountStatus.OPEN); + BankAccountStatus actual = bankAccount.getAccountStatus(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setBankAccountStatusOpenFromClosed() { + // needs to fail + bankAccount = new InvestmentAccount(BankAccountType.SAVINGS, 0.0, + "John", 10.0, BankAccountStatus.CLOSED, + OverdraftProtection.ENABLED); + + BankAccountStatus expected = BankAccountStatus.CLOSED; + bankAccount.setAccountStatus(BankAccountStatus.OPEN); + BankAccountStatus actual = bankAccount.getAccountStatus(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setBankAccountStatusFrozenFromOpen() { + bankAccount = new InvestmentAccount(BankAccountType.SAVINGS, 0.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + BankAccountStatus expected = BankAccountStatus.OFAC_FROZEN; + bankAccount.setAccountStatus(BankAccountStatus.OFAC_FROZEN); + BankAccountStatus actual = bankAccount.getAccountStatus(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setBankAccountStatusFrozenFromClosed() { + bankAccount = new InvestmentAccount(BankAccountType.SAVINGS, 0.0, + "John", 10.0, BankAccountStatus.CLOSED, + OverdraftProtection.ENABLED); + + BankAccountStatus expected = BankAccountStatus.CLOSED; + bankAccount.setAccountStatus(BankAccountStatus.OFAC_FROZEN); + BankAccountStatus actual = bankAccount.getAccountStatus(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setBankAccountStatusClosedWithZeroBalance() { + bankAccount = new InvestmentAccount(BankAccountType.SAVINGS, 0.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + BankAccountStatus expected = BankAccountStatus.CLOSED; + bankAccount.setAccountStatus(BankAccountStatus.CLOSED); + BankAccountStatus actual = bankAccount.getAccountStatus(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setBankAccountStatusClosedWithNonZeroBalance() { + bankAccount = new InvestmentAccount(BankAccountType.SAVINGS, 10.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + BankAccountStatus expected = BankAccountStatus.OPEN; + bankAccount.setAccountStatus(BankAccountStatus.CLOSED); + BankAccountStatus actual = bankAccount.getAccountStatus(); + Assert.assertEquals(expected, actual); + } +} + diff --git a/src/test/java/squier/john/reusingClasses/ListRotatorTest.java b/src/test/java/squier/john/reusingClasses/ListRotatorTest.java new file mode 100644 index 0000000..8a179cd --- /dev/null +++ b/src/test/java/squier/john/reusingClasses/ListRotatorTest.java @@ -0,0 +1,88 @@ +package squier.john.reusingClasses; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import squier.john.reusingClasses.rotatableList.RotatableList; + +/** + * Created by johnsquier on 1/18/17. + */ +public class ListRotatorTest { + + RotatableList expectedList; + + @Before + public void setup() { + expectedList = new RotatableList(); + expectedList.createAndInitializeList(); + } + + @Test + public void rotateListByZeroTest() { + Object[] expected = {1, 2, 3, 4, 5, 6}; + Object[] actual = expectedList.rotateList(0).toArray(); + Assert.assertArrayEquals(expected, actual); + } + + @Test + public void rotateListByOneTest() { + Object[] expected = {2, 3, 4, 5, 6, 1}; + Object[] actual = expectedList.rotateList(1).toArray(); + Assert.assertArrayEquals(expected, actual); + } + + @Test + public void rotateListByTwoTest() { + Object[] expected = {3, 4, 5, 6, 1, 2}; + Object[] actual = expectedList.rotateList(2).toArray(); + Assert.assertArrayEquals(expected, actual); + } + + @Test + public void rotateListByThreeTest() { + Object[] expected = {4, 5, 6, 1, 2, 3}; + Object[] actual = expectedList.rotateList(3).toArray(); + Assert.assertArrayEquals(expected, actual); + } + + @Test + public void rotateListByFourTest() { + Object[] expected = {5, 6, 1, 2, 3, 4}; + Object[] actual = expectedList.rotateList(4).toArray(); + Assert.assertArrayEquals(expected, actual); + } + + @Test + public void rotateListByFiveTest() { + Object[] expected = {6, 1, 2, 3, 4, 5}; + Object[] actual = expectedList.rotateList(5).toArray(); + Assert.assertArrayEquals(expected, actual); + } + + @Test + public void rotateListBySixTest() { + Object[] expected = {1, 2, 3, 4, 5, 6}; + Object[] actual = expectedList.rotateList(6).toArray(); + Assert.assertArrayEquals(expected, actual); + } + + @Test + public void rotateListBySevenTest() { + Object[] expected = {2, 3, 4, 5, 6, 1}; + Object[] actual = expectedList.rotateList(7).toArray(); + Assert.assertArrayEquals(expected, actual); + } + + @Test + public void createAndInitializeListTest() { + Object[] expected = {1, 2, 3, 4, 5, 6}; + + RotatableList temp = new RotatableList(); + temp.createAndInitializeList(); + + Object[] actual = temp.toArray(); + + Assert.assertArrayEquals(expected, actual); + } +} diff --git a/src/test/java/squier/john/reusingClasses/ProductTest.java b/src/test/java/squier/john/reusingClasses/ProductTest.java new file mode 100644 index 0000000..a2a83f8 --- /dev/null +++ b/src/test/java/squier/john/reusingClasses/ProductTest.java @@ -0,0 +1,68 @@ +package squier.john.reusingClasses; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import squier.john.reusingClasses.inventory.Product; + +/** + * Created by johnsquier on 1/18/17. + */ +public class ProductTest { + + Product product; + + @Before + public void setup() { + product = new Product(100.0, "1234", 500); + } + + @Test + public void getPriceTest() { + Double expected = 100.0; + Double actual = product.getPrice(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setPriceTest() { + Double expected = 500.0; + product.setPrice(500.0); + Double actual = product.getPrice(); + Assert.assertEquals(expected, actual); + } + + @Test + public void getProductIDTest() { + String expected = "1234"; + String actual = product.getProductID(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setProductIDTest() { + String expected = "4321"; + product.setProductID("4321"); + String actual = product.getProductID(); + Assert.assertEquals(expected, actual); + } + + @Test + public void getQuantityOnHandTest() { + Integer expected = 500; + Integer actual = product.getQuantityOnHand(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setQuantityOnHandTest() { + Integer expected = 1; + product.setQuantityOnHand(1); + Integer actual = product.getQuantityOnHand(); + Assert.assertEquals(expected, actual); + } + + + + +} diff --git a/src/test/java/squier/john/reusingClasses/SavingsAccountTest.java b/src/test/java/squier/john/reusingClasses/SavingsAccountTest.java new file mode 100644 index 0000000..73d93cb --- /dev/null +++ b/src/test/java/squier/john/reusingClasses/SavingsAccountTest.java @@ -0,0 +1,317 @@ +package squier.john.reusingClasses; + +import org.junit.Assert; +import org.junit.Test; +import squier.john.reusingClasses.ATM.*; + + +/** + * Created by johnsquier on 1/17/17. + */ +public class SavingsAccountTest { + + ATM.Account bankAccount; + double delta = 0.00001; + + @Test + public void getBalanceAccountUnfrozenTest() { + bankAccount = new SavingsAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + Double expected = 100.0; + Double actual = bankAccount.getBalance(); + Assert.assertEquals(expected, actual, delta); + } + + @Test + public void getBalanceAccountFrozenTest() { + bankAccount = new SavingsAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OFAC_FROZEN, + OverdraftProtection.ENABLED); + + // using null to indicate frozen acct status + Double actual = bankAccount.getBalance(); + Assert.assertNull(actual); + } + + @Test + public void getAccountHoldersNameTest() { + bankAccount = new SavingsAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OFAC_FROZEN, + OverdraftProtection.ENABLED); + + String expected = "John"; + String actual = bankAccount.getAccountHoldersName(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setAccountHoldersNameOpenTest() { + bankAccount = new SavingsAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + String expected = "Bob"; + bankAccount.setAccountHoldersName("Bob"); + String actual = bankAccount.getAccountHoldersName(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setAccountHoldersNameClosedTest() { + bankAccount = new SavingsAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.CLOSED, + OverdraftProtection.ENABLED); + + String expected = "John"; + bankAccount.setAccountHoldersName("Bob"); + String actual = bankAccount.getAccountHoldersName(); + Assert.assertEquals(expected, actual); + } + + @Test + public void updateBalanceAccountClosedTest() { + bankAccount = new SavingsAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.CLOSED, + OverdraftProtection.ENABLED); + + Double expected = 100.0; + bankAccount.updateBalanceWithCreditOrDebit(5.0); + Double actual = bankAccount.getBalance(); + Assert.assertEquals(expected, actual, delta); + } + + @Test + public void updateBalancePositiveTest() { + bankAccount = new SavingsAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + Double expected = 200.0; + bankAccount.updateBalanceWithCreditOrDebit(100.0); + Double actual = bankAccount.getBalance(); + Assert.assertEquals(expected, actual, delta); + } + + @Test + public void updateBalanceNegativeTest() { + bankAccount = new SavingsAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + Double expected = 50.0; + bankAccount.updateBalanceWithCreditOrDebit(-50.0); + Double actual = bankAccount.getBalance(); + Assert.assertEquals(expected, actual, delta); + } + + @Test + public void updateBalanceZeroTest() { + bankAccount = new SavingsAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + ATM.ApprovalStatus expected = ATM.ApprovalStatus.ZERO_TRANSACTION; + ATM.ApprovalStatus actual = bankAccount.updateBalanceWithCreditOrDebit(0.0); + Assert.assertEquals(expected, actual); + } + + @Test + public void transferBalanceFromTestBothEndAboveZeroTest() { + SavingsAccount transferFrom = new SavingsAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + SavingsAccount transferTo = new SavingsAccount(BankAccountType.CHECKING, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + transferTo.transferBalanceFrom(transferFrom, 50.0); + + Double[] expected = {150.0, 50.0}; + Double[] actual = {transferTo.getBalance(), transferFrom.getBalance()}; + Assert.assertArrayEquals(expected, actual); + } + + @Test + public void transferBalanceToTestBothEndAboveZeroTest() { + SavingsAccount transferFrom = new SavingsAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + SavingsAccount transferTo = new SavingsAccount(BankAccountType.CHECKING, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + transferFrom.transferBalanceTo(transferTo, 50.0); + + Double[] expected = {150.0, 50.0}; + Double[] actual = {transferTo.getBalance(), transferFrom.getBalance()}; + Assert.assertArrayEquals(expected, actual); + } + + @Test + public void transferBalanceFromTestNotEnoughInFromTest() { + SavingsAccount transferFrom = new SavingsAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + SavingsAccount transferTo = new SavingsAccount(BankAccountType.CHECKING, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + transferTo.transferBalanceFrom(transferFrom, 500.0); + + Double[] expected = {100.0, 100.0}; + Double[] actual = {transferTo.getBalance(), transferFrom.getBalance()}; + Assert.assertArrayEquals(expected, actual); + } + + @Test + public void transferBalanceToTestNotEnoughInFromTest() { + SavingsAccount transferFrom = new SavingsAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + SavingsAccount transferTo = new SavingsAccount(BankAccountType.CHECKING, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + transferFrom.transferBalanceTo(transferTo, 500.0); + + Double[] expected = {100.0, 100.0}; + Double[] actual = {transferTo.getBalance(), transferFrom.getBalance()}; + Assert.assertArrayEquals(expected, actual); + } + + @Test + public void overdrawWithProtectionOnTest() { + bankAccount = new SavingsAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + Double expected = 100.0; + bankAccount.updateBalanceWithCreditOrDebit(-500.0); + Double actual = bankAccount.getBalance(); + + Assert.assertEquals(expected, actual); + } + + @Test + public void overdrawWithProtectOffTest() { + bankAccount = new SavingsAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.DISABLED); + + Double expected = -400.0; + bankAccount.updateBalanceWithCreditOrDebit(-500.0); + Double actual = bankAccount.getBalance(); + + Assert.assertEquals(expected, actual); + } + + @Test + public void getBankAccountStatusTest() { + bankAccount = new SavingsAccount(BankAccountType.SAVINGS, 100.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + BankAccountStatus expected = BankAccountStatus.OPEN; + BankAccountStatus actual = bankAccount.getAccountStatus(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setBankAccountStatusClosedFromOpen() { + bankAccount = new SavingsAccount(BankAccountType.SAVINGS, 0.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + BankAccountStatus expected = BankAccountStatus.CLOSED; + bankAccount.setAccountStatus(BankAccountStatus.CLOSED); + BankAccountStatus actual = bankAccount.getAccountStatus(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setBankAccountStatusClosedFromFrozen() { + bankAccount = new SavingsAccount(BankAccountType.SAVINGS, 0.0, + "John", 10.0, BankAccountStatus.OFAC_FROZEN, + OverdraftProtection.ENABLED); + + BankAccountStatus expected = BankAccountStatus.CLOSED; + bankAccount.setAccountStatus(BankAccountStatus.CLOSED); + BankAccountStatus actual = bankAccount.getAccountStatus(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setBankAccountStatusOpenFromFrozen() { + bankAccount = new SavingsAccount(BankAccountType.SAVINGS, 0.0, + "John", 10.0, BankAccountStatus.OFAC_FROZEN, + OverdraftProtection.ENABLED); + + BankAccountStatus expected = BankAccountStatus.OPEN; + bankAccount.setAccountStatus(BankAccountStatus.OPEN); + BankAccountStatus actual = bankAccount.getAccountStatus(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setBankAccountStatusOpenFromClosed() { + // needs to fail + bankAccount = new SavingsAccount(BankAccountType.SAVINGS, 0.0, + "John", 10.0, BankAccountStatus.CLOSED, + OverdraftProtection.ENABLED); + + BankAccountStatus expected = BankAccountStatus.CLOSED; + bankAccount.setAccountStatus(BankAccountStatus.OPEN); + BankAccountStatus actual = bankAccount.getAccountStatus(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setBankAccountStatusFrozenFromOpen() { + bankAccount = new SavingsAccount(BankAccountType.SAVINGS, 0.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + BankAccountStatus expected = BankAccountStatus.OFAC_FROZEN; + bankAccount.setAccountStatus(BankAccountStatus.OFAC_FROZEN); + BankAccountStatus actual = bankAccount.getAccountStatus(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setBankAccountStatusFrozenFromClosed() { + bankAccount = new SavingsAccount(BankAccountType.SAVINGS, 0.0, + "John", 10.0, BankAccountStatus.CLOSED, + OverdraftProtection.ENABLED); + + BankAccountStatus expected = BankAccountStatus.CLOSED; + bankAccount.setAccountStatus(BankAccountStatus.OFAC_FROZEN); + BankAccountStatus actual = bankAccount.getAccountStatus(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setBankAccountStatusClosedWithZeroBalance() { + bankAccount = new SavingsAccount(BankAccountType.SAVINGS, 0.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + BankAccountStatus expected = BankAccountStatus.CLOSED; + bankAccount.setAccountStatus(BankAccountStatus.CLOSED); + BankAccountStatus actual = bankAccount.getAccountStatus(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setBankAccountStatusClosedWithNonZeroBalance() { + bankAccount = new SavingsAccount(BankAccountType.SAVINGS, 10.0, + "John", 10.0, BankAccountStatus.OPEN, + OverdraftProtection.ENABLED); + + BankAccountStatus expected = BankAccountStatus.OPEN; + bankAccount.setAccountStatus(BankAccountStatus.CLOSED); + BankAccountStatus actual = bankAccount.getAccountStatus(); + Assert.assertEquals(expected, actual); + } +}