Programming Assignment 2 (Shopping)
Write a Python program (
Shopping.py) that reads data from multiple data files
stored within a folder whose name will be provided in the command line.
The program should processes the data to provide answers to questions about the data.
Input data
The following is a sample content of one of these files:
mirage:p2 raj$ more invoices/1.dat
1,SV Stores,Srivatsan
Carrots,Vegetables/Food,1.5,50
Soap,Toiletries,4,32
Tomatoes,Vegetables/Food,2,40
Bananas,Vegetables/Food,8,8
Socks,Footwear/Apparel,3,56
Curd,Dairy/Food,0.5,32
Milk,Dairy/Food,1.5,24
mirage:p2 raj$
The data in the file describes a shopping bill or invoice. The first line has
a unique bill number, the name of the store, and the name of the customer.
Subsequent lines in the file (of which there will be at least one) contain
details about items purchased. Each subsequent line contains
name of item, category of item, quantity purchased, and unit price of item.
Sample data folder is available at
invoices.
A larger data set is available at
invoices.zip.
Data Structure
The data should be read and stored in a list with the following structure:
[('1', 'SV Stores', 'Srivatsan',
[('Carrots', 'Vegetables/Food', 1.5, 50.0),
('Soap', 'Toiletries', 4.0, 32.0),
('Tomatoes', 'Vegetables/Food', 2.0, 40.0),
('Bananas', 'Vegetables/Food', 8.0, 8.0),
('Socks', 'Footwear/Apparel', 3.0, 56.0),
('Curd', 'Dairy/Food', 0.5, 32.0),
('Milk', 'Dairy/Food', 1.5, 24.0)
]
),
('2', 'Big Bazaar', 'Sudeep',
[('Baked Beans', 'Canned/Food', 1.0, 125.0),
('Chicken Wings', 'Meat/Food', 0.5, 600.0),
('Cocoa Powder', 'Canned/Food', 1.0, 160.0),
('Capsicum', 'Vegetables/Food', 0.8, 180.0),
('Tie', 'Apparel', 2.0, 390.0),
('Clips', 'Household', 0.5, 32.0)
]
),
('3', 'Sun General', 'Srivatsan',
[('Batteries', 'Utilities', 6.0, 14.0),
('USB Cable', 'Electronics', 1.0, 85.0),
('Ball Pens', 'Stationery', 5.0, 12.0),
('Onions', 'Vegetables/Food', 1.25, 100.0)
]
),
('4', 'SV Stores', 'Akshaya',
[('Face Wash', 'Toiletries', 1.0, 89.0),
('Shampoo', 'Toiletries', 1.0, 140.0),
('Onions', 'Vegetables/Food', 1.0, 98.0),
('Bananas', 'Fruits/Food', 4.0, 8.0),
('Milk', 'Dairy/Food', 1.0, 24.0),
('Biscuits', 'Packed/Food', 2.0, 22.0),
('Maggi', 'Packed/Food', 1.0, 85.0),
('Horlicks', 'Packed/Food', 1.0, 270.0),
('Chips', 'Packed/Food', 1.0, 20.0),
('Chocolates', 'Packed/Food', 4.0, 10.0),
('Cereal', 'Packed/Food', 1.0, 220.0),
('Handwash', 'Toiletries', 1.0, 139.0),
('Air Freshener', 'Toiletries', 2.0, 70.0)
]
),
('5', 'Big Bazaar', 'Akshaya',
[('Trousers', 'Women/Apparel', 2.0, 870.0),
('Shirts', 'Women/Apparel', 1.0, 1350.0),
('Detergent', 'Household', 0.5, 270.0),
('Tee Shirts', 'Women/Apparel', 4.0, 220.0),
('Instant Noodles', 'Canned/Food', 3.0, 23.0)
]
)
]
Menu
After reading, the program prompts the user with a menu of 6 options:
- See all customers
- See all stores
- See all categories
- See customer purchases at various stores for a given customer
- See loyal customers - customers who purchase only from one store
- Quit
Modular design using functions
You should modularize the program by defining several functions, such as the following:
read_data() - this function reads data and returns the list of data as described earlier
get_all_customers()
get_all_stores()
get_all_categories()
customer_spend_per_store() - given a customer, returns list of expenditure per store for that customer
loyal() - returns loyal customers
Sample run
ASCSC1PP629W1:p2 raj$ python3 Shopping.py invoices
Welcome to Shopping Program
See all customers (c)
See all stores (s)
See all categories (g)
See customer purchases at various stores (p cname)
See loyal customers (y)
Quit (q)
What do you want to see? c
Customers:
Akshaya
Srivatsan
Sudeep
See all customers (c)
See all stores (s)
See all categories (g)
See customer purchases at various stores (p cname)
See loyal customers (y)
Quit (q)
What do you want to see? s
Stores:
Big Bazaar
SV Stores
Sun General
See all customers (c)
See all stores (s)
See all categories (g)
See customer purchases at various stores (p cname)
See loyal customers (y)
Quit (q)
What do you want to see? g
Categories:
Apparel
Canned/Food
Dairy/Food
Electronics
Footwear/Apparel
Fruits/Food
Household
Meat/Food
Packed/Food
Stationery
Toiletries
Utilities
Vegetables/Food
Women/Apparel
See all customers (c)
See all stores (s)
See all categories (g)
See customer purchases at various stores (p cname)
See loyal customers (y)
Quit (q)
What do you want to see? w
Invalid command
See all customers (c)
See all stores (s)
See all categories (g)
See customer purchases at various stores (p cname)
See loyal customers (y)
Quit (q)
What do you want to see? p Akshaya
Akshaya spend per store:
Big Bazaar 4174.0
SV Stores 1341.0
Sun General 0
See all customers (c)
See all stores (s)
See all categories (g)
See customer purchases at various stores (p cname)
See loyal customers (y)
Quit (q)
What do you want to see? y
Loyal customers:
Sudeep
See all customers (c)
See all stores (s)
See all categories (g)
See customer purchases at various stores (p cname)
See loyal customers (y)
Quit (q)
What do you want to see? q
Bye!
ASCSC1PP629W1:p2 raj$