diff --git a/demo.rb b/demo.rb new file mode 100644 index 00000000..7339328a --- /dev/null +++ b/demo.rb @@ -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 diff --git a/lib/account.rb b/lib/account.rb index e69de29b..45cf21d9 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -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 + #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 diff --git a/lib/checking_account.rb b/lib/checking_account.rb new file mode 100644 index 00000000..b37a84d2 --- /dev/null +++ b/lib/checking_account.rb @@ -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 diff --git a/lib/savings_account.rb b/lib/savings_account.rb new file mode 100644 index 00000000..b9707f91 --- /dev/null +++ b/lib/savings_account.rb @@ -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) diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 6c399139..92f9c807 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -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 @@ -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 @@ -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 + 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 diff --git a/specs/checking_account_spec.rb b/specs/checking_account_spec.rb index 7f95339e..4cbaa4b0 100644 --- a/specs/checking_account_spec.rb +++ b/specs/checking_account_spec.rb @@ -1,9 +1,10 @@ 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 @@ -11,7 +12,7 @@ # 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 @@ -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 diff --git a/specs/savings_account_spec.rb b/specs/savings_account_spec.rb index 3f4d1e4a..a5002c41 100644 --- a/specs/savings_account_spec.rb +++ b/specs/savings_account_spec.rb @@ -1,9 +1,10 @@ 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/savings_account.rb -# require_relative '../lib/savings_account' +require_relative '../lib/savings_account' # Because a SavingsAccount is a kind # of Account, and we've already tested a bunch of functionality @@ -11,7 +12,7 @@ # Here we'll only test things that are different. # TODO: change 'xdescribe' to 'describe' to run these tests -xdescribe "SavingsAccount" do +describe "SavingsAccount" do describe "#initialize" do it "Is a kind of Account" do # Check that a SavingsAccount is in fact a kind of account @@ -19,40 +20,59 @@ account.must_be_kind_of Bank::Account end - it "Requires an initial balance of at least $10" do + it "Throws an error if balance is less than $10" do # TODO: Your test code here! + proc { + Bank::SavingsAccount.new(1337, 8) #testing an action, no need of variable + }.must_raise ArgumentError end end describe "#withdraw" do it "Applies a $2 fee each time" do # TODO: Your test code here! + account = Bank::SavingsAccount.new(12345, 100.0) + account.withdraw(10).must_equal 88 end it "Outputs a warning if the balance would go below $10" do # TODO: Your test code here! + account = Bank::SavingsAccount.new(12345, 11) + proc{account.withdraw(2)}.must_output "Your balance has to be more than 10" end it "Doesn't modify the balance if it would go below $10" do # TODO: Your test code here! + account = Bank::SavingsAccount.new(12345, 11) + account.withdraw(2).must_equal 11 #bring it below 10 end it "Doesn't modify the balance if the fee would put it below $10" do # TODO: Your test code here! + account = Bank::SavingsAccount.new(12345, 11) + account.withdraw(1).must_equal 11 end end describe "#add_interest" do it "Returns the interest calculated" do # TODO: Your test code here! + account = Bank::SavingsAccount.new(12345, 100) + account.add_interest(25).must_equal 25 end it "Updates the balance with calculated interest" do # TODO: Your test code here! + account = Bank::SavingsAccount.new(12345, 100) + account.add_interest(25) + balance = account.balance + balance.must_equal 125 end it "Requires a positive rate" do # TODO: Your test code here! + account = Bank::SavingsAccount.new(12345, 100) + proc{account.add_interest(-25)}.must_output "The rate has to be positive" end end end diff --git a/support/accounts.csv b/support/accounts.csv index 3417f2dc..cca76f80 100644 --- a/support/accounts.csv +++ b/support/accounts.csv @@ -1,12 +1,12 @@ -1212,1235667,1999-03-27 11:30:09 -0800 -1213,66367,2010-12-21 12:21:12 -0800 -1214,9876890,2007-09-22 11:53:00 -0800 -1215,919191,2011-10-31 13:55:55 -0800 -1216,100022,2000-07-07 15:07:55 -0800 -1217,12323,2003-11-07 11:34:56 -0800 -15151,9844567,1993-01-17 13:30:56 -0800 -15152,34343434343,1999-02-12 14:03:00 -0800 -15153,2134,2013-11-07 09:04:56 -0800 -15154,43567,1996-04-17 08:44:56 -0800 -15155,999999,1990-06-10 13:13:13 -0800 -15156,4356772,1994-11-17 14:04:56 -0800 +1212, 1235667, 1999-03-27 11:30:09 -0800 +1213, 66367, 2010-12-21 12:21:12 -0800 +1214, 9876890, 2007-09-22 11:53:00 -0800 +1215, 919191, 2011-10-31 13:55:55 -0800 +1216, 100022, 2000-07-07 15:07:55 -0800 +1217, 12323, 2003-11-07 11:34:56 -0800 +15151, 9844567, 1993-01-17 13:30:56 -0800 +15152, 34343434343, 1999-02-12 14:03:00 -0800 +15153, 2134, 2013-11-07 09:04:56 -0800 +15154, 43567, 1996-04-17 08:44:56 -0800 +15155, 999999, 1990-06-10 13:13:13 -0800 +15156, 4356772, 1994-11-17 14:04:56 -0800