图书管理系统

图书管理系统,第1张

前言

      最近刚开始学习Java,整个系统的基本功能都得以实现,但就代码而言,整个程序的代码显得比较复杂,还可以对代码进行优化和改进,对IO流的理解和掌握还不够熟练。除此以外这个程序以及笔者的笔力还有很多的不足之处,欢迎各位的指正和建议。

正文

     总体为一个包四个类


     总体的功能构成和实现

UI类进行系统的启动的方法的调用
BookInfo类存放下文图书信息
ReaderInfo类存放下文读者信息
BookBorrow类存放下文借阅查询信息

菜单以及系统的启动
    //建立三个类的Arraylist
    static ArrayList<BookInfo> booklist = new ArrayList<>();
    static ArrayList<ReaderInfo> readlist = new ArrayList<>();
    static ArrayList<BookBorrow> borrowlist = new ArrayList<>();
public   static void Show()//菜单
    {
        System.out.println("****************************************************************");
        System.out.println("———————————————————————系统启动———————————————————————————————————");
        System.out.println("——————0、退出本系统————————————————————————————————————————————————");
        System.out.println("——————1、图书信息添加(书号、书名、作者、出版社、存馆数量、定价)——————————");
        System.out.println("——————2、图书信息查询(分别按书名、作者名、出版社等进行查询———————————————");
        System.out.println("——————3、图书信息排序(按书号、书名等按升序进行排序)————————————————————");
        System.out.println("——————4、图书信息修改、删除(按书号或书名进行图书的修改和删除)————————————");
        System.out.println("——————5、读者信息添加(包括学号、姓名、学院、专业班级等)—————————————————");
        System.out.println("——————6、读者信息查询(按学号、姓名、专业班级等进行查询)—————————————————");
        System.out.println("——————7、读者信息排序(按学号、学院等按升序进行排序)—————————————————————");
        System.out.println("——————8、读者信息修改、删除(按学号+姓名进行读者信息的修改和删除)——————————");
        System.out.println("——————9、图书借阅(输入学号+书号)————————————————————————————————————");
        System.out.println("——————10、图书归还(输入学号+书号)———————————————————————————————————");
        System.out.println("——————11、图书借阅查询(分别按学号、书名、学院等进行查询)————————————————");
        System.out.println("———————————————————————请进行功能项选择————————————————————————————");
        System.out.println("*****************************************************************");
    }
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        InBookInfoFile();
        InReaderFile();
        InBorrowFile();
        while (true) {
            Show();
            int flag = sc.nextInt();
            switch (flag) {
                case 0:
                    OutReader();
                    OutBookBorrow();
                    OutBookInformation();
                    System.out.println("退出图书借阅系统");
                    System.exit (0);
                case 1:
                    AddBook(); // 图书信息添加
                    break;
                case 2:
                    PrintBook(booklist);// 图书信息查询
                    break;
                case 3:
                    SortBook(booklist);//图书信息排序
                    break;
                case 4:
                    ChangeBook(booklist);//图书信息修改
                    break;
                case 5:
                    AddReader();     // 学生信息添加
                    break;
                case 6:
                    PrintReader();    //学生信息查询
                    break;
                case 7:
                    SortStu();// 学生信息排序
                    break;
                case 8:
                    ChaReStu();  //学生信息的修改、删除
                    break;
                case 9:
                    BorrowBook(); //图书借阅
                    break;
                case 10:
                    BackBook();//图书归还
                    break;
                case 11:PriBookReader();//图书借阅、查询
                    break;

                default:
                    System.out.println("数据有误,请重新输入");
                    break;
            }

        }
    }

参考下文建立相应的类,然后通过上文进行功能的调用即可

图书 创建图书类
public class BookInfo {
    String BookNum; //书号
    String BookName;//书名
    String BookAuthor; // 作者
    String Publisher; //出版社
    String PublishTime; //出版日期
    int BookCount;  //存书数
    double Price;//定价
    public BookInfo(String bookNum, String bookName, String bookAuthor, String publisher, String publishTime, int bookCount, double price) {
        BookNum = bookNum;
        BookName = bookName;
        BookAuthor = bookAuthor;
        Publisher = publisher;
        PublishTime = publishTime;
        BookCount = bookCount;
        Price = price;
    }
    public void setBookInfo(String bookNum, String bookName, String bookAuthor, String publisher, String publishTime, int bookCount, double price) {
        BookNum = bookNum;
        BookName = bookName;
        BookAuthor = bookAuthor;
        Publisher = publisher;
        PublishTime = publishTime;
        BookCount = bookCount;
        Price = price;
    }
    public String getBookNum() {
        return BookNum;
    }
    public void setBookNum(String bookNum) {
        BookNum = bookNum;
    }//其余的直接通过编辑器构造即可(不再赘述)
    public String toString()//输出条目
    {
        return ("书号:" + BookNum + "\t" + "书名:" + BookName
                + "\t" + "作者:" + BookAuthor + "\t" + "出版社:" + Publisher
                + "\t" + "出版日期:" + PublishTime + "\t" + "存馆数量:" + BookCount
                + "\t" + "定价:" + Price);
    }
}
图书信息添加功能

包括书号、书名、作者、出版社、存馆数量、定价等。

 public static void AddBook()//图书信息添加
    {

        while (true) {
            System.out.println("当前共有" + booklist.size() + "种书!");
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入书号");
            String ID = sc.next();
            System.out.println("请输入书名、作者、出版社、出版日期");
            String bookname = sc.next();
            String author = sc.next();
            String publisher = sc.next();
            String publishTime = sc.next();
            System.out.println("请输入存馆数量");
            int BookCount = sc.nextInt();
            System.out.println("请输入定价");
            double Price = sc.nextDouble();
            BookInfo book = new BookInfo(ID, bookname, author, publisher, publishTime, BookCount, Price);
            booklist.add(book);
            System.out.println("书籍添加成功");
            System.out.println("1、继续添加\t2、结束添加");
            int chioce = sc.nextInt();
            if (chioce == 1) {
                continue;
            }
            if (chioce == 2) {
                break;
            }
        }
    }
图书信息查询功能

分别按书名,按作者名,按出版社等进行查询。

  public static void PrintBook(ArrayList<BookInfo> booklist)//图书信息查询
    {
        Scanner sc = new Scanner(System.in);
        while (true) {
            int flag = 0;//标记
            System.out.println("请选择查询方式");
            System.out.println("1、按书名查询  2、按作者名查询  3、按出版社查询");
            int num1 = sc.nextInt();
            switch (num1) {
                case 1:
                    System.out.println("请输入您要查找的书名:");
                    String bookname = sc.next();

                    for (int i = 0; i < booklist.size(); i++) {
                        BookInfo B = booklist.get(i);
                        if (B.getBookName().equals(bookname)) {
                            System.out.println("书号:" + B.getBookNum() + "\t" + "书名:" + B.BookName
                                    + "\t" + "作者:" + B.getBookAuthor() + "\t" + "出版社:" + B.getPublisher()
                                    + "\t" + "出版日期:" + B.getPublishTime() + "\t" + "存馆数量:" + B.getBookCount()
                                    + "\t" + "定价:" + B.getPrice());
                            flag = 1;
                        }

                    }
                    if (flag == 0) {
                        System.out.println("查无此书,请重新输入");
                    }
                    if (flag == 1) {
                        System.out.println("1、继续查阅  2、退出");
                        int choice = sc.nextInt();
                        if (choice == 1) {
                            break;
                        }
                        if (choice == 2) {
                            return;
                        }
                    }
                    break;
                case 2:
                    System.out.println("请输入您要查找的作者名:");
                    String author = sc.next();
                    for (int i = 0; i < booklist.size(); i++) {
                        BookInfo B = booklist.get(i);
                        if (B.getBookAuthor().equals(author)) {
                            System.out.println("书号:" + B.getBookNum() + "\t" + "书名:" + B.BookName
                                    + "\t" + "作者:" + B.getBookAuthor() + "\t" + "出版社:" + B.getPublisher()
                                    + "\t" + "出版日期:" + B.getPublishTime() + "\t" + "存馆数量:" + B.getBookCount()
                                    + "\t" + "定价:" + B.getPrice());
                            flag = 1;
                        }
                        if (flag == 0) {
                            System.out.println("查无此书,请重新输入");
                        }
                        if (flag == 1) {
                            System.out.println("1、继续查阅  2、退出");
                            int choice = sc.nextInt();
                            if (choice == 1) {
                                break;
                            }
                            if (choice == 2) {
                                return;
                            }
                        }
                    }
                    break;
                case 3:
                    System.out.println("请输入您要查找的出版社名:");
                    String publisher = sc.next();
                    for (int i = 0; i < booklist.size(); i++) {
                        BookInfo B = booklist.get(i);
                        if (B.getPublisher().equals(publisher)) {

                            System.out.println("书号:" + B.getBookNum() + "\t" + "书名:" + B.BookName
                                    + "\t" + "作者:" + B.getBookAuthor() + "\t" + "出版社:" + B.getPublisher()
                                    + "\t" + "出版日期:" + B.getPublishTime() + "\t" + "存馆数量:" + B.getBookCount()
                                    + "\t" + "定价:" + B.getPrice());
                            flag = 1;
                        }
                        if (flag == 0) {
                            System.out.println("查无此书,请重新输入");
                        }
                        if (flag == 1) {
                            System.out.println("1、继续查阅  2、退出");
                            int choice = sc.nextInt();
                            if (choice == 1) {
                                break;
                            }
                            if (choice == 2) {
                                return;
                            }
                        }
                    }
                    break;
                default:
                    System.out.println("输入有误,请重新输入");
                    break;
            }
        }
    }
图书信息排序

按书号、书名等按升序进行排序。

 static void SortBook(ArrayList<BookInfo> booklist)//图书信息排序
    {
        System.out.println("请选择排序方式");
        System.out.println("1、按书号排序" + "\t" + "2、按书名排序");
        Scanner sc = new Scanner(System.in);
        int num1 = sc.nextInt();
        if (num1 == 1) {
            Collections.sort(booklist, ((o1, o2) -> o1.getBookNum().compareTo(o2.getBookNum())));
            System.out.println("按书号排序成功");
            System.out.println("图书列表为:");
            for (int i = 0; i < booklist.size(); i++) {
                BookInfo B = booklist.get(i);
                System.out.println("书号:" + B.getBookNum() + "\t" + "书名:" + B.BookName
                        + "\t" + "作者:" + B.getBookAuthor() + "\t" + "出版社:" + B.getPublisher()
                        + "\t" + "出版日期:" + B.getPublishTime() + "\t" + "存馆数量:" + B.getBookCount()
                        + "\t" + "定价:" + B.getPrice());
            }
            System.out.println("请输入0退出排序");
            if (sc.nextInt() == 0) {
                return;
            }
        }
        if (num1 == 2) {
            Collections.sort(booklist, ((o1, o2) -> o1.getBookName().compareTo(o2.getBookName())));
            System.out.println("按书名排序成功");
            System.out.println("图书列表为:");

            for (int i = 0; i < booklist.size(); i++) {
                System.out.println(booklist.get(i).toString());
            }
            System.out.println("请输入0退出排序");
            if (sc.nextInt() == 0) {
                return;
            }
        }
    }
图书信息的修改、删除

按书号或书名进行图书的修改和删除。

  public static void ChangeBook(ArrayList<BookInfo> booklist)//图书信息修改
    {
        while (true) {
            int flag = 1;//标记是否有相应的书
            System.out.println("请选择修改类型");
            System.out.println("1、按书号" + "\t" + "2、按书名");
            Scanner sc = new Scanner(System.in);
            int Num1 = sc.nextInt();
            if (Num1 == 1) {
                System.out.println("请输入要查询的书号");
                String Num2 = sc.next();
                for (int i = 0; i < booklist.size(); i++) {

                    BookInfo B = booklist.get(i);
                    if (Num2.equals(booklist.get(i).BookNum)) {
                        flag = 0;//标记有相应的书
                        System.out.println("原书名为" + booklist.get(i).BookName);
                        System.out.println("请输入修改后的书号、书名、作者、出版社、存馆数量、定价");
                        B.setBookInfo(sc.next(), sc.next(), sc.next(), sc.next(), sc.next(), sc.nextInt(), sc.nextDouble());
                        System.out.println("修改成功");
                        System.out.println("1、继续修改\t2、结束修改");
                        int chioce = sc.nextInt();
                        if (chioce == 1) {
                            break;
                        } else if (chioce == 2) {
                            return;
                        } else {
                            System.out.println("输入有误,默认继续修改");
                        }
                    }
                }
                if (flag == 1)
                    System.out.println("查无此书,请重新输入");
            } else if (Num1 == 2) {
                System.out.println("请输入要查询的书名");
                String Bookname = sc.next();
                for (int i = 0; i < booklist.size(); i++) {
                    BookInfo B = booklist.get(i);
                    if (Bookname.equals(booklist.get(i).BookName)) {
                        flag = 0;
                        System.out.println("原书名为" + booklist.get(i).BookName);
                        System.out.println("请输入修改后的书号、书名、作者、出版社、存馆数量、定价");
                        B.setBookInfo(sc.next(), sc.next(), sc.next(), sc.next(), sc.next(), sc.nextInt(), sc.nextDouble());
                        System.out.println("修改成功");

                        System.out.println("1、继续修改\t2、结束修改");
                        int chioce = sc.nextInt();
                        if (chioce == 1) {
                            break;
                        } else if (chioce == 2) {
                            return;
                        } else {
                            System.out.println("输入有误,默认继续修改");
                        }
                    }
                }
                if (flag == 1)
                    System.out.println("查无此书,请重新输入");
            } else {
                System.out.println("输入有误,请重新输入");
            }

        }
    }
学生 创建学生类
public class ReaderInfo  {
    public ReaderInfo(String ID, String readerName, String college, String aClass) {
        this.ID = ID;
        ReaderName = readerName;
        College = college;
        Class = aClass;
    }
public void setReader(String ID, String readerName, String college, String aClass)
        {
            this.ID = ID;
            ReaderName = readerName;
            College = college;
            Class = aClass;
        }
    public String toString() {
        return "学生ID:" + ID + "\t" + " 学生姓名:" + ReaderName + "\t" +
                "学院:" + College + "\t" + " 班级:" + Class ;
    }
    public String ID;  //学号
    public String ReaderName; //姓名
    public String College; //学院
    public  String Class; // 专业班级
}
学生信息添加功能

包括学号、姓名、学院、专业班级等

public   static void AddReader()//学生信息添加
    {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("当前共有" + readlist.size() + "名读者!");
            System.out.println("请输入学生学号、姓名、学院、专业班级");
            ReaderInfo R = new ReaderInfo(sc.next(), sc.next(), sc.next(), sc.next());
            readlist.add(R);
            System.out.println("学生信息添加成功");
            System.out.println("1、继续添加\t2、结束添加");
            int chioce = sc.nextInt();
            if (chioce == 1) {
                continue;
            }
            if (chioce == 2) {
                break;
            }
        }
    }
学生信息查询

分别按学号、姓名、专业班级等进行查询

public static void PrintReader()//学生信息查询
    {
        Scanner sc = new Scanner(System.in);
        OUT1:
        while (true) {
            System.out.println("输入要查询学生的学号或者姓名或着班级");
            int flag = 1;
            String Info = sc.next();
            for (int i = 0; i < readlist.size(); i++) {
                if (Info.equals(readlist.get(i).ReaderName) || Info.equals(readlist.get(i).ID) || Info.equals(readlist.get(i).Class)) {
                    flag = 0;
                    System.out.println(readlist.get(i).toString());
                    System.out.println("1、继续查询" + "\t" + "2、结束查询");
                    int choice = sc.nextInt();
                    if (choice == 1) {
                        break;
                    }
                    if (choice == 2) {
                        break OUT1;
                    }
                }
            }
            if (flag == 1) {
                System.out.println("未查询到此人,请重新输入");
            }
        }
    }
学生信息排序

按学号、学院等按升序进行排序

public static void SortStu()//学生信息排序
    {
        System.out.println("1、按学号排序" + "\t" + "2、按学院排序");
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
        if (choice == 1) {
            Collections.sort(readlist, (o1, o2) -> o1.ID.compareTo(o2.ID));
            System.out.println("排序成功");
            System.out.println("学生列表:");
            for (int i = 0; i < readlist.size(); i++) {
                System.out.println(readlist.get(i).toString());
            }
            System.out.println("请输入0退出排序");
            if (sc.nextInt() == 0) {
                return;
            }
        }
        if (choice == 2) {
            Collections.sort(readlist, (o1, o2) -> o1.College.compareTo(o2.College));
            System.out.println("排序成功");
            System.out.println("学生列表:");
            for (int i = 0; i < readlist.size(); i++) {
                System.out.println(readlist.get(i).toString());
            }
            System.out.println("请输入0退出排序");
            if (sc.nextInt() == 0) {
                return;
            }
        }
    }
学生信息的修改、删除

按学号+姓名进行读者信息的修改和删除

 public static void ChaReStu()//学生信息的修改、删除
    {
        Scanner sc = new Scanner(System.in);
        while (true) {
            int flag = 1;//标记
            System.out.println("按学号+姓名对学生信息进行修改和删除");
            System.out.println("请输入学号和学生姓名");
            String Id = sc.next();
            String StuName = sc.next();
            for (int i = 0; i < readlist.size(); i++) {
                if (readlist.get(i).ID.equals(Id) && readlist.get(i).ReaderName.equals(StuName)) {
                    flag = 0;
                    System.out.println("1、进行修改" + "\t" + "2、进行删除");
                    int chioce = sc.nextInt();
                    if (chioce == 1) {
                        ReaderInfo R1 = readlist.get(i);
                        System.out.println("请输入新的学生信息(学生ID、姓名、学院、班级)");
                        R1.setReader(sc.next(), sc.next(), sc.next(), sc.next());
                        System.out.println("修改成功");
                        System.out.println("1、继续对学生信息 *** 作" + "\t" + "2、结束 *** 作");
                        int Op = sc.nextInt();
                        if (Op == 1) {
                            break;
                        }
                        if (Op == 2) {
                            return;
                        }
                    }
                    if (chioce == 2) {
                        readlist.remove(i);
                        System.out.println("删除成功");
                        System.out.println("1、继续对学生信息 *** 作" + "\t" + "2、结束 *** 作");
                        int Op = sc.nextInt();
                        if (Op == 1) {
                            break;
                        }
                        if (Op == 2) {
                            return;
                        }
                    }
                }
            }

            if (flag == 1) {
                System.out.println("学生信息有误,请重新输入");
            }
        }
    }
图书借阅 创建图书借阅者的信息类
public class BookBorrow  {
    public BookBorrow(String ID, String name, String College, String bookNum, String bookName, String lendTime, String legalTime) {
        this.ID = ID;
        Name = name;
        this.College = College;
        BookNum = bookNum;
        BookName = bookName;
        LendTime = lendTime;
        LegalTime = legalTime;
    }
    @Override
    public String toString() {
        return "ID:" + ID + "\t" + "学生姓名:" + Name + "\t" +
                "书号:" + BookNum + "\t" + "书名:" + BookName +
                "\n" + "应还时间:" + LegalTime + '\n' +
                "归还时间: " + BorrowTime;
    }

    String ID;  //学号
    String Name;//姓名
    String College;
    String BookNum; //书号
    String BookName; //书名
    String LendTime;//借阅时间
    String LegalTime;//应还时间
    String BorrowTime = "借阅中"; //归还时间


    public void setBookReader(String ID, String name)/*对学生信息进行登记*/ {
        this.ID = ID;
        Name = name;
    }
//其余的直接通过编辑器构造即可(不再赘述)
}

学号+书号的方式借阅

  public static void BorrowBook() //图书借阅
    {
        Scanner sc = new Scanner(System.in);
        int flag = 0;/*标记*/
        int Br = 0;//图书借阅人数
        while (true) {
            System.out.println("请输入要借阅书的书号和个人学号登录:");
            String BookNum = sc.next();
            String Id = sc.next();
            OUT:
            for (int i = 0; i < booklist.size(); i++) {
                for (int i1 = 0; i1 < readlist.size(); i1++) {
                    if (BookNum.equals(booklist.get(i1).BookNum) && Id.equals(readlist.get(i1).ID)) {
                        flag = 1;//标记找到了相应的学生信息和书籍信息
                        if (booklist.get(i).BookCount >= 1) {

                            booklist.get(i).BookCount -= 1;
                            System.out.println("请输入个人的学号、姓名、学院以及所借书的书号、书名:");
                            String Id1 = sc.next();
                            String StuName = sc.next();
                            String College = sc.next();
                            String BookId = sc.next();
                            String BookName = sc.next();
                            String LendT = LendTime();
                            String LegT = LealTime();
                            BookBorrow B = new BookBorrow(Id1, StuName, College, BookId, BookName, LendT, LegT);
                            borrowlist.add(B);

                            System.out.println("学生学号:" + borrowlist.get(Br).ID + "\t" + "姓名" + borrowlist.get(Br).Name +
                                    "\t" + "书号:" + borrowlist.get(Br).BookNum + "\t" + "书名:" + borrowlist.get(Br).BookName);
                            System.out.println("借阅时间:" + borrowlist.get(Br).LendTime);
                            System.out.println("应还时间:" + borrowlist.get(Br).LegalTime);
                            System.out.println("借阅成功");
                            System.out.println("1、继续借阅" + "\t" + "2、退出系统");
                            int chioce = sc.nextInt();
                            if (chioce == 1) {
                                break OUT;
                            }
                            if (chioce == 2) {
                                return;
                            }
                        } else if (booklist.get(i).BookCount < 1) {
                            System.out.println("此书已被完全借阅,请重新选择");
                            System.out.println("1、继续借阅" + "\t" + "2、退出系统");
                            int chioce = sc.nextInt();
                            if (chioce == 1) {
                                break OUT;
                            }
                            if (chioce == 2) {
                                return;
                            }
                        }
                    }
                }
            }
            if (flag == 0) {
                System.out.println("输入有误,请重新输入");
            }
        }
    }
图书归还

学号+书号的方式归还书籍

  public static void BackBook()//图书归还
    {
        Scanner sc = new Scanner(System.in);
        int flag = 1;//标记是否找到正确信息
        OUT:
        while (true) {
            System.out.println("请输入学号和书号:");
            String StuId = sc.next();
            String BookId = sc.next();
            for (int i = 0; i < borrowlist.size(); i++) {
                if (StuId.equals(borrowlist.get(i).ID) && BookId.equals(borrowlist.get(i).BookNum)) {
                    flag = 0;//标记找到正确信息
                    System.out.println(borrowlist.get(i).toString());
                    System.out.println("1、归还  2、返回");
                    int choice = sc.nextInt();
                    if (choice == 1) {
                        booklist.get(i).BookCount++;
                        String CurrentTime = LendTime();
                        borrowlist.get(i).setBorrowTime(CurrentTime);
                        System.out.println("归还成功");
                        System.out.println(borrowlist.get(i).toString());
                        System.out.println("1、继续归还  2、退出");
                        int choice1 = sc.nextInt();
                        if (choice1 == 1) {
                            break OUT;
                        }
                        if (choice1 == 2) {
                            return;
                        }

                    }
                    if (choice == 2) {
                        System.out.println("1、继续归还  2、退出");
                        int choice1 = sc.nextInt();
                        if (choice1 == 1) {
                            break OUT;
                        }
                        if (choice1 == 2) {
                            return;
                        }

                    }
                }
            }
            if (flag == 1) {
                System.out.println("请输入正确信息");
            }
        }
    }
图书借阅查询
public static void PriBookReader() //图书借阅查询
    {
        Scanner sc = new Scanner(System.in);
        while (true) {
            int flag = 0;
            System.out.println("1、按学号查询  2、按书名查询  3、按学院查询");
            int chioce = sc.nextInt();
            switch (chioce) {
                case 1:
                    System.out.println("请输入要查询的学号");
                    String Id = sc.next();
                    for (int i = 0; i < borrowlist.size(); i++) {
                        if (Id.equals(borrowlist.get(i).ID)) {
                            flag = 1;
                            System.out.println(borrowlist.get(i).toString());
                        }
                    }
                    if (flag == 1) {
                        System.out.println("1、继续查询  2、退出");
                        int chioce1 = sc.nextInt();
                        if (chioce1 == 1) {
                            break;
                        }
                        if (chioce1 == 2) {
                            return;
                        }
                    }
                    if (flag == 0) {
                        System.out.println("信息有误,请重新输入");
                    }
                    break;
                case 2:
                    System.out.println("请输入要查询的书名:");
                    String BN = sc.next();
                    for (int i = 0; i < borrowlist.size(); i++) {
                        if (BN.equals(borrowlist.get(i).BookName)) {
                            flag = 1;
                            System.out.println(borrowlist.get(i).toString());
                        }
                    }
                    if (flag == 1) {
                        System.out.println("1、继续查询  2、退出");
                        int chioce1 = sc.nextInt();
                        if (chioce1 == 1) {
                            break;
                        }
                        if (chioce1 == 2) {
                            return;
                        }
                    }
                    if (flag == 0) {
                        System.out.println("信息有误,请重新输入");
                    }
                    break;
                case 3:
                    System.out.println("请输入要查询的学院");
                    String College = sc.next();
                    for (int i = 0; i < borrowlist.size(); i++) {
                        if (College.equals(borrowlist.get(i).College)) {
                            flag = 1;
                            System.out.println(borrowlist.get(i).toString());
                        }
                    }
                    if (flag == 1) {
                        System.out.println("1、继续查询  2、退出");
                        int chioce1 = sc.nextInt();
                        if (chioce1 == 1) {
                            break;
                        }
                        if (chioce1 == 2) {
                            return;
                        }
                    }
                    if (flag == 0) {
                        System.out.println("信息有误,请重新输入");
                    }
                    break;
                default:
                    System.out.println("输入有误,请重新选择");
                    break;
            }
        }
    }
IO流

下面以书籍信息的存入和读取为例(采用字符流)
存入:

   public static void OutBookInformation() throws Exception{
        Writer file=null;
        try {
            file=new FileWriter("D:\BookInfo.txt",false);
            for (int i = 0; i < booklist.size(); i++) {
                BookInfo c=booklist.get(i);
                file.write(String.valueOf(c));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
                file.close();
            }
        }

读取:

 public static void Print() throws Exception{
        Reader re=new FileReader("D:\BookInfo.txt");
        char[] buff=new char[100];
         int len;
        while ((len=re.read(buff))!=-1)
            {
                String rs=new String(buff,0,len);
                System.out.print(rs);
            }
        }
补充(关于时间的获取)
 public static String LendTime()//获取当前时间
    {
        Calendar rightnow = Calendar.getInstance();
        int year = rightnow.get(Calendar.YEAR);
        int month = rightnow.get(Calendar.MONTH) + 1;
        int day = rightnow.get(rightnow.DAY_OF_MONTH);
        int hour = rightnow.get(rightnow.HOUR_OF_DAY);//24小时制
        int minute = rightnow.get(rightnow.MINUTE);
        int second = rightnow.get(rightnow.SECOND);
        int millisecond = rightnow.get(rightnow.MILLISECOND);
        String TIME = year + "-" + month + "-" + day + "\t" + hour + ":" + minute+":"+second;
        return TIME;
    }

其他时间的设置参考当前时间的获取的修改方式修改即可

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/904591.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-15
下一篇 2022-05-15

发表评论

登录后才能评论

评论列表(0条)

保存