用java编写一个碟包,需要能保存CD和DVD碟片,记录碟片的发行信息,有存入、取出、外借、归还、点数、遍历等功能的程序
时间: 2024-05-21 12:13:14 浏览: 102
很高兴回答你的问题。您可以使用Java编写一个碟包程序,其中包括保存CD和DVD碟片、记录碟片的发行信息、存入、取出、外借、归还、点数、遍历等功能。您可以使用Java的面向对象编程来实现这些功能,例如创建一个Disc类来表示CD和DVD碟片,使用HashMap来记录碟片的发行信息,以及使用ArrayList来存储和管理碟片。如果您需要更具体的代码实现,请告诉我,我会尽力帮助您。
相关问题
用java编写一个碟包,需要能保存CD和DVD碟片,记录碟片的发行信息,有存入、取出、外借、归还、点数、遍历等功能。
好的,这是一个很实用的程序,需要用到Java的文件操作和面向对象编程知识,您可以使用Java的IO流来实现存储和读取碟片信息,同时可以设计一个CD类和DVD类来记录每个碟片的发行信息,并且可以使用一些数据结构如链表或数组来实现功能。对于存入、取出、外借、归还、点数、遍历等功能,您可以在类中定义相应的方法来实现。
JAVA代码题 创建一个碟包,需要能保存CD和DVD碟片,记录碟片的发行信息,有存入、取出、外借、归还、点数、遍历等功能。
以下是一个简单的Java代码实现,包含了所述的基本功能:
// 碟片类
class Disc {
private String title; // 碟片名称
private String artist; // 艺术家/演员
private String genre; // 风格/流派
private int year; // 发行年份
private boolean isLent; // 是否借出
private String borrower; // 借出人
public Disc(String title, String artist, String genre, int year) {
this.title = title;
this.artist = artist;
this.genre = genre;
this.year = year;
this.isLent = false;
this.borrower = null;
}
public String getTitle() {
return title;
}
public String getArtist() {
return artist;
}
public String getGenre() {
return genre;
}
public int getYear() {
return year;
}
public boolean isLent() {
return isLent;
}
public void lend(String borrower) {
if (isLent) {
System.out.println("This disc is already lent out!");
} else {
isLent = true;
this.borrower = borrower;
System.out.println("Successfully lent out to " + borrower);
}
}
public void returnDisc() {
if (isLent) {
isLent = false;
borrower = null;
System.out.println("Successfully returned!");
} else {
System.out.println("This disc is not currently lent out.");
}
}
public void printDetails() {
System.out.println("Title: " + title);
System.out.println("Artist: " + artist);
System.out.println("Genre: " + genre);
System.out.println("Year: " + year);
if (isLent) {
System.out.println("Currently lent out to: " + borrower);
} else {
System.out.println("Currently available.");
}
}
}
// 碟包类
class DiscPackage {
private ArrayList<Disc> discs;
public DiscPackage() {
discs = new ArrayList<Disc>();
}
public void addDisc(Disc disc) {
discs.add(disc);
System.out.println("Successfully added disc: " + disc.getTitle());
}
public void removeDisc(Disc disc) {
if (discs.remove(disc)) {
System.out.println("Successfully removed disc: " + disc.getTitle());
} else {
System.out.println("Disc not found in package.");
}
}
public Disc findDisc(String title) {
for (Disc disc : discs) {
if (disc.getTitle().equals(title)) {
return disc;
}
}
return null;
}
public void lendDisc(String title, String borrower) {
Disc disc = findDisc(title);
if (disc != null) {
disc.lend(borrower);
} else {
System.out.println("Disc not found in package.");
}
}
public void returnDisc(String title) {
Disc disc = findDisc(title);
if (disc != null) {
disc.returnDisc();
} else {
System.out.println("Disc not found in package.");
}
}
public void printDetails() {
if (discs.size() == 0) {
System.out.println("The disc package is empty.");
} else {
System.out.println("Disc package contents:");
for (Disc disc : discs) {
disc.printDetails();
System.out.println();
}
}
}
}
// 测试代码
public class Main {
public static void main(String[] args) {
Disc cd = new Disc("The Dark Side of the Moon", "Pink Floyd", "Progressive rock", 1973);
Disc dvd = new Disc("The Lord of the Rings: The Fellowship of the Ring", "Peter Jackson", "Fantasy", 2001);
DiscPackage package1 = new DiscPackage();
package1.addDisc(cd);
package1.addDisc(dvd);
package1.printDetails();
package1.lendDisc("The Dark Side of the Moon", "John Smith");
package1.lendDisc("The Dark Side of the Moon", "Jane Doe");
package1.printDetails();
package1.returnDisc("The Dark Side of the Moon");
package1.returnDisc("The Dark Side of the Moon");
package1.printDetails();
}
}
阅读全文