图书馆管理系统(三)

开始

package LibraryManage;

import LibraryManage.Library;

public class Main {
   

    public static void main(String[] args) throws Exception {
   
        Library library = new Library(); //构造Library对象
        library.login(); //调用对象的登录方法。
    }
}

图书

package bookManage;

import LibraryManage.Log;
import LibraryManage.Test4;
import LibraryManage.TimeDate;
import readerManage.Reader;
import readerManage.Test2;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;


public class Book {
   
    private int id;
    //ISBN
    private String ISBN;
    // 书名
    private String bookName;
    // 作者
    private String author;
    //出版社
    private String publish;
    //出版日期
    private String publishDate;
    //印刷次数
    private String printTime;
    // 单价
    private String unitPrice;
    //图书类型
    private String typeName;
    // 书的数量
    private String num;
    TimeDate timeDate = new TimeDate();
    Log log = new Log();
    // 用于从控制台输入内容
    public Book(int id, String ISBN, String bookName, String author, String publish, String publishDate, String printTime, String unitPrice, String typeName, String num) throws IOException {
   
        this.id = id;
        this.ISBN = ISBN;
        this.bookName = bookName;
        this.author = author;
        this.publish = publish;
        this.publishDate = publishDate;
        this.printTime = printTime;
        this.unitPrice = unitPrice;
        this.typeName = typeName;
        this.num = num;
    }

    public Book() throws IOException {
   
        super();
    }

    public int getId() {
   
        return id;
    }

    public void setId(int id) {
   
        this.id = id;
    }

    public String getNum() {
   
        return num;
    }

    public String getISBN() {
   
        return ISBN;
    }

    public void setISBN(String ISBN) {
   
        this.ISBN = ISBN;
    }

    public String getBookName() {
   
        return bookName;
    }

    public void setBookName(String bookName) {
   
        this.bookName = bookName;
    }

    public String getAuthor() {
   
        return author;
    }

    public void setAuthor(String author) {
   
        this.author = author;
    }

    public String getPublish() {
   
        return publish;
    }

    public void setPublish(String publish) {
   
        this.publish = publish;
    }

    public String getPublishDate() {
   
        return publishDate;
    }

    public void setPublishDate(String publishDate) {
   
        this.publishDate = publishDate;
    }

    public String getPrintTime() {
   
        return printTime;
    }

    public void setPrintTime(String printTime) {
   
        this.printTime = printTime;
    }

    public String getUnitPrice() {
   
        return unitPrice;
    }

    public void setUnitPrice(String unitPrice) {
   
        this.unitPrice = unitPrice;
    }

    public String getTypeName() {
   
        return typeName;
    }

    public void setTypeName(String typeName) {
   
        this.typeName = typeName;
    }

    public void setNum(String num) {
   
        this.num = num;
    }

    final static int SIZE = 20;
    // 创建一个长度为20的数组,用于存放书籍
    Book[] books = new Book[SIZE];

    static int count = 0;
    Scanner scanner = new Scanner(System.in);

    //查看图书列表
    public void list() throws IOException {
   
        // 从文件中读出数据
        books = Service.readData();
        // 如果count为0,说明系统里面还没有书籍
        long lines = Files.lines(Paths.get(new File("Books.txt").getPath())).count();
        int s = (int) lines;
        if (s == 0) {
   
            System.out.println("图书管理系统中还没有输入书籍,快去添加吧!");
        } else {
   
            // 如果有书籍则遍历书籍
            System.out.println("图书ID\t" + "图书编号\t\t" + "书名\t\t\t" + "作者\t\t\t" + "出版社\t\t\t" + "出版日期\t\t" + "印刷次数\t\t" + "单价\t\t" + "图书类型\t\t" + "数量\t\t");
            for (int i = 0; i < s; i++) {
   
                System.out.print(books[i].getId() + "\t\t");
                System.out.print(books[i].getISBN() + "\t\t");
                System.out.print(books[i].getBookName() + "\t\t");
                System.out.print(books[i].getAuthor() + "\t\t");
                System.out.print(books[i].getPublish() + "\t\t");
                System.out.print(books[i].getPublishDate() + "\t");
                System.out.print(books[i].getPrintTime() + "\t\t\t");
                ;
                System.out.print(books[i].getUnitPrice() + "\t\t");
                System.out.print(books[i].getTypeName() + "\t\t");
                System.out.println(books[i].getNum() + "\t\t");
                // 确保书的种类不能多于20种
                //count++;
            }
        }
    }

    //图书查询
    public void Inquire(String account) throws IOException {
   
        ArrayList<Book> books = new ArrayList<Book>();
        Test1 test1 = new Test1();
        long lines = Files.lines(Paths.get(new File("Books.txt").getPath())).count();
        int s = (int) lines;
        String[][] bookly = test1.ReadData(lines, 10);

        System.out.println("输入对应进行查询(1、图书ID 2、图书ISBN 3、图书名字)");
        String a = scanner.next();
        boolean result = a.matches("[0-9]+");
        int cv = 0;
        String z = null;
        while (cv == 0) {
   

            if (result == true) {
   
                switch (a) {
   
                    case "1":
                            System.out.println("请输入图书的ID");
                            String id1 = scanner.next();
                            boolean result1 = id1.matches("[0-9]+");
                            if (result1 == true) {
   
                                int b= Integer.parseInt(id1);
                                for (int i = 0; i < s; i++) {
   
                                    int c = Integer.parseInt(bookly[i][0]);
                                    if (c == b) {
   
                                        System.out.println(i + " " + bookly[i][1] + " " + bookly[i][2] + " " + bookly[i][3] + " " + bookly[i][4] + " "
                                                + bookly[i][5] + " " + bookly[i][6] + " " + bookly[i][7] + " " +
                                                bookly[i][8] + " " + bookly[i][9]);
                                        z = bookly[i][1];
                                        cv = 1;
                                        break;
                                    } else if (i == s - 1) {
   
                                        System.out.println("该图书不存在");
                                    }
                                }
                            } else {
   
                                System.out.println("输入的数字违法,请重新输入");
                        }
                        break;
                    case "2":
                        System.out.println("请输入图书的ISBN");
                        String d = scanner.next();
                        boolean result2 = d.matches("[0-9]+");
                        if (result2 == true) {
   
                            for (int i = 0; i < s; i++) {
   
                                if (bookly[i][1].equals(d)) {
   
                                    System.out.println(i + " " + bookly[i][1] + " " + bookly[i][2] + " " + bookly[i][3] + " " + bookly[i][4] + " "
                                            + bookly[i][5] + " " + bookly[i][6] + " " + bookly[i][7] + " " +
                                            bookly[i][8] + " " + bookly[i][9]);
                                    z = bookly[i][1];
                                    cv = 1;
                                    break;
                                } else if (i == s - 1) {
   
                                    System.out.println("该图书不存在");
                                }
                            }

                        } else {
   
                            System.out.println("输入的数字违法,请重新输入");
                        }
                        break;
                    case "3":
                        System.out.println("请输入图书的名字");
                        String c = scanner.next();
                        boolean result3 = c.matches("[0-9]+");
                        if (result3 == true) {
   
                            for (int i = 0; i < s; i++) {
   
                                if (bookly[i][1].equals(c)) {
   
                                    System.out.println(i + " " + bookly[i][1] + " " + bookly[i][2] + " " + bookly[i][3] + " " + bookly[i][4] + " "
                                            + bookly[i][5] + " " + bookly[i][6] + " " + bookly[i][7] + " " +
                                            bookly[i][8] + " " + bookly[i][9] + " " + bookly[i][10]);
                                    z = bookly[i][1];
                                    cv = 1;
                                    break;
                                } else if (i == s - 1) {
   
                                    System.out.println("该图书不存在");
                                }
                            }
                        } else {
   
                            System.out.println("输入数字违法,请重新输入");
                        }

                        break;
                    default:
                        System.out.println("输入数字违法,请重新输入");
                }
            } else {
   
                System.out.println("输入数字违法,请重新输入");
            }
        }


        String time = timeDate.getTimeDates();
        String str = "用户" + account + "查看了图书" + z + "的信息  " + time + "\n";
        log.Loged(str);
    }

    //添加图书
    public void add(String account) throws IOException {
   
        Test1 test1 = new Test1();
        long lines = Files.lines(Paths.get(new File("Books.txt").getPath())).count();
        int s = (int) lines;
        String[][] bookly = test1.ReadData(lines, 10);
        System.out.println("----------------------------");
        int id = (int) lines;
        int c = 0;
        String ISBN = null;
        while (c == 0) {
   
            System.out.println("请输入图书ISBN编号:");
            ISBN = scanner.next();
            boolean result = ISBN.matches("[0-9]+");
            if (result == true) {
   
                for (int i = 0; i < s; i++) {
   
                    if (ISBN.equals(bookly[i][1])) {
   
                        System.out.println("该ISBN编号已存在,请重新输入");
                        break;
                    } else if (i == s - 1) {
   
                        c = 1;
                    }
                }
            } else {
   
                System.out.println("输入的数据违法请重新输入");
            }

        }
        String bookName = null;
        while (c == 1) {
   
            System.out.println("请输入图书名称:");
            bookName = scanner.next();
            for (int i = 0; i < s; i++) {
   
                if (ISBN.equals(bookly[i][2])) {
   
                    System.out.println("该图书名称已存在(如有需要,请在图书后添加序号避免重名)");
                } else if (i == s - 1) {
   
                    c = 0;
                }
            }
        }
        System.out.println("请输入作者:");
        String author = scanner.next();
        System.out.println("请输入出版社:");
        String publish = scanner.next();
        System.out.println("请输入出版日期:");
        String publishDate = scanner.next();
        String printTime = null;
        while (c == 0) {
   
            System.out.println("请输入印刷次数:");
            printTime = scanner.next();
            boolean result = printTime.matches("[0-9]+");
            if (result == true) {
   
                int t= Integer.parseInt(printTime);
                if(t>0){
   
                    c = 1;
                }
                else {
   
                    System.out.println("输入的印刷次数应大于0,请重新输入!");
                }
            } else {
   
                System.out.println("输入的数据违法,请重新输入!");
            }
        }
        String unitPrice = null;
        while (c == 1) {
   
            System.out.println("请输入图书单价:");
            unitPrice = scanner.next();
            boolean result = unitPrice.matches("[0-9]+");
            if (result == true) {
   
                int t= Integer.parseInt(unitPrice);
                if(t>0){
   
                    c = 0;
                }
                else {
   
                    System.out.println("输入的图书单价应大于0,请重新输入!");
                }
            } else {
   
                System.out.println("输入的数据违法请重新输入");
            }
        }
        System.out.println("请输入图书类型:");
        String typeName = scanner.next();
        String num = null;
        while (c == 0) {
   
            System.out.println("请输入图书数量:");
            num = scanner.next();
            boolean result = num.matches("[0-9]+");
            if (result == true) {
   
                int t= Integer.parseInt(num);
                if(t>0){
   
                    c = 1;
                }
                else {
   
                    System.out.println("输入的图书数量应大于0,请重新输入!");
                }
            } else {
   
                System.out.println("输入的数据违法请重新输入");
            }
        }
        ArrayList<Book> books1 = new ArrayList<Book>();
        Book book = new Book(id, ISBN, bookName, author, publish, publishDate, printTime, unitPrice, typeName, num);

        books1.add(book);
        Book.write(books1);
        System.out.println("添加成功!");
        String time = timeDate.getTimeDates();
        String str = "用户" + account + "添加了图书" + bookName + "的信息  " + time + "\n";
        log.Loged(str);

    }

    //删除图书
    public void delete(String account) throws IOException {
   
        Test4 test4 = new Test4();
        long lines4 = Files.lines(Paths.get(new File("BooksLending.txt").getPath())).count();
        int s4 = (int) lines4;
        String[][] bookreadly = test4.ReadData(lines4, 19);
        System.out.println("----------------------------");
        int c = 0;
        long lines = Files.lines(Paths.get(new File("Books.txt").getPath())).count();
        int s = (int) lines;
        int id = 0;
        while (c == 0) {
   
            System.out.println("请输入要删除书籍的id:");
            String ids = scanner.next();
            boolean result = ids.matches("[0-9]+");
            if (result == true) {
   
                id = Integer.parseInt(ids);
                int sum=0;
                for(int j=0;j<s4;j++){
   
                    for(int z=0;z<8;z++)
                    {
   
                        if(bookreadly[j][z*2+3].equals(bookName)){
   
                            sum++;
                        }
                    }
                }
                if(sum==0){
   
                    c = 1;
                }
                else {
   
                    System.out.println("该书还有未归还,不能删除该图书");
                }

            } else {
   
                System.out.println("输入的数据违法请重新输入");
            }
        }


        if (s > id) {
   
            ArrayList<Book> books1 = new ArrayList<Book>();
            Test1 test1 = new Test1();
            String[][] bookly = test1.ReadData(lines, 10);
            for (int x = 0; x < s; x++) {
   
                if (id == x) {
   

                } else if (x > id) {
   
                    Book book = new Book(x - 1, bookly[x][1], bookly[x][2], bookly[x][3], bookly[x][4],
                            bookly[x][5], bookly[x][6], bookly[x][7], bookly[x][8], bookly[x][9]);
                    books1.add(book);
                } else {
   
                    Book book = new Book(x, bookly[x][1], bookly[x][2], bookly[x][3], bookly[x][4],
                            bookly[x][5], bookly[x][6], bookly[x][7], bookly[x][8], bookly[x][9]);
                    books1.add(book);
                }
            }
            Book.writes(books1);
            System.out.println("删除成功!");
            String time = timeDate.getTimeDates();
            String str = "用户" + account + "删除了图书" + bookly[id][2] + "的信息  " + time + "\n";
            log.Loged(str);
            // 重写写到文件里面
        } else {
   
            System.out.println("删除失败!");
        }

    }

    //修改图书
    public void update(String account) throws IOException {
   
        Test1 test1 = new Test1();
        long lines = Files.lines(Paths.get(new File("Books.txt").getPath())).count();
        int s = (int) lines;
        String[][] bookly = test1.ReadData(lines, 10);
        Test4 test4 = new Test4();
        long lines4 = Files.lines(Paths.get(new File("BooksLending.txt").getPath())).count();
        int s4 = (int) lines4;
        String[][] bookreadly = test4.ReadData(lines4, 19);
        System.out.println("----------------------------");
        int c = 1;
        int id = 0;
        while (c == 1) {
   
            System.out.println("请输入你需要修改图书的ID:");
            String ids = scanner.next();
            boolean result = ids.matches("[0-9]+");
            if (result == true) {
   
                c = 0;
                id = Integer.parseInt(ids);
            } else {
   
                System.out.println("输入的数据违法请重新输入");
            }
        }

        String ISBN = bookly[id][1];
        String bookName = bookly[id][2];
        String author=bookly[id][3];
        String publish=bookly[id][4];
        String publishDate=bookly[id][5];
        String printTime = bookly[id][6];
        String unitPrice = bookly[id][7];
        String typeName=bookly[id][8];
        String num = bookly[id][9];



        int cv=0;
        while (cv==0){
   
            System.out.println("请输入需要修改的图书信息(1、图书ISBN 2、图书姓名 3、图书作者 4、图书出版社\n" +
                    " 5、图书出版日期 6、图书印刷次数 7、图书单价 8、图书类型 9、图书图书数量 )");
            String o=scanner.next();
            switch (o){
   

                case "1":

                    while (c == 0) {
   
                        System.out.println("请输入图书ISBN编号:");
                        ISBN = scanner.next();
                        boolean result = ISBN.matches("[0-9]+");
                        if (result == true) {
   
                            for (int i = 0; i < s; i++) {
   
                                if (ISBN.equals(bookly[i][1])) {
   
                                    System.out.println("该ISBN编号已存在");
                                } else if (i == s - 1) {
   
  
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值