Java 将成员或书籍添加到我的库数组列表

Java 将成员或书籍添加到我的库数组列表,java,arraylist,bluej,Java,Arraylist,Bluej,好的,我正在用bluej用java做一个项目,我的任务是编写一个库系统,我遇到了一个问题,当我在库类中创建一个新成员或书籍时,如何在创建时将其添加到我的数组列表中?这可能很简单,但我就是想不出来 这是我的代码,如果有人觉得他们想看,我道歉,如果它没有列出正确的,但这是我第一次在论坛上发布: import java.util.*; /** * The library class represents an enrolment list for a library. It stores * t

好的,我正在用bluej用java做一个项目,我的任务是编写一个库系统,我遇到了一个问题,当我在库类中创建一个新成员或书籍时,如何在创建时将其添加到我的数组列表中?这可能很简单,但我就是想不出来

这是我的代码,如果有人觉得他们想看,我道歉,如果它没有列出正确的,但这是我第一次在论坛上发布:

import java.util.*;

/**
 * The library class represents an enrolment list for a library. It stores
 * the library name, address and members of the library, as well as the libraries opening times.
 * 
 * @author Kelvin Goodram
 * @version 2014.11.01
 */
public class Library
{
   //name of library to be written as a string ("" "")
   private String libraryName;
   //address of library to be written as a string ("" "") 
   private String address;
   //opening times of library to be written as a string ("" "")
   private String openingTimes;

   private List<Member> members;

   private List<Book> books;
   //the member ID to be written as a whole integer number
   private int capacity;
   //the member ID to be written as a whole integer number
   private int bookCapacity;
   //the memebers fullname to be written as a string ("" "")
   private String name;
   //the member ID to be written as a whole integer number
   private int id; 
   //members contact number to be written as a string ("" "")
   private String tele;
   //books author to be written as a string ("" "")
   private String author;
   //books title to be written as a string ("" "")
   private String title;
   //books reference number to be written as a whole integer number
   private int refNum;
   //books genre to be written as a string ("" "")
   private String genre;

    /**
     * Create a Library with an option to input a maximum number of members & books. All other details
     * are set to default values.
     */
    public Library(int maxNumberOfMembers, int maxNumberOfBooks)
    {
        libraryName = "Bolton Central Library";
        address = "110-119 Le Mans Cresent Bolton BL1 1SE";
        openingTimes = "Open Monday - Friday (8am-7pm)";
        members = new ArrayList<Member>();
        books = new ArrayList<Book>();
        capacity = maxNumberOfMembers;
        bookCapacity = maxNumberOfBooks;
    }


    /**
     * Add a member to this Library.
     */
    public void addMember(String fullName, String telephoneNumber)
    {
        // if the member capacity has been reached
        if(members.size() == capacity) {
            // do this
            System.out.println("The Library is not currently taking new members as we are full, please enroll at a different library.");
        }
        //otherwise do this:
        else {
             name = fullName;
             tele = telephoneNumber;
             id = 0;
        }
    }

    /**
     * Add a book to this Library.
     */
    public void addBook(String bookAuthor, String bookTitle, int bookRef, String bookGenre)
    {
         // if the book capacity has been reached   
        if(books.size() == capacity) {
            //do this
            System.out.println("The Library currently does not have space for new books."); 
        }
        //otherwise do this:
        else {
            author = bookAuthor;
            title = bookTitle;
            refNum = bookRef;
            genre = bookGenre;
        }
    }

    /**
     * Return the number of members currently enrolled in this Library.
     */
    public int numberOfMembers()
    {
        return members.size(); //returns an integer number of how many members are in the arrayList <Member>
    }

    /**
     * Return the number of books currently registered in this Library.
     */
    public int numberOfBooks()
    {
        return books.size(); //returns an integer number of how many books are in the arrayList <Book>
    }

    /**
     * Set the Name for this Library.
     */
    public void changeLibraryName(String LibName)
    {
        libraryName = LibName; //enter a string value to mutate the library name
    }

    /**
     * alter the opening times for this Library. The parameter should define the opening hour
     * and the closing time, such as "open Mon - Friday: 10am - 5pm".
     */
    public void changeOpeningTimes(String timeAndDayString)
    {
        openingTimes = timeAndDayString; //enter a string value to mutate the library opening times
    }

    /**
     * Alter the street address of the library.
     */
    public void changeAdress(String streetAddress)
    {
        address = streetAddress; //enter a string value to mutate the library treet address
    }

    /**
     * Print out a member list to the standard
     * terminal.
     */
    public void printMemberList()
    {
        System.out.println("***************************"); //header
        System.out.println("LibraryName " + libraryName);
        System.out.println("Address:" + address);
        System.out.println("Opening Times:" + openingTimes);
        System.out.println("-------------------------------"); //break
        System.out.println("Member list:");
        for(Member member : members) {
            member.print();
        }
        System.out.println("Number of members: " + numberOfMembers());
        System.out.println("***************************"); //footer
    }

     /**
     * Print out a book list to the standard
     * terminal.
     */
    public void printBookList()
    {
        System.out.println("***************************"); //header
        System.out.println("LibraryName " + libraryName);
        System.out.println("Address:" + address);
        System.out.println("Opening Times:" + openingTimes);
        System.out.println("-------------------------------"); //break
        System.out.println("Book list:");
        for(Book book : books) {
            book.print();
        }
        System.out.println("Number of books: " + numberOfBooks());
        System.out.println("***************************"); //footer
    }
}
如果不需要指定索引,可以执行以下操作:

books.add(newbook);
成员和书籍将是另外两个类。您希望创建书籍和成员的对象,因此您所做的可能不起作用,您应该为每个对象创建一个类。 顺便说一句,当你创作一本书时,你应该这样做:

Book newbook = new Book(param1, param2....);
您应该有这样一个类:

/**
 * A class that maintains information on a book.
 * 
 *
 * @author (Insert your name here.)
 * @version (Insert today's date here.)
 */
class Book
{
    // The fields.
    private String author;
    private String title;
    private String genre;
    private int refNum;

    /**
     * Set the author, title fields and reference number when this object
     * is constructed.
     */
    public Book(String bookAuthor, String bookTitle, int bookRef, String bookGenre )
    {
        author = bookAuthor;
        title = bookTitle;
        refNum = bookRef;
        genre = bookGenre;

    }

    /**
     * Print the author's name, book title, ref number  and genre to the o utput terminal.
     */ 
    public void print()
    {
        System.out.println("Author = " +author);
        System.out.println("Title = " +title);
        System.out.println("Book Reference = " +refNum);
        System.out.println("This book can be found in the " +genre + " section");
        System.out.println("----------------------------------------------------");


    } 

}
public class Book
{

  //variables_
private String name;^

   //Constructor
   public Book(String name, String...){
       this.name = name;
       this..... = ....;
        }

 //methods
  public String getName(){
return name;
    }
etc....


}

然后在library类中,您可以创建该类的新对象。

我更新了答案,看一看基本上我想知道是否有方法添加此项:name=fullName;电话号码;id=0;这是:私人名单成员;抱歉打断一下,但我发现java真的很难理解:
public class Book
{

  //variables_
private String name;^

   //Constructor
   public Book(String name, String...){
       this.name = name;
       this..... = ....;
        }

 //methods
  public String getName(){
return name;
    }
etc....


}