package DVD;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.TreeSet;
class Dvd{
String name;
String date;
int state;
int count;
public Dvd(String name,int state,String date,int count){
this.name = name;
this.date = date;
this.state = state;
this.count = count;
}
public String toString() {
// TODO Auto-generated method stub
if(state==1){
return "可借 "+"《"+name+"》 "+date;
}
return "不可借 "+"《"+name+"》 "+date;
}
}
//比较器
class DVDCompar implements Comparator<Dvd>{
@Override
public int compare(Dvd o1, Dvd o2) {
// TODO Auto-generated method stub
return o2.count-o1.count;
}
}
class DvdMgr{
List<Dvd> list = new ArrayList<Dvd>(); //创建一个list集合
public void initial(){
Dvd dvd = new Dvd("罗马假日", 0, "2016-12-1", 20);
Dvd dvd2 = new Dvd("浪漫满屋", 1, " ", 30);
Dvd dvd3 = new Dvd("风声鹤唳", 1, " ", 10);
// 初始化集合
list.add(dvd);
list.add(dvd2);
list.add(dvd3);
}
// 菜单
public void returnMenu(){
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.print("请选择:");
Scanner scanner = new Scanner(System.in);
int input = scanner.nextInt();
switch (input) {
case 0:
Top();
break;
case 1:
add();
break;
case 2:
search();
break;
case 3:
delete();
break;
case 4:
lend();
break;
case 5:
returnDVD();
break;
case 6:
System.out.println("*****欢迎下次继续使用!*****");
break;
default:
System.out.println("输入错误,这个功能正在完善中....");
break;
}
}
//添加
public void add(){
System.out.println("欢迎进入新增页面!");
System.out.print("请输入DVD名称: ");
Scanner scanner = new Scanner(System.in);
String inputName = scanner.next();
int num = list.size();
Dvd dvd = new Dvd(inputName, 1, " ", 0);
list.add(dvd);
if(list.size()>num){
System.out.println("新增《"+inputName+"》成功");
returnCom();
} else {
System.out.println("新增失败");
}
}
//查找
public void search(){
System.out.println("********欢迎进入查找页面********
");
System.out.println("序号 状态 "+"名称 "+"借出日期");
for(int i = 0; i < list.size(); i++){
System.out.println((i+1)+" "+list.get(i));
}
returnCom();
}
//删除
public void delete(){
System.out.println("*****欢迎进入删除页面*****");
while(true){
System.out.print("请输入DVD名称:");
Scanner scanner = new Scanner(System.in);
String input = scanner.next();
boolean b = false;
for(int i = 0; i < list.size(); i++){
if(list.get(i).name.equals(input)){
list.remove(i);
b = true;
}
}
if(b == true){
System.out.println("删除《"+input+"》成功");
returnCom();
}else {
System.out.println("没有这个DVD,请重新输入!");
}
}
}
// 借出
public void lend(){
System.out.println("******欢迎进入借出页面*****");
System.out.print("请输入DVD名称:");
Scanner scanner = new Scanner(System.in);
String input = scanner.next();
boolean b = false;
for(int i = 0; i < list.size(); i++){
Dvd dvd = list.get(i);
if(dvd.name.equals(input)){
b = true;
if(dvd.state==1){
System.out.println("请输入借出日期:(yyyy-MM-dd):");
Scanner scanner2 = new Scanner(System.in);
String string = scanner2.next();
dvd.date = string;
dvd.state = 0;
dvd.count++;
}else {
System.out.println("您要借的DVD已被借出!");
}
}
}
if(b == false){
System.out.println("您输入的DVD不存在");
}
returnCom();
}
//归还
public void returnDVD(){
System.out.println("******欢迎进入归还页面*****");
System.out.print("请输入DVD名称:");
Scanner scanner = new Scanner(System.in);
String input = scanner.next();
for(int i = 0; i < list.size(); i++){
Dvd dvd = list.get(i);
if(dvd.name.equals(input)){
if(dvd.state == 0){
dvd.state = 1;
System.out.print("请输入归还的日期(yyyy-MM-dd): ");
Scanner scanner2 = new Scanner(System.in);
String string = scanner2.next();
long money = money(string,dvd);
System.out.println("归还《"+input+"》成功");
System.out.println("需要付租金(¥):"+money);
}else {
System.out.println("这DVD不是我们这里的吧,我们的DVD没有被借出!");
}
}
}
returnCom();
}
// 算钱
public long money(String string, Dvd dvd){
long change = 0;
SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd"); // 字符串转为Date做计算
try{
Date d1 = sd.parse(dvd.date); //该时间是以毫秒为单位,计算时记得转换
Date d2 = sd.parse(string);
change = (d2.getTime()-d1.getTime())/(24*60*60*1000); //转换得到天
} catch(ParseException e){
e.printStackTrace();
}
dvd.date = " ";
return change;
}
// 排行榜
public void Top(){
System.out.println("------TOP------");
System.out.println("次数 名称");
Iterator<Dvd> it = list.iterator();
TreeSet<Dvd> tSet = new TreeSet<>(new DVDCompar()); //传个比较器比较
while(it.hasNext()){
tSet.add(it.next());
}
it = tSet.iterator();
while(it.hasNext()){
Dvd dvd = it.next();
System.out.println(dvd.count+" "+dvd.name);
}
returnCom();
}
// 输入0返回菜单
public void returnCom(){
while(true){
System.out.print("请输入0返回: ");
Scanner scanner2 = new Scanner(System.in);
int inputNum = scanner2.nextInt();
if(inputNum == 0){
System.out.println();
returnMenu();
break;
} else {
System.out.println("指令错误,请重新输入!");
}
}
}
}
public class Demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
DvdMgr dvdMgr = new DvdMgr();
dvdMgr.initial();
dvdMgr.returnMenu();
}
}