diff --git a/Olivia's BankAccounts.rb b/Olivia's BankAccounts.rb new file mode 100644 index 00000000..4089436b --- /dev/null +++ b/Olivia's BankAccounts.rb @@ -0,0 +1,175 @@ +require 'csv' + +module Bank + class Account + + attr_reader :balance, :id, :open_date +#can self.all be refactored if for no other reason than my understanding + def self.all + new_account_info = [] + accounts_master = CSV.read("../support/accounts.csv") + accounts_master.each do |account_array| + id = account_array[0].to_i + balance = account_array[1].to_i + date = account_array[2] + new_account_info << Account.new(id, balance, date) + # end_with_object(self).to_a + @opening_balance + end + return new_account_info + end + + def self.find(id) + accounts = Bank::Account.all + accounts.each do |account| + if account.id == id + return account + end + end + raise ArgumentError.new "Account: #{id} does not exist" + end + + def initialize(id, balance, open_date = nil) + raise ArgumentError.new "Balance must be positive or 0" unless balance >= 0 + @id = id + @balance = balance + @open_date = open_date + end + + def opening_balance(balance) + if @opening_balance >= 0 + @opening_balance + else + raise ArgumentError.new "You can only open a new account with real money:)" + end + end + + def withdraw(amount_to_withdraw) + if amount_to_withdraw > @balance + raise ArgumentError.new "Insufficient funds. Your balance is #{@balance}" + elsif amount_to_withdraw < 0 + raise ArgumentError.new "Withdraw amount must be a positive number." + else + @balance -= amount_to_withdraw + end + return @balance + end + + def deposit(amount_to_deposit) + if amount_to_deposit < 0 + raise ArgumentError.new "You must deposit real money." + else + @balance += amount_to_deposit + end + end + end +end + +#Sself.find(id) #- returns an instance of Account where the value of the id field in the CSV matches the passed parameter. + + + +#SAVINGS ACCOUNT + + +require_relative 'account' + +module Bank + + class SavingsAccount < Bank::Account + + def initialize(id, balance, date) + super(id, balance, date) + raise ArgumentError.new("You can only open a new account with at least $10.00") if balance < 1000 + @min_balance = 0 + @fee = 200 + #@account = Bank::SavingsAccount.new + end + + def withdraw_fee(amount) + if @balance - amount - @fee < @min_balance + puts "This withdral would bring you below required minimum balance." + else + @balance -= amount + @fee + end + return @balance + end + + def add_interest(rate) + raise ArgumentError.new("You can only use positive value") if rate < 0 + interest = @balance * rate + @balance += interest + return interest + end + end +end + +# def check_min_balance(amount) +# if @min_balance < 10 +# raise ArgumentError.new "You can only open a new account with at least $10.00" +# else +# return "Great job. New savings account." +# end +# end + + + +#CHECKING ACCOUNT + + +module Bank + + class CheckingAccount < Bank::Account + + def initialize(id, balance, date) + super(id, balance, date) + @@min_balance = 0 + @@check_min_balance = -1000 + @fee = 100 + end + end +end + +def withdraw_math(amount) + if @balance - amount - @fee > @@min_balance + puts "This withdral plus withdraw fee would bring you below required minimum balance of #{@@min_balance}." + raise ArgumentError.new "Insufficient funds. Your balance is #{@balance}" + else + @balance -= amount + @fee + end + return "Your balance after withdrawl is #{@balance}." +end + +def withdraw(amount_to_withdraw) + if amount_to_withdraw > @balance + raise ArgumentError.new "Insufficient funds. Your balance is #{@balance}" + elsif amount_to_withdraw < 0 + raise ArgumentError.new "Withdraw amount must be a positive number." + else + @balance -= amount_to_withdraw + return "Your balance after withdrawl is #{@balance}." + end +end + +def check_withdraw + if amount_to_withdraw > @balance - 10 + raise ArgumentError.new "We can't honor your check. Your balance is #{@balance}" + elsif + amount_to_withdraw > @balance && >= @balance + -10 + puts "This check with overdraft your account. Are you sure you want to write it?" + end + + + def self.withdraw_using_check(number) + checks_written = [] + while checks_written is =< 3 + elsif + checks_written >= 3 + checks == @balance - 200 + #for each loop here + #checks_written.each do |fee| + #i += 1 + @balance -= 2.00 + end + reset_checks = checks_written(0) + end diff --git a/lib/account.rb b/lib/account.rb index e69de29b..060724cf 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -0,0 +1,68 @@ +require 'csv' + +module Bank + class Account + + attr_reader :balance, :id, :open_date +#can self.all be refactored if for no other reason than my understanding + def self.all + new_account_info = [] + accounts_master = CSV.read("../support/accounts.csv") + accounts_master.each do |account_array| + id = account_array[0].to_i + balance = account_array[1].to_i + date = account_array[2] + new_account_info << Account.new(id, balance, date) + # end_with_object(self).to_a + @opening_balance + end + return new_account_info + end + + def self.find(id) + accounts = Bank::Account.all + accounts.each do |account| + if account.id == id + return account + end + end + raise ArgumentError.new "Account: #{id} does not exist" + end + + def initialize(id, balance, open_date = nil) + raise ArgumentError.new "Balance must be positive or 0" unless balance >= 0 + @id = id + @balance = balance + @open_date = open_date + end + + def opening_balance(balance) + if @opening_balance >= 0 + @opening_balance + else + raise ArgumentError.new "You can only open a new account with real money:)" + end + end + + def withdraw(amount_to_withdraw) + if amount_to_withdraw > @balance + raise ArgumentError.new "Insufficient funds. Your balance is #{@balance}" + elsif amount_to_withdraw < 0 + raise ArgumentError.new "Withdraw amount must be a positive number." + else + @balance -= amount_to_withdraw + end + return @balance + end + + def deposit(amount_to_deposit) + if amount_to_deposit < 0 + raise ArgumentError.new "You must deposit real money." + else + @balance += amount_to_deposit + end + end + end +end + +#Sself.find(id) #- returns an instance of Account where the value of the id field in the CSV matches the passed parameter. diff --git a/lib/account_classes.rb b/lib/account_classes.rb new file mode 100644 index 00000000..3fca9e14 --- /dev/null +++ b/lib/account_classes.rb @@ -0,0 +1,36 @@ + + +require 'csv' + +def self.all + accounts_master = CSV.read("../support/accounts.csv") + accounts_master.each_with_object(self).to_a +end +self.all.count + + + +# def self.find(id) +# #returns an instance of Account where the value of the id field in the CSV matches the passed parameter. +# end +# + + + + + + + + + + + + + + + + + +# each do |line| +# (1...line.length).each do |i| +# @words[ line[0] ] << line[i] diff --git a/lib/checking_account.rb b/lib/checking_account.rb new file mode 100644 index 00000000..77626d14 --- /dev/null +++ b/lib/checking_account.rb @@ -0,0 +1,56 @@ +module Bank + + class CheckingAccount < Bank::Account + + def initialize(id, balance, date) + super(id, balance, date) + @@min_balance = 0 + @@check_min_balance = -1000 + @fee = 100 + end + end +end + +def withdraw_math(amount) + if @balance - amount - @fee > @@min_balance + puts "This withdral plus withdraw fee would bring you below required minimum balance of #{@@min_balance}." + raise ArgumentError.new "Insufficient funds. Your balance is #{@balance}" + else + @balance -= amount + @fee + end + return "Your balance after withdrawl is #{@balance}." +end + +def withdraw(amount_to_withdraw) + if amount_to_withdraw > @balance + raise ArgumentError.new "Insufficient funds. Your balance is #{@balance}" + elsif amount_to_withdraw < 0 + raise ArgumentError.new "Withdraw amount must be a positive number." + else + @balance -= amount_to_withdraw + return "Your balance after withdrawl is #{@balance}." + end +end + +def check_withdraw + if amount_to_withdraw > @balance - 10 + raise ArgumentError.new "We can't honor your check. Your balance is #{@balance}" + elsif + amount_to_withdraw > @balance && >= @balance + -10 + puts "This check with overdraft your account. Are you sure you want to write it?" + end + + + def self.withdraw_using_check(number) + checks_written = [] + while checks_written is =< 3 + elsif + checks_written >= 3 + checks == @balance - 200 + #for each loop here + #checks_written.each do |fee| + #i += 1 + @balance -= 2.00 + end + reset_checks = checks_written(0) + end diff --git a/lib/planet_csv.rb b/lib/planet_csv.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/savings_account.rb b/lib/savings_account.rb new file mode 100644 index 00000000..e3083d19 --- /dev/null +++ b/lib/savings_account.rb @@ -0,0 +1,39 @@ +require_relative 'account' + +module Bank + + class SavingsAccount < Bank::Account + + def initialize(id, balance, date) + super(id, balance, date) + raise ArgumentError.new("You can only open a new account with at least $10.00") if balance < 1000 + @min_balance = 0 + @fee = 200 + #@account = Bank::SavingsAccount.new + end + + def withdraw_fee(amount) + if @balance - amount - @fee < @min_balance + puts "This withdral would bring you below required minimum balance." + else + @balance -= amount + @fee + end + return @balance + end + + def add_interest(rate) + raise ArgumentError.new("You can only use positive value") if rate < 0 + interest = @balance * rate + @balance += interest + return interest + end + end +end + +# def check_min_balance(amount) +# if @min_balance < 10 +# raise ArgumentError.new "You can only open a new account with at least $10.00" +# else +# return "Great job. New savings account." +# end +# end diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 6c399139..e2331a54 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -3,169 +3,230 @@ require 'minitest/skip_dsl' require_relative '../lib/account' -describe "Wave 1" do - describe "Account#initialize" do - it "Takes an ID and an initial balance" do - id = 1337 - balance = 100.0 - account = Bank::Account.new(id, balance) +# describe "Wave 1" do +# describe "Account#initialize" do +# it "Takes an ID and an initial balance" do +# id = 1337 +# balance = 100.0 +# account = Bank::Account.new(id, balance) +# +# account.must_respond_to :id +# account.id.must_equal id +# +# account.must_respond_to :balance +# account.balance.must_equal balance +# end +# +# it "Raises an ArgumentError when created with a negative balance" do +# # Note: we haven't talked about procs yet. You can think +# # of them like blocks that sit by themselves. +# # This code checks that, when the proc is executed, it +# # raises an ArgumentError. +# proc { +# Bank::Account.new(1337, -100.0) +# }.must_raise ArgumentError +# end +# +# it "Can be created with a balance of 0" do +# # If this raises, the test will fail. No 'must's needed! +# Bank::Account.new(1337, 0) +# end +# end +# +# describe "Account#withdraw" do +# it "Reduces the balance" do +# start_balance = 100.0 +# withdrawal_amount = 25.0 +# account = Bank::Account.new(1337, start_balance) +# +# account.withdraw(withdrawal_amount) +# +# expected_balance = start_balance - withdrawal_amount +# account.balance.must_equal expected_balance +# end +# +# it "Returns the modified balance" do +# start_balance = 100.0 +# withdrawal_amount = 25.0 +# account = Bank::Account.new(1337, start_balance) +# +# updated_balance = account.withdraw(withdrawal_amount) +# +# expected_balance = start_balance - withdrawal_amount +# updated_balance.must_equal expected_balance +# end +# +# it "Outputs a warning if the account would go negative" do +# start_balance = 100.0 +# withdrawal_amount = 200.0 +# account = Bank::Account.new(1337, start_balance) +# +# # Another proc! This test expects something to be printed +# # to the terminal, using 'must_output'. /.+/ is a regular +# # expression matching one or more characters - as long as +# # anything at all is printed out the test will pass. +# proc { +# account.withdraw(withdrawal_amount) +# }.must_output /.+/ +# end +# +# it "Doesn't modify the balance if the account would go negative" do +# start_balance = 100.0 +# withdrawal_amount = 200.0 +# account = Bank::Account.new(1337, start_balance) +# +# updated_balance = account.withdraw(withdrawal_amount) +# +# # Both the value returned and the balance in the account +# # must be un-modified. +# updated_balance.must_equal start_balance +# account.balance.must_equal start_balance +# end +# +# it "Allows the balance to go to 0" do skip +# account = Bank::Account.new(1337, 100.0) +# updated_balance = account.withdraw(account.balance) +# updated_balance.must_equal 0 +# account.balance.must_equal 0 +# end +# +# it "Requires a positive withdrawal amount" do skip +# start_balance = 100.0 +# withdrawal_amount = -25.0 +# account = Bank::Account.new(1337, start_balance) +# +# proc { +# account.withdraw(withdrawal_amount) +# }.must_raise ArgumentError +# end +# end +# +# describe "Account#deposit" do +# it "Increases the balance" do +# start_balance = 100.0 +# deposit_amount = 25.0 +# account = Bank::Account.new(1337, start_balance) +# +# account.deposit(deposit_amount) +# +# expected_balance = start_balance + deposit_amount +# account.balance.must_equal expected_balance +# end +# +# it "Returns the modified balance" do +# start_balance = 100.0 +# deposit_amount = 25.0 +# account = Bank::Account.new(1337, start_balance) +# +# updated_balance = account.deposit(deposit_amount) +# +# expected_balance = start_balance + deposit_amount +# updated_balance.must_equal expected_balance +# end +# +# it "Requires a positive deposit amount" do +# start_balance = 100.0 +# deposit_amount = -25.0 +# account = Bank::Account.new(1337, start_balance) +# +# proc { +# account.deposit(deposit_amount) +# }.must_raise ArgumentError +# end +# end +# end - account.must_respond_to :id - account.id.must_equal id - - account.must_respond_to :balance - account.balance.must_equal balance - end - - it "Raises an ArgumentError when created with a negative balance" do - # Note: we haven't talked about procs yet. You can think - # of them like blocks that sit by themselves. - # This code checks that, when the proc is executed, it - # raises an ArgumentError. - proc { - Bank::Account.new(1337, -100.0) - }.must_raise ArgumentError - end - - it "Can be created with a balance of 0" do - # If this raises, the test will fail. No 'must's needed! - Bank::Account.new(1337, 0) +# TODO: change 'xdescribe' to 'describe' to run these tests +describe "Wave 2" do + describe "Account.all" do + it "Returns an array of all accounts" do + new_account_info = Bank::Account.all end end - - describe "Account#withdraw" do - it "Reduces the balance" do - start_balance = 100.0 - withdrawal_amount = 25.0 - account = Bank::Account.new(1337, start_balance) - - account.withdraw(withdrawal_amount) - - expected_balance = start_balance - withdrawal_amount - account.balance.must_equal expected_balance - end - - it "Returns the modified balance" do - start_balance = 100.0 - withdrawal_amount = 25.0 - account = Bank::Account.new(1337, start_balance) - - updated_balance = account.withdraw(withdrawal_amount) - - expected_balance = start_balance - withdrawal_amount - updated_balance.must_equal expected_balance - end - - it "Outputs a warning if the account would go negative" do - start_balance = 100.0 - withdrawal_amount = 200.0 - account = Bank::Account.new(1337, start_balance) - - # Another proc! This test expects something to be printed - # to the terminal, using 'must_output'. /.+/ is a regular - # expression matching one or more characters - as long as - # anything at all is printed out the test will pass. - proc { - account.withdraw(withdrawal_amount) - }.must_output /.+/ - end - - it "Doesn't modify the balance if the account would go negative" do - start_balance = 100.0 - withdrawal_amount = 200.0 - account = Bank::Account.new(1337, start_balance) - - updated_balance = account.withdraw(withdrawal_amount) - - # Both the value returned and the balance in the account - # must be un-modified. - updated_balance.must_equal start_balance - account.balance.must_equal start_balance - end - - it "Allows the balance to go to 0" do - account = Bank::Account.new(1337, 100.0) - updated_balance = account.withdraw(account.balance) - updated_balance.must_equal 0 - account.balance.must_equal 0 - end - - it "Requires a positive withdrawal amount" do - start_balance = 100.0 - withdrawal_amount = -25.0 - account = Bank::Account.new(1337, start_balance) - - proc { - account.withdraw(withdrawal_amount) - }.must_raise ArgumentError - end +end +# - Account.all returns an array +describe "Account array" do + it "returns something" do + new_account_info = Bank::Account.all + new_account_info.class.must_equal Array + new_account_info.length.must_equal 12 end +end - describe "Account#deposit" do - it "Increases the balance" do - start_balance = 100.0 - deposit_amount = 25.0 - account = Bank::Account.new(1337, start_balance) - - account.deposit(deposit_amount) - - expected_balance = start_balance + deposit_amount - account.balance.must_equal expected_balance - end - - it "Returns the modified balance" do - start_balance = 100.0 - deposit_amount = 25.0 - account = Bank::Account.new(1337, start_balance) - - updated_balance = account.deposit(deposit_amount) - - expected_balance = start_balance + deposit_amount - updated_balance.must_equal expected_balance - end - - it "Requires a positive deposit amount" do - start_balance = 100.0 - deposit_amount = -25.0 - account = Bank::Account.new(1337, start_balance) - proc { - account.deposit(deposit_amount) - }.must_raise ArgumentError - end +# - Everything in the array is an Account +describe "Is it an account?" do + it "verifies if it's an account" do + new_account_info = Bank::Account.all + #new_account_info.each do |acct_array| + new_account_info[0].must_be_instance_of Bank::Account end end -# TODO: change 'xdescribe' to 'describe' to run these tests -xdescribe "Wave 2" do - describe "Account.all" do - it "Returns an array of all accounts" do - # TODO: Your test code here! - # Useful checks might include: - # - Account.all returns an array - # - Everything in the array is an Account - # - The number of accounts is correct - # - The ID and balance of the first and last - # accounts match what's in the CSV file - # Feel free to split this into multiple tests if needed - end +# - The number of accounts is correct +describe "the number of accounts?" do + it "Checks the length" do + new_account_info = Bank::Account.all + new_account_info.length.must_equal 12 end +end - describe "Account.find" do - it "Returns an account that exists" do - # TODO: Your test code here! - end - - it "Can find the first account from the CSV" do - # TODO: Your test code here! +# # - The ID and balance of the first and last +describe "check first and last" do + it "Checks the ID & Balance of the first and last." do + new_account_info = Bank::Account.all + test_id = new_account_info[0].id + test_balance = new_account_info[1].balance + # new_account_info = + # new_account_info + end +end +# # accounts match what's in the CSV file +# # Feel free to split this into multiple tests if needed +# +describe "checks random sample" do + it "Checks account entry against csv file for a match" do + new_account_info = Bank::Account.all + accounts_master = CSV.read("../support/accounts.csv") + lines = [] + accounts_master.each do |line| + lines << line + end + lines[3][0].to_i.must_equal new_account_info[3].id.to_i + end +end +# +# +describe "Account.find" do + it "Returns an account that exists" do + new_account_info = Bank::Account.all + new_account_info.each do |line| + return lines[0][0] end + end +end - it "Can find the last account from the CSV" do - # TODO: Your test code here! +describe "Can find the first account from the CSV" do + it "Found first account from csv" do + accounts_master = CSV.read("../support/accounts.csv") + lines = [] + accounts_master.each do |line| + lines[0][0] end - - it "Raises an error for an account that doesn't exist" do - # TODO: Your test code here! + end +end +# # end +# # +describe "Can find the last account from the CSV" do + it "Found first account from csv" do + accounts_master = CSV.read("../support/accounts.csv") + lines = [] + accounts_master.each do |line| + lines.(0...lines.length) end end end +# # +it "Raises an error for an account that doesn't exist" do + # TODO: Your test code here! +end diff --git a/specs/savings_account_spec.rb b/specs/savings_account_spec.rb index 3f4d1e4a..d79f4a80 100644 --- a/specs/savings_account_spec.rb +++ b/specs/savings_account_spec.rb @@ -2,8 +2,8 @@ require 'minitest/reporters' require 'minitest/skip_dsl' -# TODO: uncomment the next line once you start wave 3 and add lib/savings_account.rb -# require_relative '../lib/savings_account' +#uncomment the next line once you start wave 3 and add lib/savings_account.rb +require_relative '../lib/savings_account' # Because a SavingsAccount is a kind # of Account, and we've already tested a bunch of functionality @@ -11,48 +11,90 @@ # Here we'll only test things that are different. # TODO: change 'xdescribe' to 'describe' to run these tests -xdescribe "SavingsAccount" do +describe "SavingsAccount" do describe "#initialize" do it "Is a kind of Account" do # Check that a SavingsAccount is in fact a kind of account - account = Bank::SavingsAccount.new(12345, 100.0) + account = Bank::SavingsAccount.new(12345, 10000, nil) account.must_be_kind_of Bank::Account end +end + describe "Requires $10 opening balance." do + + it "Requires an initial balance of at least $10" do + proc { Bank::SavingsAccount.new(12345, 900, nil) + }.must_raise ArgumentError + end - it "Requires an initial balance of at least $10" do - # TODO: Your test code here! end end describe "#withdraw" do it "Applies a $2 fee each time" do - # TODO: Your test code here! + account = Bank::SavingsAccount.new(12345, 1000, nil) + account.balance.must_equal 10000 + account.withdraw_fee(1000) + account.balance.must_equal 8800 end + end - it "Outputs a warning if the balance would go below $10" do - # TODO: Your test code here! + describe "Outputs a warning if the balance would go below $10" do + it "Outputs a warning if the account would go negative" do + start_balance = 1000 + withdrawal_amount = 2000 + account = Bank::Account.new(1337, start_balance) + proc { + account.withdraw(withdrawal_amount) + }.must_output /.+/ end + end + # Another proc! This test expects something to be printed + # to the terminal, using 'must_output'. /.+/ is a regular + # expression matching one or more characters - as long as + # anything at all is printed out the test will pass. + + describe "If balance goes below 10, don't modify" do it "Doesn't modify the balance if it would go below $10" do - # TODO: Your test code here! + start_balance = 1000 + withdrawal_amount = 9500 + account = Bank::Account.new(1337, start_balance) end - + end + describe "Won't modify balance if fee brings below minimum" do it "Doesn't modify the balance if the fee would put it below $10" do - # TODO: Your test code here! + start_balance = 1000 + withdrawal_amount = 8900 + account = Bank::SavingsAccount.new(1337, start_balance, open_date = nil) + account.withdraw_fee(withdrawal_amount) + account.balance.must_equal start_balance end end - describe "#add_interest" do +describe "balance + interest" do + # before do + # @account = Bank::SavingsAccount.new(1337, start_balance, min_balance = 10, open_date = nil) + # end + + + it "Updates the balance with calculated interest" do + account = Bank::SavingsAccount.new(1337, 10000, open_date = nil) + account.add_interest(0.25) + account.balance.must_equal 12500 + + end + + # describe "#add_interest" do it "Returns the interest calculated" do - # TODO: Your test code here! + account = Bank::SavingsAccount.new(1337, 10000, open_date = nil) + account.add_interest(0.25).must_equal 2500 end - it "Updates the balance with calculated interest" do - # TODO: Your test code here! - end - it "Requires a positive rate" do - # TODO: Your test code here! - end + it "Requires a positive rate" do + account = Bank::SavingsAccount.new(1337, 10000, open_date = nil) + proc { + account.add_interest(-3.3) + }.must_raise ArgumentError end end