先新建一个package在建一个class第一个类
package DVD;
public class DVDSet {
String[] name=new String[50];
boolean[] state =new boolean[50];
String[] date=new String[50];
int[] count=new int[50];
}
第二个类
package DVD;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class DVDMgr {
Scanner input= new Scanner(System.in);
DVDSet dvd=new DVDSet();//初始化对象
boolean isjiechu=true;
public void initial(){
dvd.name[0]="罗马假日";
dvd.state[0]=true;
dvd.date[0]="2015-12-31";
dvd.name[1]="风声鹤唳";
dvd.state[1]=false ;
dvd.name[2]="浪漫满屋";
dvd.state[2]=false ;
}
public void rentrnMain(){
System.out.println("输入0返回");
if(input.nextInt()==0){
startMenu();
}
}
public void startMenu(){
System.out.println("\t\t\t欢迎进入迷你DVD管理器");
System.out.println("------------------------------------------------------------------------------------------");
System.out.println("\t\t0.借出排行榜");
System.out.println("\t\t1.新增DVD");
System.out.println("\t\t2.查看DVD");
System.out.println("\t\t3.删除DVD");
System.out.println("\t\t4.借出DVD");
System.out.println("\t\t5.归还DVD");
System.out.println("\t\t6.退 出");
System.out.println("------------------------------------------------------------------------------------------");
System.out.println("请选择");
int choice=input.nextInt();
switch(choice){
case 0:
list();
break;
case 1:
add();
break;
case 2:
ck();//查看DVD
break;
case 3:
del();
break;
case 4:
lend();
break;
case 5:
gh();
break;//归还DVD
case 6:
System.out.println("感谢你使用");
break;
default:
System.out.println("你输入有误请重新输入");
returnMain();
}
}
public void returnMain(){
System.out.println("输入0返回");
if(input.nextInt()==0){
startMenu();
}
}
/*
* 排行榜
*/
public void list(){
String[] newname=new String[50];
int[] newcount=new int [50];
for(int i=0;i<dvd.name.length;i++){
newname[i]=dvd.name[i];
newcount[i]=dvd.count[i];
}
for(int i=0;i<newname.length;i++){
for(int j=i+1;j<newname.length;j++){
if(newcount[i]>newcount[j]){
int temp=newcount[i];//2次交换实现次数和名字的替换
newcount[i]=newcount[j];
newcount[j]=temp;
String temp1=newname[i];
newname[i]=newname[j];
newname[j]=temp1;
}
}
}
System.out.println("-----<排行旁>-----");
System.out.println("------------------------------------------------------------------------------------------");
System.out.println("次数\t名称");
/*
* 逆序打印排行旁
*/
for(int i=newname.length-1;i>=0;i--){
if(newname[i]!=null){
System.out.println(newcount[i]+"\t《"+newname[i]+"》");
}
}
System.out.println("------------------------------------------------------------------------------------------");
returnMain();
}
/*新增DVD
*
*/
public void add(){
System.out.println("新增DVD");
System.out.println("请输入DVD名称");
String name=input.next();
for(int i=0;i<dvd.name.length;i++){
/*
* 判断名字位置数组是否为空的
*/
if(dvd.name[i]==null){
dvd.name[i]=name;
dvd.state[i]=false;//新添加的是没接触的
System.out.println("新增《"+name+"》成功");
break;
}
}
System.out.println("------------------------------------------------------------------------------------------");
returnMain();
}
/*
* 查看dvd
*/
public void ck() {
System.out.println("查看DVD");
System.out.println("序号\t状态\t名称\t\t借出日期");
for (int i = 0; i < dvd.name.length; i++) {
if (dvd.name[i] == null) {
break;
} else if (dvd.state[i] == true) {
System.out.println((i + 1) + "\t已借出\t《" + dvd.name[i] + "》" +" \t"+ dvd.date[i]);
} else if (dvd.state[i] == false) {
System.out.println((i + 1) + "\t可借\t《" + dvd.name[i] + "》");
}
}
System.out.println("------------------------------------------------------------------------------------------");
returnMain();
}
/*
* 删除dvd
*/
public void del(){
System.out.println("删除DVD");
System.out.println("请输入要删除DVD的名称");
String name =input.next();
boolean flag=false;
for(int i=0;i<dvd.name[i].length();i++){
if(dvd.name[i]!=null&&dvd.name[i].equalsIgnoreCase(name)&&dvd.state[i]==false){
int j=i;
while(dvd.name[j+1]!=null){
dvd.name[j]=dvd.name[j+1];
dvd.date[j]=dvd.date[j+1];
j++;
}
dvd.name[j]=null;
dvd.date[j]=null;
System.out.println("成功删除《"+name+"》");
flag=true;//标示位
break;
}
else if(dvd.name[i]!=null&&dvd.name[i].equalsIgnoreCase(name)&&dvd.state[i]==true){
System.out.println("此DVD为借出状态,不能删除");
flag=true;
break;
}
if(flag==true){
System.out.println("没有找到信息,请核实");
}
}
System.out.println("------------------------------------------------------------------------------------------");
returnMain();
}
/*
* 借出
*/
public void lend(){
System.out.println("借出DVD ");
System.out.println("请输入借出DVD的名称");
String name =input.next();
for(int i=0;i<dvd.name.length;i++){
if(dvd.name[i]==null){
System.out.println("没有找到匹配的信息");
break;
}
else if(dvd.name[i].equals(name)&&dvd.state[i]==false){
System.out.println("请输入借出日期(年-月-日)");
dvd.date[i]=input.next();
System.out.println("《"+name+"》借出成功");
dvd.state[i]=true;
dvd.count[i]++;
break;
}
else if(dvd.name[i].equals(name)&&dvd.state[i]==true){
System.out.println("不好意思《"+name+"》已借出");
break;
}
}
System.out.println("------------------------------------------------------------------------------------------");
returnMain();
}
//gh
public void gh(){
System.out.println("归还DVD");
System.out.println("请输入DVD名称");
String name=input.next();
long loan=0;//租金
for(int i=0;i<dvd.name.length;i++){
if(dvd.name[i]==null){
System.out.println("没有找到匹配信息");
break;
}
else if(dvd.name[i].equals(name)&&dvd.state[i]==true){
System.out.println("请输入归还日期(年-月-日)");
String redate=input.next();
loan=charge(dvd.date[i],redate);
System.out.println("归还《"+name+"》成功");
dvd.state[i]=false;
System.out.println("借出日期为"+dvd.date[i]);
System.out.println("归还日期为"+redate);
System.out.println("应付租金(元):"+loan);
break;
}
else if(dvd.name[i].equals(name)&&dvd.state[i]==false){
System.out.println("《"+name+"》没有被借出,请核实");
break;
}
}
System.out.println("------------------------------------------------------------------------------------------");
returnMain();
}
/*
* 计费类
*/
public long charge(String dstr1,String dstr2){
long charge=0;
SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd");
try{
Date d1=sd.parse(dstr1);
Date d2=sd.parse(dstr2);
charge=(d2.getTime()-d1.getTime())/(24*30*30*1000);
}catch(ParseException e){
e.printStackTrace();
}
return charge;
}
}
第三个类//测试
package DVD;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DVDMgr test=new DVDMgr();
test.initial();
test.startMenu();
}
}