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 LastNameFirstInitial_CookieJar.py, implement a program with a class called Jar. Your program must have a command-line 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 non-negative 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 3 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 __init__(self, capacity=12):
...
def __str__(self):
...
def deposit(self, n):
...
def withdraw(self, n):
...
@property
def capacity(self):
...
@capacity.setter
def capacity(self, capacity):
...
@property
def size(self):
...
@size.setter
def size(self, size=0):
...
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 12 as the command-line argument to set the capacity of the jar. Note, the below main function does not include code to handle errors and/or incorrect use of the command-line argument by the user. You are expected to include code to handle those instances.
def main():
capacity = int(sys.argv[1])
jar = Jar(capacity)
jar.deposit(10)
print(jar)
print(jar.size)
print(jar.capacity)
jar.withdraw(2)
print(jar)
print(jar.size)
print(jar.capacity)
jar.size =12
print(jar)
print(jar.size)
Hints
Refer to the Object-Oriented Programming Tutorial and Raise and Decorators Tutorial from this module for additional guidance if you are struggling.
Refer to the Command-Line Arguments Tutorial from a prior module if you need a refresher on using command-line 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 rules/restrictions 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 __name__=='__main__': code. See this tutorial from module 5.
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 command-line arguments. Your program should exit using sys.exit and provide an error message:
Too few command-line arguments
Run your program with python "YourFileName.py"1213. Your program should exit using sys.exit and provide an error message:
Too many command-line arguments
Run your program with python "YourFileName.py"-1. 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 sys.exit and provide an error message:
Command-line 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!