使用数组保存书、CD、磁带等信息,实现插入、删除、查找功能。输入、删除时,要显示操作正确与否的信息;查找时按关键字值进行查找,并显示查找结果。
时间: 2023-07-10 19:13:21 浏览: 49
好的,我可以为你提供一个基于数组的类来保存书、CD、磁带等信息,并实现插入、删除、查找功能。以下是一个简单的示例代码:
```
class MediaLibrary:
def __init__(self):
self.media_list = []
def insert_media(self, media):
self.media_list.append(media)
print("Media inserted successfully.")
def delete_media(self, media):
if media in self.media_list:
self.media_list.remove(media)
print("Media deleted successfully.")
else:
print("Media not found.")
def search_media(self, keyword):
results = []
for media in self.media_list:
if keyword.lower() in media.title.lower():
results.append(media)
if results:
print("Search results:")
for result in results:
print(result)
else:
print("No results found.")
class Book:
def __init__(self, title, author, publisher, isbn):
self.title = title
self.author = author
self.publisher = publisher
self.isbn = isbn
def __str__(self):
return f"Book: {self.title} by {self.author}, published by {self.publisher}, ISBN: {self.isbn}"
class CD:
def __init__(self, title, artist, label, upc):
self.title = title
self.artist = artist
self.label = label
self.upc = upc
def __str__(self):
return f"CD: {self.title} by {self.artist}, label: {self.label}, UPC: {self.upc}"
class Tape:
def __init__(self, title, artist, label, upc):
self.title = title
self.artist = artist
self.label = label
self.upc = upc
def __str__(self):
return f"Tape: {self.title} by {self.artist}, label: {self.label}, UPC: {self.upc}"
```
这个示例代码中,我们定义了一个名为 `MediaLibrary` 的类来管理媒体对象。这个类包括了三个方法:`insert_media`、`delete_media`、和 `search_media`。`insert_media` 方法用于将一个媒体对象添加到媒体列表中,并打印添加成功的信息。`delete_media` 方法用于从媒体列表中删除一个指定的媒体对象,并打印删除结果的信息。`search_media` 方法用于按照关键字值进行查找,并打印查找结果的信息。
我们还定义了三个不同的媒体对象类:`Book`、`CD` 和 `Tape`。每个媒体对象类都有一个 `__str__` 方法来定义对象的字符串表示形式,以便在打印媒体对象时使用。
下面是一个简单的示例使用代码:
```
# 创建一个媒体库对象
media_library = MediaLibrary()
# 创建一些媒体对象
book1 = Book("The Great Gatsby", "F. Scott Fitzgerald", "Scribner", "9780743273565")
cd1 = CD("Thriller", "Michael Jackson", "Epic Records", "5099749626928")
tape1 = Tape("Purple Rain", "Prince", "Warner Bros. Records", "07599251074")
# 将媒体对象添加到媒体库中
media_library.insert_media(book1)
media_library.insert_media(cd1)
media_library.insert_media(tape1)
# 查找媒体对象
media_library.search_media("thriller")
# 删除媒体对象
media_library.delete_media(cd1)
media_library.delete_media(cd1)
```
输出结果如下:
```
Media inserted successfully.
Media inserted successfully.
Media inserted successfully.
Search results:
CD: Thriller by Michael Jackson, label: Epic Records, UPC: 5099749626928
Media deleted successfully.
Media not found.
```
这个示例代码演示了如何使用 `MediaLibrary` 类来管理媒体对象,并实现插入、删除、查找功能。你可以根据自己的需求,修改这个示例代码以适应你的具体需求。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)