|
| 1 | +require 'csv' |
| 2 | + # Create a module with the name Bank that will contain the class Account |
1 | 3 | module Bank
|
| 4 | + # Create an Account class |
| 5 | + |
| 6 | +class Owner |
| 7 | + |
| 8 | + attr_accessor :name |
| 9 | + |
| 10 | + def initialize(name) |
| 11 | + @name = name |
| 12 | + #@balance = balance |
| 13 | + puts "Hello, #{@name}." |
| 14 | + end |
| 15 | +end |
| 16 | + |
2 | 17 | class Account
|
3 | 18 |
|
4 |
| - def initialize(initial_balance) |
5 |
| - @internal_id = internal_id |
6 |
| - @initial_balance = @initial_balance |
| 19 | + attr_reader :account_id, :balance, :opendate |
| 20 | + # set my deafualt balance to 100 |
| 21 | + def initialize(id, balance=100, opendate) |
| 22 | + if balance < 0 |
| 23 | + raise ArgumentError, "Your balance is negative!" |
| 24 | + end |
| 25 | + @id = id |
| 26 | + @balance = balance |
| 27 | + @opendate = opendate |
| 28 | + #@id_field = [] |
| 29 | + # @id_field = firs |
| 30 | + end |
7 | 31 |
|
| 32 | + def display_account |
| 33 | + # show balance of account at initial start |
| 34 | + # show the account id |
| 35 | + puts @balance |
8 | 36 | end
|
| 37 | + |
| 38 | + def withdraw(amount) |
| 39 | + # accepts single parameters that represents the amount to be withdraw |
| 40 | + # return updated balance |
| 41 | + ## balance sets up the method that will subtract the amount from the balance |
| 42 | + ## have the (-) subtracts from the set from below variable |
| 43 | + @balance -= amount |
| 44 | + puts @balance |
| 45 | + if @balance - amount < 0 |
| 46 | + puts "You have a negative balance!" |
| 47 | + puts "Please deposit money into your account." |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + |
| 52 | + def deposit(amount) |
| 53 | + # accept single parameter that represents the amount of money to be depostied |
| 54 | + @balance += amount |
| 55 | + # should return updated account balance |
| 56 | + puts @balance |
| 57 | + end |
| 58 | + |
| 59 | + def self.all |
| 60 | + # open a file and puts it in the variable back_csv |
| 61 | + # read tells it to format the text. |
| 62 | + # puts the whole thing in an array, puts the lines in little arrays. |
| 63 | + bank_csv = CSV.read("./support/accounts.csv") |
| 64 | + # displaying the content |
| 65 | + print bank_csv |
| 66 | + # This will make sure the content of the file is spit out. |
| 67 | + # need to put all the info into the accounts and then return the accounts. |
| 68 | + # This gives you access to each little array |
| 69 | + bank_csv.map do |acct_info| |
| 70 | + puts acct_info |
| 71 | + Account.new(acct_info.first, acct_info[1].to_i, acct_info[2]) |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + def self.find(id) |
| 76 | + accounts = self.all |
| 77 | + accounts.each do |account| |
| 78 | + if account.id == id |
| 79 | + return account |
| 80 | + end |
| 81 | + end |
| 82 | + end |
| 83 | + |
9 | 84 | end
|
10 |
| -end |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | +# new_user = Bank::Owner.new("Andre") |
| 89 | +# new_account = Bank::Account.new(1234, 2000) |
| 90 | +# new_account.display_account |
| 91 | +# new_account.withdraw(60) |
| 92 | +# new_account.deposit(150) |
0 commit comments