Skip to content
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
15 changes: 15 additions & 0 deletions demo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'csv'


# CSV.read("./support/accounts.csv").each do |line|
# #print line
# print line[0]
# end

csv = []
CSV.read("./support/accounts.csv").each do |data|
print data
#csv << data[0]
end

# print csv
55 changes: 55 additions & 0 deletions lib/account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require 'csv'

module Bank
class Account
attr_reader :id, :balance, :openDate
def initialize(id, balance, openDate = '1999-03-27 11:30:09 -0800')
raise ArgumentError.new("balance must be >= 0") if balance < 0
@id = id
@balance = balance
@openDate = openDate
end

def self.all
all_accounts = [] #array of classes

Choose a reason for hiding this comment

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

Good work on Account.all and Account.find, I feel like this implementation is about as clean as it gets.

#iterate /new accounts objects
CSV.open("./support/accounts.csv").each do |line|

# all_accounts << Bank::Account.new(line[0].to_i,line[1].to_i,line[2])
all_accounts << self.new(line[0].to_i,line[1].to_i,line[2])
end
return all_accounts
end

def self.find(id) #looking all_account finding matches with id
self.all.each do |account| #account - a class
if account.id == id
return account
end
end
raise ArgumentError.new("id must be account id") #No clause on this one because if the "each" loop ends, without returning an account, then there are no id numbers that match ahe accounts. So, if the "each" loop ends, then the ArgumentError triggers because there are no ids.
end


def withdraw(amount)
raise ArgumentError.new("amount must be > 0") if amount < 0 #the clause at the end of this ArgumentError triggers the error when the "if" statement is true. If the "if" clause is not true, it skips.
@balance -= amount
if @balance < 0
puts "You can't withdraw!"
@balance += amount
end
return @balance
end

def deposit(amount)
raise ArgumentError.new("amount must be > 0") if amount < 0
@balance += amount
end
end
end


#new_account = Bank::Account.new(1234, 2000)
#new_account = Bank::Account.all
# puts Bank::Account.all
#puts Bank::Account.find(1212).id
57 changes: 57 additions & 0 deletions lib/checking_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require File.expand_path('../account.rb', __FILE__)

module Bank
class CheckingAccount < Bank::Account
attr_accessor :number_of_checks
#attr_reader :free_chashout
def initialize(id, balance, openDate = '1999-03-27 11:30:09 -0800')
super(id, balance, openDate = '1999-03-27 11:30:09 -0800')
# @last_check_date #maybe =nil?
@number_of_checks = 0
@free_chashout = true
end

def withdraw(amount)
super(amount)
if @balance == 0
puts "You can't have negative balance in your account"
@balance += amount
else
return @balance -= 1
end
end

def withdraw_using_check(check_amount)
if check_amount > 0
if @number_of_checks >= 3
@balance -= check_amount + 2
else
@balance -= check_amount
end
if @balance < -10
puts "Overdraft is only allowed upto $10"
@balance += check_amount
else
@number_of_checks +=1
end
else
puts "Positive withdrawal amount is needed"
end
return @balance
end

def reset_checks
@number_of_checks = 0
end
end
end

# checkingaccount = CheckingAccount.new(1212, 1000)
# # puts checkingaccount.withdraw_using_check(110)
# p checkingaccount.withdraw_using_check(200)
# p checkingaccount.withdraw_using_check(200)
# p checkingaccount.withdraw_using_check(200)
# p checkingaccount.withdraw_using_check(200)
# p checkingaccount.number_of_checks
# p checkingaccount.reset_checks
# p checkingaccount.number_of_checks
35 changes: 35 additions & 0 deletions lib/savings_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require File.expand_path('../account.rb', __FILE__)

module Bank
class SavingsAccount < Bank::Account
#attr_accessor
def initialize(id, balance, openDate = '1999-03-27 11:30:09 -0800')
super(id, balance, openDate = '1999-03-27 11:30:09 -0800')
#@interest_rate
raise ArgumentError.new("initial balance must be > 10") if @balance < 10
#@balance > 10
end

def withdraw(amount)
super(amount)
@balance -= 2
if @balance < 10
print "Your balance has to be more than 10"
@balance += amount + 2
end
return @balance
end

def add_interest(rate)
if rate < 0
print "The rate has to be positive" #no puts
else
interest = @balance * (rate/100.0)
@balance += interest
return interest #float
end
end
end
end

#savingsaccount = SavingsAccount.new(1212, 1)
48 changes: 38 additions & 10 deletions specs/account_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'minitest/autorun'
require 'minitest/reporters'
require 'minitest/skip_dsl'
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
require_relative '../lib/account'

describe "Wave 1" 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 @@ -137,35 +138,62 @@
end

# TODO: change 'xdescribe' to 'describe' to run these tests
xdescribe "Wave 2" do
describe "Wave 2" do
describe "Account.all" do

before do
@accounts = Bank::Account.all
end
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
#new_account = Bank::Account.all #- Account.all returns an array
@accounts.must_be_kind_of Array
@accounts.each do |account|
account.must_be_instance_of Bank::Account #Account class
end
end
it "The number of accounts is correct" do
#new_account = Bank::Account.all
@accounts.length.must_equal 12 #(12)
#creating test need must xxx
end
end

describe "Account.find" do
it "Returns an account that exists" do
# TODO: Your test code here!
acct_id_1212 = Bank::Account.find(1212)
acct_id_1212.must_be_instance_of Bank::Account #Account class
end

it "Can find the first account from the CSV" do
# TODO: Your test code here!
acct_id_1212 = Bank::Account.find(1212)
acct_id_1212.must_be_instance_of Bank::Account #Account class

Choose a reason for hiding this comment

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

In addition to checking that what you got back from Account.find is an account, you should verify that the ID matches what you asked for. As is, find could return any old account and your test would still pass.


end

it "Can find the last account from the CSV" do
# TODO: Your test code here!
last_acct = Bank::Account.find(15156)
last_acct.must_be_instance_of Bank::Account
last_acct.balance.must_equal 4356772
end

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


# 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
49 changes: 47 additions & 2 deletions specs/checking_account_spec.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
require 'minitest/autorun'
require 'minitest/reporters'
require 'minitest/skip_dsl'
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

# TODO: uncomment the next line once you start wave 3 and add lib/checking_account.rb
# require_relative '../lib/checking_account'
require_relative '../lib/checking_account'

# Because a CheckingAccount is a kind
# of Account, and we've already tested a bunch of functionality
# on Account, we effectively get all that testing for free!
# Here we'll only test things that are different.

# TODO: change 'xdescribe' to 'describe' to run these tests
xdescribe "CheckingAccount" do
describe "CheckingAccount" do
describe "#initialize" do
# Check that a CheckingAccount is in fact a kind of account
it "Is a kind of Account" do
Expand All @@ -23,58 +24,102 @@
describe "#withdraw" do
it "Applies a $1 fee each time" do
# TODO: Your test code here!
account = Bank::CheckingAccount.new(12345, 200)
account.withdraw(90).must_equal 109
end

it "Doesn't modify the balance if the fee would put it negative" do
# TODO: Your test code here!
account = Bank::CheckingAccount.new(12345, 11)
account.withdraw(11).must_equal 11
end
end

describe "#withdraw_using_check" do
it "Reduces the balance" do
# TODO: Your test code here!
account = Bank::CheckingAccount.new(12345, 200)
account.withdraw_using_check(100).must_equal 100
end

it "Returns the modified balance" do
# TODO: Your test code here!
account = Bank::CheckingAccount.new(12345, 200)
account.withdraw_using_check(100).must_equal 100
end

it "Allows the balance to go down to -$10" do
# TODO: Your test code here!
account = Bank::CheckingAccount.new(12345, 100)
account.withdraw_using_check(110).must_equal (-10)
end

it "Outputs a warning if the account would go below -$10" do
# TODO: Your test code here!
account = Bank::CheckingAccount.new(12345, 100)
proc{account.withdraw_using_check(111)}.must_output (/.+/)
end

it "Doesn't modify the balance if the account would go below -$10" do
# TODO: Your test code here!
account = Bank::CheckingAccount.new(12345, 100)
account.withdraw_using_check(111).must_equal 100
end

it "Requires a positive withdrawal amount" do
# TODO: Your test code here!
account = Bank::CheckingAccount.new(12345, 100)
proc{account.withdraw_using_check(-100)}.must_output (/.+/)
end

it "Allows 3 free uses" do
# TODO: Your test code here!
account = Bank::CheckingAccount.new(12345, 500)
account.withdraw_using_check(100).must_equal 400
account.withdraw_using_check(100).must_equal 300
account.withdraw_using_check(100).must_equal 200
end

it "Applies a $2 fee after the third use" do
# TODO: Your test code here!
account = Bank::CheckingAccount.new(12345, 700)
account.withdraw_using_check(100).must_equal 600
account.withdraw_using_check(100).must_equal 500
account.withdraw_using_check(100).must_equal 400
account.withdraw_using_check(100).must_equal 298
account.withdraw_using_check(100).must_equal 196
end
end

describe "#reset_checks" do
it "Can be called without error" do
# TODO: Your test code here!
account = Bank::CheckingAccount.new(12345, 500)
account.reset_checks
end

it "Makes the next three checks free if less than 3 checks had been used" do
# TODO: Your test code here!
account = Bank::CheckingAccount.new(12345, 1000)
account.withdraw_using_check(100).must_equal 900
account.withdraw_using_check(100).must_equal 800
account.reset_checks
account.withdraw_using_check(100).must_equal 700
account.withdraw_using_check(100).must_equal 600
account.withdraw_using_check(100).must_equal 500
end

it "Makes the next three checks free if more than 3 checks had been used" do
# TODO: Your test code here!
account = Bank::CheckingAccount.new(12345, 1000)
account.withdraw_using_check(100).must_equal 900
account.withdraw_using_check(100).must_equal 800
account.withdraw_using_check(100).must_equal 700
account.withdraw_using_check(100).must_equal 598
account.reset_checks
account.withdraw_using_check(100).must_equal 498
account.withdraw_using_check(100).must_equal 398
account.withdraw_using_check(100).must_equal 298
end
end
end
Loading