diff --git a/account.rb b/account.rb new file mode 100644 index 00000000..e12f6fc7 --- /dev/null +++ b/account.rb @@ -0,0 +1,122 @@ +require 'csv' + +module Bank + class Account + MINIMUM_BALANCE = 0 + TRANSACTION_FEE = 0 + + attr_reader :id, :balance, :open_date + + def initialize(account_hash) + @id = account_hash[:id] + @balance = account_hash[:balance] + @open_date = account_hash[:open_date] + @total_withdrawn = nil + + unless @balance >= self.class::MINIMUM_BALANCE + raise ArgumentError.new("Account balances cannot be less than #{ self.class::MINIMUM_BALANCE } cents.") + end + end + + def self.all + csv_accounts = [] + CSV.read('./support/accounts.csv').each do |line| + csv_account = {} + csv_account[:id] = line[0].to_i + csv_account[:balance] = line[1].to_i + csv_account[:open_date] = line[2].to_i + csv_accounts << self.new(csv_account) + end + csv_accounts + end + + def self.find(id) + self.all.each do |object| + if object.id == id + return object + end + end + end + + def withdraw(withdrawal_amount) + #COME BACK HERE, can I save this as a method and then call this method here and in the checking withdraw method? + unless withdrawal_amount.is_a?(Numeric) + raise ArgumentError.new("You can only withdraw numerical values. Please log-in again.") + end + + @total_withdrawn = withdrawal_amount + self.class::TRANSACTION_FEE + + transaction_fee_incurred = "Please note: you were charged a #{ self.class::TRANSACTION_FEE } cent transaction fee on this transaction." + + if valid_withdrawal? + @balance = @balance - @total_withdrawn + puts "You successfully withdrew #{ withdrawal_amount } cents. Your updated account balance is #{ @balance } cents." + if self.class::TRANSACTION_FEE != 0 + puts transaction_fee_incurred + end + return @balance + else + puts "Your current account balance is #{ @balance } cents. You cannot withdraw #{ @total_withdrawn } cents (this includes any transaction fees, if applicable) because your account must maintain a minimum of #{ self.class::MINIMUM_BALANCE } cents. Please login again to try a different withdrawal request." + return @balance + end + end + + def valid_withdrawal? + @balance - @total_withdrawn >= self.class::MINIMUM_BALANCE + end + + def valid_opening_amount? + @balance < self.class::MINIMUM_BALANCE + end + + def deposit(deposit_amount) + unless deposit_amount.is_a?(Numeric) + raise ArgumentError.new("You can only deposit numerical values. Please log-in again.") + end + + @balance = @balance + deposit_amount + + puts "You successfully deposited #{ deposit_amount } cents. Your updated account balance is #{ @balance } cents." + return @balance + end + + # def balance + # puts "Your current balance is $#{ @balance}." + # return @balance + # end + end +end + +# # WAVE THREE TESTING... +# +# puts "Does this work?" +# works = Bank::Account.find(1216) +# puts works.balance +# +# puts "Yay!" +# +# works.deposit(50) +# +# works.withdraw(50) +# +# works.withdraw(1000002) +# +# puts "End of Thursday's testing..." +# +# +# # WAVE TWO TESTING... +# test = Bank::Account.all +# +# puts test +# +# puts "Woo hoo!" +# +# test.each do |item| +# puts item.balance +# end +# +# puts "Does this work?" +# works = Bank::Account.find(1216) +# puts works.balance +# +# puts "Yay!" diff --git a/checking_account.rb b/checking_account.rb new file mode 100644 index 00000000..d3a2eeee --- /dev/null +++ b/checking_account.rb @@ -0,0 +1,103 @@ +require_relative 'account' + +module Bank + class CheckingAccount < Account + # MINIMUM_BALANCE = 1000 + TRANSACTION_FEE = 100 + + attr_reader :check_number + + def initialize(account_hash) + super(account_hash) + @check_account_overdraw_maximum = -1000 + @check_number = 0 + @free_checks = 3 + @check_transaction_fee = 0 + end + + def withdraw(withdrawal_amount) + super(withdrawal_amount) + end + + def withdraw_using_check(amount) + unless amount.is_a?(Numeric) + raise ArgumentError.new("You can only withdraw numerical values. Please log-in again.") + end + + @total_withdrawn = amount + @check_transaction_fee + + if @check_number < @free_checks + if valid_check_withdrawal? + @balance = @balance - @total_withdrawn + @check_number += 1 + puts "You successfully withdrew #{ amount } cents. Your updated account balance is #{ @balance } cents." + return @balance + else + puts "Your current account balance is #{ @balance } cents. You cannot withdraw #{ @total_withdrawn } cents because your account cannot exceed your #{ @check_account_overdraw_maximum.abs } cent overdraft limit. Please login again to try a different withdrawal request." + return @balance + end + else + @check_transaction_fee = 200 + if valid_check_withdrawal? + @balance = @balance - @total_withdrawn + @check_number += 1 + puts "You successfully withdrew #{ amount } cents. NOTE: You are allowed #{ @free_checks } free check withdrawals per month. You incurred a #{ @check_transaction_fee } cent check transaction fee today because you have written #{ @check_number } checks this month. Your updated account balance is #{ @balance } cents." + return @balance + else + puts "Your current account balance is #{ @balance } cents. You cannot withdraw #{ @total_withdrawn } cents (includes your check transaction fee) because your account cannot exceed your #{ @check_account_overdraw_maximum.abs } cent overdraft limit. Please login again to try a different withdrawal request." + return @balance + end + end + end + + def valid_check_withdrawal? + @balance - @total_withdrawn >= @check_account_overdraw_maximum + end + + def reset_checks + @check_number = 0 + end + end +end + + +# WAVE THREE TESTING... + +puts "Initiating a checking account with manual information:" +test = Bank::CheckingAccount.new({:id => 5268, :balance => 2000, :open_date => 1965}) +print "Here's the account balance (in cents): " +puts test.balance + +puts "\n\nWithdrawing 600 cents..." +puts test.withdraw(600) + +puts "Successfully withdrew $6 so my account is now $13--simply confirming that this aligns with the cents below..." +puts test.balance + +puts "\n\nI ought to receive an error when trying to withdraw $14 more..." +puts test.withdraw(1400) + +# NOW TESTING MY CHECK WITHDRAWAL METHOD... + +puts "\n\n\nHere's my 'first' check..." +puts test.withdraw_using_check(100) +print "Here's the check number: " +puts test.check_number + +puts "\nHere's my 'second' check..." +puts test.withdraw_using_check(100) +print "Here's the check number: " +puts test.check_number + +puts "\nHere's my 'third' check..." +puts test.withdraw_using_check(100) +print "Here's the check number: " +puts test.check_number + +puts "\nHere's my 'fourth' check, incurring a fee?" +puts test.withdraw_using_check(50) +print "Here's the check number: " +puts test.check_number + +puts "\nThis ought to error..." +puts test.withdraw_using_check(1960) diff --git a/program.rb b/program.rb new file mode 100644 index 00000000..ff6dd84c --- /dev/null +++ b/program.rb @@ -0,0 +1,14 @@ +# WAVE ONE TESTING... +## THIS NO LONGER WORKS since Wave 2 was implemented... + +require_relative 'account' + +test = Bank::Account.new(100) + +puts test.balance + +test.deposit(50) + +test.withdraw(50) + +test.withdraw(96) diff --git a/savings_account.rb b/savings_account.rb new file mode 100644 index 00000000..5d93ad6c --- /dev/null +++ b/savings_account.rb @@ -0,0 +1,61 @@ +require_relative 'account' + +module Bank + class SavingsAccount < Account + MINIMUM_BALANCE = 1000 + TRANSACTION_FEE = 200 + + def initialize(account_hash) + super(account_hash) + end + + def withdraw(withdrawal_amount) + super(withdrawal_amount) + end + + def add_interest(rate) + interest = (rate/100) * @balance + @balance = @balance + interest + return interest + end + + end +end + + +# WAVE THREE TESTING... + +puts "Initiating a savings account with manual information:" +test = Bank::SavingsAccount.new({:id => 5268, :balance => 2000, :open_date => 1965}) +print "Here's the account balance (in cents): " +puts test.balance + +puts "\n\nWithdrawing 600 cents..." +puts test.withdraw(600) + +puts "Successfully withdrew $6 so my account is now $12--simply confirming that this aligns with the cents below..." +puts test.balance + +puts "\n\nI'd love this savings account! Adding 25% interest..." +print "Here's the interest amount: " +puts test.add_interest(0.25) +print "Here's the updated account balance: " +puts test.balance + +puts "\n\nI ought to receive an error when trying to withdraw $2 more..." +puts test.withdraw(200) + + +puts "\n\nThursday's testing... Does this work?" +works = Bank::SavingsAccount.find(1216) +puts works.balance + +puts "Yay!" + +works.deposit(50) + +works.withdraw(50) + +works.withdraw(1000002) + +puts "End of Thursday's testing...\n"