图书馆分配问题

时间:2019-06-15 01:13:21

标签: java

基本上,我正在尝试为学校教师建立图书馆系统。该程序必须能够添加书籍,添加学生,搜索书籍,签出书籍等。但是问题是,当添加一个学生并要求显示该学生时,它在显示中设置的数组甚至是空的尽管正在使用同一对象。

我尝试使用setter和getter来设置数组和对象的值,但似乎没有任何效果。

我了解到,学生类中的showAllstudents()方法认为该数组为空,但我不明白为什么会这样认为,因为我为调用该方法的对象设置了值。

package Library;
import java.util.Scanner;



public class book {

    public int sNo;
    public String bookName;
    public String authorName;
    public int bookQty;
    public int bookQtyCopy;

    public int getsNo() {
        return sNo;
    }

    public void setsNo(int sNo) {
        this.sNo = sNo;
    }

    public String getBookName() {
        return bookName;
    }

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

    public String getAuthorName() {
        return authorName;
    }

    public void setAuthorName(String authorName) {
        this.authorName = authorName;
    }

    public int getBookQty() {
        return bookQty;
    }

    public void setBookQty(int bookQty) {
        this.bookQty = bookQty;
    }

    public int getBookQtyCopy() {
        return bookQtyCopy;
    }

    public void setBookQtyCopy(int bookQtyCopy) {
        this.bookQtyCopy = bookQtyCopy;
    }

    Scanner input = new Scanner(System.in);

    public book(){

        System.out.println("Enter Serial No of Book:");
        this.sNo = input.nextInt();
        input.nextLine();
        System.out.println("Enter Book Name:");
        this.bookName = input.nextLine();
        System.out.println("Enter Author Name:");
        this.authorName = input.nextLine();
        System.out.println("Enter Quantity of Books:");
        this.bookQty = input.nextInt();
        bookQtyCopy = this.bookQty;

    }
}
package Library;

import java.util.Scanner;

public class books {
    book theBooks[] = new book[50];     // Array that stores 'book' Objects.
    public static int count;    // Counter for No of book objects Added in Array.

    Scanner input = new Scanner(System.in);




    public int compareBookObjects(book b1, book b2){

        if (b1.bookName.equalsIgnoreCase(b2.bookName)){

            System.out.println("Book of this Name Already Exists.");
            return 0;

        }
        if (b1.sNo==b2.sNo){

            System.out.println("Book of this Serial No Already Exists.");
            return 0;
        }
        return 1;
    }

    public void addBook(book b){

        for (int i=0; i<count; i++){

            if (this.compareBookObjects(b, this.theBooks[i]) == 0)
                return;

        }

        if (count<50){

            theBooks[count] = b;
            count++;

        }
        else{

            System.out.println("No Space to Add More Books.");

        }

    }

    public void searchBySno(){

        System.out.println("\t\t\t\tSEARCH BY SERIAL NUMBER\n");

        int sNo;
        System.out.println("Enter Serial No of Book:");
        sNo = input.nextInt();

        int flag = 0;
        System.out.println("S.No\t\tName\t\tAuthor\t\tAvailable Qty\t\tTotal Qty");
        for (int i=0; i<count; i++){

            if (sNo == theBooks[i].sNo){

                System.out.println(theBooks[i].sNo + "\t\t" + theBooks[i].bookName + "\t\t" + theBooks[i].authorName + "\t\t" + 
                        theBooks[i].bookQtyCopy + "\t\t" + theBooks[i].bookQty);
                flag++;
                return;

            }

        }
        if (flag == 0)
            System.out.println("No Book for Serial No " + sNo + " Found.");

    }

    public void searchByAuthorName(){

        System.out.println("\t\t\t\tSEARCH BY AUTHOR'S NAME");
        input.nextLine();
        System.out.println("Enter Author Name:");
        String authorName = input.nextLine();
        int flag = 0;
        System.out.println("S.No\t\tName\t\tAuthor\t\tAvailable Qty\t\tTotal Qty");
        for (int i=0; i<count; i++){

            if (authorName.equalsIgnoreCase(theBooks[i].authorName)){

                System.out.println(theBooks[i].sNo + "\t\t" + theBooks[i].bookName + "\t\t" + theBooks[i].authorName + "\t\t" + 
                        theBooks[i].bookQtyCopy + "\t\t" + theBooks[i].bookQty);
                flag++;
            }

        }
        if (flag == 0)
            System.out.println("No Books of " + authorName + " Found.");

    }


    public void showAllBooks(){

        System.out.println("\t\t\t\tSHOWING ALL BOOKS\n");
        System.out.println("S.No\t\tName\t\tAuthor\t\tAvailable Qty\t\tTotal Qty");
        for (int i=0; i<count; i++){
            System.out.println(theBooks[i].sNo + "\t\t" + theBooks[i].bookName + "\t\t" + theBooks[i].authorName + "\t\t" + 
                    theBooks[i].bookQtyCopy + "\t\t" + theBooks[i].bookQty);
        }

    }

    public void upgradeBookQty(){

        System.out.println("\t\t\t\tUPGRADE QUANTITY OF A BOOK\n");
        System.out.println("Enter Serial No of Book");
        int sNo = input.nextInt();
        for (int i=0; i<count; i++){

            if (sNo == theBooks[i].sNo){

                System.out.println("Enter No of Books to be Added:");
                int addingQty = input.nextInt();
                theBooks[i].bookQty += addingQty;
                theBooks[i].bookQtyCopy += addingQty;
                return;

            }

        }

    }



    public int isAvailable(int sNo){

        //returns the index number if available



        for (int i=0; i<count; i++){

            if (sNo == theBooks[i].sNo){
                if(theBooks[i].bookQtyCopy > 0){

                    System.out.println("Book is Available.");
                    return i;

                }
                System.out.println("Book is Unavailable");
                return -1;

            }

        }

        System.out.println("No Book of Serial Number " + " Available in Library.");
        return -1;


    }

    public book checkOutBook(){

        System.out.println("Enter Serial No of Book to be Checked Out.");
        int sNo = input.nextInt();

        int bookIndex =isAvailable(sNo);

        if (bookIndex!=-1){

            //int bookIndex = isAvailable(sNo);
            theBooks[bookIndex].bookQtyCopy--;

            return theBooks[bookIndex];
        }

        return null;

    }

    public void checkInBook(book b){

        for (int i=0; i<count; i++){

            if (b.equals(theBooks[i]) ){

                theBooks[i].bookQtyCopy++;
                return;

            }

        }

    }
}
package Library;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Library extends JFrame implements ActionListener{
    private JPanel controlPanel;
    private JButton btnAddBook, btnChangeBookQuantity, btnSearchBook,btnShowAllBooks,btnRegisterStudent,btnShowStudents,btnCheckOut,btnCheckIn,btnExit; 
    private int width, height;
    private books ob;
    private students obStudent;
    private student s;
    private book b;

    public void GUI() {
        width = 500;
        height = 500;
        setLayout(null);  // layout for my frame
        controlPanel = new JPanel();  // panel for buttons

        btnAddBook = new JButton("Add Book");  // create button
        btnChangeBookQuantity = new JButton("Change Book Quantity");
        btnSearchBook = new JButton("Search Book");
        btnShowAllBooks = new JButton("Show All Books");
        btnRegisterStudent = new JButton("Register Student");
        btnShowStudents = new JButton("Show Students");
        btnCheckOut = new JButton("Check Out");
        btnCheckIn = new JButton("Check In");
        btnExit = new JButton("Exit");

        // set the size and position of the panels
        controlPanel.setBounds(0,0,height,width);

        btnAddBook.setPreferredSize(new Dimension(200, 40));
        btnChangeBookQuantity.setPreferredSize(new Dimension(200, 40));
        btnSearchBook.setPreferredSize(new Dimension(200, 40));
        btnShowAllBooks.setPreferredSize(new Dimension(200, 40));
        btnRegisterStudent.setPreferredSize(new Dimension(200, 40));
        btnShowStudents.setPreferredSize(new Dimension(200, 40));
        btnCheckOut.setPreferredSize(new Dimension(200, 40));
        btnCheckIn.setPreferredSize(new Dimension(200, 40));
        btnExit.setPreferredSize(new Dimension(200, 40));

        // add components to panels
        controlPanel.add(btnAddBook);    //button to control panel
        controlPanel.add(btnSearchBook);
        controlPanel.add(btnChangeBookQuantity);
        controlPanel.add(btnShowAllBooks);
        controlPanel.add(btnRegisterStudent);
        controlPanel.add(btnShowStudents);
        controlPanel.add(btnCheckOut);
        controlPanel.add(btnCheckIn);
        controlPanel.add(btnExit);


        add(controlPanel);  // add the button and drawing panels to the frame

        // add button as a listener in this frame
        btnAddBook.addActionListener(this);
        btnChangeBookQuantity.addActionListener(this);
        btnSearchBook.addActionListener(this);
        btnShowAllBooks.addActionListener(this);
        btnRegisterStudent.addActionListener(this);
        btnShowStudents.addActionListener(this);
        btnCheckOut.addActionListener(this);
        btnCheckIn.addActionListener(this);
        btnExit.addActionListener(this);

        // set size and location of frame
        setSize(width,height);  
        setLocation(100,100);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void actionPerformed (ActionEvent e){
        //This method recieves an action from either button or the timer and executes the code for each. The start button starts the timer. The timer moves the pictures
        // depending on what they rolled. The exit button quits the application. The restart button removes everything from the drawing panel and reinitializes the variables.
        ob = new books();   
        obStudent = new students();
        setVisible(false);
        if (e.getSource()==btnAddBook){  
            b = new book();
            ob.addBook(b);
        }
        else if (e.getSource()==btnChangeBookQuantity) {
            ob.upgradeBookQty();
        }
        else if (e.getSource()==btnSearchBook) {
            ob.searchByAuthorName();
        }
        else if(e.getSource()==btnShowAllBooks) {
            ob.showAllBooks();
        }
        else if(e.getSource()==btnRegisterStudent) {
            s = new student();
            obStudent.addStudent(s);
        }
        else if (e.getSource()==btnShowStudents) {
            obStudent.showAllStudents();
        }
        else if(e.getSource()==btnCheckOut) {
            obStudent.checkOutBook(ob);
        }
        else if(e.getSource()==btnCheckIn) {
            obStudent.checkInBook(ob);
        }
        else if (e.getSource()==btnExit) {
            System.exit(0);
        }
        setVisible(true);
    }
    public static void main(String[] args) {
        Library c = new Library();
        c.GUI();
    }

}
package Library;

import java.util.Scanner;    
public class student {

    String studentName;
    String regNum;

    book borrowedBooks[] = new book[3];
    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public String getRegNum() {
        return regNum;
    }

    public void setRegNum(String regNum) {
        this.regNum = regNum;
    }

    public book[] getBorrowedBooks() {
        return borrowedBooks;
    }

    public void setBorrowedBooks(book[] borrowedBooks) {
        this.borrowedBooks = borrowedBooks;
    }

    public int getBooksCount() {
        return booksCount;
    }

    public void setBooksCount(int booksCount) {
        this.booksCount = booksCount;
    }

    public Scanner getInput() {
        return input;
    }
    public String toString() {
        return getStudentName() + " " + getRegNum() + "\n";

    }
    public void setInput(Scanner input) {
        this.input = input;
    }

    public int booksCount = 0;

    Scanner input = new Scanner(System.in);

    public student(){
        String test = "";
        System.out.println("Enter Student Name:");
        test = input.nextLine();
        this.setStudentName(test);
        System.out.println("Enter Reg Number:");
        test = input.nextLine();
        this.setRegNum(test);

    }
}
package Library;


import java.util.Scanner;

import javax.swing.JOptionPane;

public class students {

    Scanner input = new Scanner(System.in);

    student theStudents[] = new student[50];


    public static int count = 0;

    public void addStudent(student s){

        for (int i=0; i<count; i++){

            if(s.getRegNum().equalsIgnoreCase(theStudents[i].getRegNum())){

                System.out.println("Student of Reg Num " + s.getRegNum() + " is Already Registered.");
                return;
            }

        }
        JOptionPane.showMessageDialog(null,count);

        if (count<=50){

            setTheStudents(s);
            setCount(getCount() + 1);

        }
        System.out.print(theStudents[0].toString());
    }

    public void showAllStudents(){

        System.out.println("Student Name\t\tReg Number");
        for (int i=0; i<count; i++){
            System.out.println(theStudents[i].toString());

        }


    }

    public student[] getTheStudents() {
        return theStudents;
    }

    public void setTheStudents(student theStudents) {
        this.theStudents[count] = theStudents;
    }

    public static int getCount() {
        return count;
    }

    public static void setCount(int count) {
        students.count = count;
    }

    public int isStudent(){
        //return index number of student if available

        //System.out.println("Enter Student Name:");
        //String getStudentName() = input.nextLine();

        System.out.println("Enter Reg Number:");
        String regNum = input.nextLine();

        for (int i=0; i<count; i++){

            if (theStudents[i].getRegNum().equalsIgnoreCase(regNum)){

                return i;

            }

        }
        System.out.println("Student is not Registered.");
        System.out.println("Get Registered First.");


        return -1;

    }
    public void checkOutBook(books book){
        int studentIndex =this.isStudent();

        if (studentIndex!=-1){
            System.out.println("checking out");

            book.showAllBooks();//jjjjjjjjjjjj
            book b = book.checkOutBook();
            System.out.println("checking out");
            if (b!= null){

                if (theStudents[studentIndex].getBooksCount()<=3){
                    System.out.println("adding book");
                    theStudents[studentIndex].getBorrowedBooks()[theStudents[studentIndex].getBooksCount()] = b;
                    theStudents[studentIndex].booksCount++;
                    return;

                }
                else {

                    System.out.println("Student Can not Borrow more than 3 Books.");
                    return;

                }
            }
            System.out.println("Book is not Available.");

        }

    }

    public void checkInBook(books book){

        int studentIndex = this.isStudent();
        if (studentIndex != -1){
            System.out.println("S.No\t\t\tBook Name\t\t\tAuthor Name");
            student s = theStudents[studentIndex];
            for (int i=0; i<s.getBooksCount(); i++){
                System.out.println(s.getBorrowedBooks()[i].sNo+ "\t\t\t" + s.getBorrowedBooks()[i].getBookName() + "\t\t\t"+
                        s.getBorrowedBooks()[i].getAuthorName());

            }
            System.out.println("Enter Serial Number of Book to be Checked In:");
            int sNo = input.nextInt();
            for (int i=0; i<s.getBooksCount(); i++){

                if (sNo == s.getBorrowedBooks()[i].sNo){

                    book.checkInBook(s.getBorrowedBooks()[i]);
                    s.getBorrowedBooks()[i]=null;
                    return;

                }


            }
            System.out.println("Book of Serial No "+sNo+"not Found");

        }



    }


}

单击showallstudents按钮时,它应该显示控制台中所有已注册的学生,但是显示此错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Library.students.showAllStudents(students.java:43)
    at Library.Library.actionPerformed(Library.java:109)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

0 个答案:

没有答案