用java编写一个碟包,需要能保存CD和DVD碟片,记录碟片的发行信息,有存入、取出、外借、归还、点数、遍历等功能的程序
时间: 2024-05-21 10:13:14 浏览: 106
很高兴回答你的问题。您可以使用Java编写一个碟包程序,其中包括保存CD和DVD碟片、记录碟片的发行信息、存入、取出、外借、归还、点数、遍历等功能。您可以使用Java的面向对象编程来实现这些功能,例如创建一个Disc类来表示CD和DVD碟片,使用HashMap来记录碟片的发行信息,以及使用ArrayList来存储和管理碟片。如果您需要更具体的代码实现,请告诉我,我会尽力帮助您。
相关问题
用java编写一个碟包,需要能保存CD和DVD碟片,记录碟片的发行信息,有存入、取出、外借、归还、点数、遍历等功能。
好的,这是一个很实用的程序,需要用到Java的文件操作和面向对象编程知识,您可以使用Java的IO流来实现存储和读取碟片信息,同时可以设计一个CD类和DVD类来记录每个碟片的发行信息,并且可以使用一些数据结构如链表或数组来实现功能。对于存入、取出、外借、归还、点数、遍历等功能,您可以在类中定义相应的方法来实现。
Java代码题 创建一个碟包,需要能保存CD和DVD碟片,记录碟片的发行信息,有存入、取出、外借、归还、点数、遍历等功能。
public class DiscPackage {
private List<Disc> discs;
public DiscPackage() {
discs = new ArrayList<>();
}
public void addDisc(Disc disc) {
discs.add(disc);
}
public Disc getDisc(String title) {
for (Disc disc : discs) {
if (disc.getTitle().equals(title)) {
return disc;
}
}
return null;
}
public void removeDisc(String title) {
for (int i = 0; i < discs.size(); i++) {
if (discs.get(i).getTitle().equals(title)) {
discs.remove(i);
break;
}
}
}
public void lendDisc(String title) {
Disc disc = getDisc(title);
if (disc != null && !disc.isLent()) {
disc.setLent(true);
}
}
public void returnDisc(String title) {
Disc disc = getDisc(title);
if (disc != null && disc.isLent()) {
disc.setLent(false);
}
}
public int getTotalPoints() {
int totalPoints = 0;
for (Disc disc : discs) {
totalPoints += disc.getPoints();
}
return totalPoints;
}
public void printDiscs() {
for (Disc disc : discs) {
System.out.println(disc);
}
}
}
public abstract class Disc {
private String title;
private String artist;
private int releaseYear;
private boolean lent;
private int points;
public Disc(String title, String artist, int releaseYear, int points) {
this.title = title;
this.artist = artist;
this.releaseYear = releaseYear;
this.points = points;
this.lent = false;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public int getReleaseYear() {
return releaseYear;
}
public void setReleaseYear(int releaseYear) {
this.releaseYear = releaseYear;
}
public boolean isLent() {
return lent;
}
public void setLent(boolean lent) {
this.lent = lent;
}
public int getPoints() {
return points;
}
public void setPoints(int points) {
this.points = points;
}
@Override
public String toString() {
return "Title: " + title + ", Artist: " + artist + ", Release Year: " + releaseYear + ", Lent: " + lent + ", Points: " + points;
}
}
public class CD extends Disc {
private int numTracks;
public CD(String title, String artist, int releaseYear, int points, int numTracks) {
super(title, artist, releaseYear, points);
this.numTracks = numTracks;
}
public int getNumTracks() {
return numTracks;
}
public void setNumTracks(int numTracks) {
this.numTracks = numTracks;
}
@Override
public String toString() {
return "CD - " + super.toString() + ", Number of Tracks: " + numTracks;
}
}
public class DVD extends Disc {
private int duration;
public DVD(String title, String artist, int releaseYear, int points, int duration) {
super(title, artist, releaseYear, points);
this.duration = duration;
}
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
@Override
public String toString() {
return "DVD - " + super.toString() + ", Duration: " + duration;
}
}
阅读全文