1 package 作业; 2 3 import java.text.DateFormat; 4 import java.text.SimpleDateFormat; 5 import java.util.ArrayList; 6 import java.util.Date; 7 import java.util.Iterator; 8 import java.util.Scanner; 9 10 import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException; 11 12 13 public class DVD管理器 { 14 15 static ArrayList<Object> arr = new ArrayList<>(); 16 static Scanner sc = new Scanner(System.in); 17 18 public static void main(String[] args) throws java.text.ParseException { 19 Dvd dvd0 = new Dvd(0,"罗马假日","已借出","2016-11-12"); 20 Dvd dvd1 = new Dvd(1,"风声鹤唳","可借",""); 21 Dvd dvd2 = new Dvd(2,"浪漫满屋","可借",""); 22 arr.add(dvd0); 23 arr.add(dvd1); 24 arr.add(dvd2); 25 boolean backtrack = true; 26 while(backtrack){ 27 String[] content = {"1.新增DVD","2.查看DVD","3.删除DVD","4.借出DVD","5.归还DVD","6.退出DVD"}; 28 System.out.println(" 欢迎使用迷你DVD管理器"); 29 System.out.println("------------------------------------"); 30 for(String i:content) 31 System.out.println(i); 32 System.out.println("------------------------------------"); 33 System.out.print("请选择: "); 34 int number = sc.nextInt(); 35 switch (number){ 36 case 1:System.out.println("---> 新增DVD");newly();break; 37 case 2:System.out.println("---> 查看DVD");examine();break; 38 case 3:System.out.println("---> 删除DVD");delete();break; 39 case 4:System.out.println("---> 借出DVD");loan();break; 40 case 5:System.out.println("---> 归还DVD");rerutn();break; 41 case 6:backtrack = quit();break; 42 default:break; 43 } 44 } 45 System.out.println("谢谢使用! "); 46 } 47 48 //新增 49 public static void newly(){ 50 System.out.print("请输入DVD名称: "); 51 String name = sc.next(); 52 Dvd dvd = new Dvd(arr.size(),name,"可借",""); 53 arr.add(dvd); 54 System.out.println("添加成功"); 55 } 56 57 //查看 58 public static void examine(){ 59 System.out.println("序号"+" "+"名称"+" "+"状态"+" "+"日期"); 60 Iterator<Object> it = arr.iterator(); 61 while(it.hasNext()){ 62 System.out.println(it.next()); 63 } 64 } 65 66 //删除 67 public static void delete(){ 68 System.out.print("请输入DVD序号(number): "); 69 int temp = sc.nextInt(); 70 if(temp < 0 || temp > arr.size()-1){ 71 System.out.print("没有该DVD序号,请重新输入 "); 72 }else{ 73 arr.remove(temp); 74 } 75 } 76 77 //借出 78 public static void loan(){ 79 System.out.print("请输入借出DVD序号(number): "); 80 int temp = sc.nextInt(); 81 if(temp < 0 || temp > arr.size()-1){ 82 System.out.print("没有该DVD序号,请重新输入 "); 83 }else { 84 Dvd dvd = (Dvd)arr.get(temp); 85 if(dvd.status.equals("已借出")){ 86 System.out.println("该书已经被借出,不可再借!"); 87 }else{ 88 //获取系统当前日期 89 Date date = new Date(); 90 DateFormat df = DateFormat.getDateInstance(); 91 String day = df.format(date); 92 dvd.status = "已借出"; 93 dvd.date = day; 94 arr.set(temp, dvd); 95 System.out.println("借出成功!"); 96 } 97 } 98 } 99 100 //归还 101 public static void rerutn() { 102 System.out.print("请输入归还的DVD序号: "); 103 int temp = sc.nextInt(); 104 if(temp < 0 || temp > arr.size()-1){ 105 System.out.print("没有该DVD序号,请重新输入 "); 106 }else { 107 Dvd dvd = (Dvd)arr.get(temp); 108 if(dvd.status.equals("已借出")){ 109 //获取系统当前日期 110 Date date = new Date(); 111 DateFormat df = DateFormat.getDateInstance(); 112 String day = df.format(date); 113 dvd.status = "可借"; 114 arr.set(temp, dvd); 115 System.out.println("借出日期为: "+dvd.date); 116 System.out.println("归还日期为: "+day); 117 118 //计算租金 119 DateFormat sd = new SimpleDateFormat("yyyy-MM-dd"); 120 long money = 0; 121 122 try{ 123 Date d3 = sd.parse(day); 124 Date d2 = sd.parse(dvd.date); 125 money = (d3.getTime()-d2.getTime())/(24*60*60*1000); 126 }catch (Exception e) { 127 e.printStackTrace(); 128 } 129 130 131 System.out.println("应付租金(元): "+money); 132 System.out.println("归还成功!"); 133 }else{ 134 System.out.println("该书未被借出,不需归还!"); 135 } 136 } 137 } 138 139 //退出 140 public static boolean quit(){ 141 return false; 142 } 143 } 144 145 146 class Dvd{ 147 int id; 148 String name; 149 String status; 150 String date; 151 public Dvd(int id,String name,String status,String date){ 152 this.id = id; 153 this.name = name; 154 this.status = status; 155 this.date = date; 156 } 157 @Override 158 public String toString() { 159 return this.id+" "+this.name+" "+" "+this.status+" "+this.date; 160 } 161 }