|
| 1 | +module Bank |
| 2 | + class Account |
| 3 | + |
| 4 | + attr_reader :balance, :owner |
| 5 | + |
| 6 | + def initialize(id, initial_balance) |
| 7 | + #@account_owner = owner.first_name |
| 8 | + @id = id |
| 9 | + @balance = initial_balance |
| 10 | + @owner = nil |
| 11 | + |
| 12 | + # Raises an argument error if the initial balance is less than 0 |
| 13 | + if initial_balance < 0 |
| 14 | + raise ArgumentError, "The balance cannot be less than 0." |
| 15 | + end |
| 16 | + end |
| 17 | + |
| 18 | + def withdraw(amount_to_withdraw) |
| 19 | + if (@balance - amount_to_withdraw)< 0 |
| 20 | + puts "The requested withdrawal is more than the available funds." |
| 21 | + puts "You only have $#{@balance} available for withdrawal." |
| 22 | + return @balance |
| 23 | + else |
| 24 | + @balance -= amount_to_withdraw |
| 25 | + puts "You have withdrawn $#{amount_to_withdraw}." |
| 26 | + puts "Your current balance is $#{@balance}" |
| 27 | + return @balance |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + def deposit(amount_to_deposit) |
| 32 | + @balance += amount_to_deposit |
| 33 | + puts "You have deposited $#{amount_to_deposit}." |
| 34 | + puts "Your current balance is $#{@balance}." |
| 35 | + return @balance |
| 36 | + end |
| 37 | + |
| 38 | + def current_balance |
| 39 | + puts "The account with ID #{@id} currently has a balance of $#{@balance}." |
| 40 | + end |
| 41 | + |
| 42 | + end |
| 43 | + |
| 44 | + class Owner |
| 45 | + attr_reader :first_name, :last_name, :street, :city, :state, :zip_code |
| 46 | + |
| 47 | + def initialize(owner_hash) |
| 48 | + @first_name = owner_hash[:first_name] |
| 49 | + @last_name = owner_hash[:last_name] |
| 50 | + @street = owner_hash[:street] |
| 51 | + @city = owner_hash[:city] |
| 52 | + @state = owner_hash[:state] |
| 53 | + @zip_code = owner_hash[:zip_code] |
| 54 | + end |
| 55 | + |
| 56 | + def print_owner_details |
| 57 | + puts "The owner of this account is #{@first_name} #{@last_name}." |
| 58 | + puts "Street: #{@street}" |
| 59 | + puts "City: #{@city}" |
| 60 | + puts "State: #{@state}" |
| 61 | + puts "Zip: #{@zip_code}" |
| 62 | + end |
| 63 | + |
| 64 | + end |
| 65 | + |
| 66 | +end |
0 commit comments