难题解疑
为某音像店开发一个迷你DVD管理器,实现DVD碟片的管理,主要包括如下功能。 ² 新增DVD。 ² 查看DVD。 ² 删除DVD。 ² 借出DVD。 ² 归还DVD。 项目的运行结果如
//时间和删除DVD那没搞清楚,分享一下大神作品
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner;
import javax.xml.crypto.Data;
import org.omg.Messaging.SyncScopeHelper;
public class DVDMgr {
DVDSet dvdSet = new DVDSet();
public void initial()
{
this.dvdSet.name[0] = "罗马假日";
this.dvdSet.state[0] = 0;
this.dvdSet.date[0] = "2013-7-1";
this.dvdSet.name[1] = "风声鹤唳";
this.dvdSet.state[1] = 1;
this.dvdSet.date[1] = "";
this.dvdSet.name[2] = "浪漫满屋";
this.dvdSet.state[2] = 1;
this.dvdSet.date[2] = "";
dvdSet.count[0] = 5;
dvdSet.count[1] = 10;
dvdSet.count[2] = 7;
}
public void startMenu(){
Scanner scanner = new Scanner(System.in);
System.out.println("----------------------------");
System.out.println("0.排行榜");
System.out.println("1.新增DVD");
System.out.println("2.查看DVD");
System.out.println("3.删除DVD");
System.out.println("4.借出DVD");
System.out.println("5.归还DVD");
System.out.println("6. 退 出");
System.out.print("请选择:");
int choice = scanner.nextInt();
switch (choice) {
case 0:
list();
returnMain();
break;
case 1:
System.out.println("此处实现新增 DVD");
System.out.println("*******************************");
add();
returnMain();
break;
case 2:
System.out.println("此处实现查看DVD");
System.out.println("*******************************");
search();
returnMain();
break;
case 3:
System.out.println("此处实现删除DVD");
System.out.println("*******************************");
delete();
returnMain();
break;
case 4:
System.out.println("此处实现借出DVD");
System.out.println("*******************************");
lend();
returnMain();
break;
case 5:
System.out.println("此处实现归还DVD");
System.out.println("*******************************");
returnDvd();
returnMain();
break;
case 6:
return ;
}
}
public void returnMain()
{
Scanner input = new Scanner(System.in);
System.out.print("输入 0 返回");
int in = input.nextInt();
if(in==0){
startMenu();
}else{
System.out.println("输入错误,异常终止!");
}
}
//实现查看 DVD 信息
public void search(){
System.out.println("序号\t\t名称\t\t状态\t\t借出日期");
for (int i = 0; dvdSet.name[i] != null; i++) {
System.out.println((i+1)+"\t\t" +dvdSet.name[i] +"\t\t" +dvdSet.state[i]+"\t\t"+dvdSet.date[i] );
}
}
//实现新增 DVD 信息
public void add()
{
int newIndex = 0;
for (newIndex = 0; dvdSet.name[newIndex] != null & newIndex < dvdSet.name.length; newIndex++) {
}
System.out.println("输入新增的DVD名:");
if (newIndex < dvdSet.name.length) {
dvdSet.name[newIndex] = new Scanner(System.in).next();
dvdSet.state[newIndex] = 1;
dvdSet.date[newIndex] ="";
System.out.println("添加成功!");
}else{
System.out.println("书籍已满,不能再添加新书籍");
}
}
//实现删除 DVD 信息
public void delete()
{
System.out.println("请输入要删除的DVD名称");
String deleteName = new Scanner(System.in).next();
int deleteState = 1; //要删除的DVD的借出状态
int deleteIndex = 0;//要删除DVD在数组中的标号
int lastIndex = 0;//第一个可插入的下标
//找到数组中第一个可插入的下标
for (lastIndex = 0;dvdSet.name[lastIndex] != null ;lastIndex++) {
}
//System.out.println(lastIndex);
//找出要删除的书是否存在,如果存在,记录是否借出
for (deleteIndex= 0; dvdSet.name[deleteIndex] != null ; deleteIndex++) {
if (dvdSet.name[deleteIndex].equals(deleteName) ) {
deleteState = dvdSet.state[deleteIndex];
break;
}
}
//判断是否存在这本书
if (lastIndex == deleteIndex) {
System.out.println("没有此DVD");
}else
{
//判断是否为借出状态
if (deleteState == 0) {
System.out.println("此DVD是借出状态,不能删除!");
} else {
for (;dvdSet.name[deleteIndex] != null; deleteIndex++) {
dvdSet.name[deleteIndex] = dvdSet.name[deleteIndex+1];
dvdSet.state[deleteIndex] = dvdSet.state[deleteIndex+1];
dvdSet.date[deleteIndex] = dvdSet.date[deleteIndex+1];
}
System.out.println("删除成功!");
}
}
//System.out.println(deleteIndex);
//System.out.println(deleteState);
}
//实现借出 DVD 的业务处理
public void lend()
{
System.out.println("请输入要借的DVD:");
String lendName = new Scanner(System.in).next();
System.out.println("请输入借出日期(年-月-日):");
//String returnDate = new Scanner(System.in).next();
String lendDate = new Scanner(System.in).next();
//是否存在此书
for (int i = 0; dvdSet.name[i] != null; i++) {
if (dvdSet.name[i].equals(lendName)) {
if (dvdSet.state[i] ==0 ) {
System.out.println("此DVD已被借出");
} else {
dvdSet.state[i] = 0;
dvdSet.date[i] = lendDate;
System.out.println("借DVD成功!");
dvdSet.count[i]++;
return ;
}
}
}
}
//实现归还 DVD 的业务处理
public void returnDvd()
{
System.out.println("输入要归还的DVD名称:");
String returnName = new Scanner(System.in).next();
System.out.println("输入归还的日期:");
String returnDate = new Scanner(System.in).next();
long charge = 0;
SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd");
for (int i = 0; dvdSet.name[i] != null; i++)
{
if (dvdSet.name[i].equals(returnName)) {
if (dvdSet.state[i] == 1) {
System.out.println("错误,此DVD是没有借出的");
return ;
}
else {
System.out.println("归还<"+returnName+">成功!");
System.out.println("借出日期:"+dvdSet.date[i]);
System.out.println("归还日期:"+returnDate);
System.out.println("应付租金:"+charge(dvdSet.date[i],returnDate));
dvdSet.date[i] = "";
dvdSet.state[i] = 1;
}
}
}
System.out.println("没有找到此DVD!");
}
//计算日期差
public long charge(String dstr1,String dstr2){
long charge=0;
SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd");
try {
Date d1=sd.parse(dstr1);
Date d2=sd.parse(dstr2);
charge=(d2.getTime()-d1.getTime())/(24*60*60*1000);
} catch (ParseException e) {
e.printStackTrace();
}
return charge;
}
//排行榜
public void list()
{
}
}
public class DVDSet {
/*String name; //碟片名称
int state; //是否可借,0代表不可借,1代表可借
String date; //借出时间
*/
String[] name = new String[50];
int[] state = new int[50];
String[] date = new String[50];
int[] count = new int[50];
}
public class Start {
public static void main(String[] args) {
DVDMgr dvdMgr = new DVDMgr();
dvdMgr.initial();
dvdMgr.startMenu();
}
}