-
Notifications
You must be signed in to change notification settings - Fork 38
Bank Account #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Bank Account #32
Changes from all commits
ad56ad4
c1499be
ea721dc
f0cfdac
83f1639
b1fdd0f
acf6fa0
9c0b3b7
39554cd
4787765
cd7190d
6eb336c
8e13e8d
5819ab3
3d67d3b
e0ea43b
1c1022d
f0ecccf
5d02ae6
7af41f6
085dc8a
6f15881
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| require 'csv' | ||
| require 'date' | ||
|
|
||
| module Bank | ||
| class Account | ||
| # Allows access to the current balance of an account at any time. | ||
| attr_accessor :balance, :owner | ||
| # Only allow reader on unique account id, and opendate | ||
| attr_reader :id, :open_date | ||
|
|
||
| # Constructs a new Account object | ||
| # Give a default value, in case the Owner class object is not passed | ||
| # Assumes passed parameters are formated in their correct data type. | ||
| def initialize id, balance, open_date, owner = nil | ||
| # error handling for initial negative balance | ||
| if balance >= 0 | ||
| @balance = balance | ||
| else | ||
| raise ArgumentError.new "Inital balance cannot be a negetive value" | ||
| end | ||
|
|
||
| @id = id | ||
| @open_date = DateTime.parse(open_date) | ||
|
|
||
| # Assumes that required csv file is accesible | ||
| CSV.read("support/account_owners.csv").each do |line| | ||
| if line[0].to_i == @id | ||
| @owner = Bank::Owner.find(line[1].to_i) | ||
| end | ||
| end | ||
|
|
||
| if owner.class == Bank::Owner | ||
| @owner = owner | ||
| else | ||
| # Default instance of the Owner class initialized with empty hash | ||
| @owner = Bank::Owner.new({}) | ||
| end | ||
| end | ||
|
|
||
| # Method that returns a collection of Account instances, from data read in CSV | ||
| def self.all | ||
| all_accounts_array= [] | ||
|
|
||
| CSV.read("support/accounts.csv").each do |line| | ||
| all_accounts_array << Bank::Account.new( line[0].to_i, line[1].to_i, line[2] ) | ||
| end | ||
| return all_accounts_array | ||
| end | ||
|
|
||
| # Method that returns an instance of an Account class, where the value of the id field in the CSV matches the passed parameter | ||
| def self.find(id) | ||
| raise ArgumentError.new ("Account id must be a positive integer value") if ( id.class != Integer || id < 1 ) | ||
|
|
||
| CSV.read("support/accounts.csv").each do |line| | ||
| if line[0].to_i == id | ||
| account = Bank::Account.new( line[0].to_i, line[1].to_i, line[2]) | ||
| return account | ||
| end | ||
| end | ||
| raise ArgumentError.new "Account id does not exist in the database" | ||
| end | ||
|
|
||
|
|
||
| # Method that overwrites existing empty @owner instance variable | ||
| def update_owner_data(owner_hash) | ||
| # Only overwrite if initially not added to account at the time of initializing account object | ||
| if @owner.id == 0 | ||
| @owner = Bank::Owner.new(owner_hash) | ||
| end | ||
| end | ||
|
|
||
| # Method that handles withdraw | ||
| def withdraw(withdraw_amount) | ||
| # Error handling for insufficient funds for a withdraw | ||
| raise ArgumentError.new ("Withdraw amount must be a positive numericla value") if ( !(withdraw_amount.class == Integer || withdraw_amount.class == Float) || withdraw_amount < 0 ) | ||
| # Insufficient funds | ||
| if @balance < withdraw_amount | ||
| puts "You do not have sufficient funds to withdraw the entered amount" | ||
| # Negative withdraw amount, invalid | ||
| elsif withdraw_amount < 0 | ||
| raise ArgumentError.new "Withdraw amount cannot be a negetive value" | ||
| # Allow withdraw and update the balance | ||
| else | ||
| @balance -= withdraw_amount | ||
| end | ||
| return @balance | ||
| end | ||
|
|
||
| # Method that handles deposits | ||
| def deposit(money_amount) | ||
| # Negative deposit amount, invalid | ||
| if money_amount < 0 | ||
| raise ArgumentError.new "Deposit amount cannot be a negetive value" | ||
| else | ||
| @balance += money_amount | ||
| return @balance | ||
| end | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| require 'csv' | ||
| require_relative 'account' | ||
|
|
||
| module Bank | ||
| class CheckingAccount < Bank::Account | ||
| attr_accessor :new_month | ||
| attr_reader :num_checks_used | ||
|
|
||
| def initialize(id, balance, open_date) | ||
| super(id, balance, open_date) | ||
| @num_checks_used = 0 | ||
| @new_month = false | ||
| end | ||
|
|
||
| # Method that handles withdraw | ||
| def withdraw(withdraw_amount) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't you be able to use |
||
| # raise error for erroneous withdraw value | ||
| raise ArgumentError.new ("Withdraw amount must be a positive numericla value") if ( !(withdraw_amount.class == Integer || withdraw_amount.class == Float) || withdraw_amount < 0 ) | ||
|
|
||
| # Error handling for insufficient funds for a withdraw | ||
| transaction_fee = 100 | ||
|
|
||
| # Balance is insufficient | ||
| if @balance <= (withdraw_amount + transaction_fee) | ||
| puts "The amount you enetered for the withdraw will cause the balance to go below the $0.00" | ||
| # Ok to withdraw, updates the balance | ||
| else | ||
| @balance -= (withdraw_amount + transaction_fee) | ||
| end | ||
| return @balance | ||
| end | ||
|
|
||
| # Method that handles withdraw_using_check | ||
| def withdraw_using_check(withdraw_amount) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should be able to use |
||
| # raise error for non integer withdraw value | ||
| raise ArgumentError.new ("Withdraw amount must be a positive numerical value") if ( !(withdraw_amount.class == Integer || withdraw_amount.class == Float) || withdraw_amount < 0 ) | ||
|
|
||
| # Error handling for insufficient funds for a withdraw | ||
| transaction_fee = 100 | ||
| allotted_negative_balance = -1000 | ||
| check_fee = 0 | ||
|
|
||
| if @num_checks_used >= 3 | ||
| check_fee = 200 | ||
| end | ||
| # Balance is insufficient | ||
| if @balance < (withdraw_amount + transaction_fee + check_fee + allotted_negative_balance) | ||
| puts "The amount you enetered for the withdraw will cause the balance to go over the allotted_negative_balance of -$10.00" | ||
| # Ok to withdraw, update @balance | ||
| else | ||
| @balance -= (withdraw_amount + transaction_fee + check_fee) | ||
| @num_checks_used += 1 | ||
| end | ||
| return @balance | ||
| end | ||
|
|
||
| # Resets the number of checks used to zero | ||
| def reset_checks() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new month functionality wasn't in the requirements and it's making your reset_checks method more cumbersome than it needs to be. |
||
| if @new_month | ||
| @num_checks_used = 0 | ||
| @new_month = false | ||
| end | ||
| end | ||
|
|
||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| require 'csv' | ||
| require_relative 'savings_account' | ||
|
|
||
| module Bank | ||
| class MoneyMarketAccount < Bank::SavingsAccount | ||
| attr_accessor :new_month, :num_transaction | ||
|
|
||
| def initialize(id, balance, open_date) | ||
| raise ArgumentError.new("Your initial balance must be at least $10,000.00") if balance < 1000000 | ||
| super(id, balance, open_date) | ||
| @num_transaction = 0 | ||
| @new_month = false | ||
| end | ||
|
|
||
| def withdraw(withdraw_amount) | ||
| # Raise error for non integer withdraw value | ||
| raise ArgumentError.new ("Withdraw amount must be a positive numerical value") if ( !(withdraw_amount.class == Integer || withdraw_amount.class == Float) || withdraw_amount < 0 ) | ||
| minimum_balance = 1000000 | ||
| penalty_fee = 0 | ||
|
|
||
| # Withdraw allowed only if num_transaction is less than 6 AND balance is above minimum_balance | ||
| if @num_transaction < 6 && @balance >= minimum_balance | ||
| # Withdraw_amount will cause balance to go below minimum_balance | ||
| if @balance < withdraw_amount + minimum_balance | ||
| penalty_fee = 10000 | ||
| end | ||
| @balance -= (withdraw_amount + penalty_fee) | ||
| @num_transaction += 1 | ||
| end | ||
| return @balance | ||
| end | ||
|
|
||
| # Method that handles interest | ||
| def add_interest(rate) | ||
| super(rate) | ||
| end | ||
|
|
||
| # Method that handles deposits | ||
| def deposit(money_amount) | ||
| # Negative deposit amount, invalid | ||
| if money_amount < 0 | ||
| raise ArgumentError.new "Deposit amount cannot be a negetive value" | ||
| elsif @num_transaction < 6 | ||
| if @balance >= 1000000 | ||
| @num_transaction += 1 | ||
| end | ||
| @balance += money_amount | ||
| end | ||
| return @balance | ||
| end | ||
|
|
||
| # Resets the number of checks used to zero | ||
| def reset_transactions() | ||
| if @new_month | ||
| if @num_transaction == 0 | ||
| puts "You haven't made any transactions yet." | ||
| else | ||
| @num_transaction = 0 | ||
| end | ||
| @new_month = false | ||
| end | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| module Bank | ||
| class Owner | ||
| attr_reader :id | ||
| attr_accessor :last_name, :first_name, :street_address, :city, :state | ||
|
|
||
| def initialize owner_hash = {} | ||
| # Assumes that the parameter value is either properly formatted hash or an empty hash | ||
| if owner_hash != {} | ||
| [:id, :last_name, :first_name, :street_address, :city, :state].each do |expected_key| | ||
| if !owner_hash.has_key?(expected_key) | ||
| raise ArgumentError.new "Hey you are missing #{expected_key} information for the owner!" | ||
| end | ||
| end | ||
| @id = owner_hash[:id] | ||
| @first_name = owner_hash[:first_name] | ||
| @last_name = owner_hash[:last_name] | ||
| @street_address = owner_hash[:street_address] | ||
| @city = owner_hash[:city] | ||
| @state = owner_hash[:state] | ||
| else | ||
| @id = 0 | ||
| end | ||
| end | ||
|
|
||
| # Method that returns a collection of Owner instances, from data read in CSV | ||
| def self.all | ||
| all_owners_array= [] | ||
|
|
||
| CSV.read("support/owners.csv").each do |line| | ||
| all_owners_array << Bank::Owner.new( | ||
| { | ||
| id: line[0].to_i, | ||
| last_name: line[1], | ||
| first_name: line[2], | ||
| street_address: line[3], | ||
| city: line[4], | ||
| state: line[5] | ||
| } | ||
| ) | ||
| end | ||
|
|
||
| return all_owners_array | ||
| end | ||
|
|
||
| # Method that returns an instance of an Owner where the value of the id field in the CSV matches the passed parameter | ||
| def self.find(id) | ||
| raise ArgumentError.new ("Account id must be an positive integer value") if ( id.class != Integer || id < 1 ) | ||
|
|
||
| CSV.read("support/owners.csv").each do |line| | ||
| if line[0].to_i == id | ||
| owner = Bank::Owner.new( | ||
| { | ||
| id: line[0].to_i, | ||
| last_name: line[1], | ||
| first_name: line[2], | ||
| street_address: line[3], | ||
| city: line[4], | ||
| state: line[5] | ||
| } | ||
| ) | ||
| return owner | ||
| end | ||
| end | ||
| raise ArgumentError.new "Owner id does not exist in the database" | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| require 'csv' | ||
| require_relative 'account' | ||
|
|
||
| module Bank | ||
| class SavingsAccount < Bank::Account | ||
|
|
||
| def initialize(id, balance, open_date) | ||
| raise ArgumentError.new("Your initial balance must be at least $10.00") if balance < 1000 | ||
| super(id, balance, open_date) | ||
| end | ||
|
|
||
| # Method that handles withdraw | ||
| def withdraw(withdraw_amount) | ||
| # Raise error for non integer withdraw value | ||
| raise ArgumentError.new ("Withdraw amount must be a positive numerical value") if ( !(withdraw_amount.class == Integer || withdraw_amount.class == Float) || withdraw_amount < 0 ) | ||
| # Raises error for negativd withdraw value | ||
| raise ArgumentError.new("Withdraw amount cannot be a negetive value") if withdraw_amount < 0 | ||
|
|
||
| # Error handling for insufficient funds for a withdraw | ||
| transaction_fee = 200 | ||
| minimum_balance = 1000 | ||
|
|
||
| # Balance is insufficient | ||
| if @balance <= (withdraw_amount + minimum_balance + transaction_fee) | ||
| puts "The amount you enetered for the withdraw will cause your balance to go below the $10.00 minimum balance required" | ||
| # Allows withdraw, update @balance | ||
| else | ||
| @balance -= (withdraw_amount + transaction_fee) | ||
| end | ||
| return @balance | ||
| end | ||
|
|
||
| # Method that handles interest | ||
| def add_interest(rate) | ||
| raise ArgumentError.new("The interest rate must be a positive numerical value") if rate <= 0 | ||
| interest = @balance * rate | ||
| @balance += interest | ||
| return interest | ||
| end | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This loop is problematic. It's reading the file and each find is reading another file each time. The
findmethods should probably be scanning through the Arrays provided by.allso you don't have to repeat things over and over again.