Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
*.gem
*.rbc
/.config
Expand Down
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
-->
135 changes: 77 additions & 58 deletions banktest.rb
Original file line number Diff line number Diff line change
@@ -1,58 +1,77 @@
require './bank_account'

# Test 1: Account 1 with positive balance

account1 = Bank::Account.new(5000)
puts "Account created with ID ##{account1.id}"
account1.print_balance
puts ("*") * 30
account1.withdraw(3000)

puts ("*") * 30
account1.deposit(3000)

puts ("*") * 30
account1.print_balance

# Test 2: Withdrawing more than the balance
puts ("*") * 30
account1.withdraw(30000000)

# Test 3: Creating an owner
puts ("*") * 30
donald_hash = {
first_name: "Donald",
last_name: "Trump",
address1: "2000 Fashion Show Dr",
city: "Las Vegas",
state: "NV",
zip: "89109",
}
donald = Bank::Owner.new(donald_hash)
puts "New owner created: #{donald.first_name} #{donald.last_name}"

# Test 4: Creating an account with an owner
puts ("*") * 30
account3 = Bank::Account.new(1000000000000, donald)
puts "Account created with owner #{account3.formatted_name}"
account3.print_balance

# Test 5: Changing the owner of an account
puts ("*") * 30
hillary_hash = {
first_name: "Hillary",
last_name: "Clinton",
address1: "1271 6th Ave",
city: "New York",
state: "NY",
zip: "10020",
}

hillary = Bank::Owner.new(hillary_hash)
account1.assign_owner(hillary)

# Test 6: Creating account 2 with negative balance
puts ("*") * 30
account2 = Bank::Account.new(-5000)
puts "Account created with #{account2.id}"
account2.print_balance
require './lib/savings'
require './lib/checking'
require './lib/mma'

# # - Money Market Account Tests -
# mma = Bank::MoneyMarketAccount.new(1, 1000000) # Valid MMA account
# mma.print_balance

# # Test withdrawing all of the MMA balance - PASS
# mma.withdraw(1000000)
# mma.withdraw(990000)

# # Test doing more than 6 transactions if the deposit gets account above $10,000
# 5.times do
# mma.deposit(1)
# end
#
# mma.withdraw(20000)
# mma.deposit(20000)
# mma.deposit(30000)


# # Test withdrawing more than 6 times
# 10.times do
# mma.withdraw(1000000)
# end
#
# mma.reset_transactions

# # Test withdrawing to balance below $10,000
# 4.times do
# mma.withdraw(3300000)
# end
#
# # Test depositing and counting transactions below $10,000
# 2.times do
# mma.deposit(5000000)
# end

# # Test Money Market interest
# mma.add_interest(0.25)
# mma.add_interest(1)
#
# mma = Bank::MoneyMarketAccount.new(1, 9999) # Invalid account - not enough funds

# - Normal Account Tests -
# account = Bank::Account.new(1, 100)
# account.print_balance
# account.withdraw(20) # Regular withdrawal
# account.withdraw(1100) # Withdrawing too much

# account = Bank::Account.new(1, -10) # Account below min

# # - Savings Account Tests -
# savings = Bank::SavingsAccount.new(1, 100000)
# savings.print_balance
# savings.withdraw(20000) # Regular withdrawal
# savings.withdraw(1000000000) # Withdrawing too much
#
# savings = Bank::SavingsAccount.new(1, 10) # Invalid account - Account below min

# savings.add_interest(1)

# # - Checking Account Tests -
# checking = Bank::CheckingAccount.new(1, 10000)
# checking.print_balance
# #
# checking = Bank::CheckingAccount.new(8, -12000)

# 5.times do
# checking.withdraw_using_check(600)
# end
#
# checking.reset_checks
# 2.times do
# checking.withdraw_using_check(6000)
# end
81 changes: 53 additions & 28 deletions bank_account.rb → lib/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,79 @@
# Dependency: Ruby money -- gem install money
require 'csv'
require 'money'
require 'colorize'
I18n.enforce_available_locales = false

module Bank

class Account
attr_reader :balance, :id, :owner, :open_date
# Instantiation of object has optional parameters of balance and owner
def initialize(id, balance, open_date, owner = nil)
attr_reader :balance, :id, :owner, :open_date, :type
FEE = 0
MIN_BALANCE = 0
# Instantiation of object has optional parameters of open_date and owner
def initialize(id, balance, open_date = "today", owner = nil)
@owner = owner
# Creates an ID of random numbers
@id = id
# Raises an error with a rescue for a negative initial balance
if balance < 0
begin
raise ArgumentError.new("You may not create an account with a negative balance.")
rescue
puts "Setting balance to a default value of 0."
@balance = 0
end
else
@balance = balance
@id = id.to_i
@open_date = open_date
@type = "Standard"
@balance = balance.to_i
# Raises an error if the balance is below the minimum balance
if balance.to_i < self.class::MIN_BALANCE
raise ArgumentError.new("You may not create an account below the minimum balance.")
end
end

def withdraw(amount)
if @balance - amount < 0
puts "You cannot withdraw more than your account balance."
# The withdraw method withdraws from the account
# check_min parameter - BOOLEAN
# whether to check if the balance minus amount & fee is less than the minimum balance
# do_penalty parameter - BOOLEAN
# whether to subtract the fee from the balance
def withdraw(amount, check_min = true, do_penalty = true)
if amount < 0
puts "You cannot withdraw a negative amount of money."
else
puts "Starting balance: " + Money.new(@balance, "USD").format
puts "Amount withdrawn: " + Money.new(amount, "USD").format
@balance -= amount
puts "Updated balance: " + Money.new(@balance, "USD").format
puts "------#{@type.upcase} WITHDRAWAL------".colorize(:blue)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool use of your colorize knowledge :-)

# If the withdrawal will put balance below 0, don't do it an output an error
if @balance - amount < 0
puts "You do not have sufficient funds to withdraw that amount."
# This check is done with standard, checking, and savings accounts
elsif @balance - (amount + self.class::FEE) < self.class::MIN_BALANCE && check_min

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job using the constant here across each of your classes

puts "You cannot go below the minimum account balance of " + Money.new(self.class::MIN_BALANCE, "USD").format
# This section does the actual withdrawal
else
puts "Starting balance: " + Money.new(@balance, "USD").format

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like there is a bunch of code where you call this same Money logic so it may be a good place to create a method to take care of the formatting for you

puts "Amount withdrawn: " + Money.new(amount, "USD").format
# This section is mostly for the money market account.
# The MMA does not do the penalty(=false) if it is above $10,000
# otherwise it incurs a penalty(=true)
if do_penalty
puts "Fee: " + Money.new(self.class::FEE, "USD").format
@balance -= self.class::FEE
end
@balance -= amount
puts "Updated balance: " + Money.new(@balance, "USD").format
end
end
return @balance
end

def deposit(amount)
puts "Starting balance: " + Money.new(@balance, "USD").format
puts "Amount deposited: " + Money.new(amount, "USD").format
@balance += amount
puts "Updated balance: " + Money.new(@balance, "USD").format
if amount < 0
puts "You cannot withdraw a negative amount of money."
else
puts "-------#{@type.upcase} DEPOSIT-------".colorize(:blue)
puts "Starting balance: " + Money.new(@balance, "USD").format
puts "Amount deposited: " + Money.new(amount, "USD").format
@balance += amount
puts "Updated balance: " + Money.new(@balance, "USD").format
end
return @balance
end

def print_balance
puts "----PRINTING BALANCE----".colorize(:blue)
money = Money.new(@balance, "USD")
puts "The current balance of this account is " + money.format
puts "The current balance of this #{@type} account is " + money.format
end

# Assign an owner to an account
Expand Down Expand Up @@ -93,7 +119,6 @@ def self.all_relationships
end
return relationships
end

end

class Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that you utilized a hash to create the owner object

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that you utilized a hash to create the owner object

Expand Down
42 changes: 42 additions & 0 deletions lib/checking.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require './lib/account'

module Bank
class CheckingAccount < Account
MAX_CHECKS = 3

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that you're using constants for these variables

FEE = 100
OVERDRAFT = -1000
def initialize(id, balance, open_date = "today", owner = nil)
super(id, balance, open_date, owner)
@type = "Checking"
@fee = 100
@num_checks = 0
end

def withdraw_using_check(amount)
if amount < 0
puts "You cannot withdraw a negative amount of money."
else
puts "---CHECK WITHDRAWAL---".colorize(:blue)
# This is a shorthand if-else statement
@num_checks > 2 ? (penalty = 200) : (penalty = 0)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice use of the single-line conditional here. You could also re-write this as:
penalty = (@num_checks > 2) ? 200 : 0

if @balance - amount - penalty >= OVERDRAFT
puts "Starting balance: " + Money.new(@balance, "USD").format
puts "Amount withdrawn: " + Money.new(amount, "USD").format
puts "Too Many Checks Penalty: " + Money.new(penalty, "USD").format
@balance -= (amount + penalty)
puts "Updated balance: " + Money.new(@balance, "USD").format
@num_checks += 1
else
puts "You cannot go below the minimum overdraft balance of " + Money.new(OVERDRAFT, "USD").format
end
end
return @balance
end

def reset_checks
puts "It's the dawn of a new month and your checks are back to 0!"
@num_checks = 0
end

end
end
Loading