【发布时间】:2020-10-17 02:25:07
【问题描述】:
我一直在创建一个程序作为作业的一部分,并且已经到了有人要求我将书面结构更改为面向对象程序的地步。这意味着创建一个带有方法的类。
这是问题/任务本身:
以下是程序代码,没有改动:
def make_album(id_num, name, band, price):
"""creates and returns a dictionary with keys id_num, name, band, and
price, with values populated directly from the function parameters."""
album_dict = {}
values = [int(id_num), name, band, float(price), 0, None, None]
keys = ['id_num', 'name', 'band', 'price', 'total_sales',
'first_date_recorded', 'last_date_recorded']
for i in range(len(keys)):
album_dict[keys[i]] = values[i]
return album_dict
def add_sales(album, date, daily_sales):
"""updates the given album dictionary by incrementing the total_sales
value by the given number of sales for that day"""
if album['first_date_recorded'] == None:
album['first_date_recorded'] = date
album['last_date_recorded'] = date
elif date > album['last_date_recorded']:
album['last_date_recorded'] = date
elif date < album['first_date_recorded']:
album['first_date_recorded'] = date
album['total_sales'] += daily_sales
def print_album(album):
"""Prints the given album in a format like the following, where the
first and last lines are both made of 40 hyphen ('-') characters"""
print('-' * 40)
print('Album Name: {}'.format(album['name']))
print('Band: {}'.format(album['band']))
print('Purchase Price: ${:.2f}'.format(album['price']))
print('First Recorded Sale: {}'.format(album['first_date_recorded']))
print('Last Recorded Sale: {}'.format(album['last_date_recorded']))
print('Total Sold: {}'.format(album['total_sales']))
print('-' * 40)
def extract_album(lines, index):
"""takes a list of lines and an index into that list and returns a
album dictionary"""
id_num, name, band, price = lines[(index + 1):(index + 5)]
album_dict = make_album(id_num, name, band, price)
return album_dict
def extract_all_albums(lines):
"""returns a list of albums in the same order as they are encountered
input lines list"""
album_dict_list = []
for i, line in enumerate(lines):
if line == '<album>':
album = extract_album(lines, i)
album_dict_list.append(album)
return album_dict_list
def read_sales(lines, albums):
"""locates the sales section if there is one and for each sales record
it should call the add_sales function on the appropriate album to update
the number of albums sold"""
start_tag = 0
end_tag = 0
for i, line in enumerate(lines):
if line == '<sales>':
start_tag = i
elif line == '</sales>':
end_tag = i
for line in lines[(start_tag + 1):(end_tag)]:
date, id_num, sales = line.split(',')
for album in albums:
if album['id_num'] == int(id_num):
add_sales(album, date, int(sales))
这是我的第一次尝试(不工作):
class Album:
"""defines the class Album.
Data attributes: id_num of album,
name of album,
band who wrote album,
price of the album.
"""
def __init__(self, id_num, name, band, price):
"""creates and new album object, a dictionary with keys id_num,
name, band, and price"""
self.id_num = id_num
self.name = name
self.band = band
self.price = price
self.album_dict = {}
values = [int(self.id_num), self.name, self.band, float(self.price),
0, None, None]
keys = ['id_num', 'name', 'band', 'price', 'total_sales',
'first_date_recorded', 'last_date_recorded']
for i in range(len(keys)):
self.album_dict[keys[i]] = values[i]
def add_sales(self,):
"""updates the given album dictionary by incrementing the
total_sales value by the given number of sales for that day"""
if self.album_dict['first_date_recorded'] == None:
self.album_dict['first_date_recorded'] = self.date
self.album_dict['last_date_recorded'] = self.date
elif self.date > self.album_dict['last_date_recorded']:
self.album_dict['last_date_recorded'] = self.date
elif self.date < self.album_dict['first_date_recorded']:
self.album_dict['first_date_recorded'] = self.date
self.album_dict['total_sales'] += self.daily_sales
def __str__(self):
"""Prints the given album in a format where the first and last lines
are both made of 40 hyphen ('-') characters"""
print('-' * 40)
print('Album Name: {}'.format(self.album['name']))
print('Band: {}'.format(self.album['band']))
print('Purchase Price: ${:.2f}'.format(self.album['price']))
print('First Recorded Sale:
{}'.format(self.album['first_date_recorded']))
print('Last Recorded Sale:
{}'.format(self.album['last_date_recorded']))
print('Total Sold: {}'.format(self.album['total_sales']))
print('-' * 40)
def extract_album(lines, index):
"""takes a list of lines and an index into that list and returns a
album dictionary"""
id_num, name, band, price = lines[(index + 1):(index + 5)]
album_dict = make_album(id_num, name, band, price)
return album_dict
def extract_all_albums(lines):
"""returns a list of albums in the same order as they are encountered
input lines list"""
album_dict_list = []
for i, line in enumerate(lines):
if line == '<album>':
album = extract_album(lines, i)
album_dict_list.append(album)
return album_dict_list
def read_sales(lines, albums):
"""locates the sales section if there is one and for each sales record
it should call the add_sales function on the appropriate album to update
the number of albums sold"""
start_tag = 0
end_tag = 0
for i, line in enumerate(lines):
if line == '<sales>':
start_tag = i
elif line == '</sales>':
end_tag = i
for line in lines[(start_tag + 1):(end_tag)]:
date, id_num, sales = line.split(',')
for album in albums:
if album['id_num'] == int(id_num):
add_sales(album, date, int(sales))
在我的尝试中有几件事我知道是错误的,但我似乎无法理解 OOP,因此很难在没有指导的情况下构造类和方法。我还没有完成任何对现在是类和方法的函数的调用,但我知道该程序将无法工作。感谢您的帮助,谢谢!
【问题讨论】: