Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
e8f71eb
Created a module and a class with nothing in it
TheresaManney Feb 21, 2017
f6c9e1d
All tests pass for Account#initialized section
TheresaManney Feb 21, 2017
f90bc45
Code now passes all Account#withdraw tests
TheresaManney Feb 22, 2017
e6beb3e
Completed all tests for Wave 1, starting to make a class Owner (optio…
TheresaManney Feb 22, 2017
8a62997
Added an initialize test for new class Owner
TheresaManney Feb 22, 2017
48b0300
Program now passes all tests that were pre-created. Created a new cla…
TheresaManney Feb 22, 2017
d06ac9f
Created all the tests needed for Account.all, they all pass
TheresaManney Feb 23, 2017
879a1c5
Created a class method that separates all accounts into separate acco…
TheresaManney Feb 23, 2017
b9240dd
Added test to make sure accounts include correct bank account too, al…
TheresaManney Feb 23, 2017
28c54b2
Wave 2 complete, completed Account.find using do loop with imbeded if…
TheresaManney Feb 23, 2017
73d09ca
Added checking account and saving account files and classes based on …
TheresaManney Feb 24, 2017
bf327a3
Completed all the test needed for savings_account_spec, all code crea…
TheresaManney Feb 24, 2017
f6324d9
Changed path of account.rb and account_spec.rb so that BankAccounts f…
TheresaManney Feb 24, 2017
1f50904
Passed a lot of tests for checking_account_spec.rb, created lots of c…
TheresaManney Feb 24, 2017
e094441
Created final tests for reset_checks and completed code for reste_che…
TheresaManney Feb 25, 2017
26c67ab
De-bugged my specs, running rake still does not work unfortunalty. K…
TheresaManney Feb 27, 2017
99f4a04
Now rake runs and completes all its tasks in terminal... also, spec_h…
TheresaManney Mar 2, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions lib/account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
require 'csv'

module Bank

class Account
attr_accessor :balance
attr_reader :id , :datetime, :owner

def initialize(id, balance, datetime=nil)
@id = id
@datetime = datetime

if balance >= 0
@balance = balance
else
raise ArgumentError.new "The value must be between greater than or equal to 0"
end
end

def self.all

accounts_array = []
CSV.read("support/accounts.csv").each do |account_info|
create_new_accounts = Bank::Account.new(account_info[0].to_f, account_info[1].to_f, account_info[2])
accounts_array.push(create_new_accounts)
end

accounts_array

end

#A class variable could be used here
def self.find(id)
#accounts_array.include?(Account.find(id))
#return Account.new(id)
answer = nil

find_accounts = Bank::Account.all
find_accounts.each do |account|
if account.id == id
answer = account
end
end

raise ArgumentError.new "Warning: Acount #{id} does not exist." if answer == nil

return answer

end

def withdraw(withdrawal_amount)

if withdrawal_amount > @balance
puts "Warning! You are about to withdraw more money than you have in your account."
#withdrawal_amount = 0
@balance
elsif withdrawal_amount > 0
@balance -= withdrawal_amount
else
raise ArgumentError.new "Warning: You cannot withdraw a negative amount of money."
end
end

def deposit(deposit_amount)
if deposit_amount > 0
@balance += deposit_amount
@balance
else
raise ArgumentError.new "Warning: You cannot deposite a negative amount of money"
end
end
end

class Owner
attr_reader :name, :address, :phone_number

def initialize(name, address, phone_number)
@name = name
@address = address
@phone_number = phone_number
end
end

end
53 changes: 53 additions & 0 deletions lib/checking_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require 'csv'
require_relative 'account'

# CheckingAccount class to inherit behavior from the Account class
module Bank
class CheckingAccount < Account
attr_accessor :balance
attr_reader :check_counter

def initialize(id, balance)
super(id, balance)
@balance = balance
# we want to use balance from Account and make sure it doesn't dip below -10
@check_counter = 0
end

def withdraw(withdrawal_amount)

super( withdrawal_amount + 1 )

end

def withdraw_using_check(amount)
#how can I make my @check_counter a counter type kinda like what I see in loops

raise ArgumentError.new("Withdraw amount must be >= 0") if amount < 0

if balance - amount < -10.0
puts "You're withdraw request must leave your balance >= -$10"
return @balance
else
if @check_counter >= 3
fee = 2
@balance -= (amount + fee)
@check_counter +=1
else
@balance -= amount
@check_counter += 1
end
end

return @balance

end

def reset_checks

@check_counter = 0

end

end
end
55 changes: 55 additions & 0 deletions lib/savings_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Question for teacher or tutor... Am I actually inheriting anything from Account??

require 'csv'
require_relative 'account'
# SavingsAccount class to inherit behavior from the Account class

module Bank
class SavingsAccount < Account
attr_reader :balance

def initialize(id, balance, datetime=nil)

if balance > 10
@balance = balance
else
raise ArgumentError.new("Initial balance must be >= $10")
end
# Calling super will now allow SavingsAccount to use info from Account
# if the balance is not less than 10
super

end

def withdraw(withdrawal_amount)
fee = 2.0
total_withdrawal = withdrawal_amount + fee

if @balance - total_withdrawal < 10
puts "Oh no! Your account will go under $10 with this transaction"
return @balance
else
@balance -= total_withdrawal
end
end

def add_interest(rate)

if rate >= 0

interest_amount = @balance * rate / 100

else
raise ArgumentError.new("rate must be >= 0")
end

@balance += interest_amount

return interest_amount

#return @balance

end
end

end
105 changes: 98 additions & 7 deletions specs/account_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require_relative 'spec_helper'
require 'minitest/autorun'
require 'minitest/reporters'
require 'minitest/skip_dsl'
require 'csv'
require_relative '../lib/account'

describe "Wave 1" do
Expand Down Expand Up @@ -67,7 +69,7 @@
# anything at all is printed out the test will pass.
proc {
account.withdraw(withdrawal_amount)
}.must_output /.+/
}.must_output (/.+/)
end

it "Doesn't modify the balance if the account would go negative" do
Expand Down Expand Up @@ -134,38 +136,127 @@
}.must_raise ArgumentError
end
end

describe "Owner#initialize" do
it "Takes an name, address and phone_number" do
name = "Elmo"
address = "123 Sesame Street"
phone_number = "(123)456-7890"
owner = Bank::Owner.new(name, address, phone_number)

owner.must_respond_to :name
owner.name.must_equal name

owner.must_respond_to :address
owner.address.must_equal address

owner.must_respond_to :phone_number
owner.phone_number.must_equal phone_number
end
end
end


# This is the first time you are writing your own tests, the comments that were
# created was guidance to how you could structure or give frame work / outline
#
# TODO: change 'xdescribe' to 'describe' to run these tests
xdescribe "Wave 2" do
describe "Wave 2" do
describe "Account.all" do
before do
@accounts_array = Bank::Account.all
end
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
#accounts_array = Bank::Account.all
@accounts_array.class.must_equal Array
end
# - Everything in the array is an Account
it "Everything in the array is an Account" do
first_account = @accounts_array[0]
first_account.class.must_equal Bank::Account
end
# - The number of accounts is correct
it "The number of accounts is correct" do
number_of_accounts = @accounts_array.length
number_of_accounts.must_equal 12
end

# - The ID and balance of the first and last
it "The ID and balance of the first and last" do
id_of_first = @accounts_array[0].id
id_of_first.must_equal 1212
balance_for_first = @accounts_array[0].balance
balance_for_first.must_equal 1235667

id_of_last = @accounts_array[11].id
id_of_last.must_equal 15156
balance_for_last = @accounts_array[11].balance
balance_for_last.must_equal 4356772
end

# accounts match what's in the CSV file
it "accounts match what's in the CSV file" do
index = 0
CSV.read("support/accounts.csv") do
accounts[index].id.must_equal line[0].to_i
accounts[index].balance.must_equal line[1].to_i
accounts[index].datetime.must_equal line[2]

index += 1
end
end
# Feel free to split this into multiple tests if needed
end

describe "Account.find" do
# describe "Account.all" do
# This is a more DRY way to write this code, it was an example
# before do
# @accounts = Bank::Account.all
# end
it "Returns an account that exists" do

# TODO: Your test code here!
account_exists = Bank::Account.find(1216)
account_exists.must_be_instance_of Bank::Account
#checking its the right account
account_exists.id.must_equal 1216
account_exists.balance.must_equal 100022
account_exists.datetime.must_equal "2000-07-07 15:07:55 -0800"
end

it "Can find the first account from the CSV" do
# TODO: Your test code here!
account_exists = Bank::Account.find(1212)
account_exists.must_be_instance_of Bank::Account

account_exists.id.must_equal 1212
account_exists.balance.must_equal 1235667
account_exists.datetime.must_equal "1999-03-27 11:30:09 -0800"

end

it "Can find the last account from the CSV" do
# TODO: Your test code here!

account_exists = Bank::Account.find(15156)
account_exists.must_be_instance_of Bank::Account

account_exists.id.must_equal 15156
account_exists.balance.must_equal 4356772
account_exists.datetime.must_equal "1994-11-17 14:04:56 -0800"

end

it "Raises an error for an account that doesn't exist" do
# TODO: Your test code here!
proc {
Bank::Account.find(11111)
}.must_raise ArgumentError
end
end
end
Loading