Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Add a new column to table If you inspect the data files that we have provided, you will notice they all contain a sales section.

Add a new column to table

If you inspect the data files that we have provided, you will notice they all contain a sales section.

A sales section begins and ends with and lines respectively. Between those lines are zero or more sales record lines. Each line contains three values separated by a single comma: the date of the sale, the id of the album sold, and the number of units sold.

For example:

 2019-05-14,100,2 2018-12-03,201,3  

In that sales section, album with ID 100 had 2 sales on 2019-05-15 and the album with ID 201 and 3 sales on 2018-12-03.

Give Music would like the summary of the albums to now include the number of sales of each album, as shown in the example table below.

To achieve this you should modify your program from the previous question to include a read_sales(lines, albums) function that is called after the albums have all been read by the extract_all_albums function. The read_sales function takes the following parameters:

  1. The list of lines read from the file
  2. The dictionary of albums returned by extract_all_albums.

The read_sales function should locate the sales section and for each sales record it should call the add_to_sold method of the appropriate album to update the number of albums sold.

Notes:

  • You will also need to update your summary printing code - hopefully it is in a function which will make this easier.
  • The table header line is now underlined by 45 '-' characters rather than 35 as before.
  • The format string used for each albums row (and the table header) is now '{:<5}{:<30}{:>10}'

album0.txt

323 Licensed to Ill Beastie Boys 25.0 70 Enter the Wu_Tang: 36 Cham... Wu Tang Clan 25.99 turning to Alice, she went on, `What's your name, child?' and there stood the Queen in front of them, with her arms folded, 115 Daydream Nation Sonic Youth 5.0 414 52nd Street Billy Joel 25.75 2010-06-09,115,8255 2015-03-15,70,6264 2010-08-09,323,684 2012-01-06,323,7340 2009-01-01,414,13164 2017-04-02,70,3259 2015-06-05,323,2378 2015-03-05,115,9463 2010-12-10,414,12952 2013-05-29,323,1687 2013-04-07,115,1594 2015-10-19,70,1469 2011-10-07,414,10867 2006-04-14,115,5806 2014-11-27,70,9864 2006-08-22,323,7088 2017-12-19,115,5970 2012-04-16,414,16813 2007-05-13,414,11626 2006-09-18,70,2641 2006-12-29,323,5578

the previous code

"""program""" import os.path class Album: """Stores the information about an album""" def __init__(self, album_id, name, band, cost): """The data that forms an album, note that: - album_id is a int - cost is a float - the rest are strings """ self.album_id = album_id self.name = name self.band = band self.cost = cost self.total_sold = 0 def add_to_sold(self, count): """Adds to the internal album counter of the album""" if count >= 0: self.total_sold += count else: template = "Attempt to add {} to Album: {}. VALUE MUST NOT BE NEGATIVE" raise ValueError(template.format(count, self.album_id)) def __str__(self): output = "" output += "Album Name: {} ".format(self.name) output += "Band: {} ".format(self.band) output += "Purchase Price: ${:.2f}".format(self.cost) return output def __repr__(self): return "<<{}>>".format(self.album_id) def extract_album(lines, index): """Insert your function here""" identity = lines[index+1].strip() album = lines[index+2].strip() band = lines[index+3].strip() price = float(lines[index+4].strip()) return Album(identity, album, band, price) def extract_all_albums(lines): '''Function to extract all albums from the given list of lines''' i = 0 album_dict = {} while i < len(lines): if "" in lines[i]: album_dict[int(lines[i+1].strip())] = extract_album(lines, i) i += 6 else: i += 1 return album_dict def main(): """h""" filename = None while filename is None: filename = input("Enter a data file name: ") if not os.path.isfile(filename): print("Invalid File Name Entered!") filename = None file = open(filename) content = file.read() file.close() lines = content.splitlines() album_dict = extract_all_albums(lines) print() print("Album Catalogue Summary") print("-" * 35) print('{:<5}{:<30}'.format("ID", "ALBUM NAME")) for i in sorted(album_dict.keys()): print("{:<5}{:<30}".format(str(i), album_dict[i].name))

main()

For example:

Input Result
album0.txt
Enter a data file name: album0.txt Album Catalogue Summary --------------------------------------------- ID ALBUM NAME # SOLD 70 Enter the Wu_Tang: 36 Cham... 23497 115 Daydream Nation 31088 323 Licensed to Ill 24755 414 52nd Street 65422

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Moving Objects Databases

Authors: Ralf Hartmut Güting, Markus Schneider

1st Edition

0120887991, 978-0120887996

More Books

Students also viewed these Databases questions