为某音像店开发一个迷你DVD管理器,实现DVD碟片的管理,包括以下功能:新增DVD,查看DVD,删除DVD,借出DVD,归还DVD。编写一个java程序实现以上要求
时间: 2023-07-31 07:10:10 浏览: 49
好的,下面是一个简单的Java程序实现DVD管理器的基本功能:
```
import java.util.ArrayList;
import java.util.Scanner;
public class DVDCollection {
private ArrayList<DVD> collection = new ArrayList<DVD>();
private int count = 0;
public void addDVD() {
Scanner input = new Scanner(System.in);
System.out.println("请输入DVD名称:");
String name = input.nextLine();
System.out.println("请输入DVD类型:");
String type = input.nextLine();
System.out.println("请输入DVD出版日期:");
String date = input.nextLine();
DVD dvd = new DVD(name, type, date);
collection.add(dvd);
count++;
System.out.println("DVD添加成功!");
}
public void viewDVD() {
if (count == 0) {
System.out.println("DVD列表为空!");
} else {
for (int i = 0; i < count; i++) {
System.out.println("DVD名称:" + collection.get(i).getName());
System.out.println("DVD类型:" + collection.get(i).getType());
System.out.println("DVD出版日期:" + collection.get(i).getDate());
System.out.println("DVD状态:" + collection.get(i).getStatus());
}
}
}
public void deleteDVD() {
if (count == 0) {
System.out.println("DVD列表为空!");
} else {
Scanner input = new Scanner(System.in);
System.out.println("请输入要删除的DVD名称:");
String name = input.nextLine();
boolean flag = false;
for (int i = 0; i < count; i++) {
if (collection.get(i).getName().equals(name)) {
collection.remove(i);
count--;
flag = true;
System.out.println("DVD删除成功!");
break;
}
}
if (!flag) {
System.out.println("未找到该DVD!");
}
}
}
public void borrowDVD() {
if (count == 0) {
System.out.println("DVD列表为空!");
} else {
Scanner input = new Scanner(System.in);
System.out.println("请输入要借出的DVD名称:");
String name = input.nextLine();
boolean flag = false;
for (int i = 0; i < count; i++) {
if (collection.get(i).getName().equals(name)) {
if (collection.get(i).getStatus().equals("已借出")) {
System.out.println("该DVD已被借出!");
} else {
collection.get(i).setStatus("已借出");
flag = true;
System.out.println("DVD借出成功!");
}
break;
}
}
if (!flag) {
System.out.println("未找到该DVD!");
}
}
}
public void returnDVD() {
if (count == 0) {
System.out.println("DVD列表为空!");
} else {
Scanner input = new Scanner(System.in);
System.out.println("请输入要归还的DVD名称:");
String name = input.nextLine();
boolean flag = false;
for (int i = 0; i < count; i++) {
if (collection.get(i).getName().equals(name)) {
if (collection.get(i).getStatus().equals("已归还")) {
System.out.println("该DVD已被归还!");
} else {
collection.get(i).setStatus("已归还");
flag = true;
System.out.println("DVD归还成功!");
}
break;
}
}
if (!flag) {
System.out.println("未找到该DVD!");
}
}
}
public static void main(String[] args) {
DVDCollection dvdCollection = new DVDCollection();
Scanner input = new Scanner(System.in);
int choice = 0;
while (true) {
System.out.println("1.新增DVD 2.查看DVD 3.删除DVD 4.借出DVD 5.归还DVD 6.退出");
System.out.println("请选择要执行的操作:");
choice = input.nextInt();
switch (choice) {
case 1:
dvdCollection.addDVD();
break;
case 2:
dvdCollection.viewDVD();
break;
case 3:
dvdCollection.deleteDVD();
break;
case 4:
dvdCollection.borrowDVD();
break;
case 5:
dvdCollection.returnDVD();
break;
case 6:
System.exit(0);
break;
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
}
class DVD {
private String name;
private String type;
private String date;
private String status;
public DVD(String name, String type, String date) {
this.name = name;
this.type = type;
this.date = date;
this.status = "可借";
}
public String getName() {
return name;
}
public String getType() {
return type;
}
public String getDate() {
return date;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
```
在这个程序中,我们定义了一个`DVDCollection`类来管理DVD的相关操作,其中包括了新增DVD、查看DVD、删除DVD、借出DVD和归还DVD等功能。我们还定义了一个`DVD`类来表示DVD的基本信息,包括名称、类型、出版日期和状态(可借、已借出、已归还)。
在程序的主函数中,我们通过循环来接受用户的输入,并根据用户的选择来执行相应的DVD管理操作。
注意,在程序中我们使用了`ArrayList`来存储DVD的信息,并通过`count`变量来记录DVD的数量。当用户选择查看DVD、删除DVD、借出DVD或归还DVD时,程序会遍历`ArrayList`中的所有DVD对象,并根据用户输入的名称来匹配相应的DVD对象。如果找到了相应的DVD对象,则进行相应的操作,否则输出未找到该DVD的提示。
以上就是一个简单的Java程序实现DVD管理器的基本功能的示例。