diff --git a/Owner.rb b/Owner.rb new file mode 100644 index 00000000..a38b4fcd --- /dev/null +++ b/Owner.rb @@ -0,0 +1,74 @@ +require "./bank.rb" +require "csv" +module Bank + + class Owner + + attr_accessor :id, :last_name, :first_name, :address, :city, :state + + def initialize (info_hash) + @id = info_hash [:id] + @last_name = info_hash [:last_name] + @first_name = info_hash [:first_name] + @address = info_hash [:address] + @city = info_hash [:city] + @state = info_hash [:state] + end + + def self.create_owner (person_array) + person_id = person_array[0].to_i + person_last_name = person_array[1] + person_first_name = person_array[2] + person_address = person_array[3] + person_city = person_array[4] + person_state = person_array[5] + owner = {id: person_id, + last_name: person_last_name, + first_name: person_first_name, + address: person_address, + city: person_city, + state: person_state + } + return Bank::Owner.new(owner) + end + + def self.all + sample = CSV.read("./support/owners.csv") + owners = [] + sample.each do |row| + owner = create_owner(row) + owners.push(owner) + end + return owners + end + + def self.find(id) + sample = CSV.read("./support/owners.csv") + sample.each do |row| + if row[0] == id + return create_owner(row) + end + end + end + + + ## Why isn't this working?? + + def self.owner_account + sample = CSV.read("./support/account_owners.csv") + owner_accounts_array =[] + sample.each do |row| + account = Bank::Account.find(row[0].to_i) + owner = Bank::Owner.find(row[1].to_i) + if account == nil + break + else + account.set_owner(owner) + owner_accounts_array.push(account) + end + return owner_accounts_array + end + end + + end +end 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/bank.rb b/bank.rb index 39c01d35..b72cb53a 100644 --- a/bank.rb +++ b/bank.rb @@ -152,8 +152,8 @@ def self.owner_account sample = CSV.read("./support/account_owners.csv") owner_accounts_array =[] sample.each do |row| - account = Bank::Account.find(row[0]) - owner = self.find(row[1]) + account = Bank::Account.find(row[0].to_i) + owner = Bank::Owner.find(row[1].to_i) if account == nil break else diff --git a/lib/ChackingAccount.rb b/lib/ChackingAccount.rb new file mode 100644 index 00000000..4503f79f --- /dev/null +++ b/lib/ChackingAccount.rb @@ -0,0 +1,33 @@ +module Bank + require "./account.rb" + class CheckingAccount < Account + + attr_accessor :num_checks + + def initialize(id, date) + super + @num_checks = 0 + end + + def withdraw(amount) + super(amount, 100, 0) + end + + def withdraw_using_check(amount) + if @balace < (amount - 100) # $10 in cents + raise ArgumentError.new("The account can't go into overdraft up to -$10") + elsif @num_checks > 3 + puts "Since you used more then 3 checks this month- you'll be charged with $2 fee" + @balance = @balance - 200 - amount + @num_checks += 1 + else + @balance = @balance - amount + end + puts " Your current balance is $#{@balace/100}" + end + + def reset_checks + @num_checks = 0 + end + end +end diff --git a/lib/MoneyMarketAccount.rb b/lib/MoneyMarketAccount.rb new file mode 100644 index 00000000..6ea4962e --- /dev/null +++ b/lib/MoneyMarketAccount.rb @@ -0,0 +1,93 @@ +module Bank + require "./account.rb" + + class MoneyMarketAccount < Account + + attr_accessor :num_transactions + + def initialize(id, date) + super + @num_transactions = 0 + end + + def initial_balance + super(1000000) + end + + def withdraw(amount) + if check_transactions && check_min_balance + puts "Your balance was $#{@balance/100}" + @balance -= amount + @num_transactions +=1 + puts "after you withdrew $#{amount/100}, your currecnt balance is $#{@balance/100}" + if @balance < 1000000 + puts "You can't do more transactions until the balance will increase to $10,000" + puts "and you've been charged with $100 fee" + @balance -= 100 + end + else + if !check_transactions + puts "Sorry you can't withdraw money since you've reached 6 transactions this month" + else + puts "you have $#{@balance/100} only in your account. Please deposit at least $#{1000000 - @balance}" + puts "It won't count as one of youe monthly transactions" + end + end + end + + #check if the costumer want to depit monet to reach the minimum balance + def reach_min_balance(amount) + if (@balance < 1000000) && (amount > (1000000 - @balance)) + return true + else + return false + end + end + + def add_interest(rate) + super(rate) + end + + def reset_transactions + @num_transactions = 0 + end + + def deposit(amount) + if check_min_balance ## check the balance is greater than 10,000$ + if !check_transactions #check that the num of trasactions are less then 7 & + puts "You've reached 6 trasactions this months. You can't deposit money" + else + super(amount) + @num_transactions += 1 + end + elsif reach_min_balance(amount) #if there is less then 10,000 in the acoount- but the deposit is greater than the gap - + super(amount) #doesn't count as a trasaction. + else + super(amount) + @num_transactions += 1 + end + end + + def check_transactions + if @num_transactions < 7 + return true + else + return false + end + end + + def show_balance + puts "Your current balance is $#{@balance/100}" + end + + def check_min_balance + if @balance < 1000000 + return false + else + return true + end + end + + + end +end diff --git a/lib/Owner.rb b/lib/Owner.rb new file mode 100644 index 00000000..ef02196b --- /dev/null +++ b/lib/Owner.rb @@ -0,0 +1,73 @@ +module Bank + require "./account.rb" + + class Owner + + attr_accessor :id, :last_name, :first_name, :address, :city, :state + + def initialize (info_hash) + @id = info_hash [:id] + @last_name = info_hash [:last_name] + @first_name = info_hash [:first_name] + @address = info_hash [:address] + @city = info_hash [:city] + @state = info_hash [:state] + end + + def self.create_owner (person_array) + person_id = person_array[0].to_i + person_last_name = person_array[1] + person_first_name = person_array[2] + person_address = person_array[3] + person_city = person_array[4] + person_state = person_array[5] + owner = {id: person_id, + last_name: person_last_name, + first_name: person_first_name, + address: person_address, + city: person_city, + state: person_state + } + return Bank::Owner.new(owner) + end + + def self.all + sample = CSV.read("./support/owners.csv") + owners = [] + sample.each do |row| + owner = create_owner(row) + owners.push(owner) + end + return owners + end + + def self.find(id) + sample = CSV.read("./support/owners.csv") + sample.each do |row| + if row[0] == id + return create_owner(row) + end + end + end + + + ## Why isn't this working?? + + def self.owner_account + sample = CSV.read("./support/account_owners.csv") + owner_accounts_array =[] + sample.each do |row| + account = Bank::Account.find(row[0].to_i) + owner = Bank::Owner.find(row[1].to_i) + if account == nil + break + else + account.set_owner(owner) + owner_accounts_array.push(account) + end + return owner_accounts_array + end + end + + end +end diff --git a/lib/account.rb b/lib/account.rb new file mode 100644 index 00000000..e31ebdea --- /dev/null +++ b/lib/account.rb @@ -0,0 +1,106 @@ +module Bank + + class Account + @@count_accounts = 0 + + attr_accessor :id, :balance, :date, :owner + + def initialize(id, date) + @balance = initial_balance + @id = id + @date = date + @@count_accounts += 1 + @owner = nil + end + + def set_owner(owner) + @owner = owner + end + + #get a row from index and return a new account + def self.create_account(person_array) + account_id = person_array[0].to_i + account_balance = person_array[1].to_i + account_date = person_array[2] + return Bank::Account.new(account_id, account_balance, account_date) + end + + #read the CSV file and row by row use the "creat account" method to create accounts and + #push it to an array - and return it + def self.all + sample = CSV.read("./support/accounts.csv") + accounts = [] + sample.each do |row| + account = create_account(row) + accounts.push(account) + end + return accounts + end + + def self.find(id) + sample = CSV.read("./support/accounts.csv") + sample.find do |row| + if row[0].to_i == id + return create_account(row) + end + end + end + + + + + def initial_balance(min_balance = 0) + puts "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$".center(70) + puts + puts "$$$$$$$ Welcome to Tammy's Bank! $$$$$$$".center(70) + puts "How much $$$ would you like to deposit to your new account ?".center(70) + puts + puts "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$".center(70) + money = gets.chomp.to_i + if money < 0 + raise ArgumentError.new("You can't deposit negativ amount of money") + money = 0 + elsif money < min_balance.to_i + raise ArgumentError.new("You can't deposit less then $#{min_balance/100}") + money = 0 + end + return money + end + + def withdraw (amount, fee = 0, min_balance = 0) + if amount > (@balance + fee + min_balance) #since there is a minimum of $10 balance + $2 fee + puts " Your current balance is $#{@balance/100}." + puts "You can't go below of #{min_balance} in your account. Pay attention for $#{fee/100} fee" + return @balance + else + puts "There is a $#{fee/100} fee for this withdraw. Would you like to continue?" + answer = gets.chomp.downcase + if answer == "y" || answer == "yes" + puts "You had $#{@balance/100}" + @balance = @balance - amount - fee + puts "Your update account balance is $#{@balance/100}" + end + end + end + + def deposit (amount) + if amount < 0 + puts "If you want to withdraw, that's a different method :)" + else + @balance = @balance + amount + puts "After you deposited $#{amount/100}" + puts "The update account balance is $#{@balance/100}" + end + end + + + def add_interest(rate) + interest = @balance * rate + @balance = @balance + interest + return interest + end + + + + end +end diff --git a/lib/bank.rb b/lib/bank.rb new file mode 100644 index 00000000..afb88586 --- /dev/null +++ b/lib/bank.rb @@ -0,0 +1,28 @@ +module Bank +require "./saving_account.rb" +require "./chackingaccount.rb" +require "./account.rb" +require "./MoneyMarketAccount.rb" +# +# d = Bank::CheckingAccount.new(100000, 11-12-15) +# +# d.withdraw(500) +# +# p = Bank::SavingsAccount.new(123, 11-12-12) +# +# p.withdraw(1000) + +# r = Bank::MoneyMarketAccount.new(12, 12-12-12) +# +# r.withdraw(1000) +# +# r.withdraw(500) +# +# 10.times do +# (r.withdraw(200)) +# end + + + + +end diff --git a/lib/saving_account.rb b/lib/saving_account.rb new file mode 100644 index 00000000..d56576fd --- /dev/null +++ b/lib/saving_account.rb @@ -0,0 +1,25 @@ +module Bank + require "./account.rb" + class SavingsAccount < Account + + + def initialize(id, date) + super + end + + def initial_balance + super(1000) + end + + + def withdraw(amount) + super(amount, 200, 1000) #in cents + end + + + def add_interest(rate) + super(rate) + end + + end +end diff --git a/saving_account.rb b/saving_account.rb new file mode 100644 index 00000000..6db5ac9b --- /dev/null +++ b/saving_account.rb @@ -0,0 +1,24 @@ +require "./bank.rb" + +class SavingsAccount < Account + + def initialize (id, balance, date) + super (id, initial_balance, date) + end + + def initial_balance + puts "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$".center(70) + puts + puts "$$$$$$$ Welcome to Tammy's Bank! $$$$$$$".center(70) + puts "How much $$$ would you like to deposit to your new account ?".center(70) + puts + puts "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$".center(70) + money = gets.chomp.to_i + if money < 10 + raise ArgumentError.new("You can't deposit negativ amount of money") + money = 0 + elsif money == 0 + puts "You have 30 days to deposit at least $10" + end + return money + end