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.rb b/account.rb new file mode 100644 index 00000000..3bb004df --- /dev/null +++ b/account.rb @@ -0,0 +1,125 @@ +module Bank + +require 'csv' + + class Account + + # @@min_balance = 0 + + attr_accessor :balance, :account_id, :owner + + def initialize(account_id, balance, datetime_open, owner = nil) + @account_id = account_id + @balance = balance + @withdrawal_fee = 0 + @min_balance = 0 + if balance < @min_balance + raise ArgumentError.new("Cannot start an account with a negative balance.") + end + @datetime_open = DateTime.strptime(datetime_open, "%Y-%m-%d %H:%M:%S %z") + @owner = owner + end + + def self.all + accounts_csv = CSV.read("./support/accounts.csv") + accounts_instances = [] + accounts_csv.each do |row| + accounts_instances.push(Account.new(row[0], row[1].to_i, row[2])) + end + return accounts_instances + end + + def self.find(id) + self.all.find do |line| + line.account_id.to_i == id + end + # new_account = Account.new(matched_id[0], matched_id[1], matched_id[2]) + # puts new_account + end + + def self.everything + account_owners_csv = CSV.read("./support/account_owners.csv") + everything_array = [] + account_owners_csv.each do |line| + each_account = self.find(line[0].to_i) + each_owner = Bank::Owner.find(line[1].to_i) + each_account.assign_owner(each_owner) + everything_array.push(each_account) + end + return everything_array + end + + def withdraw(withdraw_amount) + # returns updated balance + if withdraw_amount > (@balance + @withdrawal_fee) + puts "Not enough money in account" + puts "The current balance is only: #{@balance}" + else + @balance = (@balance - withdraw_amount - @withdrawal_fee) + puts "Updated balance is: #{@balance}" + end + end + + def deposit(deposit_amount) + # returns updated balance + @balance += deposit_amount + puts "Updated balance is: #{@balance}" + end + + def add_interest(rate) + interest = @balance * rate/100 + puts "The interest earned on the account is: $#{interest.to_i}" + @balance = @balance + interest + puts "The new balance is: $#{@balance.to_i}" + end + + + # def assign_owner(owner_hash) + # @owner = owner + # # puts "The owner of this account is #{@owner.first_name} #{@owner.last_name}." + # end + end + + class Owner + + attr_reader :owner_id, :first_name, :last_name, :street, :city, :state, :zip + + def initialize(owner_hash) + @owner_id = owner_hash[:owner_id] + @first_name = owner_hash[:first_name] + @last_name = owner_hash[:last_name] + @street = owner_hash[:street] + @city = owner_hash[:city] + @state = owner_hash[:state] + @zip = owner_hash[:zip] + end + + def self.all + owners_csv = CSV.read("./support/owners.csv") + owners_array = [] + owners_csv.each do |row| + owner_hash = {:owner_id => row[0], :last_name => row[1], :first_name => row[2], :street => row[3], :city => row[4], :state => row[5]} + owners_array.push Owner.new(owner_hash) + end + return owners_array + end + + def self.find(id) + self.all.find do |line| + line.owner_id.to_i == id + end + end + + # def self.everything + # account_owners_csv = CSV.read("./support/account_owners.csv") + # everything_array = [] + # account_owners_csv.each do |line| + # each_owner = self.find(line[1].to_i) + # each_account = Bank::Account.find(line[0].to_i) + # each_owner.account = each_account + # everything_array.push(each_owner) + # end + # return everything_array + # end + end +end diff --git a/bank.rb b/bank.rb new file mode 100644 index 00000000..38008983 --- /dev/null +++ b/bank.rb @@ -0,0 +1,4 @@ +require "./account.rb" +require "./savings_account.rb" +require "./checking_account.rb" +require "./moneymarket_account.rb" diff --git a/bankaccount_w1.rb b/bankaccount_w1.rb index 46fc7983..d7ae7db4 100644 --- a/bankaccount_w1.rb +++ b/bankaccount_w1.rb @@ -4,19 +4,16 @@ module Bank class Account - attr_accessor :balance, :account_id + attr_accessor :balance, :account_id, :owner - def initialize(account_id, balance, datetime_open) #owner = nil) + def initialize(account_id, balance, datetime_open, owner = nil) @account_id = account_id - + @balance = balance if balance < 0 raise ArgumentError.new("Cannot start an account with a negative balance.") end - - @balance = balance - # @owner = owner - @datetime_open = DateTime.strptime(datetime_open, "%Y-%m-%d %H:%M:%S %z") - # @accounts_array = [] + @datetime_open = datetime_open # DateTime.strptime(datetime_open, "%Y-%m-%d %H:%M:%S %z") + @owner = owner end def self.all @@ -29,9 +26,23 @@ def self.all end def self.find(id) - self.all.find do |line| + self.all.find do |line| line.account_id.to_i == id end + # new_account = Account.new(matched_id[0], matched_id[1], matched_id[2]) + # puts new_account + end + + def self.everything + account_owners_csv = CSV.read("./support/account_owners.csv") + everything_array = [] + account_owners_csv.each do |line| + each_account = self.find(line[0].to_i) + each_owner = Bank::Owner.find(line[1].to_i) + each_account.owner = each_owner + everything_array.push(each_account) + end + return everything_array end def withdraw(withdraw_amount) @@ -48,7 +59,6 @@ def withdraw(withdraw_amount) puts "Your updated balance is: #{@balance}" end - end def deposit(deposit_amount) # returns updated balance @@ -58,9 +68,9 @@ def deposit(deposit_amount) # def assign_owner(owner_hash) # @owner = owner - # puts "The owner of this account is #{@owner.first_name} #{@owner.last_name}." + # # puts "The owner of this account is #{@owner.first_name} #{@owner.last_name}." # end - + end class Owner @@ -91,6 +101,18 @@ def self.find(id) line.owner_id.to_i == id end end + + # def self.everything + # account_owners_csv = CSV.read("./support/account_owners.csv") + # everything_array = [] + # account_owners_csv.each do |line| + # each_owner = self.find(line[1].to_i) + # each_account = Bank::Account.find(line[0].to_i) + # each_owner.account = each_account + # everything_array.push(each_owner) + # end + # return everything_array + # end end end diff --git a/checking_account.rb b/checking_account.rb new file mode 100644 index 00000000..8272494a --- /dev/null +++ b/checking_account.rb @@ -0,0 +1,37 @@ +module Bank + +require 'csv' + + class CheckingAccount < Account + + def initialize(account_id, balance, datetime_open, owner = nil) + super + @withdrawal_fee = 1 + @checks_used = 0 + end + + def withdraw_using_check(amount) + if @checks_used >= 3 + @withdrawal_fee = 0 + end + if (@balance - amount - @withdrawal_fee) < -10 + puts "Unable to make this withdrawal" + else + @balance -= (amount + @withdrawal_fee) + puts @withdrawal_fee + puts "The new balance is: #{@balance}" + @checks_used += 1 + end + return @balance + end + + def add_interest(rate) + return "Not a function of this account." + end + + def reset_checks + @checks_used = 0 + @withdrawal_fee = 1 + end + end +end diff --git a/moneymarket_account.rb b/moneymarket_account.rb new file mode 100644 index 00000000..60f7d4df --- /dev/null +++ b/moneymarket_account.rb @@ -0,0 +1,54 @@ +module Bank + +require 'csv' + + class MoneyMarket < Account + + # @@min_balance = 10000 + + attr_accessor :transactions + + def initialize(account_id, balance, datetime_open, owner = nil) + super + #max of 6 transactions (deposit or withdrawal) + #initial balance !< $10000 - will raise ArgumentError + @min_balance = 10000 + @transactions = 0 + @min_balance_fee = 100 + end + + # def transactions() + # @transactions += 1 + # end + + def withdraw(withdraw_amount) + if (@balance - withdraw_amount) < @min_balance && @transactions < 6 + @balance -= (@min_balance_fee) + puts "A $100 fee is imposed for going below $10000" + end + if @transactions >= 6 + puts "No more transactions left available this month." + else + @balance -= withdraw_amount + @transactions += 1 + end + end + + def deposit(deposit_amount) + if @balance >= @@min_balance + if @transactions < 6 + super + @transactions += 1 + else + puts "No more transactions left available this month." + end + else + super + end + end + + def reset_transactions + @transactions = 0 + end + end +end diff --git a/savings_account.rb b/savings_account.rb new file mode 100644 index 00000000..c5522c28 --- /dev/null +++ b/savings_account.rb @@ -0,0 +1,15 @@ +module Bank + +require 'csv' + + class SavingsAccount < Account + + # @@min_balance = 10 + + def initialize(account_id, balance, datetime_open, owner = nil) + super + @min_balance = 10 + @withdrawal_fee = 2 + end + end +end