Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Brenna's BankAcccounts #20

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
4f85a7a
added proj requirements in comments to account.rb
bcmdarroch Feb 21, 2017
b4984e8
created Bank module, Account class, instance vars @id and @balance, r…
bcmdarroch Feb 21, 2017
0ca3728
raises error if negative balance initialized, added withdrawal method
bcmdarroch Feb 21, 2017
eb692d1
prints warning if withdrawal causes balance to go negative
bcmdarroch Feb 21, 2017
b517f97
prevents user from entering a negative num to withdraw
bcmdarroch Feb 22, 2017
95e2c65
add deposit method
bcmdarroch Feb 22, 2017
809768a
deposit method only accepts positive nums, code meets wave1 requirements
bcmdarroch Feb 22, 2017
97ab4e9
option wave1: created owner class, added owner parameter to account_test
bcmdarroch Feb 22, 2017
add88f2
made tests for owner reader methods
bcmdarroch Feb 22, 2017
34c20f4
uncomment wave 2 requirements
bcmdarroch Feb 22, 2017
355a209
wrote .all method test and self.all stub
bcmdarroch Feb 22, 2017
25b017b
.self method returns an array, passes test 1
bcmdarroch Feb 22, 2017
e6f1b8b
wrote .find test and .find stub
bcmdarroch Feb 22, 2017
1c53fba
implemented csv importation, broke everything <:D
bcmdarroch Feb 22, 2017
6172874
passes wave 1 tests once more! on to wave 2 errors n failures
bcmdarroch Feb 23, 2017
3cfb02b
Merge branch 'master' of https://github.com/bcmdarroch/BankAccounts
bcmdarroch Feb 23, 2017
553ce6d
WIP self.find not working
bcmdarroch Feb 23, 2017
2538f41
passes all testsgit add lib/account.rb wave 2 complete
bcmdarroch Feb 23, 2017
b4b8827
ucommented wave 3 in README
bcmdarroch Feb 23, 2017
a7a4dbc
edited wave 2 code, created savings and checking account stub classes
bcmdarroch Feb 24, 2017
4eb1d95
savings acct cannot have a balance below 10 after initialization or w…
bcmdarroch Feb 24, 2017
c9f6a10
savings has interest method, passes all tests, checking acct has with…
bcmdarroch Feb 24, 2017
38bb277
created withdraw method with fee, withdraw_using_check, passed all te…
bcmdarroch Feb 24, 2017
745df10
added owner spec, tested initialize
bcmdarroch Feb 24, 2017
af4df8e
owners all and find method passed tests
bcmdarroch Feb 25, 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
6 changes: 1 addition & 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,6 @@ 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 +158,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

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

module Bank

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

def initialize(id, balance, date, owner = "Customer Name")
raise ArgumentError.new("You cannot create a bank account with a 0 or negative balance.") if balance < 0
@id = id
@balance = balance
@date = date
@owner = owner
end

def self.all
return @all_accounts if @all_accounts
@all_accounts = []
CSV.open("/Users/brenna/ada/week3/BankAccounts/support/accounts.csv").each do | line |
@all_accounts << Bank::Account.new(line[0].to_i, line[1].to_f, line[2])

Choose a reason for hiding this comment

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

You can get this to work with: support/accounts.csv instead, that way it will work if you move the project to another folder.

end
@all_accounts
end

def self.find(id)
self.all.each do |acct|
if acct.id == id
return acct
end
end
raise ArgumentError.new("There's no such account ID.")
end

def withdraw(withdrawal_amount)
raise ArgumentError.new("You cannot withdraw a negative amount of money.") if withdrawal_amount < 0
if withdrawal_amount > @balance
print "You can't overdraw your account."
else
return @balance -= withdrawal_amount
end
return @balance
end

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

end

end
43 changes: 43 additions & 0 deletions lib/checking.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

module Bank

class CheckingAccount < Account

def initialize(id, balance, date, owner = "Customer Name")
super
@check_count = 0
end

def withdraw(withdrawal_amount)

Choose a reason for hiding this comment

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

Could you use super in this method?

if (withdrawal_amount + 1) > @balance
print "You can't overdraw your account."
return @balance
else
@balance -= (withdrawal_amount + 1)
end
return @balance
end

def withdraw_using_check(withdrawal_amount)
raise ArgumentError.new("You cannot withdraw a negative amount of money using a check.") if withdrawal_amount < 0

if @balance < withdrawal_amount - 10
print "There must always be at least $10 in your savings."
return @balance
end

@check_count += 1
if @check_count <= 3
return @balance -= withdrawal_amount
else
return @balance -= (withdrawal_amount + 2)
end
end

def reset_checks
@check_count = 0
end

end

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

module Bank

class Owner
attr_reader :id, :last_name, :first_name, :st_address, :city, :state

def initialize(owner_hash)
@id = owner_hash[:id]
@last_name = owner_hash[:last_name]
@first_name = owner_hash[:first_name]
@st_address = owner_hash[:st_address]
@city = owner_hash[:city]
@state = owner_hash[:state]
end

def self.all
# pro tip from Jeremy! memoization: first time, @all_owners is nil, so it reads the CSV, but next time, it just returns @all_owners
return @all_owners if @all_owners
@all_owners = []
CSV.open("/Users/brenna/ada/week3/BankAccounts/support/account_owners.csv").each do | line |
owner_hash = {

Choose a reason for hiding this comment

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

Similar to the above, you can use support/account_owners.csv.

id: line[0].to_i,
last_name: line[1],
first_name: line[2],
st_address: line[3],
city: line[4],
state: line[5]
}
@all_owners << Bank::Owner.new(owner_hash)
end

@all_owners
end

def self.find(id)
self.all.each do |owner|
if owner.id == id
return owner
end
end

raise ArgumentError.new("There's no such owner ID.")
end

end

end
30 changes: 30 additions & 0 deletions lib/savings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

Choose a reason for hiding this comment

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

The file should probably be named savings_account.rb instead, but not a big deal.

module Bank

class SavingsAccount < Account

attr_accessor :interest

def initialize(id, balance, date, owner = "Customer Name")
super
raise ArgumentError.new("Balance must be over $10.") if balance < 10
end

def withdraw(withdrawal_amount)
if @balance < withdrawal_amount + 12
print "There must always be at least $10 in your savings."
return @balance
end
super

Choose a reason for hiding this comment

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

Good use of super.

@balance -= 2
end

def add_interest(rate)
raise ArgumentError.new("You can't calculate negative interest.") if rate < 0
@interest = @balance * rate / 100
@balance += @interest
end

end

end
Loading