| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1840 人关注过本帖
标题:第一次写的图书馆管理系统,代码量很大,而且数据插入只能插入一条
只看楼主 加入收藏
程序猿单身狗
Rank: 1
等 级:新手上路
帖 子:36
专家分:4
注 册:2016-7-11
结帖率:100%
收藏
 问题点数:0 回复次数:3 
第一次写的图书馆管理系统,代码量很大,而且数据插入只能插入一条
第一次写的图书馆管理系统,代码量很大,而且数据插入只能插入一条,想用数组来存储多条数据,但感觉又有点问题,求建议!!

程序代码:
//第一个Main.java
package Main;
import java.util.Scanner;
class Main{
   public static void main(String[] args){
         Main main=new  Main();
         main.view();
     }
    void view(){
    //界面布局
    System.out.println("------------   图书馆管理系统     -----------");
    System.out.println("               1.登录管理员帐号        ");
    System.out.println("               2.注册帐号                    ");
    System.out.println("               3.退出系统                   ");
    System.out.println("--------------------------------------");
    try{
    /*异常处理*/
    System.out.print("请选择选项:");
    Scanner scan=new Scanner(System.in);
    int select =scan.nextInt();
    
    //判断用户选择
    if((select>0)&&(select<4)){
    if(select==1){
    //跳转到登录界面
     new Login().login();
    }
     if(select==2){
    //跳转到注册界面
     new Zhuce().zhuce();
     }
     //退出
     if(select==3){
         System.out.println("谢谢使用本系统,欢迎下次使用!!!");
        System.exit(0);
     }
      }else {
           System.out.println("输入错误,请重新输入:");
        view();
      }
    }catch(Exception e){
        System.out.println("请输入序号!!!");
        view();
    }
    
}
}
//第二个
package Main;

import java.util.Scanner;

public class Login {
//设置帐号和密码的变量名
public     static String username;
public static int password;
Scanner canner=new Scanner(System.in);
    String un;
    int pw;
    void login(){
        System.out.println("--------------图书馆登录界面--------------");
        try{
        //登录帐号
        System.out.print("管理员帐号: ");
        un=canner.next();
        //输入密码
        System.out.print("密码:");
        pw =canner.nextInt();
        
        System.out.println("-------------------------------------");
        
        
        //判断用户登录
        if((equals(username)==equals(un))&&(password==pw)||(("超级管理员帐号").equals(un))){
            if(equals(un)==equals("超级管理员帐号")){
                
            }
            System.out.println("登录成功!");
            System.out.println("");
             new View().view();
        }else{
            System.out.println("输入有误请重新输入!");
            new Main().view();
        }
        }catch(Exception e){
            System.out.println("输入错误!!!!!");
            login();
        }
        }
    

}
第三个
package Main;

import java.util.Scanner;

public class View {
    Scanner scan=new Scanner(System.in);
    int select;
    void view(){
    System.out.println("欢迎您"+Login.username);
    System.out.println("------------- 图书馆管理系统----------------");
    System.out.println("-------       1.图书信息                            -------");
    System.out.println("-------       2.读者信息                             -------");
    System.out.println("-------       3.借阅信息                             -------");
    System.out.println("-------       4.查询图书                            -------");
    System.out.println("-------       5.退出系统                            -------");
    System.out.println("-------------------------------------");
    //异常处理
    try{
    System.out.print("请选择选项:");
    select=scan.nextInt();
    }catch(Exception e){
        System.out.println("请输入序号!!");
        view();
    }
    /*
      1.判断用户选择
     
    */
    switch(select){
        //图书信息
    case 1:
        new Bookinfo().bookinfo();
        break;
   
    case 2:
        //读者信息
        new Readerinfo().readerinfo();
        break;
    
    case 3:
        //借阅信息
        new Reading().reading();
        break;
   
    case 4:
        //借阅信息
        System.out.println("----------------");
        System.out.println("本功能还没实现!!");
        System.out.println("----------------");
        view();
        break;

 
    case 5:
        //退出
        System.out.println("谢谢使用本系统,欢迎下次使用!!!");
        System.exit(0);
        break;
        default:
            System.out.println("错误!!!");
            view();
    }
    
    }
    
}
//第四个
package Main;

import java.util.Scanner;

//读者信息
public class Readerinfo {
    private String name="";
    private int id;
    private int num;
Scanner scan=new Scanner(System.in);
        void readerinfo(){
            System.out.println("----------- 管理界面    ----------------");
            System.out.println("            1.读者信息");
            System.out.println("            2.添加读者信息");
            System.out.println("            3.删除读者信息");
            System.out.println("            4.返回");
            System.out.println("-----------------------------------");
            try{
            System.out.print("请选择菜单:");
            int select=scan.nextInt();
            switch(select){
            //读者信息
            case 1:
                String rename[][]=new String[3][];
                System.out.println("--------------    读者信 息        -----------");
                //判断是否有信息
                if("".equals(name))
                    System.out.println("当前没有信息!!!");
                else
                System.out.println("姓名:"+name+" "+"证号:"+id+" "+"当前借量:"+num);
                
                System.out.println("---------------------------------------");
                
                System.out.println("");
                scan.nextLine();
                readerinfo();
                break;
            //添加读者信息
            case 2:
                System.out.println("---------  添加读者信息    ----------");
                System.out.print("请输入读者姓名:");
                name=scan.next();
                System.out.print("请输入读者证号:");
                id=scan.nextInt();
                System.out.print("请输入读者当前借量:");
                num=scan.nextInt();
                System.out.println("-----------------------------");
                System.out.println("添加成功!!");
                
                
                
                scan.nextLine();
                readerinfo();
            
            //删除读者信息
            case 3:
                System.out.println("---------  删除读者信息    ----------");
                System.out.println("是否删除?");
                System.out.println("输入“是” 或者“否”");
                String sel =scan.next();
                if("是".equals(sel)){
                 name="";
                 id=0;
                 num=0;
                 System.out.println("删除成功");
                 readerinfo();
                }
                if("否".equals(sel)){
                    readerinfo();
                    
                }
                /*System.out.println("按Enter键返回。。。");
                scan.nextLine();
                break;*/
            //返回
            case 4:
                new View().view();
                
                
            default:
                    System.out.println("");
                    System.out.println("输入有误,请重新输入");
                    System.out.println("");
                    readerinfo();
                    
               }
            }catch(Exception e){
                //异常处理
                System.out.println("错误,请重新输入!!!");
                new View().view();
            }
            }
        }


//第五个
package Main;

import java.util.Scanner;

//图书信息
public class Bookinfo {
/*    public static String name;
    public static String reader;
    public static int bookID;
    public static int time;
*/
    Scanner scan=new Scanner(System.in);

      
      
        void bookinfo(){
            System.out.println("------------  图书信息:   ---------");
            
            System.out.println("1.JAVA基础程序开发  作者:李小明");
            System.out.println("");
            System.out.println("2.JAVA中级程序开发   作者:李中明");
            System.out.println("");
            System.out.println("3.JAVA高级程序开发  作者:李高明");
            System.out.println("");
            System.out.println("-------------------------------");
            System.out.println("按Enter返回。。。");
            //返回选择界面
            scan.nextLine();
            new    View().view();
            }
            }
    

//第六个
package Main;

import java.util.Scanner;

//借阅信息
public class Reading {
    private String name="";
    private int id;
    private int bid;
    private String retime="";
    Scanner cann=new Scanner(System.in);
    
      void rt(){
      //显示借阅信息
      System.out.println("证号:"+id+" "+"姓名:"+name+" "+"书号:"+bid+" "+"还书日期:"+retime);
      }
    
      void reading(){
        System.out.println("------------借阅信息:------------");
        System.out.println("            1.已借信息 ");
        System.out.println("            2.登入信息");
        System.out.println("            3.书本归还");
        System.out.println("            4.返回");
        System.out.println("-------------------------------");
        try{
        System.out.print("选择菜单:");
        int select=cann.nextInt();
        switch(select){
        case 1:
            //已借信息
            System.out.println("-------------------    已借信息          -----------------------");
            if("".equals(name))
                System.out.println("当前没有信息!!!");
            else
            rt();
            System.out.println("-------------------------------------------------------");
            cann.nextLine();
            reading();
        case 2:
            //登入信息
            System.out.println("--------------登入信息-----------");
            System.out.print("证号:");
             id =cann.nextInt();
            System.out.print("姓名:");
             name =cann.next();
            System.out.print("书号:");
            bid=cann.nextInt();
            System.out.print("还书日期(格式 :1990-01-2):");
            retime =cann.next();
            System.out.println("------------------------------");
            System.out.println("添加成功!!");
            cann.nextLine();
            reading();
        case 3:
            
            //书本归还
            System.out.println("-------------书本归还------------");
            System.out.println("请输入书号:");
            int rt=cann.nextInt();
            
            reading();
        case 4:
            //返回
            new View().view();
            
        }
        }catch(Exception e){
            System.out.println("输入错误!!");
            reading();
        }
        
    }
}
//第七个
package Main;

import java.util.Scanner;

  public class Zhuce {
    String user;
    int pad;
    Scanner ce =new Scanner(System.in);
    void zhuce(){
        try{
        System.out.println("--------------图书馆注册界面--------------");
        System.out.print("管理员帐号:");
        user=ce.next();
        System.out.print("密码:");
         pad=ce.nextInt();
         System.out.println("-------------------------------------");
         System.out.println("您注册的昵称为:"+user);
         System.out.println("您注册的密码为:"+pad);
         System.out.println("-------------------------------------");
         System.out.println("");
         //赋值给登录帐号
         Login.username=user;
         //赋值给登录密码
         Login.password=pad;
        }catch(Exception e){
            System.out.println("输入错误!重新输入!!!");
            System.out.println(e);
            zhuce();
        }
         //返回登录界面
        new Login().login();
    }
}

搜索更多相关主题的帖子: 管理系统 图书馆 而且 管理系统 图书馆 而且 管理系统 图书馆 而且 
2016-07-13 20:09
程序猿单身狗
Rank: 1
等 级:新手上路
帖 子:36
专家分:4
注 册:2016-7-11
收藏
得分:0 

这个类出现异常时会无限循环。怎么回事
程序代码:
package Main;

import java.util.Scanner;

public class View {
    Scanner scan=new Scanner(System.in);
    int select;
    void view(){
    System.out.println("欢迎您"+Login.username);
    System.out.println("------------- 图书馆管理系统----------------");
    System.out.println("-------       1.图书信息                            -------");
    System.out.println("-------       2.读者信息                             -------");
    System.out.println("-------       3.借阅信息                             -------");
    System.out.println("-------       4.查询图书                            -------");
    System.out.println("-------       5.退出系统                            -------");
    System.out.println("-------------------------------------");
    //异常处理
    try{
    System.out.print("请选择选项:");
    select=scan.nextInt();
    }catch(Exception e){
        System.out.println("请输入序号!!");
        view();
    }
    /*
      1.判断用户选择
     
    */
    switch(select){
        //图书信息
    case 1:
        new Bookinfo().bookinfo();
        break;
   
    case 2:
        //读者信息
        new Readerinfo().readerinfo();
        break;
    
    case 3:
        //借阅信息
        new Reading().reading();
        break;
   
    case 4:
        //借阅信息
        System.out.println("----------------");
        System.out.println("本功能还没实现!!");
        System.out.println("----------------");
        view();
        break;

 
    case 5:
        //退出
        System.out.println("谢谢使用本系统,欢迎下次使用!!!");
        System.exit(0);
        break;
        default:
            System.out.println("错误!!!");
            view();
    }
    
    }
    
}

人生几个十年?最重要活的痛快。
2016-07-13 20:14
千与千寻123
Rank: 1
等 级:新手上路
威 望:1
帖 子:2
专家分:0
注 册:2016-7-14
收藏
得分:0 
回复 2楼 程序猿单身狗
package com.njit.day712.exe;
//书
//标准的实体类
public class Book {
    private String name;
    private double price;//本次作业中没有使用
    private int num;//该书在图书馆中的本数
    private int outNum;//改书已经借出去的本数
    private int count;//改书被借出去的次数
   
   
   
   
    public Book() {
    }
    public Book(String name, double price, int num, int outNum, int count) {
        this.name = name;
        this.price = price;
        this.num = num;
        this.outNum = outNum;
        this.count = count;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public int getNum() {
        return num;
    }
    public void setNum(int num) {
        this.num = num;
    }
    public int getOutNum() {
        return outNum;
    }
    public void setOutNum(int outNum) {
        this.outNum = outNum;
    }
    public int getCount() {
        return count;
    }
    public void setCount(int count) {
        this.count = count;
    }

}




import java.util.Arrays;


//图书管理器
public class BookManager {
    //数组   用于管理一组书
     Book[] books = new Book[100];
     public BookManager(){
         for(int i=0;i<books.length;i++){
             Book book = new Book();
             book.setName("java"+i);
             book.setNum(i+1);
             book.setPrice(i+100);
             books[i]=book;
         }
         books[99].setCount(99);
     }
    //进书
    public  void inBook(Book book){
        //能够将书放在书架上
       //遍历
        if(null!=books[books.length-1]){
            System.out.println("书架已满!");
            return;
        }
        for(int i=0;i<books.length;i++){
            if(books[i]!=null){
               
            if(books[i].getName().equals(book.getName())){
                 books[i].setNum(books[i].getNum()+book.getNum());
                 break;
            }
        }else{
            books[i]=book;
            break;
        }
            
    }
        System.out.println(book.getName()+",成功添加到书架上");
       //1.看书架上是否已经有这本书,如果有,则将书的数量相加
//       2.一直找到空格,发现仍然没有这本书,则将改书直接放入到此格子上   
}
    /**
     * 根据书名借书
     * @param name 书名
     */
     
    public void borrowBook(String name,int borrowNum){
        /*
         * 1,name对应的书可能没有
         * 2,name对应的书有     书是有可能被借完  num-outNum>=borrowNum
         *
         * 可以借outNum+borrowNum
         * count
         */   
            int index = -1;
            for (int i = 0; i < books.length; i++)
              {
                // 书架上有书
                if (null != books[i])
                    {
                    // 找到想要借的书
                     if (name.equals(books[i].getName()))
                       {
                        index = i;
                        }
                     }
                else {
                    break;
                    }
             }
            if(index == -1)
              {
                System.out.println("没有你你要借的书:'" + name + "'.");
               }
            else
              {
                if (books[index].getNum() - books[index].getOutNum() < borrowNum)
                   {
                    System.out.println("还有"
                            + (books[index].getNum() - books[index].getOutNum())
                            + "本书,不够你借的!");
                    }
                else
                   {
                    books[index].setOutNum(books[index].getOutNum() + borrowNum);
                    books[index].setCount(books[index].getCount() + borrowNum);
                    System.out.println("成功借出" + borrowNum + "本'"
                            + books[index].getName() + "'.");
                   }
              }
        }
   
    //还书,入库
    public void returnBook(String name, int backNum) {
        /*
         * 1, name对应的书可能没有2,name对应的书有 书是有可能被借完
         *
         * 可以借 outNum - borrowNum
         */
        int index = -1;
        for (int i = 0; i < books.length; i++)
              {
            // 书架上有书
              if (null != books[i])
                  {
                // 找到想要借的书
                  if (name.equals(books[i].getName()))
                     {
                      index = i;
                       break;
                     }
                  }
              else
                {
                break;
                }
             }
        if(index != -1)
          {
            books[index].setOutNum(books[index].getOutNum() - backNum);
            System.out.println("成功还了" + backNum + "本'"
                    + books[index].getName() + "'.");
          }
         else
           {
            System.out.println("没有你你要还的书:'" + name + "'.");
           }
    }
   
    /**
     * 根据书名删除书
     * @param name 书的名字
     */
    public void deleteOutBook(String name){
 
        int index=-1;
        for(int i=0;i<books.length;i++){
            
            if(null!=books[i]){
                if(books[i].getName().equals(name)){
                    index=i;
                    break;
                }
               
            }else{
                break;
            }
        }
        if(index==-1){
            System.out.println("没有找到要删除的书!");   
        }else{
            books[index]=null;
        }
        
        
        if(index==books.length-1){
            books[index]=null;
        }else{
            for(int i=index;i<books.length;i++){
                 
                books[i]=books[i+1];
                if(null==books[i+1]){
                    break;
                }
            }
            
        }
        System.out.println("成功删除"+name+"这本书");
     
    }
    //查看书
    public void lookOneBook(String name){
        for(int i=0;i<books.length;i++){
            if(books[i].getName().equals(name)){
//                books[i].setName(books[i].getName());
//                books[i].setNum(books[i].getNum());
//                books[i].setPrice(books[i].getPrice());
//                books[i].setOutNum(books[i].getOutNum());
//                books[i].setCount(books[i].getCount());   
                System.out.println("书名t价格t本数t已借数量t借出次数");   
                System.out.println(books[i].getName()+"t"+books[i].getPrice()+"t"+books[i].getNum()+"t"+books[i].getOutNum()+"t"+books[i].getCount());
            break;
            }
            
        }
    }
   
   
    public void rankBook(Book book){
        int[] rankArry = new int[100];
        int i,j,temp;
        for( i=0;i<books.length-1;i++){
            rankArry[i]=book.getCount();
        }
        for( i =0;i<rankArry.length;i++){
            for( j=0;j<rankArry.length-i;j++){
                if(rankArry[j]>rankArry[j+1]){
                    temp=rankArry[j];
                    rankArry[j]=rankArry[j+1];
                    rankArry[j+1]=temp;
                }   
            }
        }
        
        for( i=0;i<books.length;i++){
            System.out.println("书名t价格t本数t已借数量t借出次数");
            System.out.println(book.getName()+"t"+book.getPrice()+"t"+book.getNum()+"t"+book.getOutNum()+"t"+rankArry[i]);
        }
        
    }
   
   
   
    public Book[] getSortBooks()
      {
        int count = 0;//记录书架上有多少本书
        if(books[books.length - 1] != null)
          {
            count = books.length;
          }
        else
          {
            for(int i = 0; i < books.length; i++)
              {
                if(null == books[i])
                  {
                    count = i;
                    break;
                  }
              }
           }
        if(count == 0)
           {
            return null;
           }
        else
          {
            Book[] sortBooks = new Book[count];
            System.arraycopy(books, 0, sortBooks, 0, count);
//            for(int i = 0; i < sortBooks.length; i++){
//                sortBooks[i] = books[i];
//            }
            //排序
            Arrays.sort(sortBooks,new BookComparator());
//            for(int i = 0; i < sortBooks.length - 1; i++){
//                for(int j = 0; j < sortBooks.length - 1 - i; j++){
//                    if(sortBooks[j + 1].getCount() > sortBooks[j].getCount()){
//                        Book b = sortBooks[j + 1];
//                        sortBooks[j + 1] = sortBooks[j];
//                        sortBooks[j] = b;
//                    }
//                }
//            }
            return sortBooks;
          }
      }

   
    //显示所有的书
    public void show(){
        if(books[0]==null){
            System.out.println("书架上一本书也没有");
        }else{
             System.out.println("书名t价格t本数t已借数量t借出次数");
             for(Book book : books){
             if(null!=book){
                System.out.println(book.getName()+"t"+book.getPrice()+"t"+book.getNum()+"t"+book.getOutNum()+"t"+book.getCount());
               }
            }
        }
   }
}





import java.util.Arrays;


//图书管理器
public class BookManager {
    //数组   用于管理一组书
     Book[] books = new Book[100];
     public BookManager(){
         for(int i=0;i<books.length;i++){
             Book book = new Book();
             book.setName("java"+i);
             book.setNum(i+1);
             book.setPrice(i+100);
             books[i]=book;
         }
         books[99].setCount(99);
     }
    //进书
    public  void inBook(Book book){
        //能够将书放在书架上
       //遍历
        if(null!=books[books.length-1]){
            System.out.println("书架已满!");
            return;
        }
        for(int i=0;i<books.length;i++){
            if(books[i]!=null){
               
            if(books[i].getName().equals(book.getName())){
                 books[i].setNum(books[i].getNum()+book.getNum());
                 break;
            }
        }else{
            books[i]=book;
            break;
        }
            
    }
        System.out.println(book.getName()+",成功添加到书架上");
       //1.看书架上是否已经有这本书,如果有,则将书的数量相加
//       2.一直找到空格,发现仍然没有这本书,则将改书直接放入到此格子上   
}
    /**
     * 根据书名借书
     * @param name 书名
     */
     
    public void borrowBook(String name,int borrowNum){
        /*
         * 1,name对应的书可能没有
         * 2,name对应的书有     书是有可能被借完  num-outNum>=borrowNum
         *
         * 可以借outNum+borrowNum
         * count
         */   
            int index = -1;
            for (int i = 0; i < books.length; i++)
              {
                // 书架上有书
                if (null != books[i])
                    {
                    // 找到想要借的书
                     if (name.equals(books[i].getName()))
                       {
                        index = i;
                        }
                     }
                else {
                    break;
                    }
             }
            if(index == -1)
              {
                System.out.println("没有你你要借的书:'" + name + "'.");
               }
            else
              {
                if (books[index].getNum() - books[index].getOutNum() < borrowNum)
                   {
                    System.out.println("还有"
                            + (books[index].getNum() - books[index].getOutNum())
                            + "本书,不够你借的!");
                    }
                else
                   {
                    books[index].setOutNum(books[index].getOutNum() + borrowNum);
                    books[index].setCount(books[index].getCount() + borrowNum);
                    System.out.println("成功借出" + borrowNum + "本'"
                            + books[index].getName() + "'.");
                   }
              }
        }
   
    //还书,入库
    public void returnBook(String name, int backNum) {
        /*
         * 1, name对应的书可能没有2,name对应的书有 书是有可能被借完
         *
         * 可以借 outNum - borrowNum
         */
        int index = -1;
        for (int i = 0; i < books.length; i++)
              {
            // 书架上有书
              if (null != books[i])
                  {
                // 找到想要借的书
                  if (name.equals(books[i].getName()))
                     {
                      index = i;
                       break;
                     }
                  }
              else
                {
                break;
                }
             }
        if(index != -1)
          {
            books[index].setOutNum(books[index].getOutNum() - backNum);
            System.out.println("成功还了" + backNum + "本'"
                    + books[index].getName() + "'.");
          }
         else
           {
            System.out.println("没有你你要还的书:'" + name + "'.");
           }
    }
   
    /**
     * 根据书名删除书
     * @param name 书的名字
     */
    public void deleteOutBook(String name){
 
        int index=-1;
        for(int i=0;i<books.length;i++){
            
            if(null!=books[i]){
                if(books[i].getName().equals(name)){
                    index=i;
                    break;
                }
               
            }else{
                break;
            }
        }
        if(index==-1){
            System.out.println("没有找到要删除的书!");   
        }else{
            books[index]=null;
        }
        
        
        if(index==books.length-1){
            books[index]=null;
        }else{
            for(int i=index;i<books.length;i++){
                 
                books[i]=books[i+1];
                if(null==books[i+1]){
                    break;
                }
            }
            
        }
        System.out.println("成功删除"+name+"这本书");
     
    }
    //查看书
    public void lookOneBook(String name){
        for(int i=0;i<books.length;i++){
            if(books[i].getName().equals(name)){
//                books[i].setName(books[i].getName());
//                books[i].setNum(books[i].getNum());
//                books[i].setPrice(books[i].getPrice());
//                books[i].setOutNum(books[i].getOutNum());
//                books[i].setCount(books[i].getCount());   
                System.out.println("书名t价格t本数t已借数量t借出次数");   
                System.out.println(books[i].getName()+"t"+books[i].getPrice()+"t"+books[i].getNum()+"t"+books[i].getOutNum()+"t"+books[i].getCount());
            break;
            }
            
        }
    }
   
   
    public void rankBook(Book book){
        int[] rankArry = new int[100];
        int i,j,temp;
        for( i=0;i<books.length-1;i++){
            rankArry[i]=book.getCount();
        }
        for( i =0;i<rankArry.length;i++){
            for( j=0;j<rankArry.length-i;j++){
                if(rankArry[j]>rankArry[j+1]){
                    temp=rankArry[j];
                    rankArry[j]=rankArry[j+1];
                    rankArry[j+1]=temp;
                }   
            }
        }
        
        for( i=0;i<books.length;i++){
            System.out.println("书名t价格t本数t已借数量t借出次数");
            System.out.println(book.getName()+"t"+book.getPrice()+"t"+book.getNum()+"t"+book.getOutNum()+"t"+rankArry[i]);
        }
        
    }
   
   
   
    public Book[] getSortBooks()
      {
        int count = 0;//记录书架上有多少本书
        if(books[books.length - 1] != null)
          {
            count = books.length;
          }
        else
          {
            for(int i = 0; i < books.length; i++)
              {
                if(null == books[i])
                  {
                    count = i;
                    break;
                  }
              }
           }
        if(count == 0)
           {
            return null;
           }
        else
          {
            Book[] sortBooks = new Book[count];
            System.arraycopy(books, 0, sortBooks, 0, count);
//            for(int i = 0; i < sortBooks.length; i++){
//                sortBooks[i] = books[i];
//            }
            //排序
            Arrays.sort(sortBooks,new BookComparator());
//            for(int i = 0; i < sortBooks.length - 1; i++){
//                for(int j = 0; j < sortBooks.length - 1 - i; j++){
//                    if(sortBooks[j + 1].getCount() > sortBooks[j].getCount()){
//                        Book b = sortBooks[j + 1];
//                        sortBooks[j + 1] = sortBooks[j];
//                        sortBooks[j] = b;
//                    }
//                }
//            }
            return sortBooks;
          }
      }

   
    //显示所有的书
    public void show(){
        if(books[0]==null){
            System.out.println("书架上一本书也没有");
        }else{
             System.out.println("书名t价格t本数t已借数量t借出次数");
             for(Book book : books){
             if(null!=book){
                System.out.println(book.getName()+"t"+book.getPrice()+"t"+book.getNum()+"t"+book.getOutNum()+"t"+book.getCount());
               }
            }
        }
   }
}












import java.

public class BookComparator implements Comparator<Book> {
    @Override
    public int compare(Book o1, Book o2) {
        return o2.getCount() - o1.getCount();
    }

}
2016-07-14 10:48
程序猿单身狗
Rank: 1
等 级:新手上路
帖 子:36
专家分:4
注 册:2016-7-11
收藏
得分:0 
,谢谢!!

人生几个十年?最重要活的痛快。
2016-07-14 21:11
快速回复:第一次写的图书馆管理系统,代码量很大,而且数据插入只能插入一条
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.045527 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved