diff --git a/README.md b/README.md index 25fd4d2a..b6e5c7b2 100644 --- a/README.md +++ b/README.md @@ -73,24 +73,23 @@ Create an `Account` class which should have the following functionality: **Account ID** - (Fixnum) a unique identifier corresponding to an account **Owner ID** - (Fixnum) a unique identifier corresponding to an owner - diff --git a/account_class.rb b/account_class.rb new file mode 100644 index 00000000..95f1cda7 --- /dev/null +++ b/account_class.rb @@ -0,0 +1,58 @@ +module Bank + class Account + attr_reader :account_id, :balance, :time + #allows account to be created with an initial balance as a parameter + def initialize(balance) + #creates ID# + @account_id = generate_id + @balance = balance_restriction(balance) + @time = account_opened + + + #prevents initial balance from being a negative amount + #if balance < 0 + # raise Exception.new, "No negative balance allowed! Live within your means!" + #end + end + + #method to create an ID number, not unique number yet + + def balance_restriction (balance) + if balance < 0 + raise Exception.new("No negative balance allowed! Live within your means!") + else + return balance + end + end + + def generate_id + user_id = rand(1000..9000) + return user_id + end + + def account_opened + time = Time.new + return time + end + + #method for withdrawal + def withdrawal(subtract_money) + if (@balance - subtract_money) >= 0 + @balance = (@balance - subtract_money) + #returns updated account balance + return @balance + else + #rejects negative balance, and returns current balance + puts "Your withdrawal was rejected. You only have #{@balance} dollars." + puts "You are not allowed to overdraft. Try a smaller withdrawal." + end + end + + #method for deposit + def deposit(add_money) + @balance = (@balance + add_money) + #returns updated account balance + return @balance + end + end +end diff --git a/bankaccount_wave3.rb b/bankaccount_wave3.rb new file mode 100644 index 00000000..b3a9ea98 --- /dev/null +++ b/bankaccount_wave3.rb @@ -0,0 +1,3 @@ +require "./account_class.rb" +require "./checking_class.rb" +require "./savings_class.rb" diff --git a/checking_class.rb b/checking_class.rb new file mode 100644 index 00000000..7b9639ba --- /dev/null +++ b/checking_class.rb @@ -0,0 +1,55 @@ +module Bank + class Checking < Account + + def initialize(balance) + super(balance) + @check_count = 0 + end + + #method for withdrawal + def withdrawal(subtract_money) + if (@balance - subtract_money - 1) >= 0 + @balance = (@balance - subtract_money - 1) + #returns updated account balance + return @balance + else + #rejects negative balance, and returns current balance + puts "Your withdrawal was rejected. You only have #{@balance} dollars." + puts "You are not allowed to overdraft. Try a smaller withdrawal." + end + end + + + def withdraw_using_check(subtract_money) + if (@balance - subtract_money.abs) >= -10 + @balance = (@balance - subtract_money.abs) + #returns updated account balance with a transaction fee of $1 removed from balance + #check_count = 0 + @check_count += 1 + if @check_count > 3 + transaction_fee = 2 + @balance = @balance - transaction_fee + end + puts @check_count + return @balance + else + #rejects negative balance, and returns current balance + puts "Your withdrawal was rejected. You only have #{@balance} dollars." + puts "You are not allowed to overdraft more than $10. Try a smaller withdrawal." + end + end + + def reset_checks + @check_count = 0 + end + end +end + +#Create a `CheckingAccount` class which should inherit behavior from the `Account` class. It should include the following updated functionality: +#- Updated withdrawal functionality: +# - Each withdrawal 'transaction' incurs a fee of $1 that is taken out of the balance. Returns the updated account balance. +# - Does not allow the account to go negative. Will output a warning message and return the original un-modified balance. +#- `#withdraw_using_check(amount)`: The input amount gets taken out of the account as a result of a check withdrawal. Returns the updated account balance. +# - Allows the account to go into overdraft up to -$10 but not any lower +# - The user is allowed three free check uses in one month, but any subsequent use adds a $2 transaction fee +#- `#reset_checks`: Resets the number of checks used to zero diff --git a/savings_class.rb b/savings_class.rb new file mode 100644 index 00000000..36d194ad --- /dev/null +++ b/savings_class.rb @@ -0,0 +1,47 @@ +module Bank + class Savings < Account + def initialize(balance) + super(balance) + @transaction_fee = 2 + end + + def balance_restriction (balance) + if balance < 10 + raise Exception.new("Your balance is not allowed to go below $10!") + else + return balance + end + end + + def interest(rate) + previous_balance = @balance + @balance = previous_balance + (@balance * rate)/100 + interest_earned = @balance - previous_balance + return "Your balance is #{@balance} dollars and you have earned #{interest_earned} dollars in interest" + end + + def withdrawal(subtract_money) + if (@balance - subtract_money - @transaction_fee) >= 10 + @balance = (@balance - subtract_money - @transaction_fee) + #returns updated account balance + return @balance + else + #rejects negative balance, and returns current balance + puts "Your withdrawal was rejected. You only have #{@balance} dollars." + puts "You are not allowed to overdraft. Try a smaller withdrawal." + end + end + end +end + +#Create a `SavingsAccount` class which should inherit behavior from the `Account` class. It should include the following updated functionality: +#- The initial balance cannot be less than $10. If it is, this will `raise` an `ArgumentError` +#- Updated withdrawal functionality: +# - Each withdrawal 'transaction' incurs a fee of $2 that is taken out of the balance. +# - Does not allow the account to go below the $10 minimum balance - Will output a warning message and return the original un-modified balance +# +#It should include the following new method: +#- `#add_interest(rate)`: Calculate the interest on the balance and add the interest to the balance. Return the **interest** that was calculated and added to the balance (not the updated balance). +# - Input rate is assumed to be a percentage (i.e. 0.25). +# - The formula for calculating interest is `balance * rate/100` +# - Example: If the interest rate is 0.25% and the balance is $10,000, then the interest that is returned is $25 and the new balance becomes $10,025.