【问题标题】:Adding a member or book to my library array list将成员或书籍添加到我的图书馆阵列列表
【发布时间】:2015-01-20 01:43:00
【问题描述】:

好的,所以我正在使用 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("----------------------------------------------------");


    } 

}

【问题讨论】:

  • 我更新了答案,看看

标签: java arraylist bluej


【解决方案1】:

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);

Members 和 Books 将是另外两个类。您想创建书籍和成员的对象,因此您所做的可能行不通,您应该为每个对象创建一个类。 顺便说一句,当你创建一本书时,你应该这样做:

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....


}

然后在你的库类中你可以创建这个类的新对象。

【讨论】:

  • 基本上我想知道是否有办法添加这个:name = fullName;电话 = 电话号码; ID = 0;对此:私有 List 成员;对不起,我觉得 java 真的很难理解 :)
猜你喜欢
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
  • 2019-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多