diff --git a/account.rb b/account.rb new file mode 100644 index 00000000..8ba15a44 --- /dev/null +++ b/account.rb @@ -0,0 +1,57 @@ +require_relative 'owner' +require 'csv' + +module Bank + class Account + attr_reader :balance + + def initialize (id, balance, open_date = Time.now) + @id = id + @min_balance = 0 + if (balance/100) >= @min_balance + @balance = balance/100 + else + raise ArgumentError, "You can't start an account with a negative balance." + end + + @date_opened = open_date + #@owner = Bank::Owner.new(@id) + @withdrawal_fee = 0 + end + + # self.all - returns a collection of Account instances, representing all of the Accounts described in the CSV. See below for the CSV file specifications + def self.all + accounts = {} + # /Documents/Ada/Week3/BankAccounts/support/accounts.csv + CSV.read('support/accounts.csv').each do |line| + accounts[line[0]] = self.new(line[0], line[1].to_i, line[2]) + end + return accounts + end + + # self.find(id) - returns an instance of Account where the value of the id field in the CSV matches the passed parameter + def self.find (id) + accounts = self.all + return accounts[id] + end + + def withdraw (amount) + if (@balance - amount - @withdrawal_fee) >= @min_balance + @balance = @balance - amount - @withdrawal_fee + return "$#{ @balance }" + else + puts "Sorry. You're transaction cannot be completed because it will take you below the required minimum amount of $#{ @min_balance }." + return "$#{ @balance }" + end + end + + # Does not allow the account to go negative. Will output a warning message and return the original un-modified balance. + #$1 checking account withdrawal fee taken out of the balance. Returns the updated account balance. + + def deposit (amount) + @balance = @balance + amount + return "$#{ @balance }" + end + + end +end diff --git a/account_w1_optionals.rb b/account_w1_optionals.rb new file mode 100644 index 00000000..f54ad1a6 --- /dev/null +++ b/account_w1_optionals.rb @@ -0,0 +1,36 @@ +require_relative 'owner' + +module Bank + class Account + attr_reader :balance + + def initialize (init_balance) + @id = rand(100..999) + + if init_balance > 0 + @balance = init_balance + else + raise ArgumentError, "You can't start an account with a negative balance" + end + @owner = Bank::Owner.new(@id) + end + + def withdraw (amt_withdrawn) + if @balance - amt_withdrawn > 0 + @balance = @balance - amt_withdrawn + return "$#{@balance}" + else + puts "Sorry, but you do not have that amount of money in your account." + return "$#{@balance}" + end + end + + def deposit (amt_deposited) + @balance = @balance + amt_deposited + return "$#{@balance}" + end + + end +end + +p = Bank::Account.new(500) diff --git a/account_w2_opt_merge_not_finished.rb b/account_w2_opt_merge_not_finished.rb new file mode 100644 index 00000000..ddd15d73 --- /dev/null +++ b/account_w2_opt_merge_not_finished.rb @@ -0,0 +1,76 @@ +require_relative 'owner' +require 'csv' + +module Bank + class Account + attr_reader :balance + + def initialize (id, balance, open_date = Time.now) + @id = id + + if balance >= 0 + @balance = balance + else + raise ArgumentError, "You can't start an account with a negative balance" + end + + @date_opened = open_date + + # @owner = Bank::Owner.find(@id) + end + +# need to figure out how to merge the one array into the another array + def self.all_with_owners + acct_list_w_owners = [] + CSV.read('support/account_owners.csv').each do |line| + a = Bank::Account.find(line[0].to_i) + o = Bank::Owner.find(line[1].to_i) + puts " " + puts o + # a.add_owner(0) + # acct_list_w_owners << a + end + end + + def add_owner + + end + + # self.all - returns a collection of Account instances, representing all of the Accounts described in the CSV. See below for the CSV file specifications + def self.all + accounts = {} + # /Documents/Ada/Week3/BankAccounts/support/accounts.csv + CSV.read('support/accounts.csv').each do |line| + accounts[line[0].to_i] = self.new(line[0].to_i, line[1].to_i, line[2]) + end + return accounts + end + + # self.find(id) - returns an instance of Account where the value of the id field in the CSV matches the passed parameter + def self.find (id) + accounts = self.all + return accounts[id] + end + + def withdraw (amt_withdrawn) + if @balance - amt_withdrawn > 0 + @balance = @balance - amt_withdrawn + return "$#{@balance/100}" + else + puts "Sorry, but you do not have that amount of money in your account." + return "$#{@balance/100}" + end + end + + def deposit (amt_deposited) + @balance = @balance + amt_deposited + return "$#{@balance/100}" + end + + end +end + + +# ID - (Fixnum) a unique identifier for that Account +# Balance - (Fixnum) the account balance amount, in cents (i.e., 150 would be $1.50) +# OpenDate - (Datetime) when the account was opened diff --git a/checking_acct.rb b/checking_acct.rb new file mode 100644 index 00000000..1f6467e9 --- /dev/null +++ b/checking_acct.rb @@ -0,0 +1,62 @@ +require_relative 'account.rb' + +module Bank + class CheckingAccount < Account + attr_reader :num_of_checks_used, :balance, :id + + def initialize (id, balance, open_date = Time.now ) + super(id, balance, open_date) + @num_of_checks_used = 0 + @withdrawal_fee = 1 + @min_balance_check = -10 + end + + def withdraw (amount) + super (amount) + # # Does not allow the account to go negative. Will output a warning message and return the original un-modified balance. + # #$1 checking account withdrawal fee taken out of the balance. Returns the updated account balance. + # if (@balance - amount - @withdrawal_fee) >= 0 + # @balance = @balance - amount - @withdrawal_fee + # return "$#{@balance}" + # else + # puts "Sorry, but you do not have that amount of money in your account." + # return "$#{@balance}" + # end + end + + def charge_fee_for_check? + @num_of_checks_used >= 3 + end + + #reset_checks: Resets the number of checks used to zero + def reset_checks + @num_of_checks_used = 0 + end + + # 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. + def withdraw_using_check (amount) + @check_withdrawel_fee = 2 #$2 transaction fee + if charge_fee_for_check? + if (@balance - amount - @check_withdrawel_fee) >= @min_balance_check + @num_of_checks_used += 1 + @balance = @balance - amount - @check_withdrawel_fee + return "$#{ @balance }" + else + puts "Sorry, but you do not have that amount of money in your account." + return "$#{ @balance }" + end + + else + + if (@balance - amount) >= @min_balance_check + @num_of_checks_used += 1 + @balance = @balance - amount + return "$#{ @balance }" + else + puts "Sorry, but you do not have that amount of money in your account." + return "$#{ @balance }" + end + end + end + end +end diff --git a/money_market_acct.rb b/money_market_acct.rb new file mode 100644 index 00000000..3daacd05 --- /dev/null +++ b/money_market_acct.rb @@ -0,0 +1,89 @@ +require_relative 'account.rb' + +module Bank + class MoneyMarketAccount < Account + attr_reader :id, :balance + + def initialize (id, balance, open_date = Time.now) + super(id, balance, open_date) + + @min_balance = 10000 + puts @min_balance + if (balance/100) >= @min_balance + @balance = balance/100 + else + raise ArgumentError, "You need at least $#{ @min_balance } to open a money market account." + end + + @below_min_balance_fee = 100 + @num_of_transactions = 0 + @max_num_transactions = 6 + end + + def reset_transactions + @num_of_transactions = 0 + end + + def transaction_limit_reached? + @num_of_transactions >= @max_num_transactions + end + + def charge_fee_for_overdraft? (amount) + (@balance - amount) < @min_balance + end + + def account_below_min_balance? + @balance < @min_balance + end + + def withdraw (amount) + if transaction_limit_reached? + puts "Sorry you have reached your maximum number of transactions/month of #{ @max_num_transactions }" + return "$#{ @balance }" + else + + if account_below_min_balance? + return "Sorry, you can only deposit money at this point until your acount balance reaches the minimum acount balance of $#{ @min_balance }." + else + + if charge_fee_for_overdraft?(amount) + @balance = @balance - amount - @below_min_balance_fee + @num_of_transactions += 1 + return "$#{ @balance }" + else + @balance = @balance - amount + @num_of_transactions += 1 + return "$#{ @balance }" + end + end + end + end + + def deposit (amount) + if account_below_min_balance? + if (@balance + amount) >= @min_balance + super(amount) + else + return "Sorry, you're account is frozen until you deposit enough money to reach the required min account balance of $#{ @min_balance }." + end + else + if transaction_limit_reached? + puts "Sorry you have reached your maximum number of transactions/month of #{ @max_num_transactions }" + return "$#{ @balance }" + else + @num_of_transactions += 1 + super(amount) + end + end + end + + def add_interest(rate) + interest_rate = rate / 100 + interest_on_balance = @balance * interest_rate + @balance = @balance + interest_on_balance + return "$" + interest_on_balance.to_s + # 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. + end + + end +end diff --git a/owner.rb b/owner.rb new file mode 100644 index 00000000..a2a5c08d --- /dev/null +++ b/owner.rb @@ -0,0 +1,45 @@ +require 'csv' + +module Bank + class Owner + + def initialize (id, last_name, first_name, street_address, city, state) + @id = id + # puts "Enter last name:" + @last_name = last_name + # puts "Enter first name:" + @first_name = first_name + # puts "Enter street address" + @street_address = street_address + # puts "Enter city" + @city = city + # puts "Enter state" + @state = state + end + + # self.all - returns a collection of Owner instances, representing all of the Owners described in the CSV. See below for the CSV file specifications + def self.all + owners = {} + CSV.read('support/owners.csv').each do |line| + owners[line[0].to_i] = self.new(line[0].to_i, line[1], line[2], line[3], line[4], line[5]) + end + return owners + end + + # self.find(id) - returns an instance of Owner where the value of the id field in the CSV matches the passed parameter + def self.find (id) + owners = self.all + return owners[id] + end + end +end + + + # Bank::Owner + # The data, in order in the CSV, consists of: + # ID - (Fixnum) a unique identifier for that Owner + # Last Name - (String) the owner's last name + # First Name - (String) the owner's first name + # Street Addess - (String) the owner's street address + # City - (String) the owner's city + # State - (String) the owner's state diff --git a/savings_acct.rb b/savings_acct.rb new file mode 100644 index 00000000..6884da4c --- /dev/null +++ b/savings_acct.rb @@ -0,0 +1,37 @@ +require_relative 'account.rb' + +module Bank + class SavingsAccount < Account + attr_reader :balance, :id + + def initialize (id, balance, open_date = Time.now) + super(id, balance, open_date) + # The initial balance cannot be less than $10. If it is, this will raise an ArgumentError + @min_balance = 10 + if (balance/100) >= @min_balance + @balance = balance/100 + else + raise ArgumentError, "You need at least $#{ @min_balance } to open a savings account." + end + + @withdrawal_fee = 2 #$2 withdrawal fee for saving acct + + end + + def withdraw (amount) + super(amount) + end + + def add_interest(rate) + #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 + interest_rate = rate / 100 + interest_on_balance = @balance * interest_rate + @balance = @balance + interest_on_balance + return "$" + interest_on_balance.to_s + # 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. + end + + end +end