使用java设计一个MyMedia(String mediaName, float price, String press, String artist)类,其中包含MyBook(String bookISBN,String editor)、MyCd(String cdISRC)及MyTape(String tapeISRC)三个子类。按照类的设计模式,完成他们的插入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-12 16:08:52 浏览: 64
好的,我来解答你的问题。
首先,需要明确一点,你的问题是让我设计一个Java类,而不是一个AI类,所以我无法使用ChitGPT来回答你的问题。但是,我可以为你提供一个基本的类设计方案。
以下是一个可能的解决方案:
MyMedia类:
```
public 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;
}
// Getters and setters for all fields
public String getMediaName() {
return mediaName;
}
public void setMediaName(String mediaName) {
this.mediaName = mediaName;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getPress() {
return press;
}
public void setPress(String press) {
this.press = press;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
// Override toString() method to return a string representation of the object
@Override
public String toString() {
return "MyMedia{" +
"mediaName='" + mediaName + '\'' +
", price=" + price +
", press='" + press + '\'' +
", artist='" + artist + '\'' +
'}';
}
}
```
MyBook类:
```
public 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;
}
// Getters and setters for all fields
public String getBookISBN() {
return bookISBN;
}
public void setBookISBN(String bookISBN) {
this.bookISBN = bookISBN;
}
public String getEditor() {
return editor;
}
public void setEditor(String editor) {
this.editor = editor;
}
// Override toString() method to return a string representation of the object
@Override
public String toString() {
return "MyBook{" +
"bookISBN='" + bookISBN + '\'' +
", editor='" + editor + '\'' +
"} " + super.toString();
}
}
```
MyCd类:
```
public 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;
}
// Getter and setter for cdISRC field
public String getCdISRC() {
return cdISRC;
}
public void setCdISRC(String cdISRC) {
this.cdISRC = cdISRC;
}
// Override toString() method to return a string representation of the object
@Override
public String toString() {
return "MyCd{" +
"cdISRC='" + cdISRC + '\'' +
"} " + super.toString();
}
}
```
MyTape类:
```
public 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;
}
// Getter and setter for tapeISRC field
public String getTapeISRC() {
return tapeISRC;
}
public void setTapeISRC(String tapeISRC) {
this.tapeISRC = tapeISRC;
}
// Override toString() method to return a string representation of the object
@Override
public String toString() {
return "MyTape{" +
"tapeISRC='" + tapeISRC + '\'' +
"} " + super.toString();
}
}
```
interfaceMedias接口:
```
import java.util.Vector;
public 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();
}
```
MediaTest测试类:
```
import java.util.Vector;
public class MediaTest implements interfaceMedias {
private Vector<MyMedia> medias = new Vector<MyMedia>();
public static void main(String[] args) {
MediaTest test = new MediaTest();
test.runTests();
}
public void runTests() {
MyMedia book1 = new MyBook("The Lord of the Rings", 20.99f, "HarperCollins", "J.R.R. Tolkien", "978-0544003415", "Christopher Tolkien");
MyMedia book2 = new MyBook("Harry Potter and the Philosopher's Stone", 12.99f, "Bloomsbury", "J.K. Rowling", "978-1408855652", "Arthur A. Levine");
MyMedia cd1 = new MyCd("The Dark Side of the Moon", 9.99f, "Harvest", "Pink Floyd", "C2K 63588");
MyMedia cd2 = new MyCd("Thriller", 7.99f, "Epic", "Michael Jackson", "EK 38112");
MyMedia tape1 = new MyTape("Nevermind", 5.99f, "DGC", "Nirvana", "DGCC-24425");
// Add media
addMedia(medias, book1);
addMedia(medias, book2);
addMedia(medias, cd1);
addMedia(medias, cd2);
addMedia(medias, tape1);
// Print all media
printlnMedia(medias);
// Find media by artist
MyMedia foundMedia = findMedia(medias, "Michael Jackson");
if (foundMedia != null) {
System.out.println("Found media: " + foundMedia);
} else {
System.out.println("Media not found.");
}
// Delete media
deleteMedia(medias);
printlnMedia(medias);
}
// Implement interface methods
public int addMedia(Vector medias, MyMedia tempMedia) {
medias.add(tempMedia);
return medias.size();
}
public int deleteMedia(Vector medias) {
if (medias.size() > 0) {
medias.removeElementAt(medias.size() - 1);
return medias.size();
} else {
return 0;
}
}
public MyMedia findMedia(Vector medias, String artist) {
for (int i = 0; i < medias.size(); i++) {
MyMedia media = medias.get(i);
if (media.getArtist().equals(artist)) {
return media;
}
}
return null;
}
public void printlnMedia(Vector medias) {
for (int i = 0; i < medias.size(); i++) {
System.out.println(medias.get(i));
}
}
public String input() {
// Not implemented
return null;
}
}
```
这个类设计方案中,MyMedia是一个基类,MyBook、MyCd和MyTape是它的三个子类。interfaceMedias是一个接口,定义了可以在MyMedia和其子类中使用的方法。MediaTest是一个测试类,实现了interfaceMedias接口,并使用它测试了MyMedia类及其子类。
希望这个类设计方案能够对你有所帮助。如果你还有其他问题,可以继续询问我。
阅读全文