1.初始化菜单目录
public void menu() { System.out.println("欢迎使用模拟DVD模拟器"); System.out.println("================="); 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("7.退\t出"); System.out.println("=================="); System.out.println("请选择:"); int number = input.nextInt(); switch (number) { case 1: method01(); break; case 2: method02(); break; case 3: method03(); break; case 4: method04(); break; case 5: method05(); break; case 6: method06(); break; case 7: System.out.println("退出系统,感谢使用"); System.exit(1); } }
2.创建各个模块需要用到的数组
int[] nums = new int[50]; int[] count = new int[50]; String[] states = new String[50]; String[] names = new String[50]; String[] dates = new String[50]; Scanner input = new Scanner(System.in);
3.初始化系统中存在的数据
public void init() { nums[0] = 1; nums[1] = 2; nums[2] = 3; count[0]=15; count[1]=12; count[2]=11; states[0] = "已借出"; states[1] = "可借"; states[2] = "可借"; names[0] = "罗马假日"; names[1] = "风声鹤唳"; names[2] = "浪漫满屋"; dates[0] = "2010-7-1"; dates[1] = ""; dates[2] = ""; }
4.主程序入口
public static void main(String[] args) { DVD dvd=new DVD(); dvd.init(); dvd.menu(); }
5.添加操作
private void method01() { System.out.println("-------->添加DVD"); System.out.println("输入要添加的DVD名称"); String newname=input.next(); for(int i=0;i<names.length;i++){ if(newname.equals(names[i])){ System.out.println("该DVD已经存在,返回菜单"); }else{ if(names[i]==null){ names[i]=newname; nums[i]=i+1; states[i]="可借"; dates[i]=""; break; } } } System.out.println("添加"+newname+"成功"); getReture(); }
6.查看操作
private void method02() { System.out.println("---------->查看DVD"); System.out.println("序号\t状态\t名称\t借出日期"); for(int i=0;i<names.length;i++){ if(names[i]!=null){ System.out.println(nums[i]+"\t"+states[i]+"\t"+names[i]+"\t"+dates[i]); }else{ break; } } getReture(); }
7.删除操作
private void method03() { System.out.println("----------->删除DVD"); System.out.println("输入要删除的DVD名称"); String delname=input.next(); boolean boo=false; for(int i=0;i<names.length;i++){ if(delname.equals(names[i])){ if("可借".equals(states[i])){ for(int j=i;j<names.length-1;j++){ nums[j-1]=nums[j]; states[j]=states[j+1]; names[j]=names[j+1]; dates[j]=dates[j+1]; } System.out.println("该DVD删除成功"); }else{ System.out.println("该DVD已借出,不能删除"); } boo=true; } } if(!boo){ System.out.println("不存在该DVD"); } getReture(); }
8.借出操作
private void method04() { System.out.println("--------->借出DVD"); System.out.println("输入要借出的DVD名称"); String lentname=input.next(); boolean boo=false; for(int i=0;i<names.length;i++){ if(lentname.equals(names[i])){ if("可借".equals(states[i])){ states[i]="已借出"; System.out.println("输入借出日期"); String date=input.next(); dates[i]=date; System.out.println("借出成功"); count[i]++; }else{ System.out.println("该DVD已借出"); } boo=true; } } if(!boo){ System.out.println("该DVD不存在"); } getReture(); }
9.归还操作
private void method05() { System.out.println("----------->归还DVD"); System.out.println("输入归还的DVD名称"); String backname=input.next(); boolean boo=false; for(int i=0;i<names.length;i++){ if(backname.equals(names[i])){ if("不可借".equals(names[i])){ System.out.println("输入归还日期"); String date2=input.next(); System.out.println("归还成功"); System.out.println("借出日期为:"+dates[i]); System.out.println("归还日期为:"+date2); SimpleDateFormat sdf=new SimpleDateFormat("yy-mm-dd"); Date perse1=null; Date perse2=null; try { perse1=sdf.parse(dates[i]); } catch (ParseException e) { e.printStackTrace(); } try { perse2=sdf.parse(date2); } catch (ParseException e) { e.printStackTrace(); } long time1=perse1.getTime(); long time2=perse2.getTime(); long money =(time2-time1)/(10000*60*60*24); System.out.println("应付租金"+money+"元)"); dates[i]=""; }else{ System.out.println("该DVD为可借状态"); } boo=true; } } if(!boo){ System.out.println("该DVD不存在"); } getReture(); }
10.根据借出次数多少,依次排列各个DVD名称
private void method06() { System.out.println("-------->排行榜"); int max=0; System.out.println("名称\t次数"); for(int i=0;i<names.length;i++){ for(int j=i+1;j<names.length;j++) { if (count[i] < count[j]) { max = count[j]; count[j] = count[i]; count[i] = max; } } if(names[i]==null){ break; } System.out.println(names[i]+"\t"+count[i]); } getReture(); }