36 lines
893 B
Text
36 lines
893 B
Text
class BankAccount
|
|
def initialize(owner, balance)
|
|
@owner = owner
|
|
@balance = balance
|
|
end
|
|
def deposit(amount)
|
|
if amount > 0
|
|
@balance += amount
|
|
return true
|
|
end
|
|
return false
|
|
end
|
|
def withdraw(amount)
|
|
if 0 < amount and amount <= @balance
|
|
@balance -= amount
|
|
return true
|
|
end
|
|
return false
|
|
end
|
|
def get_balance
|
|
return @balance
|
|
end
|
|
def to_s
|
|
owner = @owner
|
|
balance = @balance
|
|
return "Account(#{owner}, Balance: #{balance})"
|
|
end
|
|
end
|
|
account = BankAccount.new("Alice", 1000)
|
|
print(account, "\n")
|
|
print("Deposit $500: #{account.deposit(500)}", "\n")
|
|
print("Balance: $#{account.get_balance()}", "\n")
|
|
print("Withdraw $200: #{account.withdraw(200)}", "\n")
|
|
print("Balance: $#{account.get_balance()}", "\n")
|
|
print("Withdraw $2000: #{account.withdraw(2000)}", "\n")
|
|
print("Final balance: $#{account.get_balance()}", "\n")
|