java使用数组保存书、CD、磁带等信息,实现插入、删除、查找功能。输入、删除时,要显示操作正确与否的信息;查找时按关键字值进行查找,并显示查找结果
时间: 2023-07-10 10:13:16 浏览: 85
好的,针对你的问题,我可以给出一份Java代码实现。
首先,我们可以定义一个抽象类`Multimedia`,表示多媒体信息,包括书、CD、磁带等。其中,包含属性`title`表示标题,`price`表示价格。
```java
public abstract class Multimedia {
protected String title;
protected double price;
public Multimedia(String title, double price) {
this.title = title;
this.price = price;
}
public String getTitle() {
return title;
}
public double getPrice() {
return price;
}
public abstract String getType();
}
```
接下来,我们可以定义三个类`Book`、`CD`、`Tape`,分别表示书、CD、磁带。这三个类都继承自`Multimedia`类,并分别添加自己的属性和方法。
```java
public class Book extends Multimedia {
private String author;
public Book(String title, String author, double price) {
super(title, price);
this.author = author;
}
public String getAuthor() {
return author;
}
@Override
public String getType() {
return "Book";
}
}
public class CD extends Multimedia {
private String artist;
public CD(String title, String artist, double price) {
super(title, price);
this.artist = artist;
}
public String getArtist() {
return artist;
}
@Override
public String getType() {
return "CD";
}
}
public class Tape extends Multimedia {
private String singer;
public Tape(String title, String singer, double price) {
super(title, price);
this.singer = singer;
}
public String getSinger() {
return singer;
}
@Override
public String getType() {
return "Tape";
}
}
```
接下来,我们可以定义一个`Catalog`类,表示多媒体信息的目录。这个类包含一个数组`items`,保存多媒体信息的实例,以及三个方法`insert`、`remove`、`search`,分别实现插入、删除、查找功能。
```java
public class Catalog {
private Multimedia[] items;
private int count;
public Catalog(int size) {
items = new Multimedia[size];
count = 0;
}
public void insert(Multimedia item) {
if (count < items.length) {
items[count++] = item;
System.out.println("Insert " + item.getType() + " " + item.getTitle() + " successfully.");
} else {
System.out.println("Catalog is full, cannot insert " + item.getType() + " " + item.getTitle() + ".");
}
}
public void remove(String title) {
boolean removed = false;
for (int i = 0; i < count; i++) {
if (items[i].getTitle().equals(title)) {
for (int j = i; j < count - 1; j++) {
items[j] = items[j + 1];
}
count--;
removed = true;
System.out.println("Remove " + items[i].getType() + " " + items[i].getTitle() + " successfully.");
}
}
if (!removed) {
System.out.println("Cannot remove " + title + ", it does not exist in the catalog.");
}
}
public void search(String keyword) {
boolean found = false;
for (int i = 0; i < count; i++) {
if (items[i].getTitle().contains(keyword)) {
System.out.println(items[i].getType() + " " + items[i].getTitle() + ", price: " + items[i].getPrice());
found = true;
}
}
if (!found) {
System.out.println("Cannot find any multi-media item containing keyword '" + keyword + "'.");
}
}
}
```
最后,我们可以在`main`方法中进行测试。下面是一份示例代码,演示了插入、删除、查找功能。
```java
public class Main {
public static void main(String[] args) {
Catalog catalog = new Catalog(5);
Book book1 = new Book("The Da Vinci Code", "Dan Brown", 20.0);
CD cd1 = new CD("Thriller", "Michael Jackson", 15.0);
Tape tape1 = new Tape("Like a Virgin", "Madonna", 10.0);
Book book2 = new Book("The Hitchhiker's Guide to the Galaxy", "Douglas Adams", 18.0);
CD cd2 = new CD("Abbey Road", "The Beatles", 20.0);
catalog.insert(book1);
catalog.insert(cd1);
catalog.insert(tape1);
catalog.insert(book2);
catalog.insert(cd2);
catalog.remove("Like a Virgin");
catalog.remove("The Catcher in the Rye");
catalog.search("the");
}
}
```
运行结果如下:
```
Insert Book The Da Vinci Code successfully.
Insert CD Thriller successfully.
Insert Tape Like a Virgin successfully.
Insert Book The Hitchhiker's Guide to the Galaxy successfully.
Insert CD Abbey Road successfully.
Remove Tape Like a Virgin successfully.
Cannot remove The Catcher in the Rye, it does not exist in the catalog.
Book The Da Vinci Code, price: 20.0
Book The Hitchhiker's Guide to the Galaxy, price: 18.0
CD Thriller, price: 15.0
CD Abbey Road, price: 20.0
```