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.
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:
Constructor
The constructor should accept one required parameter,
designating the spending limit on the card, and one optional
parameter that designates an initial balance (with the balance
being 0 by default). For example the syntax
visa = ChargeCard(5000)would create a new card, with a spending limit of 5000 and an initial balance of zero.
visa = ChargeCard(5000, 1375)would create a new card, with a spending limit of 5000 and an initial balance of 1375 (perhaps because that balance was transfered from some other card).
debit = ChargeCard(0, -500)would effectively serve as a debit card because the spending limit of zero means that the user can never go into debt with this card, but an initial balance of -500 reflects an initial deposit of money that allows for spending.
Standard version: You may assume that the parameters are integers and that limit is nonnegative.
Extra credit: Validate these assumptions about the parameters.
getBalance()
An accessor that returns the current balance.
getLimit()
An accessor that returns the spending limit for this card.
charge(amount)
A call to this method reflects the customer attempting to use
the card to spend the indicated
amount of money. Typically this would cause the
balance on the card to rise by that given amount. However, the
customer should not be allowed to make the purchase if that
would cause the new balance to become strictly greater than the
designated spending limit.
This function should return True if the charge was accepted (and balance adjusted accordingly), or False if the charge was rejected because of the account limit (in which case the resulting balance should be unchanged).
Standard version: You may assume that the parameter is a positive integer.
Extra credit: Validate that the parameter is a positive integer
deposit(amount)
A call to this method reflects the customer sending payment to
the bank that is to be credited to reduce the current
balance. For example a customer with a balance of 400 who
deposits 50 would have a new balance of only 350.
We allow a customer to deposit even more than their current
balance (in which they would then have a credit that could be
used for later spending). For example a customer with a balance
of 400 who deposits 500 would then have a resulting balance of
-100.
This function should not return anything. (Technically, None would be returned.)
Standard version: You may assume that the parameter is a positive integer.
Extra credit: Validate that the parameter is a positive integer
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
One member of the pair must submit your project electronically, placing it in the quiz15 folder of their git repository.
Submit source code titled ChargeCard.py.
Make sure that BOTH students' names are in comments at the top of the source code.
If you have time, implement the error checking of all parameters, raising the appropriate errors for the circumstances.