JAVA代码题 创建一个碟包,需要能保存CD和DVD碟片,记录碟片的发行信息,有存入、取出、外借、归还、点数、遍历等功能。
时间: 2024-01-02 21:02:14 浏览: 59
public class DiscPackage {
private List<Disc> discList;
public DiscPackage() {
discList = new ArrayList<>();
}
public void addDisc(Disc disc) {
discList.add(disc);
}
public Disc getDisc(int index) {
return discList.get(index);
}
public void removeDisc(int index) {
discList.remove(index);
}
public void lendDisc(int index) {
Disc disc = discList.get(index);
if (disc.isLend()) {
System.out.println("该碟片已被借出!");
} else {
disc.setLend(true);
System.out.println("借出成功!");
}
}
public void returnDisc(int index) {
Disc disc = discList.get(index);
if (disc.isLend()) {
disc.setLend(false);
System.out.println("归还成功!");
} else {
System.out.println("该碟片未被借出!");
}
}
public int countDisc() {
return discList.size();
}
public void displayDiscs() {
for (Disc disc : discList) {
System.out.println(disc);
}
}
}
public class Disc {
private String name;
private String author;
private String publishDate;
private boolean lend;
public Disc(String name, String author, String publishDate) {
this.name = name;
this.author = author;
this.publishDate = publishDate;
lend = false;
}
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
public String getPublishDate() {
return publishDate;
}
public boolean isLend() {
return lend;
}
public void setLend(boolean lend) {
this.lend = lend;
}
@Override
public String toString() {
return "Disc{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", publishDate='" + publishDate + '\'' +
", lend=" + lend +
'}';
}
}
public class CD extends Disc {
private int trackCount;
public CD(String name, String author, String publishDate, int trackCount) {
super(name, author, publishDate);
this.trackCount = trackCount;
}
public int getTrackCount() {
return trackCount;
}
@Override
public String toString() {
return "CD{" +
"name='" + getName() + '\'' +
", author='" + getAuthor() + '\'' +
", publishDate='" + getPublishDate() + '\'' +
", lend=" + isLend() +
", trackCount=" + trackCount +
'}';
}
}
public class DVD extends Disc {
private int duration;
public DVD(String name, String author, String publishDate, int duration) {
super(name, author, publishDate);
this.duration = duration;
}
public int getDuration() {
return duration;
}
@Override
public String toString() {
return "DVD{" +
"name='" + getName() + '\'' +
", author='" + getAuthor() + '\'' +
", publishDate='" + getPublishDate() + '\'' +
", lend=" + isLend() +
", duration=" + duration +
'}';
}
}
阅读全文