一个小的dvd管理器
import java.util.*;
//日期要导入的
import java.text.*;
//放数据用的
class DVDSet
{
static String[] name=new String[50];
static int[] state=new int[50];
static String[] date=new String[50];
static int[] count=new int[50];
}
class DVDMgr
{
DVDSet dvd =new DVDSet();
public void initial(){
dvd.name[0]="罗马假日";
dvd.state[0]=0;
dvd.count[0]=1;
dvd.date[0]="2012-2-1";
dvd.name[1]="风声鹤唳";
dvd.state[1]=1;
dvd.count[1]=20;
dvd.name[2]="浪漫满屋";
dvd.state[2]=1;
dvd.count[2]=30;
}
public void startMenu(){
System.out.println("欢迎使用迷你DVD 管理器");
System.out.println("*******************************");
System.out.println("0.借书排行榜");
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("*******************************");
System.out.print("请输入一个数字: ");
Scanner sca = new Scanner(System.in);
int choice = sca.nextInt();
//判断你要输出的数字选择合适方法
switch(choice){
case 0:
System.out.println("借书排行榜");
System.out.println(" ");
ranKing();
System.out.println("*******************************");
returnMain();
break;
case 1:
System.out.println("此处实现新增DVD");
System.out.println(" ");
add();
System.out.println("*******************************");
returnMain();
break;
case 2:
System.out.println("--> 查看DVD");
System.out.println(" ");
search();
System.out.println("*******************************");
returnMain();
break;
case 3:
System.out.println("--> 删除DVD");
System.out.println(" ");
delete();
System.out.println("*******************************");
returnMain();
break;
case 4:
System.out.println("--> 借出DVD");
System.out.println(" ");
lend();
System.out.println("*******************************");
returnMain();
break;
case 5:
System.out.println("--> 归还DVD");
System.out.println(" ");
returnDvd();
System.out.println("*******************************");
returnMain();
break;
case 6:
System.out.println(" ");
System.out.println("谢谢使用");
System.out.println("*******************************");
break;
default:
System.out.println("输入错误,异常终止!");
break;
}
}
//返回方法
public void returnMain(){
Scanner input = new Scanner(System.in);
System.out.print("输入0返回 ");
if(input.nextInt()==0){
startMenu();
}
else{
System.out.println("输入错误,异常终止!");
}
}
//查看数组方法
public void search(){
for (int i= 0;i<dvd.name.length ;i++ )
{
if (dvd.name[i]==null)
{
break;
}else if (dvd.state[i]==0)
{
System.out.println((i+1)+"\t已借出\t"+"<<"+dvd.name[i]+">> \t"+dvd.date[i]);
}else if (dvd.state[i]==1)
{
System.out.println((i+1)+"\t可借\t"+"<<"+dvd.name[i]+">>");
}
}
}
//添加方法
public void add(){
System.out.print("请输入DVD名称: ");
for (int i= 0;i<dvd.name.length ;i++ )
{
if (dvd.name[i]==null)
{
Scanner sca1 = new Scanner(System.in);
String str=sca1.nextLine();
dvd.name[i]=str;
dvd.state[i]=1;
dvd.date[i]=" ";
System.out.println("新增《"+str+"》成功");
break;
}
}
}
//删除方法
public void delete(){
System.out.print("请输入DVD名称: ");
Scanner sca1 = new Scanner(System.in);
String str=sca1.nextLine();
for (int i= 0;i<dvd.name.length ;i++ )
{
if (dvd.name[i]!=null&&dvd.name[i].equalsIgnoreCase(str)&&dvd.state[i]==1)
{
int j=i;
//先把选出来的那个元素移到最后去在把他置空;
while(dvd.name[j+1]!=null){
dvd.name[j]=dvd.name[j+1];
dvd.state[j]=dvd.state[j+1];
dvd.date[j]=dvd.date[j+1];
j++;
}
dvd.name[j]=null;
//dvd.state[j]=null;
dvd.date[j]=null;
System.out.println("删除《"+str+"》成功!");
break;
}else if (dvd.name[i]!=null&&dvd.name[i].equalsIgnoreCase(str)&&dvd.state[i]==0)
{
System.out.println("《"+str+"》为借出状态,不能删除!");
break;
}
}
}
//借出操作
public void lend(){
System.out.print("输入要借的书 ");
Scanner sca2 = new Scanner(System.in);
String str=sca2.nextLine();
for (int i= 0;i<dvd.name.length ;i++ )
{
if (dvd.name[i]==null)
{
System.out.println("没有找到匹配信息!");
break;
}else if (dvd.name[i].equals(str)&& dvd.state[i]==1)
{
dvd.state[i]=0;
dvd.count[i]=dvd.count[i]+1;
System.out.print("请输入借出日期(年-月-日):");
Scanner sca3 = new Scanner(System.in);
String str2=sca3.nextLine();
dvd.date[i]=str2;
System.out.println("借出《"+str+"》成功!");
break;
}else if (dvd.name[i].equals(str)&& dvd.state[i]==0)
{
System.out.println("《"+str+"》已被借出!");
break;
}
}
}
//归还操作
public void returnDvd(){
System.out.print("输入要归还的书 ");
Scanner sca4 = new Scanner(System.in);
String str=sca4.nextLine();
for (int i= 0;i<dvd.name.length ;i++ )
{
if (dvd.name[i]==null)
{
System.out.println("没找到这本书");
break;
}else if (dvd.name[i].equals(str))
{
dvd.state[i]=1;
System.out.print("请输入归回的日期(年-月-日):");
Scanner sca5 = new Scanner(System.in);
String str2=sca5.nextLine();
//定义格式
SimpleDateFormat ri=new SimpleDateFormat("yyyy-MM-dd");
//把字符串转成可以计算的日期
long charge;
//日期的转换和计算要系在这个方法里面
try{
Date riqi = ri.parse(str2);
Date riqi1 = ri.parse(dvd.date[i]);
charge=(riqi.getTime()-riqi1.getTime())/(24*60*60*1000);
System.out.println("归还书《"+str+"》成功!收入"+charge+"元");
}catch (ParseException e) {
e.printStackTrace();
}
break;
}
}
}
//查看排行榜
public void ranKing(){
String[] name1=new String[50];
int[] count1=new int[50];
for (int i=0;i<dvd.name.length ;i++ )
{
if (dvd.name[i]==null)
{
break;
}
name1[i]=dvd.name[i];
count1[i]=dvd.count[i];
}
for (int i = 0 ;i<dvd.name.length-1 ;i++ )
{
for (int j=0;j<dvd.name.length-1-i ;j++ )
{
if (name1[j]==null)
{
break;
} else if (count1[j]<count1[j+1])
{
int tem = count1[j];
count1[j]= count1[j+1];
count1[j+1]=tem;
String tem1 = name1[j];
name1[j]=name1[j+1];
name1[j+1]=tem1;
}
}
}
for (int i=0;i<dvd.name.length ;i++ )
{
if (name1[i]==null)
{
break;
}else{
System.out.println(count1[i]+ "\t"+ name1[i]);
}
}
}
}
class Work6
{
public static void main(String[] args)
{
DVDMgr dm = new DVDMgr();
dm.initial();
dm.startMenu();
}
}