1 package com; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 import java.util.Scanner; 6 7 public class Demo{ 8 public static void main(String[] args) { 9 Scanner input = new Scanner(System.in); 10 11 String[] names = new String[10]; 12 String[] states = new String[10]; 13 String[] date = new String[10]; 14 int[] counts = new int[10]; 15 16 names[0]="罗宁"; 17 states[0]="可借"; 18 19 names[1]="死亡之翼"; 20 states[1]="可借"; 21 22 names[2]="国王之谷"; 23 states[2]="可借"; 24 25 names[3]="闪光平原"; 26 states[3]="可借"; 27 28 String isBack = ""; 29 30 do{ 31 System.out.println("欢迎使用迷你DVD管理系统"); 32 System.out.println("---------------------"); 33 System.out.println("1.新增DVD"); 34 System.out.println("2.查看DVD"); 35 System.out.println("3.删除DVD"); 36 System.out.println("4.借出DVD"); 37 System.out.println("5.归还DVD"); 38 System.out.println("6.退出DVD"); 39 System.out.println("---------------------"); 40 41 System.out.println("请选择:"); 42 int choose = input.nextInt(); 43 44 if(choose == 6){ 45 break; 46 } 47 48 switch(choose){ 49 case 1: 50 System.out.println("--->新增DVD"); 51 //定义保存信息的下标 52 int index1=-1; 53 for(int i = 0;i<names.length;i++){ 54 if(names[i]==null){ 55 index1=i; 56 break; 57 } 58 } 59 //判断是否可以保存 60 if(index1==-1){ 61 System.out.println("保存满了,请稍候再试"); 62 }else{ 63 System.out.println("请输入新增DVD名称!"); 64 String name = input.next(); 65 66 names[index1]=name; 67 states[index1]="可借"; 68 69 System.out.println("增加成功-_-!"); 70 } 71 72 break; 73 case 2: 74 System.out.println("--->查看DVD"); 75 int count = 1; 76 77 System.out.println("序号\t状态\t名称\t\t借出日期\t\t借出次数"); 78 for(int i = 0;i<names.length;i++){ 79 if(date[i]==null){ 80 date[i]=""; 81 } 82 if(names[i]==null){ 83 continue; 84 } 85 System.out.println(); 86 System.out.println(count+"\t"+states[i]+"\t"+date[i]+"\t"+counts); 87 count++; 88 } 89 break; 90 case 3: 91 System.out.println("--->删除DVD"); 92 System.out.println("请输入要删除DVD的名称:"); 93 String delName=input.next(); 94 95 int index3 = -1; 96 for(int i = 0;i<names.length;i++){ 97 if(delName.equals(names[i])){ 98 if("可借".equals(states[i])){ 99 index3=i; 100 break; 101 } 102 index3=-2; 103 break; 104 } 105 } 106 if(index3==-1){ 107 System.out.println("输入的DVD信息不存在,请重新输入!!!!"); 108 }else if(index3==-2){ 109 System.out.println("该DVD已借出,不能删除!!!"); 110 } 111 else{ 112 names[index3]=null; 113 System.out.println("删除成功!!!"); 114 } 115 break; 116 case 4: 117 System.out.println("--->借出DVD"); 118 System.out.println("请输入要借的DVD名称:"); 119 String borrowName = input.next(); 120 121 int index4=-1; 122 123 //循环遍历该DVD是否存在 124 for(int i = 0;i<names.length;i++){ 125 if(borrowName.equals(names[i])){ 126 if("可借".equals(states[i])){ 127 index4=i; 128 break; 129 } 130 index4=-2; 131 break; 132 } 133 } 134 if(index4==-1){ 135 System.out.println("输入的DVD信息不存在!!!"); 136 }else if(index4==-2){ 137 System.out.println("该DVD已借出!"); 138 }else{ 139 Date d =new Date(); 140 SimpleDateFormat df=new SimpleDateFormat(); 141 String dateStr=df.format(d); 142 143 states[index4]="已借出"; 144 date[index4]=dateStr; 145 counts[index4] = counts[index4]+1; 146 System.out.println("借出成功!"); 147 } 148 break; 149 case 5: 150 System.out.println("--->归还DVD"); 151 System.out.println("请输入要归还的DVD名称:"); 152 String backName=input.next(); 153 154 int index5=-1; 155 for(int i = 0;i<names.length;i++){ 156 if(backName.equals(names[i])){ 157 if("已借出".equals(states[i])){ 158 index5=i; 159 break; 160 } 161 index5=-2; 162 break; 163 } 164 } 165 if(index5==-1){ 166 System.out.println("该DVD不存在,不能归还!"); 167 }else if(index5==-2){ 168 System.out.println("该DVD未借出,无法归还!"); 169 }else{ 170 states[index5]="可借"; 171 date[index5]=""; 172 System.out.println("归还成功!"); 173 } 174 break; 175 176 } 177 System.out.println("****************************"); 178 System.out.println("输入0返回"); 179 isBack=input.next(); 180 } 181 while(isBack.equals("0")); 182 System.out.println("谢谢使用!"); 183 } 184 }
时间: 2024-10-11 23:24:04