Skip to content
Open

Hrw #53

Show file tree
Hide file tree
Changes from all commits
Commits
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
30 changes: 14 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,23 @@ Create an `Account` class which should have the following functionality:
**Account ID** - (Fixnum) a unique identifier corresponding to an account
**Owner ID** - (Fixnum) a unique identifier corresponding to an owner

<!--
## Wave 3
Create a `SavingsAccount` class which should inherit behavior from the `Account` class. It should include updated logic with the following functionality:
- An updated `initialize` method:
- The initial balance cannot be less than $10. If it is, this will `raise` an `ArgumentError`
- An updated `withdraw` method:
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 methods:
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.

Create a `CheckingAccount` class which should inherit behavior from the `Account` class. It should include updated logic with the following functionality:
- `#withdraw(amount)`: The input amount gets taken out of the account as result of an ATM transaction. 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.
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
Expand All @@ -101,16 +100,15 @@ Create a `CheckingAccount` class which should inherit behavior from the `Account

## Optional:

Create a `MoneyMarketAccount` class with a minimum of 6 specs. The class should inherit behavior from the `Account` class.
Create a `MoneyMarketAccount` class which should inherit behavior from the `Account` class.
- A maximum of 6 transactions (deposits or withdrawals) are allowed per month on this account type
- `self.new(id, initial_balance)`: creates a new instance with the instance variable `id` and 'initial_balance' assigned
- The initial balance cannot be less than $10,000 - this will `raise` an `ArgumentError`
- `#withdraw(amount)`: The input amount gets taken out of the account as result of an ATM transaction. Returns the updated account balance.
- The initial balance cannot be less than $10,000 - this will `raise` an `ArgumentError`
- Updated withdrawal logic:
- 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.
- Each transaction will be counted against the maximum number of transactions
- `#deposit(amount)`. Returns the updated account balance.
- Updated deposit logic:
- Each transaction will be counted against the maximum number of transactions
- Exception to the above: A deposit performed to reach or exceed the minimum balance of $10,000 is not counted as part of the 6 transactions.
- `#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.
- `#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
-->
58 changes: 58 additions & 0 deletions account_class.rb
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

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

#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)

Choose a reason for hiding this comment

The 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)

Choose a reason for hiding this comment

The 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)

Choose a reason for hiding this comment

The 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)

Choose a reason for hiding this comment

The 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
3 changes: 3 additions & 0 deletions bankaccount_wave3.rb
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"
55 changes: 55 additions & 0 deletions checking_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

Choose a reason for hiding this comment

The 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
47 changes: 47 additions & 0 deletions savings_class.rb
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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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)

Choose a reason for hiding this comment

The 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.