diff --git a/Account.rb b/Account.rb new file mode 100644 index 00000000..15cd9cc2 --- /dev/null +++ b/Account.rb @@ -0,0 +1,67 @@ +require 'CSV' +#require_relative 'Owner.rb' +module Bank + class Account + attr_accessor :id, :balance, :account_open_date + + def initialize(id, balance, minimum_account= "Your savings account cannot go below $10!") + @id = id + @balance = balance + if @balance <= self.class::MINIMUM_BALANCE + raise ArgumentError.new(minimum_account) + end + end + +#second wave initialize method to take in CSV file + # def initialize(accounts_hash, empty_account = "We're sorry, but you cannot open an account without some money!") + # @id = accounts_hash[:id] + # @balance = accounts_hash[:balance] + # @account_open_date = accounts_hash[:account_open_date] + # + # if @balance <= self.class::MINIMUM_BALANCE + # raise ArgumentError.new(empty_account) + # end + # + # end #end initialize method + + #returns a collection of Account instances, representing all of the Accounts described in the CSV. See below for the CSV file specifications + def self.all + accounts = [] + CSV.read("./support/accounts.csv").each do |line| + accounts_hash = {} + accounts_hash[:id] = line[0].to_i + accounts_hash[:balance] = line[1].to_f/100 + accounts_hash[:account_open_date] = line[2] + accounts << Bank::Account.new(accounts_hash) + end + return accounts + end + + #returns an instance of Account where the value of the id field in the CSV matches the passed parameter + def self.find(id) + self.all.each do |account| + if account.id == id + return account + end + end + end + #method to withdraw money from an account + def withdraw(withdraw_amount, message = "Your balance cannot go below 0") + + if withdraw_amount > @balance + puts message + return @balance + else + @balance = @balance - withdraw_amount + return @balance + end + end #withdraw method end + + #method to deposit money into an account + def deposit(deposit_amount) + @balance = @balance + deposit_amount + return @balance + end #deposit method end + + end #Account class end +end diff --git a/CheckingAccount.rb b/CheckingAccount.rb new file mode 100644 index 00000000..c258f9f6 --- /dev/null +++ b/CheckingAccount.rb @@ -0,0 +1,79 @@ +require_relative "Account" +module Bank + class CheckingAccount < Account + MINIMUM_BALANCE = 0 + attr_accessor :check_count + + def initialize(id, balance) + super + # @id = id + # @balance = balance + @check_count = 0 + end + +#withdraw method that subtracts 1 for withdraw fee + def withdraw(withdraw_amount, message = "Your balance cannot go below 0") + withdraw_amount = withdraw_amount + 1 + super + end + + def withdraw_using_check(amount, message = "Don't go below $-10") + #make check_count an instance variable? + if @balance - amount < -10 + raise ArgumentError.new(message) + else + if @check_count > 3 + @check_count += 1 + @balance = @balance - 2 - amount + return @balance + else + @check_count += 1 + @balance = @balance - amount + return @balance + end + end + end + + #reset_checks -- resets the checks to zero + def reset_checks + @check_count = 0 + end + + end #CheckingAccount +end #bank + + +#Code I run in Terminal to test + +puts foots = Bank::CheckingAccount.new(1234, 400) +puts foots.withdraw(300) + #=> 99 +puts foots.check_count +# => 0 +puts foots.withdraw_using_check(5) +# => 94 +puts foots.check_count +# => 1 +puts foots.withdraw_using_check(5) +# => 89 +puts foots.withdraw_using_check(5) +# => 84 +puts foots.withdraw_using_check(5) +# => 79 +puts foots.withdraw_using_check(5) +# => 72 +puts foots.withdraw_using_check(70) +# => 0 +puts foots.withdraw_using_check(5) +# => -7 +puts foots.reset_checks +# => 0 +puts foots.withdraw_using_check(5) +# ArgumentError: Don't go below $-10 +puts foots.balance +# => -7 +puts foots.deposit(500) +# => 493 + +puts foots.withdraw(80) +# => 412 diff --git a/SavingsAccount.rb b/SavingsAccount.rb new file mode 100644 index 00000000..d2de438f --- /dev/null +++ b/SavingsAccount.rb @@ -0,0 +1,51 @@ +require_relative "Account" +module Bank + # create a class called SavingsAccount + class SavingsAccount < Account + MINIMUM_BALANCE = 10 + attr_accessor :balance #add :id here? + + def initialize(id, balance)# minimum_account= "Your savings account must start with at least $10!") + super + # @id = id + # @balance = balance + #super() should allow us in inherit the instance variables of the Account class + # - It cannot go below a balance of $10 + # if @balance < MINIMUM_BALANCE + # raise ArgumentError.new(minimum_account) + # end + # if it does, raise an ArgumentError + end #end initialize + + #old savings withdrawal method + def withdraw(withdraw_amount, minimum_account= "Your savings account cannot go below $10!") + withdraw_amount = withdraw_amount + 2 + if (@balance - withdraw_amount) < MINIMUM_BALANCE + puts minimum_account + return @balance + end + super + end + + #add method for adding the interest rate + def add_interest(rate) + interest_amount = @balance * rate/100 + return @balance = @balance + interest_amount + end + end +end + + +#Stuff run in terminal +foot_saves = Bank::SavingsAccount.new("FootsMoney", 600) + #=> # +foot_saves.withdraw(100) + #=> 498 +foot_saves.add_interest(20) +# => 597 +foot_saves.balance +# => 597 +foot_saves.withdraw(590) +#Your savings account cannot go below $10! +# => 597 +foots.add_interest(20)