美文网首页
java 简单创建一个图书管理系统

java 简单创建一个图书管理系统

作者: 夏有清风丶 | 来源:发表于2016-11-28 08:03 被阅读0次

    直接上代码

    import java.util.*;

    import java.text.SimpleDateFormat;

    import java.text.ParseException;

    class DVDSet

    {

    String[] name =new String[50];

    int[] state = new int[50];

    String[] date = new String[50];

    int[] count = new int[50];

    }

    class DVDMgr

    {

    DVDSet dvd = new DVDSet();

    public void initial(){

    dvd.count[0] = 1;

    dvd.name[0] = "百年孤独";

    dvd.state[0] = 0;

    dvd.date[0] = "2012-2-1";

    dvd.name[1] = "三国演义";

    dvd.state[1] = 1;

    dvd.name[2] = "三体";

    dvd.state[2] = 1;

    }

    public void startMenu(){

    Scanner reader = new Scanner(System.in);

    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("请选择:");

    int choice = reader.nextInt();

    System.out.println();

    switch (choice)

    {

    case 0:

    show();

    returnMain();

    break;

    case 1:

    add();

    returnMain();

    break;

    case 2:

    check();

    returnMain();

    break;

    case 3:

    del();

    returnMain();

    break;

    case 4:

    borrow();

    returnMain();

    break;

    case 5:

    returnDvd();

    returnMain();

    break;

    case 6:

    System.out.println("有缘再会");

    break;

    default :

    System.out.println("就内6个数也能输错?你还是别玩了");

    break;

    }

    }

    public void returnMain(){

    Scanner reader = new Scanner(System.in);

    System.out.print("输入0返回主菜单:");

    if(reader.nextInt() == 0){

    startMenu();

    }

    else{

    System.out.println("连个0都不会输,你可长点儿心吧!");

    }

    }

    //各个功能方法

    //排行榜

    public void show(){

    String[] newDvds = new String[50];

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

    newDvds[i] = dvd.name[i];

    }

    int[] newCounts = new int[50];

    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\t名称");

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

    if(newDvds[i] == null){

    break;

    }

    System.out.println(newCounts[i] + "\t\t" + "<<" + newDvds[i] + ">>");

    }

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

    }

    //1.新增

    public void add(){

    Scanner reader = new Scanner(System.in);

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

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

    System.out.println("请输入要新增DVD的名字");

    dvd.name[i] = reader.next();

    dvd.state[i] = 1;

    System.out.println("新增成功~");

    break;

    }

    }

    }

    //2.查看

    public void check(){

    System.out.println("序号\t状态\t名称\t\t借出日期");

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

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

    break;

    }

    if (dvd.state[i] == 1)

    {

    System.out.println( i + "\t" + "可借" + "\t" + "<<" +dvd.name[i] + ">>" );

    }

    else {

    System.out.println( i + "\t" + "不可借" + "  " + "<<" + dvd.name[i] + ">>" + "\t" + dvd.date[i]);

    }

    }

    }

    //3.删除

    public void del(){

    Scanner reader = new Scanner(System.in);

    System.out.println("请输入要删除DVD的名字");

    String name = reader.next();

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

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

    System.out.println("并没有找到你所要删除的DVD");

    break;

    }else if(dvd.name[i].equalsIgnoreCase(name)){

    if(dvd.state[i] == 0){

    System.out.println("<<" + name + ">>为借出状态,不能删除!" );

    break;

    }

    else{

    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("删除《"+name+"》成功!");

    break;

    }

    }

    }

    }

    //4.借出

    public void borrow(){

    Scanner reader = new Scanner(System.in);

    System.out.println("少年,你所追寻的是那张碟啊");

    String name = reader.next();

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

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

    System.out.println("少年郎,然而并没有找到你想要的碟诶!");

    break;

    }

    else if(dvd.name[i].equalsIgnoreCase(name)){

    if(dvd.state[i] == 0){

    System.out.println("少年郎,你要的碟已经被借走了");

    break;

    }else{

    dvd.state[i] = 0;

    System.out.println("请输入借出的日期 (年-月-日  按照格式来啊魂淡)");

    dvd.date[i] = reader.next();

    dvd.count[i]++;

    System.out.println("现在这张" + "<<" + dvd.name[i] + ">>" + "就是你的了~\n人类的未来靠你了!!");

    break;

    }

    }

    }

    }

    //5归还

    public void returnDvd(){

    Scanner reader = new Scanner(System.in);

    System.out.println("哦?来还哪张碟子啊");

    String name = reader.next();

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

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

    System.out.println("小伙子 你是不是记错了?我们这儿原来没这种片儿啊");

    break;

    }

    if(dvd.name[i].equalsIgnoreCase(name)){

    if(dvd.state[i] == 1){

    System.out.println("这张珍藏版在我们仓库里好好放着呢  你是不是记错了");

    }else{

    System.out.println("现在是什么时辰 (年-月-日)");

    String newDate = reader.next();

    System.out.println("归还" + "<<" + name + ">>" + "成功");

    System.out.println("借出日期为:" + dvd.date[i]);

    System.out.println("归还日期为:" + newDate);

    System.out.println("应付租金(元)" + charge(dvd.date[i],newDate));

    dvd.state[i] = 1;

    break;

    }

    }

    }

    }

    //日期计算

    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*60*60*1000);

    } catch (ParseException e) {

    e.printStackTrace();

    }

    return charge;

    }

    }

    class  MiniDvd

    {

    public static void main(String[] args)

    {

    DVDMgr dm = new DVDMgr();

    dm.initial();

    dm.startMenu();

    }

    }

    相关文章

      网友评论

          本文标题:java 简单创建一个图书管理系统

          本文链接:https://www.haomeiwen.com/subject/yxtopttx.html