迷你DVD管理器项目
1 package chapter5; 2 import java.util.*; 3 4 public class MiniDVD { 5 public static void main(String[] args){ 6 //扫描器 7 Scanner input = new Scanner(System.in); 8 //重要参数及初始化 9 //Create four arrays with length of 7 10 //定义四个数组长度为7 11 int date[] = new int[7]; //borrow date 12 int count[] = new int[7]; //total count 13 String name[] =new String[7]; //name of DVDs 14 String state[] = new String[7]; //state 15 //Initiate three DVD 16 //FirstDVD 17 name[0] = "罗马假日"; 18 state[0] = "已借出"; 19 date[0] = 1; 20 count[0] =15; 21 //Second DVD 22 name[1] = "风声鹤唳"; 23 state[1] = "可借"; 24 date[1] = 0; 25 count[1] =12; 26 //Third DVD 27 name[2] = "浪漫满屋"; 28 state[2] = "可借"; 29 date[2] = 0; 30 count[2] =30; 31 32 //循环进入条件 33 String choice = " "; 34 boolean flag = true; 35 do{ 36 //Create DVD Menu 37 System.out.println("欢迎使用迷你DVD管理器"); 38 System.out.println("------------------------------------------------------"); 39 System.out.println("1.新增DVD"); 40 System.out.println("2.查看DVD"); 41 System.out.println("3.删除DVD"); 42 System.out.println("4.借出DVD"); 43 System.out.println("5.归还DVD"); 44 System.out.println("6.退出DVD"); 45 System.out.println("------------------------------------------------------"); 46 System.out.print("请选择:"); 47 choice = input.next(); 48 49 //Switch choice menu 50 switch (choice){ 51 case "1": 52 System.out.println("--->新增DVD"); 53 System.out.print("\n请输入DVD名称:"); 54 boolean firstMonitor = true; 55 boolean secondMonitor = false; 56 String bookName = " "; 57 do{ 58 bookName= input.next(); 59 if(firstMonitor){ 60 for(int i = 0; i<name.length; i++){ 61 if(bookName.equals(name[i])){ 62 System.out.println("货架上已经存在该DVD,请返回目录重新选择!"); 63 secondMonitor = true; 64 break; 65 } 66 } 67 } 68 firstMonitor =false; 69 }while(firstMonitor); 70 71 if(!secondMonitor){ 72 for (int j = 0;j<name.length; j++){ 73 if((name[j])==null){ 74 name[j]= bookName; 75 state[j] = "可借"; 76 count[j] = 0; 77 System.out.println("新增《"+bookName+"》成功!"); 78 System.out.println("***************************"); 79 break; 80 } 81 if(name[name.length-2] !=null){ 82 System.out.println("DVD货架已满,添加失败!"); 83 System.out.println("***************************"); 84 break; 85 } 86 } 87 } 88 break; 89 case "2": 90 System.out.println("--->查看DVD"); 91 System.out.println("序号\t状态\t名称\t\t借出日期\t借出次数"); 92 for (int i = 0; i<name.length; i++){ 93 if(name[i] == null){ 94 System.out.println("***************************"); 95 break; 96 } 97 String myDate = " "; 98 if(date[i] != 0){ 99 myDate = date[i]+"日"; 100 } 101 System.out.println((i+1)+"\t"+state[i]+"\t"+"《"+name[i]+"》"+"\t"+myDate+"\t"+count[i]+"次"); 102 } 103 break; 104 case "3": 105 System.out.println("--->删除DVD"); 106 System.out.print("\n请输入DVD名称:"); 107 String delName = input.next(); 108 //define index monitor: check 109 int check = -1; 110 for(int i = 0; i < name.length; i++){ 111 if(delName.equals(name[i])){ 112 check = i; 113 break; 114 } 115 } 116 if(check != -1){ 117 if(state[check].equals("可借")){ 118 //Delete operation 119 for(int j = check; j < name.length-1; j++){ 120 name[j] = name[j+1]; 121 state[j] = state[j+1]; 122 date[j] = date[j+1]; 123 count[j] = count[j+1]; 124 } 125 System.out.println("删除《"+delName+"》成功!"); 126 System.out.println("***************************"); 127 break; 128 }else{ 129 System.out.println("DVD为借出状态,不允许删除!"); 130 System.out.println("***************************"); 131 } 132 }else{ 133 System.out.println("没有找到匹配信息!"); 134 System.out.println("***************************"); 135 } 136 break; 137 case "4": 138 System.out.println("--->借出DVD"); 139 System.out.print("\n请输入DVD名称:"); 140 String lendName = input.next(); 141 //Initiate lendDate with 1, so as to enter the do-loop 142 int lendDate = 1; 143 System.out.print("请输入借出日期:"); 144 //Avoid user input wrong date 145 do{ 146 lendDate = input.nextInt(); 147 if((lendDate<1)||(lendDate>31)){ 148 System.out.print("必须输入大于等于1且小于等于31的数字,请重新输入:"); 149 } 150 }while((lendDate<1)||(lendDate>31)); 151 152 //define index monitor 153 int index = -1; 154 for(int i = 0; i < name.length; i++){ 155 if(lendName.equals(name[i])){ 156 index = i; 157 break; 158 } 159 160 } 161 //lend operation 162 if(index != -1){ 163 if(state[index].equals("可借")){ 164 state[index] = "已借出"; 165 count[index] +=1; 166 date[index] = lendDate; 167 System.out.println("借出《"+lendName+"》成功!"); 168 System.out.println("***************************"); 169 }else{ 170 System.out.println("《"+lendName+"》已被借出!"); 171 System.out.println("***************************"); 172 } 173 }else{ 174 System.out.println("没有找到匹配信息!"); 175 System.out.println("***************************"); 176 } 177 break; 178 case "5": 179 System.out.println("--->归还DVD"); 180 System.out.print("\n请输入DVD名称:"); 181 String returnName = input.next(); 182 //define index monitor 183 int monitor = -1; 184 for(int i = 0; i < name.length; i++){ 185 if(returnName.equals(name[i])){ 186 monitor = i; 187 break; 188 } 189 190 } 191 //Initiate myLendPeriod with 0, and enter the do-loop 192 int returnDate = 0; 193 int myLendPeriod = 0; 194 do{ 195 System.out.print("请输入归还日期:"); 196 //Avoid user input wrong date 197 do{ 198 returnDate = input.nextInt(); 199 if(monitor != -1){ 200 myLendPeriod = returnDate - date[monitor]; 201 } 202 if(returnDate > 31){ 203 System.out.print("一个月只有31天,请重新输入:"); 204 } 205 if(myLendPeriod < 0){ 206 System.out.println("归还日期不能小于借出日期,请重新输入:"); 207 } 208 }while((returnDate>31)||(myLendPeriod<0)); 209 //Return operation 210 if(monitor != -1){ 211 if(state[monitor].equals("已借出")){ 212 state[monitor] = "可借"; 213 date[monitor] = 0; 214 System.out.println("归还《"+returnName+"》成功!"); 215 System.out.println("借出日期为:"+date[monitor]+"日"); 216 System.out.println("归还日期为:"+returnDate+"日"); 217 System.out.println("应付租金(元):"+myLendPeriod); 218 System.out.println("***************************"); 219 }else{ 220 System.out.println("该DVD没有被借出!无法进行归还操作。"); 221 System.out.println("***************************"); 222 } 223 }else{ 224 System.out.println("没有找到匹配信息!"); 225 System.out.println("***************************"); 226 } 227 break; 228 }while(myLendPeriod < 0); 229 break; 230 case "6": 231 flag = false; 232 break; 233 default: 234 } 235 if(flag){ 236 System.out.print("输入任意值返回:"); 237 choice = input.next(); 238 } 239 }while(flag); 240 241 //program exit 242 System.out.println("程序结束"); 243 } 244 }