|
| 1 | +require 'csv' |
| 2 | +require 'pry' |
| 3 | + |
1 | 4 | module Bank
|
2 | 5 | class Account
|
3 | 6 |
|
4 |
| - attr_reader :balance, :owner |
| 7 | + attr_reader :balance, :id |
| 8 | + attr_accessor :owner |
5 | 9 |
|
6 |
| - def initialize(id, initial_balance) |
7 |
| - #@account_owner = owner.first_name |
8 |
| - @id = id |
9 |
| - @balance = initial_balance |
10 |
| - @owner = nil |
| 10 | + def initialize(id, initial_balance, open_date) |
| 11 | + @id = id.to_i |
| 12 | + @balance = initial_balance.to_i/100.0 |
| 13 | + @open_date = DateTime.strptime(open_date, "%Y-%m-%d %H:%M:%S %z") |
| 14 | + #@owner = nil |
11 | 15 |
|
12 | 16 | # Raises an argument error if the initial balance is less than 0
|
13 |
| - if initial_balance < 0 |
| 17 | + if initial_balance.to_i < 0 |
14 | 18 | raise ArgumentError, "The balance cannot be less than 0."
|
15 | 19 | end
|
16 | 20 | end
|
17 | 21 |
|
| 22 | + # Creates accounts from the accounts.csv file |
| 23 | + def self.all |
| 24 | + @accounts = [] |
| 25 | + accounts_csv = CSV.read("support/accounts.csv") |
| 26 | + accounts_csv.each do |id, balance, date| |
| 27 | + id = Bank::Account.new(id,balance,date) |
| 28 | + @accounts.push(id) |
| 29 | + end |
| 30 | + #puts @accounts |
| 31 | + return @accounts |
| 32 | + end |
| 33 | + |
| 34 | + # Finds the account with the ID that matches the passed parameter and returns the instance |
| 35 | + def self.find(id_search) |
| 36 | + found = @accounts.find do |account| |
| 37 | + account.id == id_search |
| 38 | + end |
| 39 | + return found |
| 40 | + end |
| 41 | + |
| 42 | + # Links owners with accounts |
| 43 | + def self.link_owner |
| 44 | + self.all |
| 45 | + Bank::Owner.all |
| 46 | + accounts_with_owners = [] |
| 47 | + account_owners_csv = CSV.read("support/account_owners.csv") |
| 48 | + account_owners_csv.each do |row| |
| 49 | + account = self.find(row[0].to_i) |
| 50 | + owner_account = Bank::Owner.find(row[1].to_i) |
| 51 | + account.owner = owner_account |
| 52 | + accounts_with_owners.push(account) |
| 53 | + end |
| 54 | + return accounts_with_owners |
| 55 | + end |
| 56 | + |
| 57 | + # ----------------------------------------- # |
| 58 | + # Work below is extra # |
| 59 | + |
| 60 | + # Displays (with formatting) the account details for all the accounts in accounts.csv |
| 61 | + # (Made this on accident and didn't want to let it go to waste!) |
| 62 | + def self.all_print_nice |
| 63 | + if @accounts == nil |
| 64 | + puts "There are no accounts." |
| 65 | + else |
| 66 | + @accounts.each do |account| |
| 67 | + puts account |
| 68 | + account.current_balance |
| 69 | + end |
| 70 | + end |
| 71 | + return |
| 72 | + end |
| 73 | + |
| 74 | + # Finds an account by id passed in as a parameter and displays the account information nicely formatted |
| 75 | + # (Made this on accident and didn't want to let it go to waste!) |
| 76 | + def self.find_and_display(id_search) |
| 77 | + found = @accounts.find do |account| |
| 78 | + account.id == id_search |
| 79 | + end |
| 80 | + return found.current_balance |
| 81 | + end |
| 82 | + # end of extra work # |
| 83 | +# ----------------------------------------# |
| 84 | + |
| 85 | + # Method for withdrawing from account |
18 | 86 | def withdraw(amount_to_withdraw)
|
| 87 | + # Checks that the user is not withdrawing more than what is available in the account |
19 | 88 | if (@balance - amount_to_withdraw)< 0
|
20 | 89 | puts "The requested withdrawal is more than the available funds."
|
21 | 90 | puts "You only have $#{@balance} available for withdrawal."
|
22 | 91 | return @balance
|
23 | 92 | else
|
| 93 | + # makes the withdrawal and displays info to the user |
24 | 94 | @balance -= amount_to_withdraw
|
25 | 95 | puts "You have withdrawn $#{amount_to_withdraw}."
|
26 | 96 | puts "Your current balance is $#{@balance}"
|
27 | 97 | return @balance
|
28 | 98 | end
|
29 | 99 | end
|
30 | 100 |
|
| 101 | + # Method for depositing into account |
31 | 102 | def deposit(amount_to_deposit)
|
| 103 | + # Makes the deposit and displays info to the user |
32 | 104 | @balance += amount_to_deposit
|
33 | 105 | puts "You have deposited $#{amount_to_deposit}."
|
34 | 106 | puts "Your current balance is $#{@balance}."
|
35 | 107 | return @balance
|
36 | 108 | end
|
37 | 109 |
|
| 110 | + # Displays current balance in the account |
38 | 111 | def current_balance
|
39 | 112 | puts "The account with ID #{@id} currently has a balance of $#{@balance}."
|
| 113 | + puts "This account was set up on #{@open_date}" |
40 | 114 | end
|
41 | 115 |
|
| 116 | + |
42 | 117 | end
|
43 | 118 |
|
44 | 119 | class Owner
|
45 |
| - attr_reader :first_name, :last_name, :street, :city, :state, :zip_code |
| 120 | + attr_reader :first_name, :last_name, :street, :city, :state, :id |
46 | 121 |
|
47 | 122 | def initialize(owner_hash)
|
| 123 | + @id = owner_hash[:id] |
48 | 124 | @first_name = owner_hash[:first_name]
|
49 | 125 | @last_name = owner_hash[:last_name]
|
50 |
| - @street = owner_hash[:street] |
| 126 | + @street_address = owner_hash[:street_address] |
51 | 127 | @city = owner_hash[:city]
|
52 | 128 | @state = owner_hash[:state]
|
53 |
| - @zip_code = owner_hash[:zip_code] |
54 | 129 | end
|
55 | 130 |
|
| 131 | + # Method to display details of an owner instance |
56 | 132 | def print_owner_details
|
57 | 133 | puts "The owner of this account is #{@first_name} #{@last_name}."
|
58 |
| - puts "Street: #{@street}" |
| 134 | + puts "Street: #{@street_address}" |
59 | 135 | puts "City: #{@city}"
|
60 | 136 | puts "State: #{@state}"
|
61 |
| - puts "Zip: #{@zip_code}" |
62 | 137 | end
|
63 | 138 |
|
| 139 | + # Creates accounts from the accounts.csv file |
| 140 | + def self.all |
| 141 | + owner_hash = Hash.new |
| 142 | + @owners = [] |
| 143 | + owners_csv = CSV.read("support/owners.csv") |
| 144 | + owners_csv.each do |id, last_name, first_name, street_address, city, state| |
| 145 | + owner_hash[:id] = id.to_i |
| 146 | + owner_hash[:last_name] = last_name |
| 147 | + owner_hash[:first_name] = first_name |
| 148 | + owner_hash[:street_address] = street_address |
| 149 | + owner_hash[:city] = city |
| 150 | + owner_hash[:state] = state |
| 151 | + id = Bank::Owner.new(owner_hash) |
| 152 | + @owners.push(id) |
| 153 | + end |
| 154 | + puts @owners |
| 155 | + return @owners |
| 156 | + end |
| 157 | + |
| 158 | + # Finds the owner with the ID that matches the passed parameter and returns the instance |
| 159 | + def self.find(id_search) |
| 160 | + found = @owners.find do |owner| |
| 161 | + owner.id == id_search |
| 162 | + end |
| 163 | + return found |
| 164 | + end |
| 165 | + |
| 166 | + |
| 167 | +# ----------------------------------------- # |
| 168 | + # Work below is extra # |
| 169 | + |
| 170 | + # Displays (with formatting) the owner details for all the accounts in owners.csv |
| 171 | + def self.all_print_nice |
| 172 | + if @owners == nil |
| 173 | + puts "There are no accounts." |
| 174 | + else |
| 175 | + @owners.each do |owner| |
| 176 | + owner.print_owner_details |
| 177 | + end |
| 178 | + end |
| 179 | + return |
| 180 | + end |
| 181 | + |
| 182 | + # Finds an owner by id passed in as a parameter and displays the owner information nicely formatted |
| 183 | + def self.find_and_display(id_search) |
| 184 | + found = @owners.find do |owner| |
| 185 | + owner.id == id_search |
| 186 | + end |
| 187 | + return found.print_owner_details |
| 188 | + end |
| 189 | + |
| 190 | + # end of extra work # |
| 191 | +# ----------------------------------------# |
| 192 | + |
64 | 193 | end
|
65 | 194 |
|
66 | 195 | end
|
0 commit comments