-
Notifications
You must be signed in to change notification settings - Fork 20
Completed Bank Accounts #64
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
Changes from all commits
b79bdfe
84e2637
0ac02bc
8651592
6ec9927
9b8e9ce
af90b60
823e667
bc3fd3f
3c881c9
6ea2ce6
947cf13
ffae6d8
a58e065
ef38cda
d2b2f12
3721918
8afb988
e83ac84
f1620fc
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 |
---|---|---|
@@ -1,18 +1,22 @@ | ||
require 'csv' | ||
require 'money' | ||
require 'colorize' | ||
I18n.enforce_available_locales = false | ||
|
||
module Bank | ||
|
||
class Owner | ||
|
||
attr_accessor :id, :last, :first, :street, :city, :state | ||
attr_accessor :id, :last, :first, :street, :city, :state, :accounts | ||
|
||
def initialize(id, last, first, street, city, state) | ||
def initialize(id, last, first, street = nil, city = nil, state = nil) | ||
@id = id.to_i | ||
@first = first | ||
@last = last | ||
@street = street | ||
@city = city | ||
@state = state | ||
@accounts = {} | ||
end | ||
|
||
def self.all | ||
|
@@ -35,14 +39,42 @@ def self.find(id) | |
end | ||
end | ||
|
||
def self.master_list # Accounts and their respective owners in one big happy array | ||
master_list = [] | ||
account_owners_csv = CSV.read("./support/account_owners.csv") | ||
|
||
account_owners_csv.each do |row| | ||
account = Bank::Account.find(row[0].to_i) | ||
account_owner = self.find(row[1].to_i) | ||
|
||
account_owner.accounts[:account] = account | ||
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. I'm not sure if this is an appropriate use of hash since it seems like this could be assigned directly to an account variable rather than setting it with a key/value pair |
||
master_list.push(account_owner) | ||
account.owner = account_owner | ||
end | ||
|
||
return master_list | ||
end | ||
|
||
def self.find_owner(id) # Find an account from the master_list! Then you can do stuff with it! Yay! | ||
master_list = self.master_list | ||
|
||
found = master_list.find do |instance| | ||
instance.id.to_i == id | ||
end | ||
|
||
return found | ||
end | ||
|
||
end | ||
|
||
class Account | ||
# @@id_variable = 1000 | ||
|
||
attr_reader :balance, :id, :date | ||
attr_accessor :owner | ||
|
||
FEE = 0 | ||
MIN_BAL = 0 | ||
|
||
def initialize(id, initial_balance, open_date = nil, owner = nil) | ||
@id = id.to_i | ||
@balance = initial_balance.to_i | ||
|
@@ -51,22 +83,43 @@ def initialize(id, initial_balance, open_date = nil, owner = nil) | |
@date = DateTime.strptime(open_date, "%Y-%m-%d %H:%M:%S %z") | ||
end | ||
@owner = owner | ||
@colors_array = [:red, :light_red, :green, :light_green, :yellow, :light_yellow, :blue, :light_blue, :magenta, :light_magenta, :cyan, :light_cyan] | ||
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. It seems like this would be good to set as a constant rather than an instance variable since I don't think the colors array will change |
||
|
||
raise ArgumentError if @balance < 0 | ||
raise ArgumentError.new("You cannot open an account with a negative balance.") if @balance < 0 | ||
end | ||
|
||
def withdraw(withdrawal) | ||
if withdrawal <= @balance | ||
@balance -= withdrawal | ||
if withdrawal <= 0 | ||
print "You can only withdraw a positive amount. " | ||
|
||
return @balance | ||
end | ||
|
||
if @balance - withdrawal - self.class::FEE >= self.class::MIN_BAL | ||
@balance -= (withdrawal + self.class::FEE) if self.class != MoneyMarketAccount | ||
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. I like that you used a single-line conditional here but it would be better to use the |
||
@balance -= withdrawal if self.class == MoneyMarketAccount | ||
|
||
puts "___Withdrawal Receipt___".colorize(@colors_array[rand(0..@colors_array.length-1)]) | ||
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. You could also use the |
||
puts "You withdrew: #{Money.new(withdrawal, "USD").format}" | ||
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. It would be good to set |
||
puts "Fees: #{Money.new(self.class::FEE, "USD").format}" if self.class != MoneyMarketAccount | ||
puts "Your balance: #{Money.new(@balance, "USD").format}" if self.class != MoneyMarketAccount | ||
else | ||
print "You cannot withdraw more than is in your bank account. " | ||
print "You cannot withdraw that amount. " | ||
end | ||
|
||
return @balance | ||
end | ||
|
||
def deposit(deposit_amt) | ||
@balance += deposit_amt | ||
if deposit_amt > 0 | ||
@balance += deposit_amt | ||
|
||
puts "___Deposit Receipt___".colorize(@colors_array[rand(0..@colors_array.length-1)]) | ||
puts "You deposited: #{Money.new(deposit_amt, "USD").format}" | ||
puts "Your balance: #{Money.new(@balance, "USD").format}" | ||
else | ||
print "You can only deposit a positive amount. " | ||
end | ||
|
||
return @balance | ||
end | ||
|
@@ -93,29 +146,5 @@ def self.find(id) # Find a particular account from accounts.csv and return its o | |
return found | ||
end | ||
|
||
def self.master_list # Accounts and their respective owners in one big happy array | ||
master_list = [] | ||
account_owners_csv = CSV.read("./support/account_owners.csv") | ||
|
||
account_owners_csv.each do |row| | ||
account = self.find(row[0].to_i) | ||
owner_of_account = Bank::Owner.find(row[1].to_i) | ||
|
||
account.owner = owner_of_account | ||
master_list.push(account) | ||
end | ||
|
||
return master_list | ||
end | ||
|
||
def self.find_account(id) # Find an account from the master_list! Then you can do stuff with it! Yay! | ||
master_list = self.master_list | ||
|
||
found = master_list.find do |instance| | ||
instance.id.to_i == id | ||
end | ||
|
||
return found | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
require './bank-accounts.rb' | ||
require './savings-account.rb' | ||
require './checking-account.rb' | ||
require './money-market-account.rb' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
module Bank | ||
class CheckingAccount < Account | ||
|
||
FEE = 100 | ||
|
||
def initialize(id, initial_balance) | ||
super | ||
|
||
@checks = 0 | ||
puts "Thank you for opening a Checking Account with Daphne's Bank.".colorize(@colors_array[rand(0..@colors_array.length-1)]) | ||
puts "___Account Summary___".colorize(@colors_array[rand(0..@colors_array.length-1)]) | ||
puts "Account ID: #{@id}" | ||
puts "Initial balance: #{Money.new(@balance, "USD").format}\n\n" | ||
puts "Each withdrawal incurs a fee of $1 that is taken out of the balance. Checking accounts may not overdraw except for check withdrawals, which allow account holders to overdraw a maximum of $10. Account holders are allowed three free checks per month, but any subsequent use incurs a $2 transaction fee." | ||
end | ||
|
||
def withdraw_using_check(withdrawal) | ||
if withdrawal <= 0 | ||
print "You can only withdraw a positive amount. " | ||
|
||
return @balance | ||
end | ||
|
||
if (@balance - withdrawal < -1000 && @checks <= 3) || (@balance - withdrawal - 200 < -1000 && @checks > 3) | ||
print "You cannot overdraft your account more than $10. " | ||
else | ||
@balance -= withdrawal | ||
@checks += 1 | ||
@balance -= 200 if @checks > 3 | ||
puts "___Check Withdrawal Receipt___".colorize(@colors_array[rand(0..@colors_array.length-1)]) | ||
puts "You withdrew: #{Money.new(withdrawal, "USD").format}" | ||
puts "Number of checks this period: #{@checks}" | ||
puts "Fees: #{Money.new(200, "USD").format}" if @checks > 3 | ||
puts "Your balance: #{Money.new(@balance, "USD").format}" | ||
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. Looks like this same section |
||
end | ||
|
||
return @balance | ||
end | ||
|
||
def reset_checks | ||
@checks = 0 | ||
end | ||
|
||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
module Bank | ||
class MoneyMarketAccount < SavingsAccount | ||
|
||
attr_reader :transactions | ||
|
||
FEE = 10000 | ||
|
||
def initialize(id, initial_balance) | ||
super | ||
|
||
@transactions = 0 | ||
@lock = false | ||
raise ArgumentError.new("Money Market Accounts require a minimum initial balance of $10,000.") if @balance < 1000000 | ||
|
||
puts "Thank you for opening a Money Market Account with Daphne's Bank.".colorize(@colors_array[rand(0..@colors_array.length-1)]) | ||
puts "___Account Summary___".colorize(@colors_array[rand(0..@colors_array.length-1)]) | ||
puts "Account ID: #{@id}" | ||
puts "Initial balance: #{Money.new(@balance, "USD").format}\n\n" | ||
puts "A maximum of 6 transactions (deposits or withdrawals) are allowed per month. If a withdrawal causes the balance to go below $10,000, a fee of $100 is imposed and no more transactions are allowed until the balance is increased using a deposit transaction. A deposit performed to reach or exceed the minimum balance of $10,000 is not counted as part of the 6 transactions." | ||
end | ||
|
||
def withdraw(withdrawal) | ||
if @transactions < 6 && @lock == false | ||
@transactions += 1 if withdrawal + 10000 <= @balance && withdrawal > 0 | ||
|
||
super | ||
if @balance < 1000000 | ||
@balance -= 10000 | ||
@lock = true | ||
puts "Fees: #{Money.new(self.class::FEE, "USD").format}" | ||
end | ||
puts "Your balance: #{Money.new(@balance, "USD").format}" if withdrawal > 0 | ||
puts "Transactions this period: #{@transactions}" | ||
else | ||
print "Your account is locked until your balance is restored to $10,000. " if @lock | ||
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. It looks like you use these same print statements more than once so it might make sense to put them in a common method |
||
print "You have reached your maximum of 6 transactions this month. " if @lock == false | ||
end | ||
|
||
return @balance | ||
end | ||
|
||
def deposit(deposit_amt) | ||
@lock = false if deposit_amt + @balance > 1000000 | ||
|
||
if @transactions < 6 && @lock == false | ||
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. Since you're using a boolean condition it would be a good place to try |
||
@transactions += 1 if @balance >= 1000000 && deposit_amt > 0 | ||
super | ||
puts "Transactions this period: #{@transactions}" | ||
else | ||
print "Your account is locked until your balance is restored to $10,000. " if @lock | ||
print "You have reached your maximum of 6 transactions this month. " if @lock == false | ||
end | ||
|
||
return @balance | ||
end | ||
|
||
def reset_transactions | ||
@transactions = 0 | ||
end | ||
|
||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module Bank | ||
class SavingsAccount < Account | ||
|
||
FEE = 200 | ||
MIN_BAL = 1000 | ||
|
||
|
||
def initialize(id, initial_balance) | ||
super | ||
|
||
raise ArgumentError.new("Savings Accounts require a minimum initial balance of $1,000.") if @balance < 1000 | ||
|
||
if self.class != MoneyMarketAccount | ||
puts "Thank you for opening a Savings Account with Daphne's Bank.".colorize(@colors_array[rand(0..@colors_array.length-1)]) | ||
puts "___Account Summary___".colorize(@colors_array[rand(0..@colors_array.length-1)]) | ||
puts "Account ID: #{@id}" | ||
puts "Initial balance: #{Money.new(@balance, "USD").format}\n\n" | ||
puts "Each withdrawal incurs a fee of $2 that is taken out of the balance. Account holders must maintain a minimum balance of $10, without exception." | ||
end | ||
end | ||
|
||
def add_interest(rate) | ||
interest = @balance * rate/100 | ||
@balance += interest | ||
puts "___Interest Receipt___".colorize(@colors_array[rand(0..@colors_array.length-1)]) | ||
puts "Interest accrued: #{Money.new(interest, "USD").format}" | ||
puts "Your balance: #{Money.new(@balance, "USD").format}" | ||
|
||
return 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.
Nice job using the
all
within thefind
below in order to write theall
logic only once