Designing the Program
List of properties :
- Brand
- Barcode
- Weight
- Price
- Discount
- ManufacturingDate
- Name
- expiryDate
- emailId
- phoneNumber
Selected properties
+Weight
+Price
+Name
+BarCode
Product Class Diagram
# Function to: read a float value as input
def get_float(prompt):
# initialize value to be a float variable 0
value = float(0.0)
# infinite loop to continue till desired value is entered
while True:
try:
# get the value from user after displaying prompt
value = float(input(prompt))
# if value entered by user is less then 0
if value < 0.0:
print(“We don’t accept negative money!”)
continue
# value is not negative
break
# suer entered a value which is not float
except ValueError:
print(‘Please enter a valid floating point value.’)
# return the value that is read
return value
# Function to: handle the packing of products into bags
def bag_products(product_list):
# initialize bag list and list of non bagged items to empty lists
# maximum weight of bag is 5
bag_list = []
non_bagged_items = []
MAX_BAG_WEIGHT = 5.0
# traverse all the products for packaging
for product in product_list:
# if weight of product is more then max weight that bag can carry
if product.weight > MAX_BAG_WEIGHT:
product_list.remove(product)
non_bagged_items.append(product)
# a new list to carry the contents
current_bag_contents = []
current_bag_weight = 0.0
# while there are still some products in products list
while len(product_list) > 0:
# remove the product from 0th index of list and store it in temp_product
temp_product = product_list[0]
product_list.remove(temp_product)
# if weight of current item and items in bag will
# be less then MAX weight of bag
if current_bag_weight + temp_product.weight < MAX_BAG_WEIGHT:
# add the current product to current bag
current_bag_contents.append(temp_product)
current_bag_weight += temp_product.weight
# if all the products are bagged
if (len(product_list) == 0):
bag_list.append(current_bag_contents)
# add the current bag to list of bags
else:
bag_list.append(current_bag_contents)
#make the current bag empty I,e take a new bag
current_bag_contents = []
current_bag_weight = 0.0
# traversing all bags of the bags list
for index, bag in enumerate(bag_list):
output = ‘Bag ‘ + str(index + 1) + ‘ contains: ‘
# traverse all the products in the current bag
for product in bag:
output += product.name + ‘t’
print(output, ‘n’)
# size of non bagged items is greater then 0
if (len(non_bagged_items) > 0):
output = ‘Non-bagged items: ‘
# traverse the items which are not bagged
for item in non_bagged_items:
output += item + ‘t’
print(output,’n’)
References:
Dawson, M. (2003). Python programming for the absolute beginner. Boston, Mass.: Premier Press.
Goldwasser, M., & Letscher, D. (2008). Object-oriented programming in Python. Upper Saddle River, N.J.: Pearson Prentice Hall.
Key, S., & Key, S. (2015). Python programming in a day. [North Charleston: CreateSpace Independent Publishing Platform].
Lee, K. (2011). Python programming fundamentals. London: Springer.
Lott, S. (2015). Functional Python Programming. Birmingham, UK: Packt Publishing.
Lutz, M. (2006). Programming Python. Sebastopol, CA: O’Reilly.