import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Scanner;
import java.text.ParseException;
public class DVDSet {
String [] name = new String[50];//存储DVD名称数组
int [] state= new int[50];//存储DVD借出状态:0 已借出 1 可借
String[] date= new String[50];//存储DVD借出日期
int [] count = new int[50];//借出次数
}
public class DVDMgr {
DVDSet dvd = new DVDSet();//实例化
int choice;
Scanner input = new Scanner(System.in);
//初始化三张DVD信息
public void initial(){
dvd.name[0]="罗马假日";
dvd.state[0]=0;
dvd.date[0]="2010-7-1";
dvd.count[0]=1;
dvd.name[1]="风声鹤唳";
dvd.state[1]=1;
dvd.name[2]="浪漫满屋";
dvd.state[2]=1;
}
//主菜单
public void startMenu(){
System.out.println("欢迎使用迷你DVD管理器");
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.println("----------------------------------");
System.out.println("请选择:");
choice=input.nextInt();
switch(choice){
case 0:
System.out.println("排行榜");
array();
break;
case 1:
System.out.println("新增DVD");
add();
break;
case 2:
System.out.println("查看DVD");
search();
break;
case 3:
System.out.println("删除DVD");
delete();
break;
case 4:
System.out.println("借出DVD");
lend();
break;
case 5:
System.out.println("归还DVD");
returndvd();
break;
case 6:
System.out.println("谢谢使用");
break;
}
}
//返回主菜单
public void returnMain(){
boolean con;
do{
System.out.println("输入0返回:");
int b = input.nextInt();
if(b==0){
startMenu();
con=false;
break;
}else{
System.out.println("输入错误,异常终止!");
con=true;
}
}while(con==true);
}
//实现新增DVD
public void add(){
System.out.println("请输入DVD名称:");
for(int i=0;i<dvd.name.length;i++){
if(dvd.name[i]==null){
dvd.name[i]=input.next();
dvd.state[i]=1;
System.out.println(" 新增<<"+dvd.name[i]+">>成功!");
break;
}else{
continue;
}
}
System.out.println("****************************************");
returnMain();
}
//查看DVD
public void search(){
System.out.println("序号\t状态\t名称\t借出日期");
for(int i=0;i<dvd.name.length;i++){
if(dvd.name[i]==null){
break;
}else if(dvd.state[i]==0){
System.out.println((i+1)+"\t已借出\t"+"<<"+dvd.name[i]+">>\t"+dvd.date[i]);
}else if(dvd.state[i]==1){
System.out.println((i+1)+"\t可借\t"+"<<"+dvd.name[i]+">>");
}
}
System.out.println("****************************************");
returnMain();
}
//删除DVD
public void delete(){
System.out.println("请输入DVD名称:");
String name = input.next();
for(int i=0;i<dvd.name.length;i++){
if(dvd.name[i]!=null&&dvd.name[i].equalsIgnoreCase(name)&&dvd.state[i]==1){
int j=i;
while(dvd.name[j+1]!=null){
dvd.name[j]=dvd.name[j+1];
dvd.state[j]=dvd.state[j+1];
dvd.date[j]=dvd.date[j+1];
j++;
}
dvd.name[j]=null;
dvd.date[j]=null;
System.out.println("删除<<"+name+">>成功!");
break;
}else if(dvd.name[i]!=null&&dvd.name[i].equalsIgnoreCase(name)&&dvd.state[i]==0){
System.out.println("<<"+name+">>为借出状态,不能删除!");
break;
}
}
System.out.println("****************************************");
returnMain();
}
//借出DVD
public void lend(){
System.out.println("请输入DVD名称:");
String names= input.next();
for(int i=0;i<dvd.name.length;i++){
if(dvd.name[i]==null){
System.out.println("没有找到匹配信息");
break;
}else if(dvd.name[i].equals(names)&&dvd.state[i]==1){
System.out.println("请输入借出日期(年-月-日):");
dvd.date[i]=input.next();
if((dvd.date[i].indexOf("-")==4)&&(dvd.date[i].lastIndexOf("-")==6||dvd.date[i].lastIndexOf("-")==7)){
dvd.state[i]=0;
System.out.println("借出<<"+names+">>成功!");
dvd.count[i]++;
break;
}else{
System.out.println("输入有误,请重新输入");
dvd.date[i]=input.next();
}
}else if(dvd.name[i].equals(names)&&dvd.state[i]==0){
System.out.println(names+"已被借出");
return;
}
}
System.out.println("****************************************");
returnMain();
}
//归还DVD
public void returndvd(){
long b ;
System.out.println("请输入DVD名称:");
String names= input.next();
for(int i=0;i<dvd.name.length;i++){
if(dvd.name[i]==null){
System.out.println("没有归还!");
break;
}else if(dvd.name[i].equals(names)&&dvd.state[i]==0){
System.out.print("请输入归还的日期:( 年-月-日)");
String date=input.next();
b=charge(dvd.date[i],date);
System.out.println("<<"+names+">>归还成功!");
System.out.println("借出的日期为:"+dvd.date[i]);
System.out.println(" 归还的日期为:"+date);
System.out.println("应该付的租金为:"+b+"元");
dvd.date[i]=date;
dvd.state[i]=1;
break;
}else if(names.equals(dvd.name[i])&&dvd.state[i]==1){
System.out.println("该DVD没被借出,无法归还!");
break;
}
}
System.out.println("****************************************");
returnMain();
}
//计算时间差
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 array(){//赋值
String [] names= new String[50];
int counts[]= new int[50];
for(int c=0;c<dvd.name.length;c++){
names[c]=dvd.name[c];
counts[c]=dvd.count[c];
}
//冒泡排序
for(int i=0;i<names.length;i++){
for(int j=i+1;j<names.length;j++){
if(counts[i]>counts[j]){
int a=counts[i];
counts[i]=counts[j];
counts[j]=a;
String d=names[i];
names[i]=names[j];
names[j]=d;
}
}
}
//显示排行榜
System.out.println("****************************");
System.out.println("次数\t名称");
for(int i=names.length-1;i>=0;i--){
System.out.println(counts[i]+"\t"+names[i]);
}
System.out.println("****************************************");
returnMain();
}
}
public class Start {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DVDMgr v = new DVDMgr();
v.initial();
v.startMenu();
}
}