From f76d63769c7e18d719fd8520ea9ba9194e132a7c Mon Sep 17 00:00:00 2001 From: Hyunji Kim Date: Wed, 22 Feb 2017 09:09:21 -0800 Subject: [PATCH 01/10] Finished Wave 1 --- lib/account.rb | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/lib/account.rb b/lib/account.rb index e69de29b..323c1109 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -0,0 +1,35 @@ +module Bank + class Account + attr_reader :id, :balance + def initialize(id, balance) + raise ArgumentError.new("balance must be >= 0") if balance < 0 + + @id = id + @balance = balance + 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 +end From 9b0511185358216c26bb9dd7f2f0581dacb3f92b Mon Sep 17 00:00:00 2001 From: Hyunji Kim Date: Wed, 22 Feb 2017 09:12:06 -0800 Subject: [PATCH 02/10] Wave1 finished --- lib/account_hyunji.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 lib/account_hyunji.rb 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 From f21c1940ef7661cae75fd4996f95d90457236dc8 Mon Sep 17 00:00:00 2001 From: Hyunji Kim Date: Wed, 22 Feb 2017 09:17:02 -0800 Subject: [PATCH 03/10] Wave 1 done --- specs/account_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 6c399139..4eaf2c9c 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -33,6 +33,7 @@ end end + describe "Account#withdraw" do it "Reduces the balance" do start_balance = 100.0 From 68e27ff0506e4162c341f9c983cb85e3ea9a6633 Mon Sep 17 00:00:00 2001 From: Hyunji Kim Date: Wed, 22 Feb 2017 19:55:11 -0800 Subject: [PATCH 04/10] Finished up to account number check --- lib/account.rb | 27 +++++++++++++++++++--- specs/account_spec.rb | 53 +++++++++++++++++++++++++++---------------- 2 files changed, 57 insertions(+), 23 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index 323c1109..19c2eb8a 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -1,13 +1,33 @@ +require 'csv' +require 'ap' module Bank class Account - attr_reader :id, :balance - def initialize(id, balance) - raise ArgumentError.new("balance must be >= 0") if balance < 0 + attr_reader :id, :balance, :open_date, :num_of_accounts + 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") + def self.read_csv + return @@csv + end + + def self.all + @@account_all = [] + @@csv.each do |account| + @@account_all << self.new(account[0].to_i, account[1].to_i, account[2]) + end + return @@account_all + end + + + def withdraw(amount) if amount < 0 raise ArgumentError.new "Negative amount entered for withdrawal" @@ -31,5 +51,6 @@ def deposit(amount) end end + end end diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 4eaf2c9c..7c512cef 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -138,35 +138,48 @@ 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 - end - describe "Account.find" do - it "Returns an account that exists" do - # TODO: Your test code here! + 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 "Can find the first account from the CSV" do - # TODO: Your test code here! + it "The number of accounts is correct" do + all_accounts = Bank::Account.all + all_accounts.length.must_equal 12 end - it "Can find the last account from the CSV" do - # TODO: Your test code here! + 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_accouts[0].must_equal Bank::Account.read_csv[0] end - it "Raises an error for an account that doesn't exist" do - # TODO: Your test code here! - end end end +# +# xdescribe "Account.find" do +# it "Returns an account that exists" do +# # TODO: Your test code here! +# end +# +# it "Can find the first account from the CSV" do +# # TODO: Your test code here! +# end +# +# it "Can find the last account from the CSV" do +# # TODO: Your test code here! +# end +# +# it "Raises an error for an account that doesn't exist" do +# # TODO: Your test code here! +# end +# end +# end From 22943ed4888a40f452c1a554ed447318f792a7ad Mon Sep 17 00:00:00 2001 From: Hyunji Kim Date: Wed, 22 Feb 2017 20:48:57 -0800 Subject: [PATCH 05/10] Finished up to checking first and last id --- lib/account.rb | 16 +++++++++------- specs/account_spec.rb | 7 ++++++- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index 19c2eb8a..fcde7ce4 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -13,21 +13,23 @@ def initialize(id, balance, open_date ="") end @@account_all = [] - @@csv = CSV.read("./support/accounts.csv") - def self.read_csv - return @@csv - end + @@csv = CSV.read("./support/accounts.csv") + + def self.read_csv + return @@csv + end + + + def self.all @@account_all = [] @@csv.each do |account| - @@account_all << self.new(account[0].to_i, account[1].to_i, account[2]) + @@account_all << self.new(account[0].to_i, account[1].to_i, account[2]) end return @@account_all end - - def withdraw(amount) if amount < 0 raise ArgumentError.new "Negative amount entered for withdrawal" diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 7c512cef..c76f0f58 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -159,7 +159,12 @@ 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_accouts[0].must_equal Bank::Account.read_csv[0] + + all_accounts.first.id.must_equal Bank::Account.read_csv.first[0].to_i + all_accounts.first.balance.must_equal Bank::Account.read_csv.first[1].to_i + all_accounts.last.id.must_equal Bank::Account.read_csv.last[0].to_i + all_accounts.last.balance.must_equal Bank::Account.read_csv.last[1].to_i + end end From 424add3c4fdbe3f83603daea192d4c8fab91fd53 Mon Sep 17 00:00:00 2001 From: Hyunji Kim Date: Wed, 22 Feb 2017 21:27:03 -0800 Subject: [PATCH 06/10] Comparing the first and the last accounts --- lib/account.rb | 20 +++++++++++++++++--- specs/account_spec.rb | 8 ++++---- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index fcde7ce4..443d64d6 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -13,19 +13,30 @@ def initialize(id, balance, open_date ="") end @@account_all = [] - @@csv = CSV.read("./support/accounts.csv") + + @@csv = [] def self.read_csv + @@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 return @@csv end + # @@csv = CSV.read("./support/accounts.csv") + # + # def self.read_csv + # return @@csv + # end + # def self.all @@account_all = [] - @@csv.each do |account| - @@account_all << self.new(account[0].to_i, account[1].to_i, account[2]) + self.read_csv.each do |account| + @@account_all << self.new(account[:id].to_i, account[:balance].to_i, account[:open_date]) end return @@account_all end @@ -56,3 +67,6 @@ def deposit(amount) end end +# puts Bank::Account.all[0].id +# puts Bank::Account.read_csv[0][:id] +# puts Bank::Account.read_csv diff --git a/specs/account_spec.rb b/specs/account_spec.rb index c76f0f58..1cd1421a 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -160,10 +160,10 @@ 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 Bank::Account.read_csv.first[0].to_i - all_accounts.first.balance.must_equal Bank::Account.read_csv.first[1].to_i - all_accounts.last.id.must_equal Bank::Account.read_csv.last[0].to_i - all_accounts.last.balance.must_equal Bank::Account.read_csv.last[1].to_i + all_accounts.first.id.must_equal Bank::Account.read_csv.first[:id] + all_accounts.first.balance.must_equal Bank::Account.read_csv.first[:balance] + all_accounts.last.id.must_equal Bank::Account.read_csv.last[:id] + all_accounts.last.balance.must_equal Bank::Account.read_csv.last[:balance] end From d42421682a7ddf6c2759ea86e20ff3481aca6ca1 Mon Sep 17 00:00:00 2001 From: Hyunji Kim Date: Wed, 22 Feb 2017 23:38:27 -0800 Subject: [PATCH 07/10] finished Wave2 --- lib/account.rb | 46 +++++++++++++++++++++--------------- specs/account_spec.rb | 55 ++++++++++++++++++++++++------------------- 2 files changed, 58 insertions(+), 43 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index 443d64d6..f62e04e0 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -3,7 +3,7 @@ module Bank class Account - attr_reader :id, :balance, :open_date, :num_of_accounts + attr_reader :id, :balance, :open_date def initialize(id, balance, open_date ="") raise ArgumentError.new("balance must be >= 0") if balance < 0 @id = id @@ -15,32 +15,43 @@ def initialize(id, balance, open_date ="") @@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.read_csv - @@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 = CSV.read("./support/accounts.csv") - # - # def self.read_csv - # return @@csv - # end - # + def self.find_csv_account(num) + return @@csv[num] + end + @@csv.each do |account| + @@account_all << self.new(account[:id].to_i, account[:balance].to_i, account[:open_date]) + end def self.all - @@account_all = [] - self.read_csv.each do |account| - @@account_all << self.new(account[:id].to_i, account[:balance].to_i, account[:open_date]) - end return @@account_all end + + def self.find(entered_id) + id_exist = false + @@account_all.each do |account| + if account.id == entered_id + return account + id_exist = true + end + end + if id_exist == false + raise ArgumentError.new "Entered ID doesn't exist" + end + end + + def withdraw(amount) if amount < 0 raise ArgumentError.new "Negative amount entered for withdrawal" @@ -67,6 +78,3 @@ def deposit(amount) end end -# puts Bank::Account.all[0].id -# puts Bank::Account.read_csv[0][:id] -# puts Bank::Account.read_csv diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 1cd1421a..d5c81510 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -68,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 @@ -160,31 +160,38 @@ 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 Bank::Account.read_csv.first[:id] - all_accounts.first.balance.must_equal Bank::Account.read_csv.first[:balance] - all_accounts.last.id.must_equal Bank::Account.read_csv.last[:id] - all_accounts.last.balance.must_equal Bank::Account.read_csv.last[:balance] + all_accounts.first.id.must_equal Bank::Account.csv.first[:id] + all_accounts.first.balance.must_equal Bank::Account.csv.first[:balance] + all_accounts.last.id.must_equal Bank::Account.csv.last[:id] + all_accounts.last.balance.must_equal Bank::Account.csv.last[:balance] end end end -# -# xdescribe "Account.find" do -# it "Returns an account that exists" do -# # TODO: Your test code here! -# end -# -# it "Can find the first account from the CSV" do -# # TODO: Your test code here! -# end -# -# it "Can find the last account from the CSV" do -# # TODO: Your test code here! -# end -# -# it "Raises an error for an account that doesn't exist" do -# # TODO: Your test code here! -# end -# end -# end + + describe "Account.find" do + it "Returns an account that exists" do + # # 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 + first_account_csv = Bank::Account.find_csv_account(0) + first_account_csv.must_equal Bank::Account.csv[0] + + end + # + it "Can find the last account from the CSV" do + last_account_csv = Bank::Account.find_csv_account(-1) + last_account_csv.must_equal Bank::Account.csv[-1] + end + # + it "Raises an error for an account that doesn't exist" do + id = 12345 + proc { + Bank::Account.find(id) + }.must_raise ArgumentError + end + end From d83f24851e55e5148f8c5bb2b5cefd0ddb34fed8 Mon Sep 17 00:00:00 2001 From: Hyunji Kim Date: Thu, 23 Feb 2017 00:41:06 -0800 Subject: [PATCH 08/10] Fixed final Wave2 --- lib/account.rb | 5 ----- specs/account_spec.rb | 16 ++++++++-------- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index f62e04e0..88e50666 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -24,11 +24,6 @@ def self.csv return @@csv end - def self.find_csv_account(num) - return @@csv[num] - end - - @@csv.each do |account| @@account_all << self.new(account[:id].to_i, account[:balance].to_i, account[:open_date]) end diff --git a/specs/account_spec.rb b/specs/account_spec.rb index d5c81510..e37abe14 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -160,10 +160,10 @@ 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 Bank::Account.csv.first[:id] - all_accounts.first.balance.must_equal Bank::Account.csv.first[:balance] - all_accounts.last.id.must_equal Bank::Account.csv.last[:id] - all_accounts.last.balance.must_equal Bank::Account.csv.last[:balance] + 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 @@ -178,14 +178,14 @@ end it "Can find the first account from the CSV" do - first_account_csv = Bank::Account.find_csv_account(0) - first_account_csv.must_equal Bank::Account.csv[0] + 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 - last_account_csv = Bank::Account.find_csv_account(-1) - last_account_csv.must_equal Bank::Account.csv[-1] + 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 From 8628ef1938c093733275e0d81ba3a6da9c6d35bc Mon Sep 17 00:00:00 2001 From: Hyunji Kim Date: Thu, 23 Feb 2017 23:02:44 -0800 Subject: [PATCH 09/10] Wave3 Done --- lib/account.rb | 47 +++++++++++++++++++-- lib/checking_account.rb | 61 ++++++++++++++++++++++++++++ lib/savings_account.rb | 41 +++++++++++++++++++ specs/account_spec.rb | 1 + specs/checking_account_spec.rb | 74 +++++++++++++++++++++++++++------- specs/savings_account_spec.rb | 39 +++++++++++++----- 6 files changed, 233 insertions(+), 30 deletions(-) create mode 100644 lib/checking_account.rb create mode 100644 lib/savings_account.rb diff --git a/lib/account.rb b/lib/account.rb index 88e50666..ceb8b1a1 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -34,16 +34,12 @@ def self.all def self.find(entered_id) - id_exist = false @@account_all.each do |account| if account.id == entered_id return account - id_exist = true end end - if id_exist == false raise ArgumentError.new "Entered ID doesn't exist" - end end @@ -72,4 +68,47 @@ def deposit(amount) 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/checking_account.rb b/lib/checking_account.rb new file mode 100644 index 00000000..82868813 --- /dev/null +++ b/lib/checking_account.rb @@ -0,0 +1,61 @@ +require_relative 'account' + +module Bank + class CheckingAccount < Bank::Account + attr_reader :id, :amount + attr_accessor :num_withdrawal + def initialize(id, balance) + # super(id, balance) + @id = id + @balance = balance + raise ArgumentError.new("balance must be >= 0") if balance < 10 + @num_withdrawal = 0 + end + + def withdraw(amount) + if @balance - (amount + 1) < 10 + 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/savings_account.rb b/lib/savings_account.rb new file mode 100644 index 00000000..69a42f1c --- /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/specs/account_spec.rb b/specs/account_spec.rb index e37abe14..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 diff --git a/specs/checking_account_spec.rb b/specs/checking_account_spec.rb index 7f95339e..b802090c 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! + acount = Bank::CheckingAccount.new(12345, 100.0) + acount.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(95) + 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/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 From d0173462aa4218de65fe37ec92a8807bdf6042b8 Mon Sep 17 00:00:00 2001 From: Hyunji Kim Date: Fri, 24 Feb 2017 15:23:02 -0800 Subject: [PATCH 10/10] Finished Wave3 & MoneyMarket --- lib/checking_account.rb | 12 ++--- lib/money_market.rb | 89 +++++++++++++++++++++++++++++++ lib/savings_account.rb | 8 +-- lib/test_super_account.rb | 16 ++++++ specs/checking_account_spec.rb | 6 +-- specs/money_market_spec.rb | 96 ++++++++++++++++++++++++++++++++++ 6 files changed, 214 insertions(+), 13 deletions(-) create mode 100644 lib/money_market.rb create mode 100644 lib/test_super_account.rb create mode 100644 specs/money_market_spec.rb diff --git a/lib/checking_account.rb b/lib/checking_account.rb index 82868813..abe2e8e0 100644 --- a/lib/checking_account.rb +++ b/lib/checking_account.rb @@ -2,18 +2,18 @@ module Bank class CheckingAccount < Bank::Account - attr_reader :id, :amount + # 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 >= 0") if balance < 10 + 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) < 10 + if @balance - (amount + 1) < 0 puts "balance must be greater than 0" return @balance else 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 index 69a42f1c..3c65404f 100644 --- a/lib/savings_account.rb +++ b/lib/savings_account.rb @@ -2,11 +2,11 @@ module Bank class SavingsAccount < Bank::Account - attr_reader :id, :amount + # attr_reader :id, :amount def initialize(id, balance) - # super(id, balance) - @id = id - @balance = balance + super(id, balance) + # @id = id + # @balance = balance raise ArgumentError.new("balance must be greater than 10") if balance < 10 end 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/checking_account_spec.rb b/specs/checking_account_spec.rb index b802090c..df4fc0ec 100644 --- a/specs/checking_account_spec.rb +++ b/specs/checking_account_spec.rb @@ -23,13 +23,13 @@ describe "#withdraw" do it "Applies a $1 fee each time" do - acount = Bank::CheckingAccount.new(12345, 100.0) - acount.withdraw(50).must_equal 49 + 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 account = Bank::CheckingAccount.new(12345, 100.0) - new_balance = account.withdraw(95) + new_balance = account.withdraw(100) new_balance.must_equal 100 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