Java 编程4
项目: DVD管理系统
存取信息:
DVD片名 借出状态(0--》可借 / 1--》已借出)借出时间 归还时间;
1)新增:
switch(choice){
case 1: //新增
System.out.println("--->新增DVD信息");
System.out.println(">>>请输入新增DVD片名:");
String dvd_name = input.next();
boolean isFull=true;
for(int i = 0;i<dNames.length;i++){
if(dNames[i]==null){
dNames[i]=dvd_name;
dState[i]=0;
isFull= false;
break;
}
}
if(isFull==true){
System.out.println("对不起,货架已满,不能继续添加");
}else{
System.out.println(String.format("%s影片新增成功", dvd_name));
}
do{
System.out.println("新增DVD成功!按‘0’返回");
choice = input.nextInt();
}while(choice!=0);
break;
2)查看:
case 2: //查看
System.out.println("--->查看DVD信息");
System.out.println("DVD片名\t\t借出状态\t\t借出时间\t\t归还时间");
System.out.println("-----------------------------------");
String str_state;//字符串类型表示借出状态
String str_borrowtime;//字符串类型表示借出时间
String str_returntime;//字符串类型表示归还时间
//simp alt+?日期类型转换;
SimpleDateFormat sdf = new SimpleDateFormat("yy--ww--dd");//日期类型转换
for(int i = 0;i<dNames.length;i++){
if(dNames[i]==null){
continue;
}
str_state = dState[i]==0?"可借":"不可借";
if(dBorrowTime[i] == null){
str_borrowtime = " ";
}else{
str_borrowtime = sdf.format(dBorrowTime[i]);
}
if(dReturnTime[i]==null){
str_returntime = " ";
}else{
str_returntime = sdf.format(dReturnTime[i]);
}
System.out.println(String.format("%s\t\t%s\t\t%s\t\t%s", dNames[i],str_state,str_borrowtime,str_returntime));
}
do{
System.out.println("列表显示完毕!按‘0’返回");
choice = input.nextInt();
}while(choice!=0);
break;
3)删除:
例:
System.out.println("--->删除DVD信息");
System.out.println(">>>请输入要删除的DVD片名:");
dvd_name = input.next();
int idx = -1;
//循环查找数据中哪一处是要删除的;
for(int i= 0;i<dNames.length ;i++){
if(dNames[i].equals(dvd_name)){
idx = i;
break;
}
}
if(idx==-1){
System.out.println("对不起,没有您要删除的DVD片");
}else{
for(int i =idx;i<dNames.length -1;i++){
dNames[i]=dNames[i+1];
dState[i]=dState[i+1];
dBorrowTime[i]= dBorrowTime[i+1];
dReturnTime[i]=dReturnTime[i+1];
}
dNames[dNames.length-1]=null;
dState[dNames.length-1]=0;
dBorrowTime[dNames.length-1]=null;
dReturnTime[dNames.length-1]=null;
System.out.println(String.format("<<%s>>影片已删除成功", dvd_name));
}
do{
System.out.println("删除DVD成功!按‘0’返回");
choice = input.nextInt();
}while(choice!=0);
break;