使用java设计一个简单的多媒体系统,基类MyMedia(String mediaName, float price, String press, String artist)类,其中包含MyBook(String bookISBN,String editor)、MyCd(String cdISRC)及MyTape(String tapeISRC)三个子类,三个子类中都有print方法输出信息。按照类的设计模式,完成他们的插入book、删除book、查找book,插入cd、删除cd、查找cd,插入tape、删除tape、查找tape以及查找全部媒体的功能。MyMedia类中有并且还拥有一个interfaceMedias接口和一个MediaTest测试类,interfaceMedias接口的方法有public int addMedia(Vector medias,MyMedia tempMedia); public int deleteMedia(Vector medias); public MyMedia findMedia(Vector medias,String artist); public void printlnMedia(Vector medias); public String input(); 并且MediaTest测试类实现了interfaceMedias接口
时间: 2024-02-13 12:07:01 浏览: 72
好的,我理解了你的问题。下面是一个简单的多媒体系统的 Java 代码,它包括 MyMedia、MyBook、MyCd、MyTape 类和一个 MediaTest 类:
```
import java.util.*;
interface interfaceMedias {
public int addMedia(Vector medias, MyMedia tempMedia);
public int deleteMedia(Vector medias);
public MyMedia findMedia(Vector medias, String artist);
public void printlnMedia(Vector medias);
public String input();
}
class MyMedia {
private String mediaName;
private float price;
private String press;
private String artist;
public MyMedia(String mediaName, float price, String press, String artist) {
this.mediaName = mediaName;
this.price = price;
this.press = press;
this.artist = artist;
}
public void print() {
System.out.println("Media Name: " + mediaName);
System.out.println("Price: " + price);
System.out.println("Press: " + press);
System.out.println("Artist: " + artist);
}
public String getArtist() {
return artist;
}
}
class MyBook extends MyMedia {
private String bookISBN;
private String editor;
public MyBook(String mediaName, float price, String press, String artist,
String bookISBN, String editor) {
super(mediaName, price, press, artist);
this.bookISBN = bookISBN;
this.editor = editor;
}
public void print() {
super.print();
System.out.println("Book ISBN: " + bookISBN);
System.out.println("Editor: " + editor);
}
public String getISBN() {
return bookISBN;
}
}
class MyCd extends MyMedia {
private String cdISRC;
public MyCd(String mediaName, float price, String press, String artist,
String cdISRC) {
super(mediaName, price, press, artist);
this.cdISRC = cdISRC;
}
public void print() {
super.print();
System.out.println("CD ISRC: " + cdISRC);
}
public String getISRC() {
return cdISRC;
}
}
class MyTape extends MyMedia {
private String tapeISRC;
public MyTape(String mediaName, float price, String press, String artist,
String tapeISRC) {
super(mediaName, price, press, artist);
this.tapeISRC = tapeISRC;
}
public void print() {
super.print();
System.out.println("Tape ISRC: " + tapeISRC);
}
public String getISRC() {
return tapeISRC;
}
}
public class MediaTest implements interfaceMedias {
public static void main(String[] args) {
Vector medias = new Vector();
MediaTest mt = new MediaTest();
// Add some media
MyBook book1 = new MyBook("Java Programming", 29.99f, "O'Reilly Media",
"John Doe", "9781492078005", "Jane Smith");
mt.addMedia(medias, book1);
MyCd cd1 = new MyCd("Greatest Hits", 12.99f, "Sony Music", "Bob Dylan",
"GBGQY1600001");
mt.addMedia(medias, cd1);
MyTape tape1 = new MyTape("Live Concert", 9.99f, "Warner Music", "Queen",
"GBAYE8300017");
mt.addMedia(medias, tape1);
// Find media by artist
System.out.println("Find media by artist: Bob Dylan");
MyMedia m = mt.findMedia(medias, "Bob Dylan");
if (m != null) {
m.print();
} else {
System.out.println("No media found.");
}
// Delete a book
System.out.println("Delete a book: Java Programming");
mt.deleteMedia(medias, book1);
// Print all media
System.out.println("Print all media:");
mt.printlnMedia(medias);
}
public int addMedia(Vector medias, MyMedia tempMedia) {
medias.addElement(tempMedia);
return medias.size();
}
public int deleteMedia(Vector medias, MyMedia tempMedia) {
int index = medias.indexOf(tempMedia);
if (index >= 0) {
medias.removeElementAt(index);
return medias.size();
} else {
return -1;
}
}
public MyMedia findMedia(Vector medias, String artist) {
for (int i = 0; i < medias.size(); i++) {
MyMedia m = (MyMedia) medias.elementAt(i);
if (m.getArtist().equals(artist)) {
return m;
}
}
return null;
}
public void printlnMedia(Vector medias) {
for (int i = 0; i < medias.size(); i++) {
MyMedia m = (MyMedia) medias.elementAt(i);
m.print();
System.out.println();
}
}
public String input() {
Scanner sc = new Scanner(System.in);
return sc.nextLine();
}
}
```
在这个代码中,MyMedia 类是基类,MyBook、MyCd、MyTape 类是它的子类。MediaTest 类实现了 interfaceMedias 接口,它包括插入、删除、查找和输出媒体的方法。在 main 方法中,我们创建了一些媒体,然后查找了一个艺术家的媒体,删除了一个书籍,最后输出了所有媒体。
阅读全文