⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 library.java

📁 利用java链表编写的小型图书馆图书管理系统
💻 JAVA
字号:
package library;

import java.io.*;
import java.util.LinkedList;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class Library {
    private AuthorList[] catalog = new AuthorList[(int) ('Z' + 1)];

    private PatronList[] people = new PatronList[(int) ('Z' + 1)];

    private String input;

    private BufferedReader br = new BufferedReader(new InputStreamReader(System.
            in));
    public Library() {
        for (int i = 0; i <= (int) 'Z'; i++) {
            catalog[i] = new AuthorList();
            people[i] = new PatronList();
        }
    }

    private String getString(String msg) {
        System.out.print(msg + " ");

        System.out.flush();

        try {
            input = br.readLine();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        return input.substring(0, 1).toUpperCase() + input.substring(1);
    }

    public void status() {
        System.out.println("Library has the following books:\n ");

        for (int i = (int) 'A'; i <= (int) 'Z'; i++) {
            if (catalog[i].isEmpty()) {
                catalog[i].display();
            }
        }

        System.out.println("\nThe following people are using the library:\n ");

        for (int i = (int) 'A'; i <= (int) 'Z'; i++) {
            if (people[i].isEmpty()) {
                people[i].display();
            }
        }
    }

    public void includeBook() {
        Author author = new Author();

        Book book = new Book();

        author.name = getString("Enter author's name:");

        book.title = getString("Enter the title of the book:");

        int oldAuthor = catalog[(int) author.name.charAt(0)].indexOf(author);

        if (oldAuthor == -1) {
            author.books.add(book);
            catalog[(int) author.name.charAt(0)].add(author);
        } else {
            ((Author) catalog[(int) author.name.charAt(0)].get(oldAuthor)).
                    books.add(book);
        }
    }


    public void checkOutBook() {
        Patron patron = new Patron(), patronRef = new Patron();

        Author author = new Author(), authorRef = new Author();

        Book book = new Book();

        patron.name = getString("Enter patron's name:");

        int patronIndex, authorIndex = -1, bookIndex = -1;

        while (authorIndex == -1) {
            author.name = getString("Enter author's name:");

            authorIndex = catalog[(int) author.name.charAt(0)].indexOf(author);

            if (authorIndex == -1)
                System.out.println("Misspelled author's name");
        }

        while (bookIndex == -1) {
            book.title = getString("Enter the title of the book:");

            authorRef = (Author) catalog[(int) author.name.charAt(0)].get(
                    authorIndex);

            bookIndex = authorRef.books.indexOf(book);

            if (bookIndex == -1)
                System.out.println("Misspelled title");
        }

        Book bookRef = (Book) authorRef.books.get(authorIndex);

        CheckOutBook checkoutbook = new CheckOutBook();

        checkoutbook.author = authorRef;

        checkoutbook.book = bookRef;

        patronIndex = people[(int) patron.name.charAt(0)].indexOf(patron);

        if (patronIndex == -1) {
            patron.books.add(checkoutbook);

            people[(int) patron.name.charAt(0)].add(patron);

            book.patron = (Patron) people[(int) patron.name.charAt(0)].getFirst();
        } else {
            patronIndex = people[(int) patron.name.charAt(0)].indexOf(patron);

            patronRef = (Patron) people[(int) patron.name.charAt(0)].get(
                    patronIndex);

            book.patron = patronRef;
        }
    }

    public void returnBook() {
        Patron patron = new Patron();

        Book book = new Book();

        Author author = new Author(), authorRef = new Author();

        int patronIndex = -1, bookIndex = -1, authorIndex = -1;

        while (patronIndex == -1) {
            patron.name = getString("Enter patron's name:");

            patronIndex = people[(int) patron.name.charAt(0)].indexOf(patron);

            if (patronIndex == -1)
                System.out.println("Patron's name misspelled");
        }

        while (authorIndex == -1) {
            author.name = getString("Enter author's name");

            authorIndex = catalog[(int) author.name.charAt(0)].indexOf(author);

            if (authorIndex == -1) {
                System.out.println("Missplled author's name");
            }
        }

        while (bookIndex == -1) {
            book.title = getString("Enter the title of the book:");

            authorRef = (Author) catalog[(int) author.name.charAt(0)].get(
                    authorIndex);

            bookIndex = authorRef.books.indexOf(book);

            if (bookIndex == -1)
                System.out.println("Missplled title");
        }
        CheckOutBook checkoutbook = new CheckOutBook();

        checkoutbook.author = authorRef;

        checkoutbook.book = (Book) authorRef.books.get(bookIndex);

        ((Book) authorRef.books.get(bookIndex)).patron = null;

        ((Patron) people[(int) patron.name.charAt(0)].get(patronIndex)).books.
                remove(checkoutbook);
    }

    public void run() {
        while (true) {
            char option = getString("\nEnter one of the following " +
                                    "options:\n" +
                                    "1. Include a book in the catalog\n" +
                                    "2. Check out a book\n" +
                                    "3. return a book \n" + "4. Status\n" +
                                    "5. Exit\n" + "Your option:").charAt(0);

            switch (option) {
            case '1':
                includeBook();
                break;
            case '2':
                checkOutBook();
                break;
            case '3':
                returnBook();
                break;
            case '4':
                status();
                break;
            case '5':
                return;
            default:
                System.out.println("Wrong option, try again.");
            }
        }
    }

    public static void main(String[] args) {
        Library library = new Library();
        library.run();
    }
}


class Author {
    String name;

    BookList books = new BookList();

    public Author() {
    }

    public boolean equals(Object obj) {
        return name.equals(((Author) obj).name);
    }

    public void display() {
        System.out.println(name);
        books.display();
    }
}


class Book {
    String title;

    Patron patron;

    public Book() {

    }

    public boolean equals(Object obj) {
        return title.equals(((Book) obj).title);
    }

    public String toString() {
        return "     * " + title +
                (patron != null ? " - checked out to " + patron.name : "") +
                "\n";
    }
}


class CheckOutBook {
    Book book = null;

    Author author = null;

    public CheckOutBook() {

    }

    public boolean equals(Object obj) {
        return book.title.equals(((Book) obj).title) &&
                author.name.equals(((Author) obj).name);
    }
}


class Patron {
    String name;

    BookList books = new BookList();

    public Patron() {

    }

    public boolean equals(Object obj) {
        return name.equals(((Patron) obj).name);
    }

    public void display() {
        if (!books.isEmpty()) {
            System.out.println(name + " has the following books:");
            books.display();
        } else {
            System.out.print(name + " has no books");
        }
    }
}


class BookList extends LinkedList {
    public BookList() {
        super();
    }

    public void display() {
        for (int i = 0; i < size(); i++) {
            System.out.print(get(i));
        }
    }
}


class AuthorList extends LinkedList {
    public AuthorList() {
        super();
    }

    public void display() {
        Object[] authors = toArray();
        for (int i = 0; i < authors.length - 1; i++) {
            ((Author) authors[i]).display();
        }
    }
}


class PatronList extends LinkedList {
    public PatronList() {
        super();
    }

    public void display() {
        for (java.util.Iterator iter = this.iterator(); iter.hasNext(); ) {
            ((Patron) iter.next()).display();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -