Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python. Question. Q3a. Write a Python class definition for SnackBoxException, which is a subclass of the Exception class. This class has no additional attribute or

Python. Question.

image text in transcribed

Q3a. Write a Python class definition for SnackBoxException, which is a subclass of the Exception class. This class has no additional attribute or method.

Q3b. Implement the SnackBox class. The SnackBox class represents a Snack box with selected FoodItem assembled by a customer. It has the following attributes:

Two class variables:

*_MIN_ITEMS and _MAX_ITEMS: representing the minimum and maximum number of FoodItem that the snack box can contain.

Four instance variables:

*_name represents the customer defined name for this snack box.

*_foodItems is a collection containing the customer selection of FoodItem objects for this snack box. It is possible that a customer can add the same FoodItem (e.g., 2 Toasted Cheese Sandwiches) into the snack box. You can choose to implement either Dictionary or List for this collection.

*_price represents the final price of the assembled snack box, which is the sum of all FoodItem's cost.

*_consumeBy is the lowest expiry hours of all FoodItem objects.

The constuctor performs the following:

* Initialise _name with the given parameter name.

* Set _foodItems to an empty collection, then add the given parameter foodItem into the collection.

* _price and _consumeBy are set to the cost and expiryHours of the given parameter foodItem.

Getter methods for name, price and consumeBy. Use property decorator. addItem method adds the given parameter foodItem into the collection _foodItems.

* If this addition results in _foodItems having more than _MAX_ITEMS, raise SnackBoxException with appropriate message.

* Ensure _price and _consumeBy is re-computed after each successful addition.

removeItem method removes from _foodItems, the first FoodItem object that matches the parameter name.

* If there is no foodItem matching the given name, then method should raise SnackBoxException with appropriate message.

* If the number of foodItems after removal is less than _MIN_ITEM, do not allow the removal and raise SnackBoxException with appropriate message.

* Ensure _price and _consumeBy is re-computed after each successful removal.

The __str__ method returns a string representation of a SnackBox object, including the name of each FoodItem objects.

This is an example in the suggested format:

Name: Revive Snack Price: $7.50 Consume in 2.0 hrs with Cold Cut Sandwich, Tiramisu

Q3c. State at least TWO(2) reasons to justify your choice of collection for the implementation of SnackBox's _foodItems.

Q3d. Write Python statements to do the following:

Q3d(i).

image text in transcribed

Q3d(ii). Create 2 snack boxes, sb1 ('His Desserts') and sb2 ('Her Dessert'), with Chendol and Cheng Teng as their first FoodItem respectively.

Q3d(iii). Add these dessrts to sb1 in this order: Cheng Teng, Ice Kachang, Pulut Hitam and Bubur Cha Cha. Ensure exceptions are properly handled.

Q3d(iv). Remove Cheng Teng from sb2. Ensure exceptions are properly handled.

Q3d(v). Based on your understanding, what will be the output from these python statements, if exceptions are handled properly in Q3(d)(iii) & Q3(d)(iv)?

print(sb1)

print(sb2)

Apply the principles of object-oriented programming and develop a program to help student create their own customised "Snack Box", by adding FoodItem into snack box or removing Fooditem from it. Your program must handle SnackBoxException which you will define for this question. Study the class diagram in Figure Q3(a), which now feature the SnackBox class. \begin{tabular}{|l|} \hline \multicolumn{1}{|c|}{ SnackBox } \\ \hline MIN ITEMS: 1 \\ \hline MAX ITEMS: 4 \\ \hline name: str \\ fooditems: collection \\ price: float \\ _consumeBy: float \\ \hline init__(self, name: str, fooditem: Fooditem) \\ name(self): str \\ price(self): float \\ consumeBy(self): float \\ additem(self, item: Fooditem) \\ removeltem(self, name: str) \\ _str__self) : str \end{tabular} \begin{tabular}{|l|} \hline \multicolumn{1}{|c|}{ HotSandwich } \\ \hline _toasted: boolean \\ \hline _init__(self, name: str, filling: str, cost: float, \\ toasted: boolean) \\ \hline \end{tabular} Figure 03(a) Create the appropriate Fooditem objects using the information in Table Q3(d)(i) below

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems On GPUs In Databases

Authors: Johns Paul ,Shengliang Lu ,Bingsheng He

1st Edition

1680838482, 978-1680838480

More Books

Students also viewed these Databases questions