|
2 | 2 |
|
3 | 3 | module Bank
|
4 | 4 | class Account
|
5 |
| - attr_accessor :account_id, :account_balance, :amount |
| 5 | + attr_accessor :id, :balance, :amount |
6 | 6 |
|
7 |
| - def initialize(account_id, account_balance = 0) |
8 |
| - @account_id = account_id |
9 |
| - @account_balance = account_balance.to_i |
10 |
| - @amount = amount.to_i |
11 |
| - if @account_balance < 0 |
| 7 | + # :file |
| 8 | + @@number_of_accounts = 0 |
| 9 | + |
| 10 | + def initialize() |
| 11 | + account_array = CSV.read("./support/accounts.csv") |
| 12 | + @id = account_array[@@number_of_accounts][0] |
| 13 | + @balance = account_array[@@number_of_accounts][1] |
| 14 | + @opendate = account_array[@@number_of_accounts][2] |
| 15 | + # @opendate = opendate |
| 16 | + # @account_id = account_id |
| 17 | + @balance = @balance.to_i |
| 18 | + # @amount = amount.to_i |
| 19 | + if @balance < 0 |
12 | 20 | raise ArgumentError.new("Cannot create a new account with a negative balance")
|
13 | 21 |
|
14 | 22 | else
|
15 |
| - puts "You deposited #{@amount} dollars." |
16 |
| - puts "Your new balance is #{@account_balance} dollars." |
| 23 | + puts "You deposited #{@balance} dollars." |
| 24 | + puts "Your new balance is #{@balance} dollars." |
17 | 25 | end
|
18 |
| - end |
| 26 | + @@number_of_accounts += 1 |
| 27 | + end #end statement for initialize method begining on line 10 |
19 | 28 |
|
20 | 29 | def withdraw
|
21 | 30 | puts "Type the amount you would like to withdraw:"
|
22 | 31 | @amount = gets.chomp.to_i
|
23 |
| - if @account_balance - @amount < 0 |
| 32 | + if @balance - @amount < 0 |
24 | 33 | puts ("Cannot withdraw an amount that will make the balance negative.")
|
25 |
| - puts "Your current balance is #{@account_balance}." |
| 34 | + puts "Your current balance is #{@balance}." |
26 | 35 | else
|
27 |
| - @account_balance -= @amount |
28 |
| - puts "Your new balance is: #{@account_balance} dollars." |
29 |
| - end |
30 |
| - end |
| 36 | + @balance -= @amount |
| 37 | + puts "Your new balance is: #{@balance} dollars." |
| 38 | + end#end statment for the if/else beginning on line 32 |
| 39 | + end#end for withdraw method beginning on line 29 |
31 | 40 |
|
32 | 41 | def deposit
|
33 | 42 | puts "Type the amount you would like to deposit:"
|
34 | 43 | @amount = gets.chomp.to_i
|
35 |
| - @account_balance += @amount |
36 |
| - puts "Your new balance is #{@account_balance} dollars." |
37 |
| - end |
| 44 | + @balance += @amount |
| 45 | + puts "Your new balance is #{@balance} dollars." |
| 46 | + end#end statement for deposit method beginning on line 41 |
38 | 47 |
|
39 | 48 | def balance
|
40 |
| - puts account_balance |
| 49 | + puts @balance |
| 50 | + end #end for balance method begining on line 48 |
| 51 | + |
| 52 | + def self.all |
| 53 | + @@number_of_accounts = 0 |
| 54 | + all_accounts_array = [] |
| 55 | + account_array = CSV.read("./support/accounts.csv") |
| 56 | + while @@number_of_accounts < account_array.length do |
| 57 | + account_name = "Account" + @@number_of_accounts.to_s |
| 58 | + account_name = Account.new() |
| 59 | + all_accounts_array.push(account_name) |
| 60 | + end |
| 61 | + return all_accounts_array |
| 62 | + end#end statement for self.all method beginning on line 52 |
| 63 | + |
| 64 | + def self.find(id) |
| 65 | + account_array = self.all |
| 66 | + # print account_array |
| 67 | + return account_array.find {|i| i.id == id.to_s} |
41 | 68 | end
|
42 | 69 |
|
43 |
| - end |
44 |
| -end |
| 70 | + end#end for the account class on line 4 |
| 71 | + |
| 72 | +end #end for the bank module begining on line 3 |
| 73 | + |
| 74 | + # def self.find(id) |
| 75 | + # @file.find do |
| 76 | + |
| 77 | + # end |
| 78 | +# end |
| 79 | +# end |
| 80 | +# |
| 81 | +# end |
| 82 | +# end |
45 | 83 | #extra code
|
46 | 84 |
|
47 | 85 |
|
|
0 commit comments