import java.util.*; import java.text.SimpleDateFormat; import java.text.ParseException; class DVDSet { String[] name =new String[50]; int[] state = new int[50]; String[] date = new String[50]; int[] count = new int[50]; } class DVDMgr { DVDSet dvd = new DVDSet(); public void initial(){ dvd.count[0] = 1; dvd.name[0] = "语文书"; dvd.state[0] = 0; dvd.date[0] = "2016-11-11"; dvd.name[1] = "数学书"; dvd.state[1] = 1; dvd.name[2] = "历史书"; dvd.state[2] = 1; } public void startMenu(){ Scanner reader = new Scanner(System.in); System.out.println("欢迎使用迷你管理器!!!"); System.out.println("-----------------------------------------"); System.out.println("0.最新排行榜"); System.out.println("1.新增"); System.out.println("2.查看"); System.out.println("3.删除"); System.out.println("4.借出"); System.out.println("5.归还"); System.out.println("6.退出"); System.out.println("-----------------------------------------"); System.out.print("请选择:"); int choice = reader.nextInt(); System.out.println(); switch (choice) { case 0: show(); returnMain(); break; case 1: add(); returnMain(); break; case 2: check(); returnMain(); break; case 3: del(); returnMain(); break; case 4: borrow(); returnMain(); break; case 5: returnDvd(); returnMain(); break; case 6: System.out.println("谢谢光临"); break; default : System.out.println("输入有误"); break; } } public void returnMain(){ Scanner reader = new Scanner(System.in); System.out.print("输入0返回主菜单:"); if(reader.nextInt() == 0){ startMenu(); } else{ System.out.println("请输入0字符号"); } } //排行 public void show(){ String[] newDvds = new String[50]; for(int i = 0; i < dvd.name.length; i++){ newDvds[i] = dvd.name[i]; } int[] newCounts = new int[50]; for(int i = 0; i < dvd.count.length; i++){ newCounts[i] = dvd.count[i]; } for(int i = 0; i < newDvds.length; i++){ if(newDvds[i] == null){ break; } for(int j = i; j < newDvds.length; j++){ if(newDvds[j] == null){ break; } if(newCounts[i] < newCounts[j]){ int temp = newCounts[i]; newCounts[i] = newCounts[j]; newCounts[j] = temp; String str = newDvds[i]; newDvds[i] = newDvds[j]; newDvds[j] = str; } } } System.out.println("-----------------------------------------"); System.out.println("次数 名称"); for(int i = 0; i < newDvds.length; i ++){ if(newDvds[i] == null){ break; } System.out.println(newCounts[i] + " " + "<<" + newDvds[i] + ">>"); } System.out.println("-----------------------------------------"); } //1.新增 public void add(){ Scanner reader = new Scanner(System.in); for(int i = 0; i < dvd.name.length; i++){ if(dvd.name[i] == null ){ System.out.println("请输入要新增的DVD"); dvd.name[i] = reader.next(); dvd.state[i] = 1; System.out.println("新增成功"); break; } } } //2.查看 public void check(){ System.out.println("序号 状态 名称 借出日期"); for(int i = 0; i < dvd.name.length; i++){ if(dvd.name[i] == null){ break; } if (dvd.state[i] == 1) { System.out.println( i + " " + "可借" + " " + "<<" +dvd.name[i] + ">>" ); } else { System.out.println( i + " " + "不可借" + " " + "<<" + dvd.name[i] + ">>" + " " + dvd.date[i]); } } } //3.删除 public void del(){ Scanner reader = new Scanner(System.in); System.out.println("请输入要删除的DVD"); String name = reader.next(); for(int i = 0; i < dvd.name.length; i++){ if(dvd.name[i] == null){ System.out.println("没有查询到结果"); break; }else if(dvd.name[i].equalsIgnoreCase(name)){ if(dvd.state[i] == 0){ System.out.println("<<" + name + ">>借出状态,不可以删除" ); break; } else{ 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; } } } } //4.借出 public void borrow(){ Scanner reader = new Scanner(System.in); System.out.println("请输入DVD名称"); String name = reader.next(); for(int i = 0; i < dvd.name.length;i++){ if(dvd.name[i] == null){ System.out.println("未查询到结果!"); break; } else if(dvd.name[i].equalsIgnoreCase(name)){ if(dvd.state[i] == 0){ System.out.println("该DVD已经借出"); break; }else{ dvd.state[i] = 0; System.out.println("请输入借出的日期"); dvd.date[i] = reader.next(); dvd.count[i]++; System.out.println("该" + "<<" + dvd.name[i] + ">>" + "DVD已经借出"); break; } } } } //5归还 public void returnDvd(){ Scanner reader = new Scanner(System.in); System.out.println("欢迎光临"); String name = reader.next(); for(int i = 0; i < dvd.name.length;i++){ if(dvd.name[i] == null){ System.out.println("未查询到结果"); break; } if(dvd.name[i].equalsIgnoreCase(name)){ if(dvd.state[i] == 1){ System.out.println("该DVD错误"); }else{ System.out.println("输入日期"); String newDate = reader.next(); System.out.println("归还" + "<<" + name + ">>" + "成功"); System.out.println("借出日期为:" + dvd.date[i]); System.out.println("归还日期为:" + newDate); System.out.println("应付租金(元)" + charge(dvd.date[i],newDate)); dvd.state[i] = 1; break; } } } } //日期计算 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; } } class MiniDvd { public static void main(String[] args) { DVDMgr dm = new DVDMgr(); dm.initial(); dm.startMenu(); } }