Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
b092c16
sets up initial structure of module and class
AlisonZ Feb 22, 2017
a7faa32
adds show balance method and verifies that new objects are being inst…
AlisonZ Feb 22, 2017
8f64dba
working withdraw method, needs to be tested with spec file
AlisonZ Feb 22, 2017
9d1ca5b
withdraw method works and passes tests
AlisonZ Feb 22, 2017
5d96b68
deposit feature working and passes tests
AlisonZ Feb 22, 2017
66f4bf0
adds error for negative starting balances and all tests for Wave 1 ar…
AlisonZ Feb 22, 2017
546ff3b
phase one features are working
AlisonZ Feb 23, 2017
87df7fe
phase one is working and passes all tests
AlisonZ Feb 23, 2017
1131b7a
code and tests for self.all are working thanks to chris video
AlisonZ Feb 23, 2017
ae34958
find tests for verifying an account and that the first matches the cs…
AlisonZ Feb 23, 2017
d24e5e5
wave one working except for open dates
AlisonZ Feb 24, 2017
d9f47c4
there is a new class savings that inherits from account and passes th…
AlisonZ Feb 24, 2017
440ea50
holy cow the initialize works and i just wrote a test before code and…
AlisonZ Feb 24, 2017
e84065a
transaction fee is working and the test is all green
AlisonZ Feb 24, 2017
8184e59
withdraw fee tests and code works
AlisonZ Feb 24, 2017
339a8ca
not letting withdraws happen if fee would put account under 10; tests…
AlisonZ Feb 24, 2017
9030ecf
holy cow all of the tests and code for savings account work
AlisonZ Feb 24, 2017
cac59ee
savings account interest is being tracked and tests all work.
AlisonZ Feb 24, 2017
102fed7
changed interest to a local variable, tests still pass, savings accou…
AlisonZ Feb 24, 2017
e27d6b0
tests and code working for initializing and fee for withdraw method
AlisonZ Feb 24, 2017
b736ce5
trying to make variables of minimum and fee to share the load of with…
AlisonZ Feb 24, 2017
4fcfd35
woohooooooo savings and checking withdraw are inheriting stuff from t…
AlisonZ Feb 24, 2017
dbfd30d
withdraw tests all done for checking working
AlisonZ Feb 24, 2017
cb2b142
test for withdraw using check working and new balance is being returned
AlisonZ Feb 24, 2017
5008cf3
does not allow to go below -10
AlisonZ Feb 24, 2017
3994ad1
doesn't allow to withdraw below -10 and requires a positive withdraw …
AlisonZ Feb 24, 2017
760dae0
allows three free checks; there are some warnings about unused variab…
AlisonZ Feb 24, 2017
3417c20
withdraw_using_checks method is a go
AlisonZ Feb 24, 2017
88bacf4
reset checks method working
AlisonZ Feb 24, 2017
221f959
all working, no errors.
AlisonZ Feb 24, 2017
744f595
adds final forgotten test in savings spec. baseline is finished and e…
AlisonZ Feb 24, 2017
664da91
managed merge conflict
AlisonZ Feb 24, 2017
61f6739
submittinh hwk
AlisonZ 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
83 changes: 83 additions & 0 deletions lib/account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#this is the new, second try at this
require "csv"
module Bank
class Account
attr_accessor :id, :balance, :opendate
def initialize (id, balance, opendate = nil )
raise ArgumentError.new("You need some positive cash flow to open an account") if balance < 0
@id = id
@balance = balance
@opendate = opendate

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a more standard name would be @open_date

end


def self.all
accounts = []

CSV.read("support/accounts.csv").each do |line|
id = line[0].to_i
balance = line[1].to_i
opendate = line[2]
account = Bank::Account.new(id, balance, opendate)
accounts << account
end
return accounts
end


def self.find(id)
accounts = Bank::Account.all
accounts.each do |account|
if account.id.to_i == id
return account
end
end
raise ArgumentError.new("this is not an account in our system")
end

def fee
0
end

def minimum
0
end


def withdraw (amount)
if amount > 0
if @balance - amount < 0 || @balance - amount < minimum || @balance - (amount + fee) < minimum
puts "You do not have enough money to withdraw this amount"
else
@balance -=(amount + fee)
end
else
raise ArgumentError.new("You can only withdraw a positive amount of money")
end
return @balance
end


def deposit (amount)
if amount > 0
@balance +=amount
return @balance
else
raise ArgumentError.new("need positive amt")
end

end


def show_balance
return @balance
end

end
end


# my_account = Bank::Account.new(16, 1000)
# puts my_account.show_balance
# puts my_account.withdraw(1200)

50 changes: 50 additions & 0 deletions lib/checking_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#creates a new subclass of Account class that is
require_relative 'account.rb'
module Bank
class CheckingAccount < Account
def initialize (id, balance, opendate = nil)
#super sets the instance variables found in the Account initialize method
super
@check_counter = 0
end


#method which returns 1, so fee for any object in checking account is now 1
def fee

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting how you used this for the inheritance in Checking & Savings.

1
end

#method which returns 0 for the minimum for a checking account is now 0
def minimum
0
end



def withdraw_using_check(amount)
check_fee = 0

if @check_counter >= 3
check_fee = 2
end

if amount < 0
puts "You must withdraw a positive amount"
else
if @balance - (amount + check_fee) < -10
puts "You do not have enough money in your account for this"
else
@check_counter +=1
@balance -=(amount + check_fee)
return @balance
end
end
end


def reset_checks
@check_counter = 0
end
end

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

module Bank
class SavingsAccount < Account
def initialize (id, balance, opendate = nil)
super
# @fee = 2
# @minimum = 10
raise ArgumentError.new("You must have at least $10") if balance < 10
end


def fee
2
end

def minimum
10
end


def add_interest(rate)
if rate > 0
interest = @balance * rate/100
@balance += interest
return interest
else
puts "Rate must be a positive amount"
end
end

end
end
88 changes: 74 additions & 14 deletions specs/account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
require 'minitest/reporters'
require 'minitest/skip_dsl'
require_relative '../lib/account'

require 'csv'
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

Expand All @@ -18,6 +17,7 @@
end

it "Raises an ArgumentError when created with a negative balance" do
# skip
# 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
Expand All @@ -28,6 +28,7 @@
end

it "Can be created with a balance of 0" do
# skip
# If this raises, the test will fail. No 'must's needed!
Bank::Account.new(1337, 0)
end
Expand All @@ -46,6 +47,7 @@
end

it "Returns the modified balance" do
# skip
start_balance = 100.0
withdrawal_amount = 25.0
account = Bank::Account.new(1337, start_balance)
Expand All @@ -57,6 +59,7 @@
end

it "Outputs a warning if the account would go negative" do
# skip
start_balance = 100.0
withdrawal_amount = 200.0
account = Bank::Account.new(1337, start_balance)
Expand All @@ -67,10 +70,11 @@
# 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
# skip
start_balance = 100.0
withdrawal_amount = 200.0
account = Bank::Account.new(1337, start_balance)
Expand All @@ -84,6 +88,7 @@
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
Expand Down Expand Up @@ -123,7 +128,7 @@
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
Expand All @@ -137,35 +142,90 @@
end

# TODO: change 'xdescribe' to 'describe' to run these tests
xdescribe "Wave 2" do
describe "Wave 2" do
#would it be possible to run the @accounts thing here so that it works for both .all and .find tests?
describe "Account.all" do
before do
@accounts = 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
@accounts.must_be_instance_of Array
end

it "Everything in the array is an Account" do
@accounts.each do |account|
account.must_be_instance_of Bank::Account
end
end

it "The number of accounts is correct" do
@accounts.length.must_equal 12
end

# - The ID and balance of the first and last
# accounts match what's in the CSV file

it "element's first and last id and balance must equal what's in csv file" do
@accounts.first.id.must_equal 1212
@accounts.first.balance.must_equal 1235667
@accounts.last.id.must_equal 15156
@accounts.last.balance.must_equal 4356772
end

it "All the elements match what's in the 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).opendate.must_equal line[2]
index += 1
end
end
# Feel free to split this into multiple tests if needed
end
end

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

it "Returns an account that exists" do
# TODO: Your test code here!
account = Bank::Account.find(1217)

account.must_be_instance_of Bank::Account
account.id.must_equal 1217
account.balance.must_equal 12323
account.opendate.must_equal "2003-11-07 11:34:56 -0800"
end




it "Can find the first account from the CSV" do
# TODO: Your test code here!
CSV.read("support/accounts.csv") do
accounts(0).id.must_equal 1212
accounts(0).balance.must_equal 1235667
accounts(0).opendate.must equal "1999-03-27 11:30:09 -0800"
end
end



it "Can find the last account from the CSV" do
# TODO: Your test code here!
CSV.read("support/accounts.csv") do
accounts(-1).id.must_equal 15156
accounts(-1).balance.must_equal 4356772
accounts(-1).opendate.must_equal "1994-11-17 14:04:56 -0800"
end

end

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