-
Notifications
You must be signed in to change notification settings - Fork 39
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 - Kayla Kubicke - Bank Accounts #36
base: master
Are you sure you want to change the base?
Changes from all commits
42ed92b
2477b8b
b760cf5
650f50d
e4cdf7c
885c9e4
7edf641
0111546
c2e0f5a
969b413
2ebd79e
7f9d035
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
require 'csv' | ||
|
||
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) | ||
raise ArgumentError.new("withdrawal amount must be >= 0") if amount < 0 | ||
if amount > @balance | ||
raise ArgumentError.new("withdrawal amount must be > 0") if @balance < 0 | ||
puts "Account would go negative." | ||
elsif amount <= @balance | ||
@balance = @balance - amount | ||
end | ||
return @balance | ||
end | ||
|
||
def deposit(amount) | ||
raise ArgumentError.new("deposit amount must be >= 0") if amount < 0 | ||
@balance = @balance + amount | ||
return @balance | ||
end | ||
|
||
def self.all | ||
accounts_array = [] | ||
|
||
read_file = CSV.read('support/accounts.csv') | ||
|
||
read_file.each do |line| | ||
id = line[0].to_i | ||
balance = line[1].to_i | ||
account = Bank::Account.new(id, balance) | ||
accounts_array << account | ||
end | ||
|
||
return accounts_array | ||
end | ||
|
||
def self.find(id) | ||
accounts = Bank::Account.all | ||
|
||
accounts.each do |account| | ||
if account.id == id | ||
return account | ||
end | ||
end | ||
raise ArgumentError.new "Account does not exist." | ||
end | ||
|
||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
require_relative './account' | ||
require 'csv' | ||
|
||
module Bank | ||
class MoneyMarketAccount < Bank::Account | ||
def initialize(id, balance) | ||
super(id, balance) | ||
@counter = 0 | ||
end | ||
|
||
def withdraw(amount) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be cleaner to use |
||
amount += 1 | ||
|
||
if (@balance - amount) < 0 | ||
return @balance | ||
raise ArgumentError.new("account must be > 0") if (@balance - amount) < 0 | ||
else | ||
@balance = @balance - amount | ||
return @balance | ||
end | ||
end | ||
|
||
#Note: In the video for Wave 3 it was mentioned that we could | ||
#forgo counting by month. I did not account for the month. | ||
def withdraw_using_check(amount) | ||
# @counter = 0 | ||
fee = 2 | ||
test_balance = @balance - amount | ||
raise ArgumentError.new("needs a positive withdrawal amount") if amount < 0 | ||
|
||
if @counter <= 3 && test_balance < -10 | ||
return @balance | ||
raise ArgumentError.new("account will go below -10") if (@balance - amount) < -10 | ||
elsif @counter <= 3 && test_balance >= -10 | ||
@balance = @balance - amount | ||
return @balance | ||
@counter += 1 | ||
elsif @counter > 3 && test_balance >= -10 | ||
@balance = @balance - amount - fee | ||
@counter += 1 | ||
elsif @counter > 3 && test_balance < -10 | ||
raise ArgumentError.new("account will go below -10") if (@balance - amount) < -10 | ||
return @balance | ||
end | ||
end | ||
|
||
end | ||
end | ||
|
||
test_1 = Bank::MoneyMarketAccount.new(666, 100) | ||
# puts test_1.withdraw_using_check(2) | ||
|
||
|
||
4.times do | ||
# test_1.withdraw_using_check(2) | ||
puts test_1.withdraw_using_check(2) | ||
puts @counter.class | ||
end | ||
|
||
|
||
|
||
# if @counter <= 3 && test_balance < -10 | ||
# return @balance | ||
# raise ArgumentError.new("account will go below -10") if (@balance - amount) < -10 | ||
# elsif @counter <= 3 && test_balance >= -10 | ||
# @balance = @balance - amount | ||
# return @balance | ||
# @counter += 1 | ||
# elsif @counter > 3 && test_balance >= -10 | ||
# @balance = @balance - amount - fee | ||
# @counter += 1 | ||
# elsif @counter > 3 && test_balance < -10 | ||
# raise ArgumentError.new("account will go below -10") if (@balance - amount) < -10 | ||
# return @balance | ||
# end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require_relative './account' | ||
require 'csv' | ||
|
||
module Bank | ||
class SavingsAccount < Bank::Account | ||
def initialize(id, balance) | ||
super(id, balance) | ||
raise ArgumentError.new("balance must be > 10") if balance < 10 | ||
@interest = 0 | ||
end | ||
|
||
def withdraw(amount) | ||
amount += 2 | ||
|
||
if (@balance - amount) < 10 | ||
return @balance | ||
raise ArgumentError.new("account must be > 10") if (@balance - amount) < 10 | ||
else | ||
@balance = @balance - amount | ||
return @balance | ||
end | ||
end | ||
|
||
def add_interest(rate) | ||
raise ArgumentError.new("interest must be > 0") if rate < 0 | ||
|
||
@interest = @balance * (rate / 100) | ||
|
||
@balance = @balance + @interest | ||
return @interest | ||
end | ||
|
||
end | ||
end | ||
|
||
# test_1 = Bank::SavingsAccount.new(666, 10_000) | ||
# puts test_1.withdraw(2) | ||
# puts test_1.add_interest(0.25) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
require 'csv' | ||
require 'minitest/autorun' | ||
require 'minitest/reporters' | ||
require 'minitest/skip_dsl' | ||
require_relative '../lib/account' | ||
|
||
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new | ||
|
||
describe "Wave 1" do | ||
describe "Account#initialize" do | ||
it "Takes an ID and an initial balance" do | ||
|
@@ -18,10 +21,6 @@ | |
end | ||
|
||
it "Raises an ArgumentError when created with a negative balance" do | ||
# Note: we haven't talked about procs yet. You can think | ||
# of them like blocks that sit by themselves. | ||
# This code checks that, when the proc is executed, it | ||
# raises an ArgumentError. | ||
proc { | ||
Bank::Account.new(1337, -100.0) | ||
}.must_raise ArgumentError | ||
|
@@ -61,10 +60,6 @@ | |
withdrawal_amount = 200.0 | ||
account = Bank::Account.new(1337, start_balance) | ||
|
||
# Another proc! This test expects something to be printed | ||
# to the terminal, using 'must_output'. /.+/ is a regular | ||
# expression matching one or more characters - as long as | ||
# anything at all is printed out the test will pass. | ||
proc { | ||
account.withdraw(withdrawal_amount) | ||
}.must_output /.+/ | ||
|
@@ -77,8 +72,6 @@ | |
|
||
updated_balance = account.withdraw(withdrawal_amount) | ||
|
||
# Both the value returned and the balance in the account | ||
# must be un-modified. | ||
updated_balance.must_equal start_balance | ||
account.balance.must_equal start_balance | ||
end | ||
|
@@ -136,36 +129,73 @@ | |
end | ||
end | ||
|
||
# TODO: change 'xdescribe' to 'describe' to run these tests | ||
xdescribe "Wave 2" do | ||
describe "Wave 2" do | ||
describe "Account.all" do | ||
|
||
before do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice use of |
||
@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 | ||
@accounts.must_be_instance_of Array | ||
end | ||
it "Everything in the array is an Account" do | ||
@accounts.each do |account| | ||
account.must_be_instance_of Bank::Account | ||
end | ||
end | ||
it "Number of accounts is correct" do | ||
@accounts.length.must_equal 12 | ||
end | ||
|
||
it "Match elements in file" do | ||
CSV.read('support/accounts.csv') do |line| | ||
counter = 0 | ||
@accounts[counter].id.must_equal line[0].to_i | ||
@accounts[counter].balance.must_equal line[1].to_i | ||
counter += 1 | ||
end | ||
end | ||
end | ||
|
||
it "ID and balance match the first and last element" do | ||
@accounts.first.id.must_equal 1212 | ||
@accounts.first.balance.must_equal 1235667 | ||
|
||
@accounts.last.id.must_equal 15156 | ||
@accounts.last.balance.must_equal 4356772 | ||
end | ||
end | ||
|
||
|
||
describe "Account.find" do | ||
it "Returns an account that exists" do | ||
# TODO: Your test code here! | ||
account = Bank::Account.find(1216) | ||
|
||
account.must_be_instance_of Bank::Account | ||
account.id.must_equal 1216 | ||
account.balance.must_equal 100022 | ||
end | ||
|
||
it "Can find the first account from the CSV" do | ||
# TODO: Your test code here! | ||
account = Bank::Account.find(1212) | ||
|
||
account.must_be_instance_of Bank::Account | ||
account.id.must_equal 1212 | ||
account.balance.must_equal 1235667 | ||
end | ||
|
||
it "Can find the last account from the CSV" do | ||
# TODO: Your test code here! | ||
account = Bank::Account.find(15156) | ||
|
||
account.must_be_instance_of Bank::Account | ||
account.id.must_equal 15156 | ||
account.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(666) | ||
}.must_raise ArgumentError | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this
MoneyMarketAccount
- I would have expected to seeCheckingAccount
in this file (and that's what you appear to have implemented).