#!/usr/bin/python3
#-------------------------------------------------------------------------------
# Create and print a shopping cart 
#-------------------------------------------------------------------------------

print("Content-Type: text/html \n")

shopCart = [  {'item':"apple",  'description':"Golden Delicious  ", 'quantity':6, 'price':1.29},
              {'item':"orange", 'description':"Navel Oranges     ", 'quantity':5, 'price':1.49},
              {'item':"banana", 'description':"Cavendish Bananas ", 'quantity':4, 'price':0.69},
              {'item':"pear",   'description':"Bartlett Pears    ", 'quantity':3, 'price':1.58} ]

cartTotal = 0

print('<h3>Before Adding Items</h3>')
for item in shopCart:                                   #iterate thru each shopCart item 
    print(item, '<br>')                                 #print each item (as a dictionary)
print('<br><br>')

item5 = {'item':"cherry", 'description':"Bing Cherries  ", 'quantity':2, 'price':2.49}
item6 = {'item':"grape",  'description':"Riesling grapes", 'quantity':1, 'price':1.79}

shopCart.append(item5)
shopCart.append(item6)

print('<h3>After Adding 2 Items</h3>')
for item in shopCart:                                   #iterate thru each shopCart item 
    print(item, '<br>')                                 #print each item (as a dictionary)
print('<br><br>')

print('<table border=1>')

item1 = shopCart[0]                                     #get element 1
print('<tr bgcolor=tan>')

for key in item1:                                       #get all the keys
    key = key.upper()                                   #change it to upper case
    print('<th>', key, '\t', end='')
print('<th>', 'TOTAL')

for dict in shopCart:
    print('<tr>')
#   for (key,value) in dict.items():                    #get all keys and values
    for value in dict.values():                         #get all the values 
        print('<td>', value, '\t', end='')
    itemTotal  = dict['quantity'] * dict['price']
    print('<th>', itemTotal, '\t')
    cartTotal += itemTotal

print('<tr>')
print('<td>\t\t<td>\t\t\t\t<td>\t<td>\t\t<th>',end='')
print( round(cartTotal,2) )






#=== link to see the python code =================================================
import os, sys
sys.path.insert(0,'/home/s/sultans/web/python/demo')
import zCode                          #import func to display the Python code
filename = os.path.abspath(__file__)  #get absolute file name 
zCode.display(filename)               #call it
#=================================================================================