-
Notifications
You must be signed in to change notification settings - Fork 38
Queues - Marisol Lopez - BankAccounts #45
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
base: master
Are you sure you want to change the base?
Changes from all commits
2b9cf5c
4468140
b44f426
a611cba
3feaeb9
7fd11a9
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,51 @@ | ||
| # Update the Account class to be able to handle all of these fields from the CSV file used as input. | ||
| # For example, manually choose the data from the first line of the CSV file and ensure you can create a new instance of your Account using that data | ||
|
|
||
| require 'csv' | ||
|
|
||
|
|
||
| module Bank | ||
| class Account | ||
| attr_accessor :id, :balance, :date_created | ||
| def initialize(id, balance, date_created = nil) | ||
| raise ArgumentError.new("balance must be >= 0") if balance < 0 | ||
| @id = id | ||
| @balance = balance | ||
| @date_created = date_created | ||
|
|
||
| end | ||
|
|
||
| def self.all | ||
| @accounts = [] | ||
| CSV.open("./support/accounts.csv").each do |line| | ||
| @accounts << self.new(line[0].to_i, line[1].to_f, line[2].to_s) | ||
| end | ||
| return @accounts | ||
| end | ||
|
|
||
| def self.find(id) | ||
| account_find = Bank::Account.all | ||
| account_find.each do |account| | ||
| if account.id == id | ||
| return account | ||
| end | ||
| end | ||
| raise ArgumentError.new "Account #{id} doesn't exist, sorry" | ||
| end | ||
|
|
||
| def withdraw(amount) | ||
| raise ArgumentError.new("amount must be >= 0") if amount < 0 | ||
| if @balance - amount < 0 | ||
| puts "Balance cannot be negative" | ||
| return @balance | ||
| else | ||
| return @balance -= amount | ||
| end | ||
| end | ||
|
|
||
| def deposit(amount) | ||
| raise ArgumentError.new("amount must be >= 0") if amount < 0 | ||
| return @balance += amount | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| require_relative 'account' | ||
|
|
||
| module Bank | ||
| class CheckingAccount< Account | ||
| attr_accessor :id, :balance | ||
|
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. Turns out the |
||
|
|
||
| def initialize(id, balance) | ||
| super(id, balance) | ||
| @check_count = 0 | ||
| @fee = 2 | ||
| raise ArgumentError.new("balance must be >= -1-") if balance < 10 | ||
| end | ||
|
|
||
| def withdraw(amount) | ||
| if amount < (@balance - 11) | ||
|
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. Would it be possible to refactor your It looks like you may have been headed way with the |
||
| puts "Outputs a warning if the balance would go below $10" | ||
| return @balance -= (amount + 1) | ||
| else | ||
| return @balance | ||
| end | ||
| end | ||
|
|
||
| def withdraw_using_check(amount) | ||
| raise ArgumentError.new("Invalid amount, try again.") if amount < 0 | ||
|
|
||
| if (@check_count < 3) && (@balance - amount) >= -10 | ||
| @check_count += 1 | ||
| @balance -= amount | ||
| elsif (@check_count >= 3) && (@balance - amount) >= -12 | ||
| @balance -= (amount + 2) | ||
| else | ||
| puts "Sorry, can't go below -$10" | ||
| end | ||
| return @balance | ||
| end | ||
|
|
||
| def reset_checks | ||
| @check_count = 0 | ||
| end | ||
| end | ||
|
|
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| require_relative 'account' | ||
|
|
||
| module Bank | ||
| class SavingsAccount < Account | ||
| attr_accessor :id, :balance | ||
|
|
||
| def initialize(id, balance) | ||
| super(id, balance) | ||
|
|
||
| raise ArgumentError.new("balance must be >= 10") if balance < 10 | ||
|
|
||
| end | ||
|
|
||
| def withdraw(amount) | ||
| raise ArgumentError.new("amount must be >= 0") if amount < 0 | ||
| if amount < (@balance - 12) | ||
| puts "Outputs a warning if the balance would go below $10" | ||
| return @balance -= (amount + 2) | ||
| else | ||
| return @balance | ||
| end | ||
| end | ||
|
|
||
| def add_interest(rate) | ||
| raise ArgumentError.new("interest rate must be >= 0") if rate < 0 | ||
| interest_calculation = (@balance * (rate/100)) | ||
| @balance += interest_calculation | ||
| return interest_calculation | ||
| 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.
Should these be
attr_accessor? Seems to me thatattr_readerwould be more appropriate - you don't want people trying to modify@balancedirectly, they should go throughwithdrawordepositinstead.