Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions lib/account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
require 'csv'

module Bank
class Account
attr_accessor :id, :balance, :account_info

@@all_accounts = {}
@@valid_ids = []

def initialize(id, balance)
raise ArgumentError, 'Cannot deposit negative number' if balance.to_f < 0
@id = id
@balance = balance

@account_info = CSV.open('support/accounts.csv')

@account_info.each do |line|
@@all_accounts[line.first] = line.drop(1)
next unless line.first == @id
@balance = line[1].to_i
@opendate = DateTime.parse(line[2].to_s)
end
end

def self.all
@account_info = CSV.open('support/accounts.csv')
@accounts_array = []
@account_info.each do |line|
@account_instance = new(line[0], line[1])
@accounts_array << @account_instance
end
@accounts_array
end

def self.find(_id)
@account_info = CSV.open('support/accounts.csv')
@found_account = []

@account_info.each do |line|
@@valid_ids << line[0]
next unless line.first == _id
@found_account = line
end

raise ArgumentError, 'Please enter valid ID' unless @@valid_ids.include?(_id)

@found_account
end

def withdraw(amt)
raise ArgumentError, 'Cannot withdraw negative number' if amt < 0
if amt > @balance
puts 'You cannot withdraw more than your account balance'
else
@balance -= amt
end
@balance
end

def deposit(amt)
raise ArgumentError, 'Cannot deposit negative number' if amt < 0
@balance += amt
@balance
end
end
end

# Cust1 = Bank::Account.new('1212', '1235667')
# Cust2 = Bank::Account.new('1213', '66367')
# Cust3 = Bank::Account.new('1214', '9876890')
# Cust4 = Bank::Account.new('1215', '919191')
#
# print Cust2.balance
#
# puts Bank::Account.all
# puts Bank::Account.find('1213')
# puts Bank::Account.find('test')
# puts Bank::Account.find('1213')
44 changes: 44 additions & 0 deletions lib/checking_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require_relative './account'
require 'csv'

module Bank
class CheckingAccount < Account
attr_accessor :id, :balance

def initialize(_id, _balance)
@id = _id
@balance = _balance
@used_checks = 0
super(_id, _balance)
raise ArgumentError, 'Must deposit at least $10' if @balance.to_f < 9
end

def withdraw(amt)
super(amt)
if @balance.to_f < 0
puts 'Account balance cannot drop below $10'
else
@balance -= 1
end
end

def withdraw_using_check(amt)
raise ArgumentError, 'Cannot withdraw negative number' if amt < 0
if @balance - amt < -10
puts 'Your account balance cannot drop below -$10'
elsif @used_checks >= 3
puts 'You will be charged a transaction fee'
@balance -= amt + 2
@used_checks += 1
else
@balance -= amt
@used_checks += 1
end
@balance
end

def reset_checks
@used_checks = 0
end
end
end
29 changes: 29 additions & 0 deletions lib/savings_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require_relative './account'
require 'csv'

module Bank
class SavingsAccount < Account
attr_accessor :id, :balance

def initialize(_id, _balance)
@id = _id
@balance = _balance
super(_id, _balance)
raise ArgumentError, 'Must deposit at least $10' if @balance.to_f < 9
end

def withdraw(amt)
super(amt)
if @balance.to_f < 9
puts 'Account balance cannot drop below $10'
else
@balance -= 2
end
end

def add_interest(rate)
raise ArgumentError, 'Interest rate must be positive' if rate < 0
@balance *= (1 + (rate.to_f / 100))
end
end
end
Loading