Skip to content
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

Queues -- Tehut Getahun -- Bank Accounts #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
73 changes: 73 additions & 0 deletions lib/account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
require 'csv'
require 'date'

module Bank
class Account
attr_reader :id, :balance, :date_opened

def initialize(id = "", balance = "", date_opened = "")
@id = id.to_i
@balance = balance.to_f
@date_opened = date_opened

raise ArgumentError.new("balance must be >= 0") if @balance < 0
if @balance < 0
then
puts "balance must be >= 0"
end

end

def self.all
array =[]
CSV.open("support/accounts.csv").each do |line|
array << self.new(line[0], line[1], DateTime.parse(line[2]))
end
return array
end

def self.find(id)
self.all.each do |account|
if account.id == id then return account
end
end
raise ArgumentError.new("You must select a valid account")
end

def withdraw(amount, limit = 0)
if amount > 0
if (@balance - amount) >= limit
@balance -= amount
else
puts "You have insufficient funds"
end
else
puts "You must withraw an amount greater than $0.00 dollars"
raise ArgumentError.new("you must withdraw an amount greater than $0.00")
end
return @balance
end

def deposit(amount)
if amount >= 0
@balance += amount
else
puts "You must deposit an amount greater than $0.00 dollars"
raise ArgumentError.new("you must deposit an amount greater than $0.00")
end
return @balance
end

def reset_checks
@count = 0
end

end
end
#
# start_balance = 100.0
# withdrawal_amount = 91.0
# account = Bank::Account.new(1337, start_balance)
#
# account.withdraw(withdrawal_amount)
# puts account.balance
65 changes: 65 additions & 0 deletions lib/account2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require 'csv'
require 'date'

module Bank
class Account
attr_reader :id, :balance, :date_opened

def initialize(id = "", balance = "", date_opened = "")
@id = id.to_i
@balance = balance.to_i
@date_opened = date_opened

raise ArgumentError.new("balance must be >= 0") if @balance < 0
if @balance < 0
then
puts "balance must be >= 0"
end

end

def self.all
array =[]
CSV.open("../support/accounts.csv").each do |line|
array << self.new(line[0], line[1], line[2])
end
return array
end

def self.find(id)
self.all.each do |account|
if account.id == id then puts account.id
return account.id
end
end
raise ArgumentError.new("You must select a valid account")
end

def withdraw(amount)
if amount > 0
if (@balance - amount) >= 0
@balance -= amount
else
puts "You have insufficient funds"
end
else
puts "You must withraw an amount greater than $0.00 dollars"
raise ArgumentError.new("you must withdraw an amount greater than $0.00")
end
return @balance
end

def deposit(amount)
if amount >= 0
@balance += amount
else
puts "You must deposit an amount greater than $0.00 dollars"
raise ArgumentError.new("you must deposit an amount greater than $0.00")
end
return @balance

end
end
end

Bank::Account.find(122)
38 changes: 38 additions & 0 deletions lib/checkingaccount.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require_relative 'account.rb'
module Bank
class CheckingAccount < Account
attr_reader :count

def initialize(id = "", balance = 10, date_opened = "" )
@count = 0
super(id, balance, date_opened)
if balance < 10
raise ArgumentError.new("balance must be >= 10")
puts "balance must be >= 10"
end
end

def withdraw(amount,limit= 10)

Choose a reason for hiding this comment

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

I like how your design of Account#withdraw makes this method and withdraw_using_check both very clean. Good work!

total_withdrawl = amount + 1
super(total_withdrawl, -10)
return @balance
end


def withdraw_using_check(amount,limit= -10)
if count <= 3
withdraw((amount - 1))
@count += 1
elsif count >= 4
withdraw((amount + 1))
@count += 1
end
return balance
end

end
end
opening_balance = 100
withdrawal_amount = 10
account = Bank::CheckingAccount.new(12345, opening_balance)
puts account.withdraw_using_check(withdrawal_amount)
28 changes: 28 additions & 0 deletions lib/savingsaccount.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require_relative 'account.rb'
module Bank
class SavingsAccount < Account
def initialize(id = "", balance = 10, date_opened = "" )
super(id, balance, date_opened)

raise ArgumentError.new("balance must be >= 10") if balance < 10


end

def withdraw(amount)
total_withdrawl = amount + 2
super(total_withdrawl, 10)
#end
return @balance
end


def add_interest(rate)

if rate > 0 then @balance = balance * (1 + (rate/100))
else puts "Rate must be greater than 0"
end
return @balance
end
end
end
65 changes: 44 additions & 21 deletions specs/account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'minitest/reporters'
require 'minitest/skip_dsl'
require_relative '../lib/account'
require 'date'

describe "Wave 1" do
describe "Account#initialize" do
Expand Down Expand Up @@ -67,7 +68,7 @@
# anything at all is printed out the test will pass.
proc {
account.withdraw(withdrawal_amount)
}.must_output /.+/
}.must_output (/.+/)
end

it "Doesn't modify the balance if the account would go negative" do
Expand Down Expand Up @@ -136,36 +137,58 @@
end
end

# TODO: change 'xdescribe' to 'describe' to run these tests
xdescribe "Wave 2" do
describe "Account.all" do
it "Returns an array of all accounts" do
# TODO: Your test code here!
# Useful checks might include:
# - Account.all returns an array
# - Everything in the array is an Account
# - The number of accounts is correct
# - The ID and balance of the first and last
# accounts match what's in the CSV file
# Feel free to split this into multiple tests if needed


describe "Account.all" do
it "Returns an array of all accounts" do
Bank::Account.all.must_be_kind_of Array, "Must be an array"

end
end

describe "Test that each account has the correct number of items" do
it "Fits the template for an account" do
Bank::Account.all.each do |account|
account.must_be_kind_of Bank::Account, "This should be an account"
end
end
end

describe "Tests that the number of accounts is correct" do

Choose a reason for hiding this comment

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

Watch your indentation through here.

it "Account.all should be same length as CSV file" do
file_size = CSV.readlines("support/accounts.csv").size
Bank::Account.all.length.must_equal file_size
end
end
#
describe "Tests that ID/Balance of 1st/nth account match whats in the CSV" do
it "Matches the opening and closing of the .csv file" do
CSV.readlines("support/accounts.csv")[0][0].to_i.must_equal Bank::Account.all[0].id.to_i, "the first item's ids should be the same"
CSV.readlines("support/accounts.csv")[-1][0].to_i.must_equal Bank::Account.all[-1].id.to_i, "the last items's ids should be the same"

CSV.readlines("support/accounts.csv")[0][1].to_i.must_equal Bank::Account.all[0].balance.to_i, "the first item's balance should be the same"
CSV.readlines("support/accounts.csv")[-1][1].to_i.must_equal Bank::Account.all[-1].balance.to_i, "the last items's balance should be the same"
end
end

describe "Account.find" do
it "Returns an account that exists" do
# TODO: Your test code here!
Bank::Account.find(1217).must_be_kind_of Bank::Account, "This should be a valid account #{}"
end

it "Can find the first account from the CSV" do
# TODO: Your test code here!
it "Can find the first acount in the CSV" do
Bank::Account.find(1212).must_be_kind_of Bank::Account, "This should show account #1212"
end

end
describe "Account.find the last" do
it "Can find the last account from the CSV" do
# TODO: Your test code here!
end
Bank::Account.find(15153).must_be_kind_of Bank::Account, "This should show account #15156"
end


it "Raises an error for an account that doesn't exist" do
# TODO: Your test code here!
proc {
Bank::Account.find(5)
}.must_raise ArgumentError
end
end
end
Loading