请用java编写一个程序实现以下内容,为音像店开发一个迷你DVD管理器实现DVD碟片的管理,包括如下功能::新增DVD,查看DVD,删除DVD,借出DVD,归还DVD
时间: 2023-08-09 07:12:14 浏览: 52
好的,以下是一个简单的迷你DVD管理器的Java代码:
```
import java.util.ArrayList;
import java.util.Scanner;
public class DVDCollection {
private ArrayList<DVD> collection;
public DVDCollection() {
collection = new ArrayList<DVD>();
}
public void addDVD(DVD dvd) {
collection.add(dvd);
System.out.println(dvd.getTitle() + " 已添加到DVD收藏中。");
}
public void viewDVDs() {
if (collection.size() == 0) {
System.out.println("DVD收藏为空。");
} else {
System.out.println("DVD收藏:");
for (int i = 0; i < collection.size(); i++) {
System.out.println((i+1) + ". " + collection.get(i).toString());
}
}
}
public void deleteDVD(int index) {
if (index < 1 || index > collection.size()) {
System.out.println("无效的DVD编号。");
} else {
DVD dvd = collection.remove(index-1);
System.out.println(dvd.getTitle() + " 已从DVD收藏中删除。");
}
}
public void borrowDVD(int index) {
if (index < 1 || index > collection.size()) {
System.out.println("无效的DVD编号。");
} else {
DVD dvd = collection.get(index-1);
if (dvd.isAvailable()) {
dvd.setAvailable(false);
System.out.println(dvd.getTitle() + " 已借出。");
} else {
System.out.println(dvd.getTitle() + " 已被借出,无法借阅。");
}
}
}
public void returnDVD(int index) {
if (index < 1 || index > collection.size()) {
System.out.println("无效的DVD编号。");
} else {
DVD dvd = collection.get(index-1);
if (!dvd.isAvailable()) {
dvd.setAvailable(true);
System.out.println(dvd.getTitle() + " 已归还。");
} else {
System.out.println(dvd.getTitle() + " 已经在DVD收藏中,无需归还。");
}
}
}
public static void main(String[] args) {
DVDCollection dvdCollection = new DVDCollection();
Scanner input = new Scanner(System.in);
while (true) {
System.out.println("\n请选择操作:\n1. 新增DVD\n2. 查看DVD\n3. 删除DVD\n4. 借出DVD\n5. 归还DVD\n6. 退出");
int choice = input.nextInt();
switch (choice) {
case 1:
System.out.print("请输入DVD标题:");
input.nextLine();
String title = input.nextLine();
System.out.print("请输入DVD类型:");
String type = input.nextLine();
DVD dvd = new DVD(title, type);
dvdCollection.addDVD(dvd);
break;
case 2:
dvdCollection.viewDVDs();
break;
case 3:
System.out.print("请输入要删除的DVD编号:");
int indexToDelete = input.nextInt();
dvdCollection.deleteDVD(indexToDelete);
break;
case 4:
System.out.print("请输入要借出的DVD编号:");
int indexToBorrow = input.nextInt();
dvdCollection.borrowDVD(indexToBorrow);
break;
case 5:
System.out.print("请输入要归还的DVD编号:");
int indexToReturn = input.nextInt();
dvdCollection.returnDVD(indexToReturn);
break;
case 6:
System.out.println("谢谢使用DVD管理器。");
System.exit(0);
break;
default:
System.out.println("无效的选项。");
break;
}
}
}
}
class DVD {
private String title;
private String type;
private boolean available;
public DVD(String title, String type) {
this.title = title;
this.type = type;
this.available = true;
}
public String getTitle() {
return title;
}
public String getType() {
return type;
}
public boolean isAvailable() {
return available;
}
public void setAvailable(boolean available) {
this.available = available;
}
public String toString() {
String status = available ? "可借" : "不可借";
return title + " (" + type + ") - " + status;
}
}
```
这个程序使用了ArrayList来存储DVD对象,并提供了新增、查看、删除、借出和归还DVD的功能。DVD类包含了标题、类型和可用性状态等属性,并提供了相应的get和set方法。在主函数中,我们使用了一个简单的用户界面来展示DVD管理器的功能,并使用switch语句来处理用户输入的操作。