import java.sql.Date; 2 import java.text.SimpleDateFormat; 3 import java.util.ArrayList; 4 import java.util.Iterator; 5 import java.util.Scanner; 6 import java.util.Set; 7 import java.util.TreeSet; 8 9 import javax.net.ssl.TrustManagerFactorySpi; 10 import javax.print.DocFlavor.STRING; 11 12 13 //------DVD数据集合 14 class DVDSet{ 15 16 //--DVD名称 17 ArrayList<String> nameList = new ArrayList<String>(); 18 //--DVD状态 19 ArrayList<Integer> stateList = new ArrayList<Integer>(); 20 //--DVD借出日期 21 ArrayList<String> dateList = new ArrayList<String>(); 22 //--DVD借出次数 23 ArrayList<Integer> countList = new ArrayList<Integer>(); 24 25 } 26 27 class Rank implements Comparable<Rank>{ 28 29 String DVDName; 30 Integer DVDState; 31 String DVDDate; 32 Integer DVDCount; 33 34 public Rank(String DVDName,Integer DVDState,String DVDDate,Integer DVDCount){ 35 36 this.DVDName = DVDName; 37 this.DVDState = DVDState; 38 this.DVDDate = DVDDate; 39 this.DVDCount = DVDCount; 40 } 41 42 @Override 43 public String toString() { 44 // TODO Auto-generated method stub 45 return " [DVD:《" + this.DVDName + "》 状态:" + this.DVDState + " 日期:" + this.DVDDate + " 次数:" + this.DVDCount + "]"; 46 } 47 @Override 48 public int compareTo(Rank o) { 49 // TODO Auto-generated method stub 50 if(o.DVDCount == o.DVDCount) 51 return -1; 52 return o.DVDCount - o.DVDCount; 53 } 54 } 55 56 class DVDMagr{ 57 58 //------构造DVD集合对象 59 DVDSet DVD = new DVDSet(); 60 61 //------DVD数据的初始化 62 public void initial(){ 63 64 DVD.nameList.add("罗马假日"); 65 DVD.nameList.add("风声鹤唳"); 66 DVD.nameList.add("浪漫满屋"); 67 68 DVD.stateList.add(0); 69 DVD.stateList.add(1); 70 DVD.stateList.add(1); 71 72 DVD.dateList.add("2016-12-1"); 73 DVD.dateList.add("无"); 74 DVD.dateList.add("无"); 75 76 DVD.countList.add(1); 77 DVD.countList.add(0); 78 DVD.countList.add(0); 79 } 80 81 //------菜单切换方法 82 public void startMenu(){ 83 84 System.out.println("1. 新增 DVD"); 85 System.out.println("2. 查看 DVD"); 86 System.out.println("3. 删除 DVD"); 87 System.out.println("4. 借出 DVD"); 88 System.out.println("5. 归还 DVD"); 89 System.out.println("6. 退 出"); 90 System.out.println("7. 借出排行榜"); 91 System.out.println("*******************************"); 92 System.out.println("请选择你要操作的栏目:"); 93 Scanner keyNum = new Scanner(System.in); 94 switch (keyNum.nextInt()) { 95 case 1: 96 System.out.println("----------->新增DVD~~~"); 97 System.out.println("*******************************"); 98 addDVD(); 99 break; 100 case 2: 101 System.out.println("----------->查看DVD~~~"); 102 System.out.println("*******************************"); 103 searchDVD(); 104 break; 105 case 3: 106 System.out.println("----------->删除DVD~~~"); 107 System.out.println("*******************************"); 108 deleteDVD(); 109 break; 110 case 4: 111 System.out.println("----------->借出DVD~~~"); 112 System.out.println("*******************************"); 113 lendDVD(); 114 break; 115 case 5: 116 System.out.println("----------->归还DVD~~~"); 117 System.out.println("*******************************"); 118 returnDVD(); 119 break; 120 case 6: 121 System.out.println("----------->退出DVD~~~"); 122 System.out.println("*******************************"); 123 returnSystem(); 124 break; 125 case 7: 126 System.out.println("----------->DVD借出排行榜~~~"); 127 System.out.println("*******************************"); 128 listDVD(); 129 break; 130 default : 131 System.out.println("操作异常,程序重载中........"); 132 break; 133 } 134 } 135 136 //------------新增DVD的方法 137 public void addDVD(){ 138 139 Scanner keyInput = new Scanner(System.in); 140 for (int i = 0; i < DVD.nameList.size(); i++) { 141 142 if (DVD.nameList.get(i).equals(DVD.nameList.get(DVD.nameList.size()-1))) { 143 144 System.out.println("请输入你要添加的 DVD名称:"); 145 DVD.nameList.add(keyInput.next()); 146 DVD.stateList.add(1); 147 DVD.dateList.add("无"); 148 DVD.countList.add(0); 149 System.out.println("新的DVD添加成功~~~~~"); 150 System.out.println("继续添加请按“1”,返回请按“0”:"); 151 Scanner keyNum = new Scanner(System.in); 152 int num = keyNum.nextInt(); 153 if (num == 1) { 154 155 addDVD(); 156 }else if (num == 0) { 157 158 startMenu(); 159 } 160 } 161 } 162 } 163 164 //------------查看DVD的方法 165 public void searchDVD(){ 166 167 System.out.println("序号 " + "状态 " + "名称 " + "借出日期"); 168 for (int i = 0; i < DVD.nameList.size(); i++) { 169 170 if (DVD.nameList.get(i) != null) { 171 172 if (DVD.stateList.get(i) == 0) { 173 174 System.out.println(( i + 1) + " 已 借出 " +"<<" + DVD.nameList.get(i) + ">> " + DVD.dateList.get(i)); 175 }else if (DVD.stateList.get(i) == 1) { 176 177 System.out.println(( i + 1) + " 可以借阅 " + "<<" + DVD.nameList.get(i) + ">> " + DVD.dateList.get(i)); 178 } 179 } 180 } 181 returnMain(); 182 } 183 184 //------------删除DVD的方法 185 public void deleteDVD(){ 186 187 System.out.println("请输入你要删除的DVD:"); 188 Scanner keyInput = new Scanner(System.in); 189 String deleteDVD = keyInput.next(); 190 Iterator<String> it = DVD.nameList.iterator(); 191 192 while (it.hasNext()) { 193 194 if (deleteDVD.equalsIgnoreCase(it.next())) { 195 196 int index = DVD.nameList.indexOf(deleteDVD); 197 if (DVD.stateList.get(index) == 1) { 198 199 DVD.nameList.remove(deleteDVD); 200 DVD.stateList.remove(index); 201 DVD.dateList.remove(index); 202 System.out.println("《" + deleteDVD + "》" + "删除成功!!!"); 203 break; 204 }else if (DVD.stateList.get(index) == 0) { 205 206 System.out.println("《" + deleteDVD + "》" +"已借出,无法删除"); 207 break; 208 } 209 } 210 211 if(it.hasNext() == false){ 212 213 System.out.println("对不起,无法获取此DVD信息,请确认信息后重试!!!"); 214 } 215 } 216 returnMain(); 217 } 218 219 //------------借出DVD的方法 220 public void lendDVD(){ 221 222 System.out.println("请输入你要借出的DVD:"); 223 Scanner keyInput = new Scanner(System.in); 224 String lendDVD = keyInput.next(); 225 Iterator<String> it = DVD.nameList.iterator(); 226 while (it.hasNext()) { 227 228 if(lendDVD.equalsIgnoreCase(it.next())){ 229 230 int index = DVD.nameList.indexOf(lendDVD); 231 if(DVD.stateList.get(index) == 1){ 232 233 System.out.println("《" + lendDVD + "》" +"已找到,可以借阅!!!"); 234 System.out.println("请输入你的借阅时间(年-月-日):"); 235 Scanner keyDate = new Scanner(System.in); 236 DVD.dateList.set(index, keyDate.next()); 237 DVD.stateList.set(index, 0); 238 int num = DVD.countList.get(index); 239 num++; 240 System.out.println("《" + lendDVD + "》" + "借阅成功!!!"); 241 break; 242 }else if (DVD.stateList.get(index) == 0) { 243 244 System.out.println("《" + lendDVD + "》" +"已借出,无法借阅!!!"); 245 break; 246 } 247 } 248 249 if (it.hasNext() == false) { 250 251 System.out.println("对不起,无法获取此DVD信息,请确认信息后重试!!!"); 252 } 253 } 254 returnMain(); 255 } 256 257 //------------归还DVD的方法 258 public void returnDVD(){ 259 260 String lendDate; 261 String returnDate; 262 System.out.println("请输入你要归还的DVD:"); 263 Scanner keyInput = new Scanner(System.in); 264 String returnDVD = keyInput.next(); 265 int index = DVD.nameList.indexOf(returnDVD); 266 if (index >= 0 && DVD.stateList.get(index) == 0) { 267 268 lendDate = DVD.dateList.get(index); 269 System.out.println("请输入你的归还日期(年-月-日):"); 270 returnDate = keyInput.next(); 271 DVD.stateList.set(index, 1); 272 DVD.dateList.set(index, "无"); 273 System.out.println("《" + returnDVD + "》归还成功!!!"); 274 System.out.println("借DVD期间你所要支付的租金为(Charge):" + charge(lendDate,returnDate) + "$"); 275 }else{ 276 277 System.out.println("你归还的DVD没有借阅信息,请重新确认信息!!!"); 278 } 279 returnMain(); 280 } 281 282 //-------------租金计算系统 283 public long charge(String lendDate,String returnDate){ 284 285 long charge = 0; 286 SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd"); 287 try { 288 java.util.Date lDate = SDF.parse(lendDate); 289 java.util.Date reDate = SDF.parse(returnDate); 290 charge = (reDate.getTime() - lDate.getTime()) / (24*60*62*1000); 291 } catch (Exception e) { 292 // TODO: handle exception 293 e.printStackTrace(); 294 } 295 return charge + 2; 296 } 297 298 //------------DVD排行榜 299 public void listDVD(){ 300 301 302 TreeSet set = new TreeSet(); 303 304 for (int i = 0; i < DVD.nameList.size(); i++) { 305 306 set.add(new Rank(DVD.nameList.get(i), DVD.stateList.get(i), DVD.dateList.get(i), DVD.countList.get(i))); 307 } 308 309 Iterator it = set.descendingIterator(); 310 311 while(it.hasNext()){ 312 313 System.out.println(it.next()); 314 } 315 } 316 317 //------------返回主菜单方法 318 public void returnMain(){ 319 320 System.out.println("*******************************"); 321 Scanner keyInput = new Scanner(System.in); 322 System.out.println("系统操作:返回(*) 退出(#):"); 323 if (keyInput.next().equals("*")) { 324 325 startMenu(); 326 }else if (keyInput.next().equals("#")) { 327 328 returnDVD(); 329 }else { 330 331 System.out.println("输入错误,异常终止!!!"); 332 return; 333 } 334 } 335 336 //------------退出DVD的方法 337 public void returnSystem(){ 338 339 Scanner keyInput = new Scanner(System.in); 340 System.out.println("确认退出请输入-0-:"); 341 if (keyInput.nextInt() == 0) { 342 343 System.out.println("DVD退出成功......"); 344 return; 345 }else{ 346 347 System.out.println("输入错误,异常终止!!!"); 348 return; 349 } 350 } 351 } 352 353 public class MiniDVD { 354 355 /** 356 * @param args 357 */ 358 public static void main(String[] args) { 359 // TODO Auto-generated method stub 360 361 //----构造管理员对象 362 DVDMagr manger = new DVDMagr(); 363 364 System.out.println(" "); 365 System.out.println("*******************************"); 366 System.out.println("欢迎使用迷你DVD管理器"); 367 System.out.println("*******************************"); 368 369 //----DVD数据的初始化 370 manger.initial(); 371 372 //----DVD的主菜单 373 manger.startMenu(); 374 } 375 376 }