简易的DVD管理器

//----------------简易版DVD

import java.util.*;
import java.text.SimpleDateFormat;
import java.text.ParseException;
//-----------------------------------数据集合----------------------------------
class DVDSet
{

  String[] name = new String [50]; //---- DVD名称数组
  int[] state = new int[50]; //---- DVD借出状态数组
  String[] date = new String[50]; //---- DVD借出日期数组
  int[] count = new int[50]; //-----DVD借出次数
}


//------------------------------数据管理类-------------------------------------------------
class DVDMgr 
{

  DVDSet DVD = new DVDSet(); //-----DVD对象


  //-----------------------------------初始化三张DVD信息 ---------------------------
  public void initial(){

    DVD.name[0] = "罗马假日";
    DVD.state[0] = 0;
    DVD.date[0] = "2016-11-17";
    DVD.count[0] = 1;

    DVD.name[1] = "风声鹤唳";
    DVD.state[1] = 1;
    DVD.count[1] = 0;

    DVD.name[2] = "浪漫满屋";
    DVD.state[2] = 1;
    DVD.count[2] = 0;
  }


  //-----------------------------------菜单的切换方法 ----------------------------------
  public void startMenu(){ 

  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("7. 借出排行榜");
  System.out.println("*******************************"); 
  System.out.println("请选择你要查看的栏目:"); 
  Scanner keyNum = new Scanner(System.in);
  int choice = keyNum.nextInt();

  switch(choice){
    case 1:
      System.out.println("----------->新增DVD~~~");
      System.out.println("*******************************");

      add();
      break;
    case 2:
      System.out.println("----------->查看DVD~~~");
      System.out.println("*******************************");

      search();
      break;
    case 3:
      System.out.println("----------->删除DVD~~~");
      System.out.println("*******************************");

      delete();
      break;
    case 4:
      System.out.println("----------->借出DVD~~~");
      System.out.println("*******************************");

      lend();
      break;
    case 5:
      System.out.println("----------->归还DVD~~~");
      System.out.println("*******************************");

      returnDVD();
      break;
    case 6:
      System.out.println("----------->退出DVD~~~");
      System.out.println("*******************************"); 

      returnMain();
      break;
    case 7:
      System.out.println("----------->DVD借出排行榜~~~");
      System.out.println("*******************************"); 

      list();
      break;
      default :
      System.out.println("不怕神一样的对手,就怕猪一样的队友,程序重载中........");
      break;
   }
  }

  //-----------------------------------新增DVD方法------ -------------------------------

  public void add(){

    System.out.println("请输入你要新增的DVD信息:");
    Scanner newName = new Scanner(System.in);
    String addName = newName.next();

    for ( int i = 0; i < DVD.name.length ; i++){

      if (DVD.name[i] == null) {

        DVD.name[i] = addName;
        DVD.state[i] = 1;
        System.out.println("书海无涯,回头是岸,《" + addName + "》兄弟你跑进来爪子嘛!!!");
        break;
      }
    }

    returnMain();
  }

  //-----------------------------------查看DVD方法------ -------------------------------

  public void search(){

    System.out.println("序号\t" + "状态\t\t" + "名称\t\t" + "借出日期");
    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\t" +"<<" + DVD.name[i] + ">>\t" + DVD.date[i]);
      }else if(DVD.state[i] == 1){

        System.out.println(( i + 1) + "\t可以借阅\t" + "<<" + DVD.name[i] + ">>");
      }
    }

    returnMain();
  }

  //-----------------------------------删除DVD方法------ -------------------------------

  public void delete(){

    System.out.println("请输入你要删除的DVD:");
    Scanner keyName = new Scanner(System.in);
    String deleteName = keyName.next();

    for(int i = 0; i < DVD.name.length; i ++){

      if(DVD.name[i] != null && DVD.name[i].equalsIgnoreCase(deleteName) && 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.date[j]=null;

        System.out.println("删除《"+ deleteName +"》成功!");

        break;
      }else if (DVD.name[i] != null && DVD.name[i].equalsIgnoreCase(deleteName) && DVD.state[i] == 0){

        System.out.println("《"+ deleteName+"》宝贝儿是人家的,你想删都删不了!!!"); 

        break;
      }
    }

    returnMain();
  }  

  //-----------------------------------借出DVD方法------ -------------------------------

  public void lend(){

    System.out.println("请输入你要借出的DVD:");
    Scanner keyName = new Scanner(System.in);
    String lendName = keyName.next();

    for (int i = 0; i < DVD.name.length; i++){

      if(DVD.name[i] == null){

        System.out.println("读DVD不积极,借DVDDVD不理~~~");
        break;
      }else if (DVD.name[i].equals(lendName) && DVD.state[i] == 1){

        System.out.println("终于找到你,还好我没放弃!!!");
        DVD.state[i] = 0;

        System.out.println("你输入DVD的借阅日期(年-月-日):");
        Scanner keyDate = new Scanner(System.in);
        String lendDate = keyDate.next();
        DVD.date[i] = lendDate;

        System.out.println("《" + lendName + "》DVD借出成功!!!");
        DVD.count[i]++;
        break;
      }else if (DVD.name[i].equals(lendName) && DVD.state[i] == 0){

        System.out.println("借DVD不积极,脑壳有问题,《" + lendName + "《已被借别人借起跑了~~~~");
        break;
      }
    }

    returnMain();
  }

  //-----------------------------------归还DVD方法------ -------------------------------

  public void returnDVD(){

    String lendDate;
    String returnDate;
    System.out.println("请输入你要归还的DVD:");
    Scanner keyName = new Scanner(System.in);
    String returnName = keyName.next();

    for(int i = 0; i < DVD.name.length; i++){

      if (DVD.name[i].equals(returnName) && DVD.state[i] == 0){

        System.out.println("有借有还,再借不难,小伙子来还DVD了~~~");
        lendDate = DVD.date[i];
        System.out.println("请输入你的归还日期(年-月-日):");
        Scanner keyDate = new Scanner(System.in);
        returnDate = keyDate.next();
        System.out.println("《" + returnName + "》归还成功!!!");

        DVD.state[i] = 1;

        DVD.date[i] = null;

        System.out.println("借DVD期间你所要支付的租金为(Charge):" + charge(lendDate,returnDate) + "$");
        break;
      }else if (DVD.name[i].equals(returnName) && DVD.state[i] == 1){

        System.out.println("这DVD在我这里,小伙子你在逗我吗?");
        break;
      }else if (DVD.name[i].equals(returnName) == false){

        System.out.println("小屁孩,你是不是跑错地方了........站住,别跑~~~~~");
        break;
      }
    }

    returnMain();
  }

  //-------------日期计算租金
  public long charge(String lendDate,String returnDate){

    long charge = 0;

    SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd");

    try {
      Date LD = SDF.parse(lendDate);
      Date RD = SDF.parse(returnDate);

      charge=(RD.getTime()-LD.getTime())/(24*60*60*1000);
    } catch (ParseException e) {

      e.printStackTrace();
    }

    return charge; 
  }

  //-----------------------------------返回主菜单方法 ----------------------------------
  public void returnMain(){

    Scanner input = new Scanner(System.in);
    System.out.print("返回主菜单请输入-0-:");
    if (input.nextInt() == 0){

    startMenu();
    }else{
  
      System.out.println("输入错误,异常终止!!!");
      returnMain();
    }
  }


  //-----------------------------------DVD借出排行榜------ -----------------------------

  public void list() {

    String[] newDVDs = new String[100];
    int[] newCounts = new int[100];

    for(int i = 0; i < DVD.name.length; i++){

      newDVDs[i] = DVD.name[i];
    }

    for(int i = 0; i < DVD.count.length; i++){

      newCounts[i] = DVD.count[i];
    }

    for(int i = 0; i < newDVDs.length; i++){

      if(newDVDs[i] == null){

        break;
      }

      for(int j = i; j < newDVDs.length; j++){

        if(newDVDs[j] == null){

        break;
      }

      if(newCounts[i] < newCounts[j]){

        int temp = newCounts[i];

        newCounts[i] = newCounts[j];

        newCounts[j] = temp;

        String str = newDVDs[i];

        newDVDs[i] = newDVDs[j];

        newDVDs[j] = str;
      }    
    }  
  }
  System.out.println("-----------------------------------------");
  System.out.println("次数\t名称");


  for(int i = 0; i < newDVDs.length; i ++){
    if(newDVDs[i] == null){
    break;
  }
  System.out.println(newCounts[i] + "\t" + "<<" + newDVDs[i] + ">>");

  }
  System.out.println("-----------------------------------------");


  returnMain();
  }

}

 

class MiniDVD
{
  public static void main(String[] args) 
  {

    DVDMgr manger = new DVDMgr(); //----构造管理员对象

    System.out.println("\n"); 
    System.out.println("*******************************"); 
    System.out.println("欢迎使用迷你DVD管理器");
    System.out.println("*******************************");

    manger.initial();

    manger.startMenu();
  }
}

posted @ 2016-12-06 15:02  姚龔子  阅读(128)  评论(0编辑  收藏  举报