1

Bentley University CS 230

Introduction to Programming with Python

Homework 7 – Starbucks Store Finder

Description

This program combines many topics we covered this semester as you create a store finder app for Starbucks locations.

The program will implement a menu of options:

1. Find Stores by City and State

2. Find Stores within Distance of City

3. Find Stores within Distance of Zip Code

4. Quit

After selecting options 1, 2, or 3, ask the user to enter the desired information, and then ask whether to display all stores in the specified location area, or only those with drive through windows. If the user requests only drive through stores, display only those, otherwise display all of them.

Your program will print information about stores in the specified location, and then show them visually on a map. Show stores with and without drive throughs using different colored map markers.

Data

Use the list of Starbucks stores in the United States (from 2013) available from the Open Data site at socrata.com: https://opendata.socrata.com/Business/All-Starbucks-Locations-in-the-US/txu4-fsic Download and save it as CSV. Use a symbolic constant in your program to store the name of the file:

STARBUCKS_FILE = “All_Starbucks_Locations_in_the_US.csv”

Do not change the name of the file! You do not have to upload the data file when you submit your completed program. Submit only the Store Finder program.

Modules

Install these modules so that your Python environment has access to them:

· pandas (data analysis tools)

· geopy (geography tools)

· folium (mapping tools)

· uszipcode (zip code tools)

Classes

· Create a class called Store which contains at least a store’s ID, name, street address, city, state, zip, latitude, longitude, drive-thru (from the Features – Stations column, which has the value “Drive-Through” if the store has a drive through window), and distance (to be calculated, the store’s distance from the user’s home location). Store a distance only if doing a distance search.

· Create a constructor (initializer) and a string method for this class. Use a default value of -1 if no distance value is available. The string method should return a string that neatly displays each attribute of the class. (Display the distance, formatted to two decimal places, when it is calculated. Only include the distance for a distance search (by city/state or zip code).

· You don’t need to write any accessor or mutator methods for this class.

Functions

· Write a function getData() which reads the Starbucks data csv file into a pandas DataFrame. You can filter the DataFrame to contain only the columns we will be working with in this program. The method returns a DataFrame. You might use a symbolic constant to store that list (as a tuple):

FILTERS = (‘Number’, ‘Name’, ‘Street Address’, ‘City’, ‘State’, ‘Zip’,‘Latitude’,‘Longitude’,‘Features – Stations’)

Doing so will help you iterate through the columns of the Data Frame elsewhere in your program.

· Write a function displayStores(dfStores) to display information for the stores contained in the Data Frame dfStores, passed in as a parameter. The function should create a Store object from each store in the data frame and use its string method to print it. (Or create a list of Stores and print the list.)

· Write a function displayMap(dfStores) which plots store information from a DataFrame dfStores on a map. Center the map at the average of the latitude and longitude coordinates. Use an appropriate method of the DataFrame class to calculate the average, don’t write your own average function. Use the folium module to create and display the map. Be sure each location has a title. Save the map info to an HTML file named map.html and launch a browser to display the map.

· Write a function findStoresByCityState(dfStores) which asks the user for the name of a city and state, and then locates all of the stores in that city and state. This function should call displayMap and displayStores to display the stores on the map and in the console window. Note that the value for distance for each store in dfStores will be -1 because you’re not calculating a distance in this function.

· Write a function findStoresWithinDistanceCityState(dfStores) which asks the user for a number of miles and a home city location, then locates all of the stores within that mile radius of the home city. The code should add a new column to dfStores containing the distance between each store in the database and the specified city and state. This function should call displayMap and displayStores to display the stores on the map and in the console window.

· Write a function findStoresWithinDistanceZip (dfStores) which asks the user for a radius of miles and a home city zip code, then locates all of the stores within that mile radius of the home city zip code. Read the documentation for the uszipcode module located at https://pypi.org/project/uszipcode/ (and install it) so you can add this capability. The code should add a new column to dfStores containing the distance between each store in the database and the specified zip code. This function should call displayMap and displayStores to display the stores on the map and in the console window.

· Write a main() function to implement the menu and program flow based on user choices.

Error Checking

· Write at least two try / except statements in your program to catch errors that might arise. Be sure to write a comment in your code about values or situations that would cause the program to crash if you didn’t include them.

One place where you can use this effectively is when creating a Store object in dfDisplay. The value for distance is found in row[9], but that column only exists when doing a distance search. When doing a city/state search, the data frame won’t have this column, so if you try to access it, you will get index out of range error.

· Display appropriate error messages for invalid data, if no stores are found in the location that the user entered, and other situations.

Extra Credit:

Add any of these features to the program for extra credit.

· Symbols on the Map: Read the documentation on the folium map class to figure out how to display a symbol (such as a coffee cup) on the map marker. Search online for how to add custom markers to a folium map in Python, or in a Python console window, type import folium and help(folium.Marker) to get complete documentation for the folium Marker class.

· Add a Store: Add information about a new store to the DataFrame and save it to the csv file. You do not have to add all the fields, just the ones contained n your Store object. When writing it to the file, you can write blank values for the columns you omitted.

· Remove a Store: Ask the user to identify a store (by its location or store ID) to delete, remove it from the DataFrame, then write the updated contents to the CSV file.

Be sure to include print statements that display when the program runs, as well as comments in your code, identifying the additional features you added.

Code and Style Requirements

Start with the starbucks_starter.py file. Fill in the code that is missing.

For full credit, please follow these style requirements for this and future assignments.

· Include introductory comments (docstrings) listing the name of the author, the date the file was created and a description of the purpose of the program. Also provide comments within the program code for any part of code that may be difficult to follow and would benefit from explanatory text. Add comments to major sections of the code so that you and other readers of your code can follow it easily.

· Use variable names that reflect the purpose of the variable and add comments if more information would be helpful (for example, units of measurement or valid values).

· Use symbolic constants to represent the constant numbers mentioned above, so that if any of these numbers changes, only the constant would need to be modified to make your program produce the right results. When your program is graded, these numbers may be modified, and the program still must work.

· Your input/output format should be similar to the sample interaction.

· We will test your program using different questions and leaderboard files, so be sure they follow the file formats listed.

Coding Hints:

The best way to develop a program is by working on it incrementally and periodically verifying the correctness of each part as you go along. For instance, start by developing the code for the menu. Then add and test your code for each feature. Continue writing and testing one feature at a time until you have a completely working program.

To organize your program more effectively, write functions for each menu option. Be sure to include a main() function!

In addition to functions for each menu item, write as many helping functions as make sense for repeated tasks. As your program has many parts or tasks, please be sure that you comment what each section of your code is doing.

Grading:

1. This assignment will be worth 8 % of your final grade.

2. The input and output of your program need to appear in exactly the order that is shown in the sample interaction above.

3. Your program should compile without syntax errors to receive any credit. If a part of your program is working, you will receive partial credit, but only if the program compiles without syntax errors. As you program, I highly recommend that you save intermediate versions of the .py file each time you get a piece of the program running. This way you can always have something to submit that works on at least some of the requirements. Otherwise, what was

working at one point may no longer work after further edits, and you may have no idea how to go back to the previous version (this is all too common!)

Submission:

· Name your script as starbucks.py and upload it onto Blackboard by the deadline. Do not submit your map.html file or the Starbucks csv file.

Sample Run:

Option 1

1
2

Option 3

3
4
5
6

Map located in: C:UsersmfrydenbergOneDrivePythonS19- CS230MiscCodePandasmap.html