Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Make sure to write tests for any optionals you implement!
- Add an `owner` property to each Account to track information about who owns the account.
- The `Account` can be created with an `owner`, OR you can create a method that will add the `owner` after the `Account` has already been created.

<!--


## Wave 2

Expand Down Expand Up @@ -110,8 +110,7 @@ To create the relationship between the accounts and the owners use the `account_

This type of table, where records from other tables are associated with each other, is often called a _join table_. We'll talk about them as a class in a few weeks.

-->
<!--


## Wave 3
### Learning Goals
Expand Down Expand Up @@ -160,5 +159,3 @@ Create a `MoneyMarketAccount` class which should inherit behavior from the `Acco
- `#add_interest(rate)`: Calculate the interest on the balance and add the interest to the balance. Return the interest that was calculated and added to the balance (not the updated balance).
- Note** This is the same as the `SavingsAccount` interest.
- `#reset_transactions`: Resets the number of transactions to zero

-->
51 changes: 51 additions & 0 deletions lib/account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require "csv" # one require files. Use file name without .rb

module Bank
class Account
attr_reader :id, :balance, :open_date
def initialize(id, balance, open_date = nil)
raise ArgumentError.new("The balance must be >= 0") if balance < 0
@id = id
@balance = balance
@open_date = open_date
end

def self.all
all_accounts = []
CSV.open("./support/accounts.csv").each do |acc|
all_accounts << self.new(acc[0].to_i, acc[1].to_f, acc[2]) # acc[2] should be transformed to date-time
end
return all_accounts
end

def self.find(id)
found_account = nil
self.all.each do |acc|
if acc.id == id
found_account = acc
break
end
end
raise ArgumentError.new "Account does not exist" if found_account == nil
return found_account
end

def withdraw(amount)
# TODO: implement withdraw
raise ArgumentError.new("Withdrawal amount must be > 0") if amount < 0
if amount > @balance
print "Withdrawal denied. The amount is bigger than your account balance"
elsif amount <= @balance
@balance -= amount
end
return @balance
end

def deposit(amount)
# TODO: implement deposit
raise ArgumentError.new("Deposit amount must be >= 0") if amount < 0
return @balance += amount
end

end # End of class Account
end # End of module Bank
39 changes: 39 additions & 0 deletions lib/checking_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require_relative "account"

module Bank
class CheckingAccount < Account
attr_accessor :check_count
attr_reader :withdraw_fee, :check_withdraw_fee
def initialize(id, balance, open_date = nil)
super(id, balance, open_date)
@withdraw_fee = 1
@check_withdraw_fee = 2
@check_count = 0
end

def withdraw(amount)
amount += @withdraw_fee
super(amount)
end

def withdraw_using_check(check_amount)
raise ArgumentError.new("Note: The check amount must be positive") if check_amount < 0
raise ArgumentError.new("Withdrawal denied. The balance will go pass the limit of -$10 ") if @balance - check_amount < -10
@check_count += 1

if @check_count <= 3
@balance -= check_amount
else
raise ArgumentError.new("Withdrawal denied. The balance will go pass the limit of -$10 ") if @balance - (check_amount + @check_withdraw_fee) < -10
@balance -= (check_amount + @check_withdraw_fee)
end

return @balance
end

def reset_checks
@check_count = 0
end

end # End of the class CheckingAccount
end # End of the module Bank
25 changes: 25 additions & 0 deletions lib/owner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## This class is unfinished

module Bank
class Owner
attr_reader :id, :last_name, :first_name, :street_address, :city, :state
def initialize(id, last_name, first_name, street_address, city, state)
@id = id
@last_name = last_name
@first_name = first_name
@street_address = street_address
@city = city
@state = state
end

def self.all

end

def self.find

end


end #End of class Owner
end # End of module Bank
30 changes: 30 additions & 0 deletions lib/savings_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require_relative "account"

module Bank
class SavingsAccount < Account
attr_accessor :fee
def initialize(id, balance, open_date = nil)
raise ArgumentError.new("The balance must be >= $10") if balance < 10.0
super(id, balance, open_date)
@withdraw_fee = 2
end

def withdraw(amount)
raise ArgumentError.new("Withdrawal amount must be > 0") if amount < 0
if amount + @withdraw_fee > @balance - 10
print "Withdrawal denied. Your balance would go below $10"
else
@balance -= amount + @withdraw_fee
end
return @balance
end

def add_interest(rate)
raise ArgumentError.new("Note: The rate must be positive") if rate < 0
interest = @balance * rate/100
@balance += interest
return interest
end

end # End of class SavingsAccount
end # End of module Bank
63 changes: 39 additions & 24 deletions specs/account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
require 'minitest/skip_dsl'
require_relative '../lib/account'

###########
# WAVE 1
###########
describe "Wave 1" do
describe "Account#initialize" do
it "Takes an ID and an initial balance" do
Expand All @@ -15,6 +18,7 @@

account.must_respond_to :balance
account.balance.must_equal balance

end

it "Raises an ArgumentError when created with a negative balance" do
Expand All @@ -31,16 +35,14 @@
# If this raises, the test will fail. No 'must's needed!
Bank::Account.new(1337, 0)
end
end
end # End of describe "Account#initialize"

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
Expand All @@ -49,9 +51,7 @@
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
Expand All @@ -60,23 +60,20 @@
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 /.+/
}.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
Expand All @@ -94,21 +91,18 @@
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
end # End of describe "Account#withdraw"

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
Expand All @@ -117,9 +111,7 @@
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
Expand All @@ -128,16 +120,16 @@
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
end # End of describe "Account#deposit"
end # End of describe "Wave 1"


# TODO: change 'xdescribe' to 'describe' to run these tests
xdescribe "Wave 2" do
describe "Wave 2" do
describe "Account.all" do
it "Returns an array of all accounts" do
# TODO: Your test code here!
Expand All @@ -148,24 +140,47 @@
# - 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
end
all_accounts = Bank::Account.all
all_accounts.must_be_kind_of Array, "Ops, an array is not returned"
all_accounts.each do |acc|
acc.must_be_kind_of Bank::Account, "Ops, everything in the array is NOT an Account"
end
# Check number of accounts
all_accounts.length.must_equal 12, "Ops, the array does not return all accounts"
# Check id
all_accounts.first.id.must_equal 1212, "First id does not match up"
all_accounts.last.id.must_equal 15156, "Last id does not match up"
# Check balance
all_accounts.first.balance.must_equal 1235667, "First balance does not match up"
all_accounts.last.balance.must_equal 4356772, "Last balance does not match up"
end
end # End of describe "Account.all"

describe "Account.find" do
it "Returns an account that exists" do
# TODO: Your test code here!
# TODO: Your test code here
Bank::Account.find(15151).must_be_kind_of Bank::Account, "Does not return a Account object"
end

it "Can find the first account from the CSV" do
# TODO: Your test code here!
first_account = Bank::Account.find(1212)
first_account.id.must_equal 1212, "Can't find the first account"
end

it "Can find the last account from the CSV" do
# TODO: Your test code here!
last_account = Bank::Account.find(15156)
last_account.id.must_equal 15156, "Can't find the last account"

end

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

end # End of describe "Account.find"
end # End of describe "Wave 2"
Loading