Question: Suppose that you d like to implement a cookie jarLinks to an external site. in which to store cookies. In a file called LastNameFirstInitial _
Suppose that youd like to implement a cookie jarLinks to an external site. in which to store cookies. In a file called LastNameFirstInitialCookieJar.py implement a program with a class called Jar. Your program must have a commandline argument integer that will be used to set the capacity of the jar. Your Jar class must have the following methods:
init should initialize a cookie jar with the given capacity, which represents the maximum number of cookies that can fit in the cookie jar. If capacity is not a nonnegative int, though, init should instead raise a ValueError.
str should return a str with n where n is the number of cookies in the cookie jar. For instance, if there are cookies in the cookie jar, then str should return
deposit should add n cookies to the cookie jar. If adding that many would exceed the cookie jars capacity, though, depositshould instead raise a ValueError.
withdraw should remove n cookies from the cookie jar. Nom nom nom. If there arent that many cookies in the cookie jar, though, withdraw should instead raise a ValueError.
capacity getter function should return the cookie jars capacity.
capacity setter function should set the cookie jars capacity and enforce constraints and validation rules before accepting a new capacity attribute value. The capacity attribute cannot be less than zero and it cannot be less than size.
size getter function should return the number of cookies actually in the cookie jar.
size setter function should set the number of cookies actually in the cookie jar and enforce constraints and validation rules before accepting a new size attribute value. The size attribute cannot be less than zero and it cannot be greater than capacity.
Structure your class per the following. You may not alter these methods parameters, but you may add your own methods.
class Jar:
def initself capacity:
def strself:
def depositself n:
def withdrawself n:
@property
def capacityself:
@capacity.setter
def capacityself capacity:
@property
def sizeself:
@size.setter
def sizeself size:
Structure your main function per the following; this is what I will use and what you can use to test your implementation of the Jar class. For testing purposes, use as the commandline argument to set the capacity of the jar. Note, the below main function does not include code to handle errors andor incorrect use of the commandline argument by the user. You are expected to include code to handle those instances.
def main:
capacity intsysargv
jar Jarcapacity
jar.deposit
printjar
printjarsize
printjarcapacity
jar.withdraw
printjar
printjarsize
printjarcapacity
jar.size
printjar
printjarsize
Hints
Refer to the ObjectOriented Programming Tutorial and Raise and Decorators Tutorial from this module for additional guidance if you are struggling.
Refer to the CommandLine Arguments Tutorial from a prior module if you need a refresher on using commandline arguments.
Let the size setter method perform the size error handling instead of the deposit and withdraw methods. When these methods attempt to change the value of size, this will automatically call your size setter method and the size rulesrestrictions will be enforced to ensure only valid values are applied to size.
Additionally, your program must include:
comments with your name, date, program name, and program purpose.
a main function with if namemain: code. See this tutorial from module
Demo
Cookie Monster
Source: Sesame Street
Demonstration of running "Cookie Jar" in a terminalLinks to an external site.
How to Test
Heres how to test your code manually:
Run your program without any commandline arguments. Your program should exit using sysexit and provide an error message:
Too few commandline arguments
Run your program with python "YourFileName.py Your program should exit using sysexit and provide an error message:
Too many commandline arguments
Run your program with python "YourFileName.py Your program should raise a ValueError and provide the following message:
ValueError: Capacity cannot be less than zero
Run your program with python "YourFileName.py ten. Your program should exit using sysexit and provide an error message:
Commandline argument is not an integer
If your main program attempts to set the size larger than the capacity, your program should raise a ValueError and provide the following message:
ValueError: No more space in the jar
If your main program attempts to set the size less than zero, your program should raise a ValueError and provide t
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
