1.简单的DVD管理器系统,熟练使用数组,循环,对内存数据进行简单增删查改,原生版本
1.1 DVDpojo类
public class Dvd {
private String name;
private boolean statue;
private Date date;
private int count;
public Dvd ( String name, boolean statue, Date date, int count) {
this . name = name;
this . statue = statue;
this . date = date;
this . count = count;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public boolean isStatue ( ) {
return statue;
}
public void setStatue ( boolean statue) {
this . statue = statue;
}
public Date getDate ( ) {
return date;
}
public void setDate ( Date date) {
this . date = date;
}
public int getCount ( ) {
return count;
}
public void setCount ( int count) {
this . count = count;
}
@Override
public String toString ( ) {
return "Dvd{" +
"name='" + name + '\'' +
", statue=" + statue +
", date=" + date +
", count=" + count +
'}' ;
}
public static Dvd[ ] initDvd ( ) {
Dvd[ ] dvds = { new Dvd ( "澳门风云" , true , new Date ( ) , 0 ) ,
new Dvd ( "水浒传" , true , new Date ( ) , 0 ) ,
new Dvd ( "红楼梦" , true , new Date ( ) , 0 ) ,
new Dvd ( "西游记" , true , new Date ( ) , 0 ) ,
new Dvd ( "三国演义" , true , new Date ( ) , 0 ) ,
new Dvd ( "迪迦奥特曼" , true , new Date ( ) , 0 )
} ;
return dvds;
}
public static Dvd[ ] addDvd ( Dvd[ ] dvds) {
System. out. println ( "请输入DVD名字" ) ;
String name = new Scanner ( System. in) . next ( ) ;
if ( Dvd. isExsit ( dvds, name) ) {
System. out. println ( "改dvd已经存在,添加失败" ) ;
return dvds;
}
Dvd dvd = new Dvd ( name, true , new Date ( ) , 0 ) ;
Dvd[ ] dvds1 = new Dvd [ dvds. length+ 1 ] ;
dvds1[ dvds. length] = dvd;
for ( int i = 0 ; i < dvds. length; i++ ) {
dvds1[ i] = dvds[ i] ;
}
System. out. println ( Arrays. toString ( dvds1) ) ;
return dvds1;
}
public static void printDvds ( Dvd[ ] dvds) {
System. out. printf ( "%-20s%-20s%-20s%20s\n" , "DVD名称" , "是否可借" , "借出天数" , "借出次数" ) ;
for ( Dvd dvd : dvds) {
System. out. printf ( "%-20s%-20s%-20s%20s\n" , dvd. getName ( ) , dvd. isStatue ( ) ? "是" : "否" , dvd. isStatue ( ) ? "" : ( ( new Date ( ) . getTime ( ) - dvd. getDate ( ) . getTime ( ) ) / ( 1000 * 60 * 60 * 24 ) + 1 ) , dvd. getCount ( ) ) ;
}
}
public static Dvd[ ] deleteDvd ( Dvd[ ] dvds) {
System. out. println ( "请输入要删除的DVD名称" ) ;
String name = new Scanner ( System. in) . next ( ) ;
if ( ! Dvd. isExsit ( dvds, name) ) {
System. out. println ( "未找到该DVD" ) ;
return dvds;
}
Dvd[ ] dvds1 = new Dvd [ dvds. length - 1 ] ;
int i = 0 ;
for ( Dvd dvd : dvds) {
if ( ! dvd. getName ( ) . equals ( name) ) {
dvds1[ i] = dvd;
i++ ;
}
}
return dvds1;
}
public static boolean isExsit ( Dvd[ ] dvds, String name) {
boolean b = false ;
for ( Dvd dvd : dvds) {
if ( dvd. getName ( ) . equals ( name) )
b = true ;
}
return b;
}
public static void lendOrRevertDvd ( Dvd[ ] dvds, boolean isLend) {
String str;
int temp ;
if ( isLend) {
str = "借" ;
temp = 1 ;
} else {
str = "还" ;
temp = 0 ;
}
System. out. println ( "请输入要" + str+ "的DVD名称" ) ;
String name = new Scanner ( System. in) . next ( ) ;
if ( ! Dvd. isExsit ( dvds, name) ) {
System. out. println ( "未找到该DVD" ) ;
} else {
for ( Dvd dvd : dvds) {
if ( dvd. getName ( ) . equals ( name) ) {
if ( ! ( dvd. isStatue ( ) ^ isLend) ) {
dvd. setCount ( dvd. getCount ( ) + temp) ;
if ( ! isLend) {
System. out. println ( "租金费用是" + ( ( new Date ( ) . getTime ( ) - dvd. getDate ( ) . getTime ( ) ) / ( 1000 * 60 * 60 * 24 ) + 1 ) ) ;
}
dvd. setDate ( new Date ( ) ) ;
dvd. setStatue ( ! isLend) ;
}
else System. out. println ( "该DVD--" + str+ "--操作失败" ) ;
break ;
}
}
}
}
}
1.2 控制流程类
public class Print {
public static void main ( String[ ] args) {
Dvd[ ] dvds = Dvd. initDvd ( ) ;
while ( true ) {
printMenu ( ) ;
Scanner scanner = new Scanner ( System. in) ;
int temp = scanner. nextInt ( ) ;
switch ( temp) {
case 1 : dvds = Dvd. addDvd ( dvds) ;
break ;
case 2 : Dvd. printDvds ( dvds) ;
break ;
case 3 : dvds = Dvd. deleteDvd ( dvds) ;
break ;
case 4 : Dvd. lendOrRevertDvd ( dvds, true ) ;
break ;
case 5 : Dvd. lendOrRevertDvd ( dvds, false ) ;
break ;
case 6 : return ;
default : break ;
}
}
}
public static void printMenu ( ) {
System. out. println ( "欢迎使用迷你DVD管理器" ) ;
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.退出" ) ;
}
}