java使用数组保存书、CD、磁带等信息,实现插入、删除、查找功能。输入、删除时,要显示操作正确与否的信息;查找时按关键字值进行查找,并显示查找结果。
时间: 2023-07-10 14:13:21 浏览: 87
好的,我可以为你提供一个基于数组的 Java 类来保存书、CD、磁带等信息,并实现插入、删除、查找功能。以下是示例代码:
```java
public class MediaLibrary {
private Media[] mediaList;
private int size;
public MediaLibrary(int capacity) {
mediaList = new Media[capacity];
size = 0;
}
public void insertMedia(Media media) {
if (size < mediaList.length) {
mediaList[size] = media;
size++;
System.out.println("Media inserted successfully.");
} else {
System.out.println("Media library is full.");
}
}
public void deleteMedia(Media media) {
int index = findMedia(media);
if (index >= 0) {
for (int i = index; i < size - 1; i++) {
mediaList[i] = mediaList[i + 1];
}
size--;
System.out.println("Media deleted successfully.");
} else {
System.out.println("Media not found.");
}
}
public void searchMedia(String keyword) {
boolean found = false;
for (int i = 0; i < size; i++) {
if (mediaList[i].getTitle().toLowerCase().contains(keyword.toLowerCase())) {
System.out.println(mediaList[i]);
found = true;
}
}
if (!found) {
System.out.println("No results found.");
}
}
private int findMedia(Media media) {
for (int i = 0; i < size; i++) {
if (mediaList[i].equals(media)) {
return i;
}
}
return -1;
}
}
public abstract class Media {
private String title;
public Media(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public abstract String toString();
}
public class Book extends Media {
private String author;
private String publisher;
private String isbn;
public Book(String title, String author, String publisher, String isbn) {
super(title);
this.author = author;
this.publisher = publisher;
this.isbn = isbn;
}
@Override
public String toString() {
return "Book: " + getTitle() + " by " + author + ", published by " + publisher + ", ISBN: " + isbn;
}
}
public class CD extends Media {
private String artist;
private String label;
private String upc;
public CD(String title, String artist, String label, String upc) {
super(title);
this.artist = artist;
this.label = label;
this.upc = upc;
}
@Override
public String toString() {
return "CD: " + getTitle() + " by " + artist + ", label: " + label + ", UPC: " + upc;
}
}
public class Tape extends Media {
private String artist;
private String label;
private String upc;
public Tape(String title, String artist, String label, String upc) {
super(title);
this.artist = artist;
this.label = label;
this.upc = upc;
}
@Override
public String toString() {
return "Tape: " + getTitle() + " by " + artist + ", label: " + label + ", UPC: " + upc;
}
}
```
这个示例代码中,我们定义了一个名为 `MediaLibrary` 的类来管理媒体对象。这个类包括了三个方法:`insertMedia`、`deleteMedia`、和 `searchMedia`。`insertMedia` 方法用于将一个媒体对象添加到媒体列表中,并打印添加成功的信息。`deleteMedia` 方法用于从媒体列表中删除一个指定的媒体对象,并打印删除结果的信息。`searchMedia` 方法用于按照关键字值进行查找,并打印查找结果的信息。
我们还定义了三个不同的媒体对象类:`Book`、`CD` 和 `Tape`。每个媒体对象类都有一个 `toString` 方法来定义对象的字符串表示形式,以便在打印媒体对象时使用。
下面是一个简单的示例使用代码:
```java
// 创建一个媒体库对象,设置容量为 10
MediaLibrary mediaLibrary = new MediaLibrary(10);
// 创建一些媒体对象
Media book1 = new Book("The Great Gatsby", "F. Scott Fitzgerald", "Scribner", "9780743273565");
Media cd1 = new CD("Thriller", "Michael Jackson", "Epic Records", "5099749626928");
Media tape1 = new Tape("Purple Rain", "Prince", "Warner Bros. Records", "07599251074");
// 将媒体对象添加到媒体库中
mediaLibrary.insertMedia(book1);
mediaLibrary.insertMedia(cd1);
mediaLibrary.insertMedia(tape1);
// 查找媒体对象
mediaLibrary.searchMedia("thriller");
// 删除媒体对象
mediaLibrary.deleteMedia(cd1);
mediaLibrary.deleteMedia(cd1);
```
输出结果如下:
```
Media inserted successfully.
Media inserted successfully.
Media inserted successfully.
CD: Thriller by Michael Jackson, label: Epic Records, UPC: 5099749626928
Media deleted successfully.
Media not found.
```
这个示例代码演示了如何使用 `MediaLibrary` 类来管理媒体对象,并实现插入、删除、查找功能。你可以根据自己的需求,修改这个示例代码以适应你的具体需求。
阅读全文