-
Notifications
You must be signed in to change notification settings - Fork 39
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
base: master
Are you sure you want to change the base?
Changes from all commits
4f85a7a
b4984e8
0ca3728
eb692d1
b517f97
95e2c65
809768a
97ab4e9
add88f2
34c20f4
355a209
25b017b
e6f1b8b
1c53fba
6172874
3cfb02b
553ce6d
2538f41
b4b8827
a7a4dbc
4eb1d95
c9f6a10
38bb277
745df10
af4df8e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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]) | ||
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 |
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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 = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the above, you can use |
||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The file should probably be named |
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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.