diff --git a/lib/account.rb b/lib/account.rb index e69de29b..ceb8b1a1 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -0,0 +1,114 @@ +require 'csv' +require 'ap' +module Bank + class Account + + attr_reader :id, :balance, :open_date + def initialize(id, balance, open_date ="") + raise ArgumentError.new("balance must be >= 0") if balance < 0 + @id = id + @balance = balance + @open_date = open_date + + end + + @@account_all = [] + + @@csv = [] + CSV.read("./support/accounts.csv").each do |account| + @@csv << {id: account[0].to_i, balance: account[1].to_i, open_date: account[2]} + end + + + def self.csv + return @@csv + end + + @@csv.each do |account| + @@account_all << self.new(account[:id].to_i, account[:balance].to_i, account[:open_date]) + end + + def self.all + return @@account_all + end + + + def self.find(entered_id) + @@account_all.each do |account| + if account.id == entered_id + return account + end + end + raise ArgumentError.new "Entered ID doesn't exist" + end + + + def withdraw(amount) + if amount < 0 + raise ArgumentError.new "Negative amount entered for withdrawal" + else + new_balance = @balance - amount + if new_balance < 0 + puts "Withdrawal amount greater than the current balance" + @balance = @balance + else + @balance = new_balance + end + end + end + + def deposit(amount) + if amount < 0 + raise ArgumentError.new "Minus amount deposited" + else + new_balance = @balance + amount + @balance = new_balance + end + + end + + end + +# class Owner +# attr_reader :last_name, :first_name, :address +# def initialize(last_name, first_name, address) +# @last_name = last_name +# @first_name = first_name +# @address = address +# end +# +# @@owner = [] +# @@csv = [] +# # CSV.read("../support/accounts.csv").each do |account| +# # @@csv << {id: account[0].to_i, balance: account[1].to_i, open_date: account[2]} +# # end +# CSV.read("../support/owners.csv").each do |owner| +# @@owner << {last_name: owner[1], first_name: owner[2], address: owner[3]} +# end +# # ap @@owner[0].last_name +# # puts Bank::Account.csv +# # puts @@owner +# +# +# def self.merge_info +# n = 0 +# @@owner.length.times do |n| +# Bank::Account.csv[n].merge!(@@owner[n]) +# n +=1 +# end +# end +# +# +# +# +# end + + + + +end + +# a = Bank::Account.new(12334, 5000) +# puts a.id +# Bank::Owner.merge_info +# ap Bank::Account.csv diff --git a/lib/account_hyunji.rb b/lib/account_hyunji.rb new file mode 100644 index 00000000..2bcddcf2 --- /dev/null +++ b/lib/account_hyunji.rb @@ -0,0 +1,18 @@ +module Bank + class Account + attr_reader :balance, :id + def initialize(id, initial_balance) + @balance = initial_balance + @id = id + if @balance < 0 + raise ArgumentError.new "Initial balance is negative" + end + end + + def withdraw(amount) + end + + def deposit(amount) + end + end +end diff --git a/lib/checking_account.rb b/lib/checking_account.rb new file mode 100644 index 00000000..abe2e8e0 --- /dev/null +++ b/lib/checking_account.rb @@ -0,0 +1,61 @@ +require_relative 'account' + +module Bank + class CheckingAccount < Bank::Account + # attr_reader :id, :balance + attr_accessor :num_withdrawal + def initialize(id, balance) + super(id, balance) + # @id = id + # @balance = balance + # raise ArgumentError.new("balance must be >= 10") if balance < 10 + @num_withdrawal = 0 + end + + def withdraw(amount) + if @balance - (amount + 1) < 0 + puts "balance must be greater than 0" + return @balance + else + return @balance = @balance - (amount + 1) + end + end + + def withdraw_using_check(amount) + if amount < 0 + puts "Withdrawl amount must be greater than 0" + else + if @balance - amount < -10 + puts "balance must be greater than -10" + return @balance + else + if @num_withdrawal >= 3 + return @balance = @balance - (amount + 2) + else + @num_withdrawal += 1 + # puts @num_withdrawal + return @balance = @balance - amount + end + end + end + end + + def reset_checks + @num_withdrawal = 0 + return "reset" + end + + end +end + +# a = Bank::CheckingAccount.new(1234, 5000) +# a.withdraw_using_check(10) +# puts a.balance +# a.withdraw_using_check(10) +# puts a.balance +# a.withdraw_using_check(10) +# puts a.balance +# a.withdraw_using_check(10) +# puts a.balance + +# puts Bank::CheckingAccount.new(1234, 5000) diff --git a/lib/money_market.rb b/lib/money_market.rb new file mode 100644 index 00000000..23abf45e --- /dev/null +++ b/lib/money_market.rb @@ -0,0 +1,89 @@ +require_relative 'account' + +module Bank + class MoneyMarket < Bank::Account + attr_accessor :num_transaction + def initialize(id, balance) + super(id, balance) + @num_transaction = 0 + raise ArgumentError.new("Balance cannot be less than $10,000") if @balance < 10000 + end + + def withdraw(amount) + if @num_transaction < 6 + if (@balance - amount) < 10000 + @num_transaction += 1 + puts "Balance is lower than 10000. Charged $100 fee" + # puts @balance - (amount + 100) + @balance = @balance - (amount + 100) + else + @num_transaction += 1 + @balance = (@balance - amount) + end + else + puts "Maximum Number of Transactions has reached." + return @balance + end + end + + def deposit(amount) + if amount < 0 + raise ArgumentError.new "Minus amount deposited" + else + if @num_transaction < 6 + @balance = @balance + amount + @num_transaction += 1 + else + if @balance < 10000 + if (@balance + amount) > 10000 + @balance = (@balance + amount) + @num_transaction = @num_transaction + puts "deposit balance< 10000 #{@num_transaction}" + else + puts "Maximum Number of Transactions has reached." + end + else + puts "Maximum Number of Transactions has reached." + end + end + end + end + + def add_interest(rate) + if rate < 0 + puts "Requires a positive rate" + else + interest = @balance * rate + @balance = @balance + (@balance * rate) + return interest + end + end + + def reset_transactions + @num_transaction = 0 + end + + + + end +end + + +a = Bank::MoneyMarket.new(1, 10000) +a.deposit(30) +puts a.balance +a.deposit(30) +puts a.balance +a.deposit(30) +puts a.balance +a.withdraw(50) +puts a.balance +a.withdraw(50) +puts a.balance +a.withdraw(50) +puts a.balance +a.deposit(500) +puts a.balance +a.deposit(500) +puts a.balance +puts a.num_transaction diff --git a/lib/savings_account.rb b/lib/savings_account.rb new file mode 100644 index 00000000..3c65404f --- /dev/null +++ b/lib/savings_account.rb @@ -0,0 +1,41 @@ +require_relative 'account' + +module Bank + class SavingsAccount < Bank::Account + # attr_reader :id, :amount + def initialize(id, balance) + super(id, balance) + # @id = id + # @balance = balance + raise ArgumentError.new("balance must be greater than 10") if balance < 10 + end + + def withdraw(withdraw_amount) + if @balance - (withdraw_amount + 2) < 10 + puts "balance must be >= 10" + return @balance + else + return @balance = @balance - (withdraw_amount + 2) + end + end + + def add_interest(rate) + if rate < 0 + puts "Requires a positive rate" + else + interest = @balance * rate + @balance = @balance + (@balance * rate) + return interest + end + end + + # a = Bank::SavingsAccount.new(1223, 5000) + # puts a.withdraw(500) + # puts a.balance + + + end +end + +# a = Bank::SavingsAccount.new(1000, 10000) +# puts a diff --git a/lib/test_super_account.rb b/lib/test_super_account.rb new file mode 100644 index 00000000..9b0d7bc0 --- /dev/null +++ b/lib/test_super_account.rb @@ -0,0 +1,16 @@ +require_relative 'account' + +module Bank + class Test < Bank::Account + def initialize(id, balance) + super(id, balance) + end + + def deposit(amount) + super(amount) + @balance = @balance + 10 + end + end +end +a = Bank::Test.new(1234, 5000) +puts a.deposit(50) diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 6c399139..5a513356 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -3,6 +3,7 @@ require 'minitest/skip_dsl' require_relative '../lib/account' + describe "Wave 1" do describe "Account#initialize" do it "Takes an ID and an initial balance" do @@ -33,6 +34,7 @@ end end + describe "Account#withdraw" do it "Reduces the balance" do start_balance = 100.0 @@ -67,7 +69,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 +139,60 @@ end # TODO: change 'xdescribe' to 'describe' to run these tests -xdescribe "Wave 2" do +describe "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 + all_accounts = Bank::Account.all + all_accounts.must_be_kind_of Array + end + + it "Everything in the array is an Account" do + all_accounts = Bank::Account.all + all_accounts.each do |account| + account.must_be_kind_of Bank::Account + end + end + + it "The number of accounts is correct" do + all_accounts = Bank::Account.all + all_accounts.length.must_equal 12 + end + + it "The ID and balance of the first and last accounts match what's in the CSV file" do + all_accounts = Bank::Account.all + + all_accounts.first.id.must_equal 1212 + all_accounts.first.balance.must_equal 1235667 + all_accounts.last.id.must_equal 15156 + all_accounts.last.balance.must_equal 4356772 + end + end +end describe "Account.find" do it "Returns an account that exists" do - # TODO: Your test code here! + # # TODO: Your test code here! + account = Bank::Account.all[0] + Bank::Account.find(1212).must_equal account end it "Can find the first account from the CSV" do - # TODO: Your test code here! - end + first_account_csv_balance = Bank::Account.csv[0][:balance] + first_account_csv_balance.must_equal 1235667 + end + # it "Can find the last account from the CSV" do - # TODO: Your test code here! + last_account_csv_id = Bank::Account.csv[-1][:id] + last_account_csv_id.must_equal 15156 end - + # it "Raises an error for an account that doesn't exist" do - # TODO: Your test code here! + id = 12345 + proc { + Bank::Account.find(id) + }.must_raise ArgumentError end end -end diff --git a/specs/checking_account_spec.rb b/specs/checking_account_spec.rb index 7f95339e..df4fc0ec 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 @@ -22,59 +23,102 @@ describe "#withdraw" do it "Applies a $1 fee each time" do - # TODO: Your test code here! + account = Bank::CheckingAccount.new(12345, 100.0) + account.withdraw(50).must_equal 49 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, 100.0) + new_balance = account.withdraw(100) + new_balance.must_equal 100 end end describe "#withdraw_using_check" do it "Reduces the balance" do - # TODO: Your test code here! + account = Bank::CheckingAccount.new(12345, 100.0) + new_balance = account.withdraw_using_check(10) + new_balance.must_equal 90 end it "Returns the modified balance" do - # TODO: Your test code here! + account = Bank::CheckingAccount.new(12345, 100.0) + new_balance = account.withdraw_using_check(10) + new_balance.must_equal 90 end it "Allows the balance to go down to -$10" do - # TODO: Your test code here! + account = Bank::CheckingAccount.new(12345, 100.0) + new_balance = account.withdraw_using_check(110) + new_balance.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.0) + 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.0) + new_balance = account.withdraw_using_check(111) + new_balance.must_equal 100 end it "Requires a positive withdrawal amount" do - # TODO: Your test code here! + account = Bank::CheckingAccount.new(12345, 100.0) + proc { + account.withdraw_using_check(-1) + }.must_output (/.+/) end it "Allows 3 free uses" do - # TODO: Your test code here! + account = Bank::CheckingAccount.new(12345, 100.0) + account.withdraw_using_check(10) + account.withdraw_using_check(10) + account.withdraw_using_check(10) + account.balance.must_equal 70 end it "Applies a $2 fee after the third use" do - # TODO: Your test code here! + account = Bank::CheckingAccount.new(12345, 100.0) + account.withdraw_using_check(10) + account.withdraw_using_check(10) + account.withdraw_using_check(10) + account.withdraw_using_check(10) + account.balance.must_equal 58 end end describe "#reset_checks" do it "Can be called without error" do - # TODO: Your test code here! + account = Bank::CheckingAccount.new(12345, 100.0) + account.reset_checks.must_equal "reset" 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, 100.0) + account.withdraw_using_check(10) + account.withdraw_using_check(10) + account.reset_checks + account.withdraw_using_check(10) + account.withdraw_using_check(10) + account.withdraw_using_check(10) + account.balance.must_equal 50 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, 100.0) + account.withdraw_using_check(10) + account.withdraw_using_check(10) + account.withdraw_using_check(10) + account.withdraw_using_check(10) + account.reset_checks + account.withdraw_using_check(10) + account.withdraw_using_check(10) + account.withdraw_using_check(10) + account.balance.must_equal 28 end end end diff --git a/specs/money_market_spec.rb b/specs/money_market_spec.rb new file mode 100644 index 00000000..b8d7c24a --- /dev/null +++ b/specs/money_market_spec.rb @@ -0,0 +1,96 @@ +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/money_market' + +describe "MoneyMarket" do + describe "#initialize" do + it "Is a kind of Account" do + # Check that a MoneyMarket is in fact a kind of account + account = Bank::MoneyMarket.new(12345, 15000.00) + account.must_be_kind_of Bank::Account + end + + it "Requires an initial balance of at least $10000" do + proc { + Bank::MoneyMarket.new(1337, 9999) + }.must_raise ArgumentError + end + end + + describe "#withdraw" do + it "Applies a $100 fee when the balance goes below $10000" do + account = Bank::MoneyMarket.new(1337, 15000.0) + account.withdraw(5001) + account.balance.must_equal 9899 + end + + it "No more transactions allowed after 6 transactions" do + account = Bank::MoneyMarket.new(1337, 15000.0) + account.withdraw(10) + account.withdraw(10) + account.withdraw(10) + account.withdraw(10) + account.withdraw(10) + account.withdraw(10) + account.withdraw(10) + account.balance.must_equal 14940 + end + end + + describe "#deposit" do + it "The deposit transaction made to push balance over 0 is not counted in max 6 transactions per month " do + account = Bank::MoneyMarket.new(12345, 10000) + account.deposit(10) + account.deposit(10) + account.deposit(10) + account.deposit(10) + account.withdraw(30) + account.withdraw(30) + account.deposit(200) + account.balance.must_equal 10080 + end + end + + describe "#add_interest" do + it "Returns the interest calculated" do + account = Bank::MoneyMarket.new(12345, 15000.0) + interest = account.add_interest(0.25) + interest.must_equal 3750 + end + + it "Updates the balance with calculated interest" do + account = Bank::MoneyMarket.new(12345, 15000.0) + account.add_interest(0.25) + account.balance.must_equal 18750 + end + + it "Requires a positive rate" do + account = Bank::MoneyMarket.new(12345, 15000.0) + proc { + account.add_interest(-0.25) + }.must_output (/.+/) + end + end + + describe "#reset_transactions" do + it "Setting the number of transactions to 0" do + account = Bank::MoneyMarket.new(12345, 10000.0) + account.deposit(10) + account.deposit(10) + account.deposit(10) + account.deposit(10) + account.withdraw(30) + account.withdraw(30) + account.deposit(200) #10080 + account.deposit(20) + account.reset_transactions + account.deposit(20) + account.balance.must_equal 10100 + end + end + +end diff --git a/specs/savings_account_spec.rb b/specs/savings_account_spec.rb index 3f4d1e4a..bb36e97c 100644 --- a/specs/savings_account_spec.rb +++ b/specs/savings_account_spec.rb @@ -1,9 +1,9 @@ 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 +11,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 @@ -20,39 +20,56 @@ end it "Requires an initial balance of at least $10" do - # TODO: Your test code here! + proc { + Bank::SavingsAccount.new(1337, 9) + }.must_raise ArgumentError end end describe "#withdraw" do it "Applies a $2 fee each time" do - # TODO: Your test code here! + acount = Bank::SavingsAccount.new(12345, 100.0) + acount.withdraw(50).must_equal 48 end it "Outputs a warning if the balance would go below $10" do - # TODO: Your test code here! + account = Bank::SavingsAccount.new(12345, 100.0) + proc { + account.withdraw(95) + }.must_output (/.+/) end it "Doesn't modify the balance if it would go below $10" do - # TODO: Your test code here! + account = Bank::SavingsAccount.new(12345, 100.0) + new_balance = account.withdraw(95) + new_balance.must_equal 100 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, 100.0) + new_balance = account.withdraw(90) + new_balance.must_equal 100 end end describe "#add_interest" do it "Returns the interest calculated" do - # TODO: Your test code here! + account = Bank::SavingsAccount.new(12345, 100.0) + interest = account.add_interest(0.25) + interest.must_equal 25 end it "Updates the balance with calculated interest" do - # TODO: Your test code here! + account = Bank::SavingsAccount.new(12345, 100.0) + account.add_interest(0.25) + account.balance.must_equal 125 end it "Requires a positive rate" do - # TODO: Your test code here! + account = Bank::SavingsAccount.new(12345, 100.0) + proc { + account.add_interest(-0.25) + }.must_output (/.+/) end end end