+1 678 648 4277 

Sales Data Tracking using Python Assignment Solution


Tracking of Sales over the year assignment

Write a program that allows the user to view and edit the sales amounts of the current year.

Sample Run:

Command Menu

view - View a sales amount for a specified month

highest - View the highest sales of the year

lowest - View the lowest sales of the year

edit - Edit a sales amount for a specified month

average - View the sales average for the whole year

range - View the sales average for a specified sales amount range

total - View the sales total for the whole year

exit - Exit the program

Command: new

Invalid command. Try again.

Command: view

Three-letter month: Jan

The sales amount for Jan is $14,317.00

Command: highest

The highest sales amount is $15,578.00 in Aug

Command: lowest

The lowest sales amount is $88.00 in Nov

Command: edit

Three-letter month: Jan

Sales amount: 15293

The sales amount of $15,293.00 for Jan has been updated

Command: average

The monthly average is $5,775.55

Command: range

Low: 2540

Hi: 8755

The sales average of this range is $4,791.33

Command: total

The yearly total is $63,531.00

Command: view

Three-letter month: July

Invalid three-letter month. Try again

Command: exit

Thank you for using my app!

________________________________

Specifications:

In your program, use monthly_sales.txt download for the month and the

monthly sales.

The program should read the file and store the sales data for each

month in a dictionary with the month abbreviation as the key for each

item.

If the sales data is edited, the program should write the updated data

to the text file.

The program should display error messages for invalid user inputs.

Use the format function to display your outputs when needed. For

instance, if the month is Jan and the amount is 14317, "Sales amount

for Jan is $14,317.00." is implemented as follows:

print("Sales amount for {:s} is ${:,.2f}.".format(month, amount))

{:s} is a string format parameter for the month.

{:,.2f} is a float number format parameter for the amount with 2 decimal places.

read_file()

write_file(dictionary)

view_sales_amount(dictionary)

get_highest_amount(dictionary)

get_lowest_amountt(dictionary)

edit_sales_amount(dictionary)

get_average(dictionary)

get_range_average(dictionary)

get_total(dictionary)

terminate_app()

Solution:

def read_file(): ''' Reading the database from the file monthly_sales.txt and mapping to dictionary Return the dictionary ''' dictionary = {} with open('monthly_sales.txt') as f: # Reading line line = f.readline() while line: # Ignore the empty line if line == "" or line == '\n': continue #print("DEBUG: {:s}".format(line)) # Add to the dictionary dictionary[str(line.split("\t")[0])] = (float(line.split("\t")[1])) # Go to read next line line = f.readline() return dictionary def write_file(dictionary): ''' Write current data to file monthly_sales.txt ''' # Open for writing. Create a new file one outfile = open('monthly_sales.txt', 'w') for k in dictionary.keys(): # Write out data outfile.write("{}\t{}\n".format(k, dictionary[k])) def view_sales_amount(dictionary): ''' View a sales amount for a specified month ''' # Ask input month from console month = str(input("Tree-letter month: ")) found = False for k in dictionary.keys(): if k.lower() == month.lower(): # If found the data. Just print it print("Sales amount for {:s} is ${:,.2f}.".format(k, dictionary[k])) found = True break if found == False: # Incase no found the month input. Just print the error print("Invalid three-letter month. Try again") def get_highest_amount(dictionary): ''' Get the highest sales of the year ''' highest = None month = None for k in dictionary.keys(): if highest == None or highest < dictionary[k]: # If we found this have a new highest. Just update it highest = dictionary[k] month = k # Print out the data print("The highest sales amount is ${:,.2f} in {:s}.".format(highest, month)) def get_lowest_amount(dictionary): ''' View the lowest sales of the year ''' lowest = None month = None for k in dictionary.keys(): if lowest == None or lowest > dictionary[k]: # Found new lowest. Update it lowest = dictionary[k] month = k # Print out the data print("The lowest sales amount is ${:,.2f} in {:s}.".format(lowest, month)) def edit_sales_amount(dictionary): ''' Edit a sales amount for a specified month ''' month = str(input("Tree-letter month: ")) found = False for k in dictionary.keys(): if k.lower() == month.lower(): # If we found the month from the input. Just ask new value and update it sales = float(input("Sales amount: ")) dictionary[k] = sales print("Sales amount of ${:,.2f} for {:s} has been updated.".format(dictionary[k], k)) found = True break if found == False: # Incase no found the month input. Just print the error print("Invalid three-letter month. Try again") def get_average(dictionary): ''' ''' sumSales = 0.0 cnt = 0 for k in dictionary.keys(): sumSales += dictionary[k] cnt += 1 print("Monthly average is ${:,.2f}".format(sumSales / cnt)) def get_range_average(dictionary): ''' View the sales average for the whole year ''' low = float(input("Low: ")) hi = float(input("Hi: ")) sumSales = 0.0 cnt = 0 for k in dictionary.keys(): if dictionary[k] >= low and dictionary[k] <= hi: # If current value is mapping the HI and LOW value. Calculate it sumSales += dictionary[k] cnt += 1 print("Sales average of this range is ${:,.2f}".format(sumSales / cnt)) def get_total(dictionary): ''' View the sales total for the whole year ''' sumSales = 0.0 for k in dictionary.keys(): # Just Calculate the total value sumSales += dictionary[k] # Print out print("Yearly total is ${:,.2f}.".format(sumSales)) def terminate_app(): ''' Exit the program ''' print("Thank you for using my app!") exit(0) if __name__ == '__main__': print("Product Sales Management System") print("Command menu") print("view - View a sales amount for a specified month") print("highest - View the highest sales of the year") print("lowest - View the lowest sales of the year") print("edit - Edit a sales amount for a specified month") print("average - View the sales average for the whole year") print("range - View the sales average for a specified sales amount range") print("total - View the sales total for the whole year") print("exit - Exit the program") dictionary = {} dictionary = read_file() # Loop until exit command is type while True: print("") cmd = (str)(input("Command: ")) if cmd == "view": view_sales_amount(dictionary) elif cmd == "highest": get_highest_amount(dictionary) elif cmd == "lowest": get_lowest_amount(dictionary) elif cmd == "edit": edit_sales_amount(dictionary) elif cmd == "average": get_average(dictionary) elif cmd == "range": get_range_average(dictionary) elif cmd == "total": get_total(dictionary) elif cmd == "exit": # before exit. Just update the database. write_file(dictionary) terminate_app() else: print("Invalid command. Try again")