Hands-on Day

Quiz: A ChargeCard class

Overview

Today, we will do a 50-minute challenge quiz involving the implementation of a user-defined class. You are to create (and eventually submit) a new file named ChargeCard.py and within that file you are to define a new class named ChargeCard adhering to the specification given below. (Note well that these names are case sensitive and must be capitalized as such.)

We will be providing you with an automated test harness, in the form of file TestChargeCard.py, which you should download and save in the same folder in which you create ChargeCard.py. Executing the TestChargeCard.py script will perform a variety of tests of various aspects of the behavior and provide a summary of the test results. Note well that you are welcome to start testing your code even if you have not yet implemented all of the required features. This allows you to validate portions of the functionality as you work.

Note: There are some features that we consider required and some that are extra credit (as detailed below). The test harness performs tests for both the required and extra credit tasks, appropriately labeled, so do not be alarmed if it is reporting failures on extra credit tasks that you did not perform.


Formal Specifications

A ChargeCard is meant as a model of a typical credit or debit card. A card has a designated credit limit and a current balance (with a positive balance representing an amount that the account holder currently owes). For the sake of this assignment, all monetary values will be integers (with the unit being irrelevant).

The ChargeCard class must support the following calling syntaxes:

If you have any questions about the semantics of these behaviors, please ask. For motivation, here are some sample sessions that might make use of the presumed class.

visa = ChargeCard(1000)     # spending limit of 1000; initial balance 0
visa.charge(200)            # returns True as charge accepted; new balance 200
visa.charge(900)            # returns False as new balance would exceed spending limit
print(visa.getBalance())    # return value of 200 displayed
visa.charge(400)            # new balance would be 600
visa.deposit(250)           # new balance down to 350
visa.charge(650)            # accepted (but just barely, as balance is now 1000)

debit = ChargeCard(0, -500)
debit.pay(300)              # accepted charge. Balance now -200
debit.pay(300)              # rejected charge. False return and balance remains -200


Submission


Additional Challenges

If you have time, implement the error checking of all parameters, raising the appropriate errors for the circumstances.


Michael Goldwasser
Last modified: Wednesday, 31 October 2018