import java.util.*;
import java.text.*;
class DVDSet
{
String[] name = new String[50]; //名字
int[] state = new int[50]; //状态 1为可借,0为借出
String[] date = new String[50]; //借出日期
int[] count = new int[50]; //排行榜
}
class DVDMgr
{
// 创建一个对象
DVDSet dvd = new DVDSet();
// 初始值
public void initial(){
dvd.name[0] = "罗马假日";
dvd.state[0] = 0;
dvd.date[0] = "2016-11-17";
dvd.count[0] = 20;
dvd.name[1] = "浪漫满屋";
dvd.state[1] = 1;
dvd.date[1] = " ";
dvd.count[1] = 30;
dvd.name[2] = "阿甘正传";
dvd.state[2] = 1;
dvd.date[2] = " ";
dvd.count[2] = 10;
}
// 添加
public void add(){
System.out.print("请输入DVD名称:");
Scanner sc = new Scanner(System.in);
String newD = sc.next();
for(int i = 0;i < dvd.name.length; i++){
// 添加就是给数组的第一个null元素赋值
if(dvd.name[i] == null){
dvd.name[i] = newD;
dvd.state[i] = 1;
dvd.date[i] = " ";
System.out.println("新增《"+newD+"》成功!");
break;
}
}
}
// 查看
public void search(){
// 直接遍历数组 state为0 为不可借, 1为可借
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) + " "+ "不可借" + " 《"+ dvd.name[i] +"》 " + dvd.date[i]);
} else if(dvd.state[i] == 1){
System.out.println((i+1) + " "+ "可借" + " 《"+ dvd.name[i] +"》 " + dvd.date[i]);
}
}
}
// 删除
public void delete(){
System.out.print("请输入DVD名称:");
Scanner sc = new Scanner(System.in);
String scDVD = sc.next();
for(int i = 0;i < dvd.name.length;i++){
if(dvd.name!=null &&dvd.name[i].equalsIgnoreCase(scDVD) && 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;
dvd.state[j] = 0;
System.out.println("删除《"+scDVD+"》成功!");
break;
} else if(dvd.name!=null &&dvd.name[i].equalsIgnoreCase(scDVD) && dvd.state[i] == 0){
System.out.println("《"+dvd.name[i]+"》该书为借出状态,不能删除");
break;
}
}
}
// 借出
public void lend(){
System.out.print("请输入DVD名称:");
Scanner sc = new Scanner(System.in);
String scDVD = sc.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(scDVD)&& dvd.state[i]==1){
dvd.state[i] = 0; // 借出时把状态设置为已借出, 模拟被借走
System.out.print("请输入借出日期(年-月-日):");
Scanner input = new Scanner(System.in);
dvd.date[i]=input.next();
System.out.println("借出《"+scDVD+"》成功!");
dvd.count[i]++;//借出一次 借出次数就加1
break;
}else if(dvd.name[i].equals(scDVD)&& dvd.state[i]==0){
System.out.println("《"+scDVD+"》已被借出!");
break;
}
}
}
// 归还 并且模拟需要租金
public void returnDvd(){
System.out.print("请输入DVD名称:");
Scanner sc = new Scanner(System.in);
String scDVD = sc.next();
for(int i = 0;i < dvd.name.length;i++){
if(dvd.name[i].equals(scDVD)){
System.out.print("请输入归还日期(年-月-日):");
Scanner sc1 = new Scanner(System.in);
String returnDVD = sc.next();
long num = change(dvd.date[i],returnDVD); // 获取时间差
System.out.println();
System.out.println("归还《"+dvd.name[i]+"》成功。");
System.out.println("借出日期为:"+dvd.date[i]);
System.out.println("归还日期为:"+returnDVD);
dvd.state[i] = 1; // 归还后 状态设置为1
dvd.date[i] = " "; //清除时间
System.out.println("应付租金(元): "+num);
break;
}else {
System.out.println("请输入正确的DVD!");
}
}
}
//计算日期差
public long change(String str1, String str2){
long change = 0;
SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd"); // 字符串转为Date做计算
try{
Date d1 = sd.parse(str1); //该时间是以毫秒为单位,计算时记得转换
Date d2 = sd.parse(str2);
change = (d2.getTime()-d1.getTime())/(24*60*60*1000); //转换得到天
} catch(ParseException e){
e.printStackTrace();
}
return change;
}
// 排行榜
public void list (){
int count = 0;
String[] n = new String[50];
int[] c = new int[50];
for(int i = 0; i < n.length; i++){
c[i] = dvd.count[i];
n[i] = dvd.name[i];
}
for(int i = 0;i < c.length; i++){ //使用排序算法
for(int j = 1; j < c.length - i;j++){
if(c[j]>c[j-1]){
String stemp = n[j];
n[j] = n[j-1];
n[j-1] = stemp;
int temp = c[j];
c[j] = c[j-1];
c[j-1] = temp;
}
}
}
System.out.println("次数 名称");
for(int i = 0;i < c.length; i++){
if(n[i]!=null){
System.out.println(c[i]+" "+n[i]);
}
}
}
// 菜单
public void startMenu(){
//界面的提示
System.out.println("欢迎使用迷你DVD管理器\n----------------------------------------");
String[] content = {"0. 借出排行帮","1. 新增DVD","2. 查看DVD","3. 删除DVD","4. 借出DVD","5. 归还DVD","6. 退 出"};
for(String s:content){
System.out.println(s);
}
System.out.println("----------------------------------------\n");
System.out.print("请选择:");
Scanner s = new Scanner(System.in);
int num = s.nextInt();
//switch 的多种选择
switch(num){
case 0:
System.out.println("---> 排行榜\n");
System.out.println("**************************");
list(); //排行榜
System.out.println("**************************");
returnMain();
break;
case 1:
System.out.println("---> 新增DVD\n");
add(); //添加
System.out.println("**************************");
returnMain();
break;
case 2:
System.out.println("---> 查看DVD\n");
System.out.println("序号 状态 名称 借出日期");
search(); // 查看
System.out.println("******************************");
returnMain();
break;
case 3:
System.out.println("---> 删除DVD\n");
delete();//删除
System.out.println("******************************");
returnMain();
break;
case 4:
System.out.println("---> 借出DVD\n");
lend(); //借出
System.out.println("******************************");
returnMain();
break;
case 5:
System.out.println("---> 归还DVD\n");
returnDvd(); //归还
System.out.println("******************************");
returnMain();
break;
default:
System.out.println("欢迎下次继续使用!");
break;
}
}
// 返回键
public void returnMain(){
Scanner input = new Scanner(System.in);
System.out.print("请输入0返回:");
if(input.nextInt() == 0){
startMenu();
} else {
System.out.println("输入错误,异常终止!");
}
}
}
class Demo
{
public static void main(String[] args)
{
DVDMgr dvdM = new DVDMgr();
//初始化数组
dvdM.initial();
// 开始菜单
dvdM.startMenu();
}
}