Coding Standards through Chapter 10 apply to this lab. Pay particular attention to the rules of style for classes, rules added in Chapter 10.
Write a Python
Account class, representing a savings account, with the following constructor and methods:
__init__method) takes an account number argument; it initializes the account number instance variable with the argument, and initializes the account's balance instance variable to 0.0.
getAcctNummethod should return the account number.
getBalancemethod should return the current balance.
depositmethod should add a given (positive) amount to the balance. If the amount given is negative or zero, it should print an error message and make no change to the balance.
withdrawmethod should subtract the given (positive) amount from the balance, unless the resulting balance would be less than or equal to zero, in which case it should print an error message "Insufficient funds" and make no change to the balance. Also, if the amount given is negative or zero, it should print an error message and make no change to the balance.
Note: Use the normal Python
class syntax, not any alternative using closures, lists, or dictionaries to "implement objects" which may have been taught by way of introduction.
Here's a sample session, to suggest how it should work:
>>> myAccount = Account(1592) >>> myAccount.getAcctNum() 1592 >>> myAccount.getBalance() 0 >>> myAccount.deposit(100) >>> myAccount.deposit(200) >>> myAccount.withdraw(50) >>> myAccount.getBalance() 250 >>> myAccount.deposit(-100) Please deposit an amount greater than 0. >>> myAccount.withdraw(10000) Insufficient funds.
Testing: Test in the IDLE "Python shell" as specified in the test procedure below and save the session (at the end) as a text file (.txt).
Test procedure: Create an account. Show the account number. Show the balance. Deposit 50, then 250, and show the balance. Withdraw 10, and show the balance. Attempt to deposit −2000. Attempt to withdraw 2000. Show the balance.
Turn in the following in Oncourse Assignments 2, with the given file extensions:
10-1 (CLASS) CA SUBTOTAL: 11 points, 9 to "pass." This is not recorded separately, and it is not "all or nothing." If you earn significantly less than passing, you may get a chance to resubmit; but do try to get it all right the first time!