Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
26ef249
respond to :id, :balance
MonalisaC Feb 22, 2017
a71e5b5
balance must not be negative
MonalisaC Feb 22, 2017
4f659dd
Withdrawl test passed
MonalisaC Feb 22, 2017
59ce982
insufficient fund
MonalisaC Feb 22, 2017
2da5b89
amount must be >= 0
MonalisaC Feb 22, 2017
9ba6c8d
method deposit increase balance and puts modified balance.
MonalisaC Feb 22, 2017
15351eb
require a positive deposit amount.
MonalisaC Feb 22, 2017
4c58457
owner initialize.
MonalisaC Feb 22, 2017
5673f40
add owner method
MonalisaC Feb 22, 2017
92ef020
Changed 'xdescribe to 'describe'.
MonalisaC Feb 22, 2017
d522082
Test: What is Account.all
MonalisaC Feb 23, 2017
3a48e92
id test using string
MonalisaC Feb 23, 2017
d3b2bab
Test describe Account.find
MonalisaC Feb 23, 2017
541de3f
Testing for class method self.all return owner instances.
MonalisaC Feb 23, 2017
8b9412b
Added self.find class method in owner class and tested it to return a…
MonalisaC Feb 23, 2017
03f0647
Create two new classes CheckingAccount < Account and SavingsAccount <…
MonalisaC Feb 24, 2017
2e5ad61
Convert the values from cents to dollar in self.all (lib/account.rb).
MonalisaC Feb 24, 2017
3eb43e8
Describe savings account and withdraw method.
MonalisaC Feb 24, 2017
9154766
Describe add_interest method in savings a/c.
MonalisaC Feb 24, 2017
9ea4f9d
Describe methode withdraw in checking a/c.
MonalisaC Feb 24, 2017
2d0bc30
intialized checks in class CheckingAccount
MonalisaC Feb 25, 2017
e6da934
Tested withdraw using checks in checking a/c.
MonalisaC Feb 27, 2017
454c45d
Fixed balance must equal value in dollar.
MonalisaC Feb 27, 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
80 changes: 80 additions & 0 deletions lib/account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
require 'csv'

module Bank
class Account
attr_reader :id, :balance, :owner

def initialize(id, balance = 0)
raise ArgumentError.new("balance must be >= 0") if balance < 0

@id = id
@balance = balance
end

def self.all
accounts = []
CSV.read("support/accounts.csv").each do |line|
accounts << Account.new(line[0], line[1].to_f/100)
end
return accounts
end

def self.find(id)
CSV.read("support/accounts.csv").each do |line|
if line[0] == id
return Account.new(line[0], line[1].to_f/100)
end
end
raise ArgumentError.new("account id doesn't exist")
end


def withdraw(amount)
raise ArgumentError.new("amount must be >= 0") if amount < 0
if amount > @balance
puts "Warning: insufficient fund."
else
@balance = @balance - amount
end
return @balance
end
def deposit(amount)
raise ArgumentError.new("amount must be >= 0") if amount < 0
@balance = @balance + amount
return @balance
# @balance += amount
end
def add_owner (owner)
@owner = owner
end
end

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
owners = []
CSV.read("support/owners.csv").each do |line|
owners << Owner.new(line[0], line[1], line[2], line[3], line[4], line[5])
end
return owners
end

def self.find(id)
CSV.read("support/owners.csv").each do |line|
if line[0] == id
return Owner.new(line[0], line[1], line[2], line[3], line[4], line[5])
end
end
raise ArgumentError.new("owner id doesn't exist")
end
end
end
43 changes: 43 additions & 0 deletions lib/checking_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require_relative 'account'
module Bank
class CheckingAccount < Bank::Account
attr_reader :id, :balance

def initialize(id, balance)
@id = id
@balance = balance
@checks = 0
end

def withdraw(amount)

new_balance = @balance - (amount+1)
if new_balance < 0
puts "Warning: insufficient fund."
return @balance
end
@balance = new_balance
end

def withdraw_using_check(amount)
raise ArgumentError.new "Please enter positive amount" if amount < 0
@checks += 1
if @checks > 3
fee = 2
else
fee = 0
end
new_balance = @balance - (amount+fee)


if new_balance < -10
puts "Warning: insufficient fund."
return @balance
end
@balance = new_balance
end
def reset_checks
@checks = 0
end
end
end
29 changes: 29 additions & 0 deletions lib/savings_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require_relative 'account'
module Bank
class SavingsAccount < Bank::Account
attr_reader :id, :balance

def initialize(id, balance = 10)
raise ArgumentError.new("balance must be >= 10") if balance < 10

@id = id
@balance = balance
end

def withdraw(amount)
new_balance = @balance - (amount+2)
if new_balance < 10
puts "Warning: insufficient fund."
return @balance
end
@balance = new_balance
end

def add_interest(rate)
raise ArgumentError.new("rate must be >= 0") if rate < 0
interest = @balance * rate/100
@balance = @balance + interest
return interest
end
end
end
125 changes: 97 additions & 28 deletions specs/account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
Bank::Account.new(1337, 0)
end
end

describe "Account#withdraw" do
it "Reduces the balance" do
start_balance = 100.0
Expand All @@ -45,6 +44,7 @@
account.balance.must_equal expected_balance
end


it "Returns the modified balance" do
start_balance = 100.0
withdrawal_amount = 25.0
Expand All @@ -69,7 +69,6 @@
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
Expand Down Expand Up @@ -112,60 +111,130 @@
expected_balance = start_balance + deposit_amount
account.balance.must_equal expected_balance
end
end

it "Returns the modified balance" do
start_balance = 100.0
deposit_amount = 25.0
account = Bank::Account.new(1337, start_balance)
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)
updated_balance = account.deposit(deposit_amount)

expected_balance = start_balance + deposit_amount
updated_balance.must_equal expected_balance
end
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
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
describe "Account#add_owner" do
it "takes an owner and updates it" do
account = Bank::Account.new(1337, 100)
owner = Bank::Owner.new("26", "Berrie", "Kevin", "8113 Sutherland Center", "Seattle", "WA")
account.add_owner(owner)
account.owner.must_equal owner
end
end
describe "Owner#initialize" do
it "Takes a name and an address" do
first_name = "Kevin"
street_address = "8113 Sutherland Center"
owner = Bank::Owner.new("26", "Berrie", "Kevin", "8113 Sutherland Center", "Seattle", "WA")
owner.must_respond_to :first_name
owner.first_name.must_equal first_name

owner.must_respond_to :street_address
owner.street_address.must_equal street_address
end
end

# TODO: change 'xdescribe' to 'describe' to run these tests
xdescribe "Wave 2" do
describe "Account.all" do
describe "Wave 2" do
end
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
Bank::Account.all.must_be_instance_of Array
# - Everything in the array is an Account
Bank::Account.all.each do |x|
x.must_be_instance_of Bank::Account
end
# - The number of accounts is correct
Bank::Account.all.length.must_equal 12
# - 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
Bank::Account.all[0].id.must_equal "1212"
Bank::Account.all[0].balance.must_equal 12356.67
Bank::Account.all[11].id.must_equal "15156"
Bank::Account.all[11].balance.must_equal 43567.72

end
end
end

describe "Account.find" do
it "Returns an account that exists" do
# TODO: Your test code here!
Bank::Account.find("15151").must_be_instance_of Bank::Account
end

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

end

it "Can find the last account from the CSV" do
# TODO: Your test code here!
Bank::Account.find("15156").must_be_instance_of Bank::Account

end

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

describe "Owner.all" do
it "Returns an array of all accounts" do
Bank::Owner.all.must_be_instance_of Array
Bank::Owner.all.each do |x|
x.must_be_instance_of Bank::Owner
end
Bank::Owner.all.length.must_equal 12
Bank::Owner.all[0].id.must_equal "14"
Bank::Owner.all[0].last_name.must_equal "Morales"
Bank::Owner.all[11].id.must_equal "25"
Bank::Owner.all[11].last_name.must_equal "Clark"

end
end

describe "Owner.find" do
it "Returns an owners that exists" do
Bank::Owner.find("20").must_be_instance_of Bank::Owner
end

it "Can find the first owner from the CSV" do
Bank::Owner.find("14").must_be_instance_of Bank::Owner

end

it "Can find the last owner from the CSV" do
Bank::Owner.find("25").must_be_instance_of Bank::Owner

end

it "Raises an error for an owner that doesn't exist" do
proc {
Bank::Owner.find("ab").must_raise ArgumentError
}
end
end
end
Loading