diff --git a/.gitignore b/.gitignore index 28f48498..5fa51ef2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.DS_Store *.gem *.rbc /.config diff --git a/README.md b/README.md index 25fd4d2a..b6e5c7b2 100644 --- a/README.md +++ b/README.md @@ -73,24 +73,23 @@ Create an `Account` class which should have the following functionality: **Account ID** - (Fixnum) a unique identifier corresponding to an account **Owner ID** - (Fixnum) a unique identifier corresponding to an owner - diff --git a/banktest.rb b/banktest.rb index efc74747..7f26e88c 100644 --- a/banktest.rb +++ b/banktest.rb @@ -1,58 +1,77 @@ -require './bank_account' - -# Test 1: Account 1 with positive balance - -account1 = Bank::Account.new(5000) -puts "Account created with ID ##{account1.id}" -account1.print_balance -puts ("*") * 30 -account1.withdraw(3000) - -puts ("*") * 30 -account1.deposit(3000) - -puts ("*") * 30 -account1.print_balance - -# Test 2: Withdrawing more than the balance -puts ("*") * 30 -account1.withdraw(30000000) - -# Test 3: Creating an owner -puts ("*") * 30 -donald_hash = { - first_name: "Donald", - last_name: "Trump", - address1: "2000 Fashion Show Dr", - city: "Las Vegas", - state: "NV", - zip: "89109", -} -donald = Bank::Owner.new(donald_hash) -puts "New owner created: #{donald.first_name} #{donald.last_name}" - -# Test 4: Creating an account with an owner -puts ("*") * 30 -account3 = Bank::Account.new(1000000000000, donald) -puts "Account created with owner #{account3.formatted_name}" -account3.print_balance - -# Test 5: Changing the owner of an account -puts ("*") * 30 -hillary_hash = { - first_name: "Hillary", - last_name: "Clinton", - address1: "1271 6th Ave", - city: "New York", - state: "NY", - zip: "10020", -} - -hillary = Bank::Owner.new(hillary_hash) -account1.assign_owner(hillary) - -# Test 6: Creating account 2 with negative balance -puts ("*") * 30 -account2 = Bank::Account.new(-5000) -puts "Account created with #{account2.id}" -account2.print_balance +require './lib/savings' +require './lib/checking' +require './lib/mma' + +# # - Money Market Account Tests - +# mma = Bank::MoneyMarketAccount.new(1, 1000000) # Valid MMA account +# mma.print_balance + +# # Test withdrawing all of the MMA balance - PASS +# mma.withdraw(1000000) +# mma.withdraw(990000) + +# # Test doing more than 6 transactions if the deposit gets account above $10,000 +# 5.times do +# mma.deposit(1) +# end +# +# mma.withdraw(20000) +# mma.deposit(20000) +# mma.deposit(30000) + + +# # Test withdrawing more than 6 times +# 10.times do +# mma.withdraw(1000000) +# end +# +# mma.reset_transactions + +# # Test withdrawing to balance below $10,000 +# 4.times do +# mma.withdraw(3300000) +# end +# +# # Test depositing and counting transactions below $10,000 +# 2.times do +# mma.deposit(5000000) +# end + +# # Test Money Market interest +# mma.add_interest(0.25) +# mma.add_interest(1) +# +# mma = Bank::MoneyMarketAccount.new(1, 9999) # Invalid account - not enough funds + +# - Normal Account Tests - +# account = Bank::Account.new(1, 100) +# account.print_balance +# account.withdraw(20) # Regular withdrawal +# account.withdraw(1100) # Withdrawing too much + +# account = Bank::Account.new(1, -10) # Account below min + +# # - Savings Account Tests - +# savings = Bank::SavingsAccount.new(1, 100000) +# savings.print_balance +# savings.withdraw(20000) # Regular withdrawal +# savings.withdraw(1000000000) # Withdrawing too much +# +# savings = Bank::SavingsAccount.new(1, 10) # Invalid account - Account below min + +# savings.add_interest(1) + +# # - Checking Account Tests - +# checking = Bank::CheckingAccount.new(1, 10000) +# checking.print_balance +# # +# checking = Bank::CheckingAccount.new(8, -12000) + +# 5.times do +# checking.withdraw_using_check(600) +# end +# +# checking.reset_checks +# 2.times do +# checking.withdraw_using_check(6000) +# end diff --git a/bank_account.rb b/lib/account.rb similarity index 59% rename from bank_account.rb rename to lib/account.rb index 92a29f23..e056195c 100644 --- a/bank_account.rb +++ b/lib/account.rb @@ -3,53 +3,79 @@ # Dependency: Ruby money -- gem install money require 'csv' require 'money' +require 'colorize' I18n.enforce_available_locales = false module Bank class Account - attr_reader :balance, :id, :owner, :open_date - # Instantiation of object has optional parameters of balance and owner - def initialize(id, balance, open_date, owner = nil) + attr_reader :balance, :id, :owner, :open_date, :type + FEE = 0 + MIN_BALANCE = 0 + # Instantiation of object has optional parameters of open_date and owner + def initialize(id, balance, open_date = "today", owner = nil) @owner = owner - # Creates an ID of random numbers - @id = id - # Raises an error with a rescue for a negative initial balance - if balance < 0 - begin - raise ArgumentError.new("You may not create an account with a negative balance.") - rescue - puts "Setting balance to a default value of 0." - @balance = 0 - end - else - @balance = balance + @id = id.to_i + @open_date = open_date + @type = "Standard" + @balance = balance.to_i + # Raises an error if the balance is below the minimum balance + if balance.to_i < self.class::MIN_BALANCE + raise ArgumentError.new("You may not create an account below the minimum balance.") end end - def withdraw(amount) - if @balance - amount < 0 - puts "You cannot withdraw more than your account balance." + # The withdraw method withdraws from the account + # check_min parameter - BOOLEAN + # whether to check if the balance minus amount & fee is less than the minimum balance + # do_penalty parameter - BOOLEAN + # whether to subtract the fee from the balance + def withdraw(amount, check_min = true, do_penalty = true) + if amount < 0 + puts "You cannot withdraw a negative amount of money." else - puts "Starting balance: " + Money.new(@balance, "USD").format - puts "Amount withdrawn: " + Money.new(amount, "USD").format - @balance -= amount - puts "Updated balance: " + Money.new(@balance, "USD").format + puts "------#{@type.upcase} WITHDRAWAL------".colorize(:blue) + # If the withdrawal will put balance below 0, don't do it an output an error + if @balance - amount < 0 + puts "You do not have sufficient funds to withdraw that amount." + # This check is done with standard, checking, and savings accounts + elsif @balance - (amount + self.class::FEE) < self.class::MIN_BALANCE && check_min + puts "You cannot go below the minimum account balance of " + Money.new(self.class::MIN_BALANCE, "USD").format + # This section does the actual withdrawal + else + puts "Starting balance: " + Money.new(@balance, "USD").format + puts "Amount withdrawn: " + Money.new(amount, "USD").format + # This section is mostly for the money market account. + # The MMA does not do the penalty(=false) if it is above $10,000 + # otherwise it incurs a penalty(=true) + if do_penalty + puts "Fee: " + Money.new(self.class::FEE, "USD").format + @balance -= self.class::FEE + end + @balance -= amount + puts "Updated balance: " + Money.new(@balance, "USD").format + end end return @balance end def deposit(amount) - puts "Starting balance: " + Money.new(@balance, "USD").format - puts "Amount deposited: " + Money.new(amount, "USD").format - @balance += amount - puts "Updated balance: " + Money.new(@balance, "USD").format + if amount < 0 + puts "You cannot withdraw a negative amount of money." + else + puts "-------#{@type.upcase} DEPOSIT-------".colorize(:blue) + puts "Starting balance: " + Money.new(@balance, "USD").format + puts "Amount deposited: " + Money.new(amount, "USD").format + @balance += amount + puts "Updated balance: " + Money.new(@balance, "USD").format + end return @balance end def print_balance + puts "----PRINTING BALANCE----".colorize(:blue) money = Money.new(@balance, "USD") - puts "The current balance of this account is " + money.format + puts "The current balance of this #{@type} account is " + money.format end # Assign an owner to an account @@ -93,7 +119,6 @@ def self.all_relationships end return relationships end - end class Owner diff --git a/lib/checking.rb b/lib/checking.rb new file mode 100644 index 00000000..97c26d95 --- /dev/null +++ b/lib/checking.rb @@ -0,0 +1,42 @@ +require './lib/account' + +module Bank + class CheckingAccount < Account + MAX_CHECKS = 3 + FEE = 100 + OVERDRAFT = -1000 + def initialize(id, balance, open_date = "today", owner = nil) + super(id, balance, open_date, owner) + @type = "Checking" + @fee = 100 + @num_checks = 0 + end + + def withdraw_using_check(amount) + if amount < 0 + puts "You cannot withdraw a negative amount of money." + else + puts "---CHECK WITHDRAWAL---".colorize(:blue) + # This is a shorthand if-else statement + @num_checks > 2 ? (penalty = 200) : (penalty = 0) + if @balance - amount - penalty >= OVERDRAFT + puts "Starting balance: " + Money.new(@balance, "USD").format + puts "Amount withdrawn: " + Money.new(amount, "USD").format + puts "Too Many Checks Penalty: " + Money.new(penalty, "USD").format + @balance -= (amount + penalty) + puts "Updated balance: " + Money.new(@balance, "USD").format + @num_checks += 1 + else + puts "You cannot go below the minimum overdraft balance of " + Money.new(OVERDRAFT, "USD").format + end + end + return @balance + end + + def reset_checks + puts "It's the dawn of a new month and your checks are back to 0!" + @num_checks = 0 + end + + end +end diff --git a/lib/mma.rb b/lib/mma.rb new file mode 100644 index 00000000..52784478 --- /dev/null +++ b/lib/mma.rb @@ -0,0 +1,63 @@ +require './lib/account' + +module Bank + class MoneyMarketAccount < SavingsAccount + MAX_TRANSACTIONS = 6 + MIN_BALANCE = 1000000 + FEE = 10000 + def initialize(id, balance, open_date = "today", owner = nil) + super(id, balance, open_date, owner) + @type = "Money Market" + @transactions = 0 + end + + def withdraw(amount) + if amount < 0 + puts "You cannot withdraw a negative amount of money." + else + # Too many transactions + if @transactions >= 6 + puts "You have already made #{@transactions} transactions this month. Sorry!" + elsif @balance >= MIN_BALANCE && @balance >= amount + FEE + @transactions += 1 + # if the withdrawal makes the balance go below the minimum balance + if @balance - amount > MIN_BALANCE + # Do the withdrawal without a penalty + super(amount, false, false) + else + # Do the withdrawal with a penalty + super(amount, false) + end + # If the balance is < $10,000 print error message + elsif @balance <= amount + FEE + puts "Withdrawing that amount would overdraw your account. Transaction cancelled." + else + puts "You may not do any more withdrawals until your balance is above " + Money.new(MIN_BALANCE, "USD").format + end + end + return @balance + end + + def reset_transactions + puts "It's the dawn of a new month and your transactions are back to 0!" + @transactions = 0 + end + + def deposit(amount) + if @transactions < MAX_TRANSACTIONS || (@balance < MIN_BALANCE && @balance + amount >= MIN_BALANCE) + below_balance = true if @balance < MIN_BALANCE + # @transactions += 1 if @balance >= MIN_BALANCE + super(amount) + @transactions += 1 unless @balance >= MIN_BALANCE && below_balance + else + puts "You have already done #{@transactions} transactions this month. The limit is #{MAX_TRANSACTIONS} per month." + end + return @balance + end + + def add_interest(rate) + super(rate) + end + + end +end diff --git a/lib/savings.rb b/lib/savings.rb new file mode 100644 index 00000000..55175b0b --- /dev/null +++ b/lib/savings.rb @@ -0,0 +1,29 @@ +require './lib/account' + +module Bank + class SavingsAccount < Account + FEE = 200 + MIN_BALANCE = 1000 + def initialize(id, balance, open_date = "today", owner = nil) + super(id, balance, open_date, owner) + @type = "Savings" + end + + def add_interest(rate) + if rate <= 0 + puts "The interest rate must be greater than 0." + return 0 + else + puts "------ADD INTEREST------".colorize(:blue) + interest = @balance * rate / 100 + puts "Starting balance: " + Money.new(@balance, "USD").format + puts "You earned " + Money.new(interest, "USD").format + " at a rate of #{rate}%" + @balance += interest + puts "Updated balance: " + Money.new(@balance, "USD").format + @balance.to_i + return interest + end + end + + end +end