将成员或书籍添加到我的库阵列列表中

时间:2014-11-21 20:03:10

标签: java arraylist bluej

好的,所以我使用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
    }
}

我确实有另外两个类,这本书看起来像这样:

/**
 * 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("----------------------------------------------------");


    } 

}

1 个答案:

答案 0 :(得分:0)

You should take a look in the Java List API

add(E e)
Appends the specified element to the end of this list (optional operation).

add(int index, E element)
Inserts the specified element at the specified position in this list (optional operation).

如果您不需要指定索引,可以执行以下操作:

books.add(newbook);

会员和书籍将是另外两个班级。您想要创建书籍和成员的对象,因此您正在做的事情可能不起作用,您应该为每个人创建一个类。 顺便说一句,当你创建一本书时,你应该这样做:

Book newbook = new Book(param1, param2....);

你应该有这样一个类:

public class Book
{

  //variables_
private String name;^

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

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


}

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