2014-11-21 1 views
0

좋아요, 제가 bluej를 사용하는 자바 프로젝트를하고 있습니다. 라이브러리 시스템을 작성해야합니다. 어떻게 작성합니까? 도서관 수업 시간에 새로운 회원이나 책을 만들 때 배열 목록에 추가 할 수 있습니까? 아마도 무언가는 정말 간단하지만 난 그걸 알아낼 수 없습니다.회원 번호 또는 도서관 정보를 추가 하시겠습니까?

여기 누군가가 그 잘 밖으로 설정하지 않으면 내가 사과보고 싶은 생각한다면 내 코드입니다 그러나 이것은 처음 내가 포럼에 게시 한 시간입니다 : 내가 다른 두를 할

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


    } 

} 
+0

나는 내 대답을 업데이트했다. – MMrj

답변

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

회원 및 도서는 다른 두 개의 클래스가됩니다. 당신은 책과 멤버의 객체를 만들고 싶으므로, 아마도 당신이하고있는 일이 잘되지 않을 것이므로, 각각을위한 클래스를 만들어야합니다. 당신이 책을 만들 때 그런데 , 당신은이 작업을 수행해야합니다

public class Book 
{ 

    //variables_ 
private String name;^ 

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

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


} 

그런 다음 라이브러리 클래스에서이 클래스의 새로운 객체를 생성 할 수 있습니다 :

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

는이 같은 클래스를 가져야한다 .

+0

기본적으로 이것을 추가 할 방법이 있는지 알고 싶다. name = fullName; tele = telephoneNumber; id = 0; this : 개인 목록 회원; 잠깐 쾅하고 미안하지만 이해하기가 정말 어려운 자바를 찾고 있습니다. –

+0

답변을 업데이트했습니다. – MMrj

관련 문제