Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用书本对象和成员对象创建库_Java - Fatal编程技术网

Java 使用书本对象和成员对象创建库

Java 使用书本对象和成员对象创建库,java,Java,我最近刚开始编程,遇到了一些小错误。我正在创建一个library对象,其中包含对book对象和member对象的引用,但是我在从member类访问方法并在library类中使用它们时遇到问题,而LibraryTester类中不会出现错误。这就是我写的代码 图书馆课 package assignment; import java.nio.channels.MembershipKey; import java.util.ArrayList; import java.util.Scanner; i

我最近刚开始编程,遇到了一些小错误。我正在创建一个library对象,其中包含对book对象和member对象的引用,但是我在从member类访问方法并在library类中使用它们时遇到问题,而LibraryTester类中不会出现错误。这就是我写的代码

图书馆课

package assignment;

import java.nio.channels.MembershipKey;
import java.util.ArrayList;
import java.util.Scanner;

import org.omg.Messaging.SyncScopeHelper;

public class Library {

    // Declared an array list of type book
    private ArrayList<Book> Books;
    private MemberList members;;
    public Library(ArrayList<Book> Books, Member member) {
        this.Books = Books;
        this.members=members;
    };

    public void displayBooks() {
        System.out.println("\t\t\t-----The Current Books in the library are-----");
        for (int i = 0; i < Books.size(); i++) {
            System.out.println("\n" + "\t\t" + Books.get(i).getTitle() + "\t\t\t" + "\n\t\tAuthor: "
                    + Books.get(i).getAuthor() + "\n\t\tThis Books ID is: " + Books.get(i).getBookID() + "\t\t\t\t\t\t"
                    + "\n\t\tIs this book on loan? " + Books.get(i).getOnLoan() + "\t\t\t\t\t"
                    + "\n\t\tThe number of times which this book has been loaned: " + Books.get(i).getNumOfLoans()
                    + "\t\t");
        }
    }

    // method to remove permanently a book object
    public void removeBook() // the parameter that is passed through
                                // is the book id of the book that is to
                                // be removed

    {
        Scanner input = new Scanner(System.in);
        int bookID = 0;

        boolean successful = false;
        try {
            do {

                System.out.println("Please enter the book ID of the book you wish to delete");
                bookID = input.nextInt();
                for (int i = 0; i < Books.size(); i++) {
                    if (bookID == Books.get(i).getBookID())

                    {
                        System.out.println("The Book " + Books.get(i).getTitle() + " was removed");
                        Books.remove(i);
                        successful = true;
                        break;

                    }

                }
                if (!successful) {
                    System.out.println("Book ID " + bookID + " does not exist");
                }

            } while (successful == false);
        } catch (Exception e) {
            System.out.println("ERROR: Invalid input" + "\nYou have been returned to the main menu");
        }
    }

    public void editBook() {

        boolean successful = false;
        try {
            do {
                Scanner sc = new Scanner(System.in);
                System.out.println("Please enter the book ID of the book who's details you wish to change");
                int bookID = sc.nextInt();
                sc.nextLine();
                for (int i = 0; i < Books.size(); i++) {
                    if (Books.get(i).getBookID() == bookID) {

                        System.out.println("Please enter the new name of the book:");
                        String newTitle = sc.nextLine();
                        Books.get(i).setTitle(newTitle);
                        System.out.println("Please enter the name of the author of the book:");
                        String newAuthor = sc.nextLine();
                        Books.get(i).setAuthor(newAuthor);
                        System.out.println("Change of book details successful" + "\nNew book title: " + newTitle
                                + "\nNew author: " + newAuthor);
                        successful = true;
                    }

                }

                if (!successful) {
                    System.out.println("This book does not exist ");
                }

            } while (successful == false);
        } catch (Exception e) {
            System.out.println("ERROR: Invalid input" + "\nYou have been returned to the main menu");
        }
    }

    public void addBook() {

        boolean successful = false;
        int bookID = 0;
        String title = "";
        String author = "";
        Scanner input = new Scanner(System.in);

        do {

            System.out.println("Please assign a 3 digit number for the books ID ");
            bookID = input.nextInt();
            input.nextLine();
            if(bookID > 99 && bookID <1000){
            for (int i = 0; i < Books.size(); i++) {
                if(Books.get(i).getBookID()!= bookID){
                    successful = true;
                } else {
                    System.out.println("This book ID already exists ");
                }
            }
            } else { System.out.println("You must enter a number between 99 and 1000 ");
            }
        } while (successful == false);

        do {
            System.out.println("Please enter the name of book");
            title = input.nextLine();

            for (int i = 0; i < Books.size(); i++) {
                if (Books.get(i).getTitle().equalsIgnoreCase(title)) {
                    System.out.println("ERROR: This book already exists");
                    successful = false;
                } else {
                    successful = true;
                }
            }
        } while (successful == false);

        do {
            System.out.println("Please enter the author of the book");
            author = input.nextLine();

            successful = true;

        } while (successful == false);
        Book Book = new Book(bookID, title, author, false, 0, 0);
        Books.add(Book);
        System.out.println(
                "Book creation succcessful:" + "\nTitle: " + title + "\nAuthor: " + author + "\nBook ID:" + bookID);
    }

    public void loanBook() {
        Scanner input = new Scanner(System.in);
        boolean successful = false;
        do {

            System.out.println(
                    "\nPlease enter the book ID of the book that you wish to take out (Press 9 to exit to the main menu)");
            int bookID = input.nextInt();
            if (bookID == 9) {
                successful = true;
                break;
            }

            for (int i = 0; i < Books.size(); i++) {
                if (Books.get(i).getBookID() == bookID) {
                    do {
                        System.out.println("\nHow long would you like to loan the book for (20 Days maximum):");
                        int durationOnLoan = input.nextInt();
                        if (durationOnLoan <= 20 && 1 <= durationOnLoan) {
                            Books.get(i).setDurationOnLoan(durationOnLoan);
                            successful = true;
                        } else {
                            System.out.println("The number of days you have entered is invalid");
                        }
                    } while (successful == false);

                    System.out.println("\nThe book " + Books.get(i).getTitle() + " is now on loan");
                    Books.get(i).setOnLoan(true);

                    Books.get(i).setNumOfLoan(Books.get(i).getNumOfLoans() + 1);
                    successful = true;
                }

            }

            if (successful == false) {

                System.out.println("This book does not exist ");
            }

        } while (successful == false);
    }

    public void returnBook() {

        boolean successful = false;
        Scanner input = new Scanner(System.in);
        try {
            do {

                System.out.println(
                        "Please enter the book ID of the book you wish to return (Press 9 to exit to the main menu");
                int bookID = input.nextInt();
                input.nextLine();
                if (bookID == 9) {
                    successful = true;
                }

                for (int i = 0; i < Books.size(); i++) {
                    if (Books.get(i).getBookID() == bookID) {
                        if (Books.get(i).getOnLoan() == true) {
                            System.out.println("How long did you loan the book for?");
                            int durationOnLoan = input.nextInt();
                            if (durationOnLoan > Books.get(i).getDurationOnLoan()) {
                                durationOnLoan -= Books.get(i).getDurationOnLoan();
                                if (durationOnLoan < 3) {

                                    System.out.println("You are " + durationOnLoan
                                            + " day(s) late in returning the book" + "\nYou have been fined £3."
                                            + "\n The book " + Books.get(i).getTitle() + " is now returned");
                                    successful = true;

                                } else {

                                    System.out.println("You are " + durationOnLoan + " days late in returning the book"
                                            + "\nYou have been fined £6.");
                                    System.out
                                            .println("The book " + Books.get(i).getTitle() + " has now been returned");
                                    successful = true;

                                }
                            } else {
                                Books.get(i).setOnLoan(false);
                                System.out.println("The book " + Books.get(i).getTitle() + " has now been returned");
                                successful = true;
                            }

                        } else if (Books.get(i).getOnLoan() == false) {
                            System.out.println("\nThis book was not on loan");
                            System.out.println("\nYou have been returned to the main menu");
                            successful = true;
                        }
                    } else if (successful == false) {
                        System.out.println("This book does not exist");
                    }
                }

            } while (successful == false);
        } catch (Exception e) {
            System.out.println("ERROR: Invalid input" + "\nYou have been returned to the main menu");

        }
    }

}
package assignment;

import java.nio.channels.MembershipKey;
import java.util.ArrayList;
import java.util.Scanner;

import org.omg.Messaging.SyncScopeHelper;

public class Library {

    // Declared an array list of type book
    private ArrayList<Book> Books;
    private ArrayList<Member> members;;
    public Library(ArrayList<Book> Books, ArrayList<Member> member) {
        this.Books = Books;
        this.members=member;
    };
我不确定逗号后面应该放什么,我已经尝试了所有的方法,但似乎没有任何效果。有什么想法吗

编辑

编辑库类

package assignment;

import java.nio.channels.MembershipKey;
import java.util.ArrayList;
import java.util.Scanner;

import org.omg.Messaging.SyncScopeHelper;

public class Library {

    // Declared an array list of type book
    private ArrayList<Book> Books;
    private MemberList members;;
    public Library(ArrayList<Book> Books, Member member) {
        this.Books = Books;
        this.members=members;
    };

    public void displayBooks() {
        System.out.println("\t\t\t-----The Current Books in the library are-----");
        for (int i = 0; i < Books.size(); i++) {
            System.out.println("\n" + "\t\t" + Books.get(i).getTitle() + "\t\t\t" + "\n\t\tAuthor: "
                    + Books.get(i).getAuthor() + "\n\t\tThis Books ID is: " + Books.get(i).getBookID() + "\t\t\t\t\t\t"
                    + "\n\t\tIs this book on loan? " + Books.get(i).getOnLoan() + "\t\t\t\t\t"
                    + "\n\t\tThe number of times which this book has been loaned: " + Books.get(i).getNumOfLoans()
                    + "\t\t");
        }
    }

    // method to remove permanently a book object
    public void removeBook() // the parameter that is passed through
                                // is the book id of the book that is to
                                // be removed

    {
        Scanner input = new Scanner(System.in);
        int bookID = 0;

        boolean successful = false;
        try {
            do {

                System.out.println("Please enter the book ID of the book you wish to delete");
                bookID = input.nextInt();
                for (int i = 0; i < Books.size(); i++) {
                    if (bookID == Books.get(i).getBookID())

                    {
                        System.out.println("The Book " + Books.get(i).getTitle() + " was removed");
                        Books.remove(i);
                        successful = true;
                        break;

                    }

                }
                if (!successful) {
                    System.out.println("Book ID " + bookID + " does not exist");
                }

            } while (successful == false);
        } catch (Exception e) {
            System.out.println("ERROR: Invalid input" + "\nYou have been returned to the main menu");
        }
    }

    public void editBook() {

        boolean successful = false;
        try {
            do {
                Scanner sc = new Scanner(System.in);
                System.out.println("Please enter the book ID of the book who's details you wish to change");
                int bookID = sc.nextInt();
                sc.nextLine();
                for (int i = 0; i < Books.size(); i++) {
                    if (Books.get(i).getBookID() == bookID) {

                        System.out.println("Please enter the new name of the book:");
                        String newTitle = sc.nextLine();
                        Books.get(i).setTitle(newTitle);
                        System.out.println("Please enter the name of the author of the book:");
                        String newAuthor = sc.nextLine();
                        Books.get(i).setAuthor(newAuthor);
                        System.out.println("Change of book details successful" + "\nNew book title: " + newTitle
                                + "\nNew author: " + newAuthor);
                        successful = true;
                    }

                }

                if (!successful) {
                    System.out.println("This book does not exist ");
                }

            } while (successful == false);
        } catch (Exception e) {
            System.out.println("ERROR: Invalid input" + "\nYou have been returned to the main menu");
        }
    }

    public void addBook() {

        boolean successful = false;
        int bookID = 0;
        String title = "";
        String author = "";
        Scanner input = new Scanner(System.in);

        do {

            System.out.println("Please assign a 3 digit number for the books ID ");
            bookID = input.nextInt();
            input.nextLine();
            if(bookID > 99 && bookID <1000){
            for (int i = 0; i < Books.size(); i++) {
                if(Books.get(i).getBookID()!= bookID){
                    successful = true;
                } else {
                    System.out.println("This book ID already exists ");
                }
            }
            } else { System.out.println("You must enter a number between 99 and 1000 ");
            }
        } while (successful == false);

        do {
            System.out.println("Please enter the name of book");
            title = input.nextLine();

            for (int i = 0; i < Books.size(); i++) {
                if (Books.get(i).getTitle().equalsIgnoreCase(title)) {
                    System.out.println("ERROR: This book already exists");
                    successful = false;
                } else {
                    successful = true;
                }
            }
        } while (successful == false);

        do {
            System.out.println("Please enter the author of the book");
            author = input.nextLine();

            successful = true;

        } while (successful == false);
        Book Book = new Book(bookID, title, author, false, 0, 0);
        Books.add(Book);
        System.out.println(
                "Book creation succcessful:" + "\nTitle: " + title + "\nAuthor: " + author + "\nBook ID:" + bookID);
    }

    public void loanBook() {
        Scanner input = new Scanner(System.in);
        boolean successful = false;
        do {

            System.out.println(
                    "\nPlease enter the book ID of the book that you wish to take out (Press 9 to exit to the main menu)");
            int bookID = input.nextInt();
            if (bookID == 9) {
                successful = true;
                break;
            }

            for (int i = 0; i < Books.size(); i++) {
                if (Books.get(i).getBookID() == bookID) {
                    do {
                        System.out.println("\nHow long would you like to loan the book for (20 Days maximum):");
                        int durationOnLoan = input.nextInt();
                        if (durationOnLoan <= 20 && 1 <= durationOnLoan) {
                            Books.get(i).setDurationOnLoan(durationOnLoan);
                            successful = true;
                        } else {
                            System.out.println("The number of days you have entered is invalid");
                        }
                    } while (successful == false);

                    System.out.println("\nThe book " + Books.get(i).getTitle() + " is now on loan");
                    Books.get(i).setOnLoan(true);

                    Books.get(i).setNumOfLoan(Books.get(i).getNumOfLoans() + 1);
                    successful = true;
                }

            }

            if (successful == false) {

                System.out.println("This book does not exist ");
            }

        } while (successful == false);
    }

    public void returnBook() {

        boolean successful = false;
        Scanner input = new Scanner(System.in);
        try {
            do {

                System.out.println(
                        "Please enter the book ID of the book you wish to return (Press 9 to exit to the main menu");
                int bookID = input.nextInt();
                input.nextLine();
                if (bookID == 9) {
                    successful = true;
                }

                for (int i = 0; i < Books.size(); i++) {
                    if (Books.get(i).getBookID() == bookID) {
                        if (Books.get(i).getOnLoan() == true) {
                            System.out.println("How long did you loan the book for?");
                            int durationOnLoan = input.nextInt();
                            if (durationOnLoan > Books.get(i).getDurationOnLoan()) {
                                durationOnLoan -= Books.get(i).getDurationOnLoan();
                                if (durationOnLoan < 3) {

                                    System.out.println("You are " + durationOnLoan
                                            + " day(s) late in returning the book" + "\nYou have been fined £3."
                                            + "\n The book " + Books.get(i).getTitle() + " is now returned");
                                    successful = true;

                                } else {

                                    System.out.println("You are " + durationOnLoan + " days late in returning the book"
                                            + "\nYou have been fined £6.");
                                    System.out
                                            .println("The book " + Books.get(i).getTitle() + " has now been returned");
                                    successful = true;

                                }
                            } else {
                                Books.get(i).setOnLoan(false);
                                System.out.println("The book " + Books.get(i).getTitle() + " has now been returned");
                                successful = true;
                            }

                        } else if (Books.get(i).getOnLoan() == false) {
                            System.out.println("\nThis book was not on loan");
                            System.out.println("\nYou have been returned to the main menu");
                            successful = true;
                        }
                    } else if (successful == false) {
                        System.out.println("This book does not exist");
                    }
                }

            } while (successful == false);
        } catch (Exception e) {
            System.out.println("ERROR: Invalid input" + "\nYou have been returned to the main menu");

        }
    }

}
package assignment;

import java.nio.channels.MembershipKey;
import java.util.ArrayList;
import java.util.Scanner;

import org.omg.Messaging.SyncScopeHelper;

public class Library {

    // Declared an array list of type book
    private ArrayList<Book> Books;
    private ArrayList<Member> members;;
    public Library(ArrayList<Book> Books, ArrayList<Member> member) {
        this.Books = Books;
        this.members=member;
    };
包分配;
导入java.nio.channels.MembershipKey;
导入java.util.ArrayList;
导入java.util.Scanner;
导入org.omg.Messaging.SyncScopeHelper;
公共班级图书馆{
//声明了book类型的数组列表
私人ArrayList书籍;
私人ArrayList成员;;
公共图书馆(ArrayList图书,ArrayList会员){
这个。书=书;
这个.成员=成员;
};
编辑的LibraryTester类

ArrayList<Member> List = new ArrayList<Member>();
MemberList memberList = new MemberList(List){};


ArrayList<Book> list = new ArrayList<Book>();
Library library = new Library(list, memberList ){};
ArrayList List=new ArrayList();
MemberList MemberList=新成员列表(列表){};
ArrayList=新建ArrayList();
Library Library=新库(列表,成员列表){};

错误显示“构造函数库(ArrayList,MemberList)未定义”,但我已更改了库类中的相关内容?

您需要传递一个Book数组和一个成员:

public Library(ArrayList<Book> Books, Member member)
公共图书馆(ArrayList图书,会员)

因为库类具有构造函数

    public Library(ArrayList<Book> Books, Member member){};
公共图书馆(ArrayList图书,会员){};
不能像在LibraryTester类中那样创建对象。
您需要传递第二个参数,即成员类的对象。

这里您错过了一个成员:

 Library library = new Library(list, );
尝试按如下方式添加它创建成员对象:

  Member memberlist = new Member(10,"Joe Blogs", 44, 5, 0,"Bleaker Street",123456789);
  Library library = new Library(list, memberlist);

您看到了什么错误?发布完整的错误消息,因为它们包含许多有用的信息,并经常告诉您错误的确切原因。您的库构造函数需要一个图书列表和一个成员:
Library Library=new Library(List,new Member();
为什么要导入(而不使用)
java.nio.channels.MembershipKey
org.omg.Messaging.SyncScopeHelper
?同样非常重要:变量和字段以小写字母开头;使用
ArrayList成员
ArrayList书籍
而不是现在所做的。啊,我明白你的意思了@Cir0X,'Library Library=new Library(list,new Member(10,“Joe Blogs”,44,5,0,“Blaker Street”,123456789)){};'可以工作,但是有没有办法让这个用户输入来存储多个成员?@C.Darwin您想用成员列表创建一个库?您需要将库构造函数更改为:
公共库(ArrayList Books,ArrayList Member>members){
现在您可以创建这样的库对象:
Library Library=new Library(list,memberList);
您可以使用以下方法创建成员列表:
ArrayList memberList=new arrarylist();
我尝试过`Library Library=new Library(list,memberList);`但它不起作用(名单,新成员(10,“Joe博客”,44,5,0,“Blaker Street”,123456789)){};works@C.Darwin试着看一下上面的内容,我认为这是一样的,在你们确定memberlist是Member的实例之前。
  Member memberlist = new Member(10,"Joe Blogs", 44, 5, 0,"Bleaker Street",123456789);
  Library library = new Library(list, memberlist);