-
Notifications
You must be signed in to change notification settings - Fork 20
Hrw #53
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: hrw
Are you sure you want to change the base?
Hrw #53
Changes from all commits
b79bdfe
b2644ad
f988c66
55f8fe6
1438d2c
8371095
7483509
1597def
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,58 @@ | ||
| module Bank | ||
| class Account | ||
| attr_reader :account_id, :balance, :time | ||
| #allows account to be created with an initial balance as a parameter | ||
| def initialize(balance) | ||
| #creates ID# | ||
| @account_id = generate_id | ||
| @balance = balance_restriction(balance) | ||
| @time = account_opened | ||
|
|
||
|
|
||
| #prevents initial balance from being a negative amount | ||
| #if balance < 0 | ||
| # raise Exception.new, "No negative balance allowed! Live within your means!" | ||
| #end | ||
| end | ||
|
|
||
| #method to create an ID number, not unique number yet | ||
|
|
||
| def balance_restriction (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. I like the way you decided to use a separate method to handle this logic |
||
| if balance < 0 | ||
| raise Exception.new("No negative balance allowed! Live within your means!") | ||
| else | ||
| return balance | ||
| end | ||
| end | ||
|
|
||
| def generate_id | ||
| user_id = rand(1000..9000) | ||
|
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 this logic prevent two accounts from having the same ID? |
||
| return user_id | ||
| end | ||
|
|
||
| def account_opened | ||
| time = Time.new | ||
| return time | ||
| end | ||
|
|
||
| #method for withdrawal | ||
| def withdrawal(subtract_money) | ||
| if (@balance - subtract_money) >= 0 | ||
| @balance = (@balance - subtract_money) | ||
|
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 are using this logic twice, I'd recommend using a variable to store this result so it does not to be calculated twice |
||
| #returns updated account balance | ||
| return @balance | ||
| else | ||
| #rejects negative balance, and returns current balance | ||
| puts "Your withdrawal was rejected. You only have #{@balance} dollars." | ||
| puts "You are not allowed to overdraft. Try a smaller withdrawal." | ||
| end | ||
| end | ||
|
|
||
| #method for deposit | ||
| def deposit(add_money) | ||
| @balance = (@balance + add_money) | ||
|
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. This would be a good place to use a |
||
| #returns updated account balance | ||
| return @balance | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| require "./account_class.rb" | ||
| require "./checking_class.rb" | ||
| require "./savings_class.rb" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| module Bank | ||
| class Checking < Account | ||
|
|
||
| def initialize(balance) | ||
| super(balance) | ||
| @check_count = 0 | ||
| end | ||
|
|
||
| #method for withdrawal | ||
| def withdrawal(subtract_money) | ||
| if (@balance - subtract_money - 1) >= 0 | ||
| @balance = (@balance - subtract_money - 1) | ||
| #returns updated account balance | ||
| return @balance | ||
| else | ||
| #rejects negative balance, and returns current balance | ||
| puts "Your withdrawal was rejected. You only have #{@balance} dollars." | ||
| puts "You are not allowed to overdraft. Try a smaller withdrawal." | ||
| end | ||
| end | ||
|
|
||
|
|
||
| def withdraw_using_check(subtract_money) | ||
| if (@balance - subtract_money.abs) >= -10 | ||
| @balance = (@balance - subtract_money.abs) | ||
| #returns updated account balance with a transaction fee of $1 removed from balance | ||
| #check_count = 0 | ||
| @check_count += 1 | ||
| if @check_count > 3 | ||
|
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. Watch your indentation here, you are indented one extra level that you don't need |
||
| transaction_fee = 2 | ||
| @balance = @balance - transaction_fee | ||
| end | ||
| puts @check_count | ||
| return @balance | ||
| else | ||
| #rejects negative balance, and returns current balance | ||
| puts "Your withdrawal was rejected. You only have #{@balance} dollars." | ||
| puts "You are not allowed to overdraft more than $10. Try a smaller withdrawal." | ||
| end | ||
| end | ||
|
|
||
| def reset_checks | ||
| @check_count = 0 | ||
| end | ||
| end | ||
| end | ||
|
|
||
| #Create a `CheckingAccount` class which should inherit behavior from the `Account` class. It should include the following updated functionality: | ||
| #- Updated withdrawal functionality: | ||
| # - Each withdrawal 'transaction' incurs a fee of $1 that is taken out of the balance. Returns the updated account balance. | ||
| # - Does not allow the account to go negative. Will output a warning message and return the original un-modified balance. | ||
| #- `#withdraw_using_check(amount)`: The input amount gets taken out of the account as a result of a check withdrawal. Returns the updated account balance. | ||
| # - Allows the account to go into overdraft up to -$10 but not any lower | ||
| # - The user is allowed three free check uses in one month, but any subsequent use adds a $2 transaction fee | ||
| #- `#reset_checks`: Resets the number of checks used to zero | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| module Bank | ||
| class Savings < Account | ||
| def initialize(balance) | ||
| super(balance) | ||
| @transaction_fee = 2 | ||
|
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. This would be a good place to use a constant since this will not change |
||
| end | ||
|
|
||
| def balance_restriction (balance) | ||
| if balance < 10 | ||
|
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. This is another good place to use a constant |
||
| raise Exception.new("Your balance is not allowed to go below $10!") | ||
| else | ||
| return balance | ||
| end | ||
| end | ||
|
|
||
| def interest(rate) | ||
| previous_balance = @balance | ||
| @balance = previous_balance + (@balance * rate)/100 | ||
| interest_earned = @balance - previous_balance | ||
| return "Your balance is #{@balance} dollars and you have earned #{interest_earned} dollars in interest" | ||
| end | ||
|
|
||
| def withdrawal(subtract_money) | ||
| if (@balance - subtract_money - @transaction_fee) >= 10 | ||
| @balance = (@balance - subtract_money - @transaction_fee) | ||
|
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. Another good place to potentially use a variable |
||
| #returns updated account balance | ||
| return @balance | ||
| else | ||
| #rejects negative balance, and returns current balance | ||
| puts "Your withdrawal was rejected. You only have #{@balance} dollars." | ||
| puts "You are not allowed to overdraft. Try a smaller withdrawal." | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| #Create a `SavingsAccount` class which should inherit behavior from the `Account` class. It should include the following updated functionality: | ||
| #- The initial balance cannot be less than $10. If it is, this will `raise` an `ArgumentError` | ||
| #- Updated withdrawal functionality: | ||
| # - Each withdrawal 'transaction' incurs a fee of $2 that is taken out of the balance. | ||
| # - Does not allow the account to go below the $10 minimum balance - Will output a warning message and return the original un-modified balance | ||
| # | ||
| #It should include the following new method: | ||
| #- `#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). | ||
| # - Input rate is assumed to be a percentage (i.e. 0.25). | ||
| # - The formula for calculating interest is `balance * rate/100` | ||
| # - Example: If the interest rate is 0.25% and the balance is $10,000, then the interest that is returned is $25 and the new balance becomes $10,025. | ||
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.
If you feel good about the logic you used, i'd recommend commenting this out