2016-09-13 7 views
-1

に要素を追加するために、私はカテゴリでソートいくつかの本でライブラリをしたいどのように、私は追加するaddBookメソッドを使用したいですカテゴリ配列内のブックが、私は、私は多くの配列をした場合、目標はカテゴリの同じシェルに本を追加でそれを行う方法がわからない、私はEstanteと呼ばれる3クラス、Libroslibreriaを思います。[JAVA]私はJavaで配列して例を作成しようとしている特定のアレイ

package com.manzacatesSAS; 

/** 
* Created by deborah on 7/09/16. 
*/ 
public class Libreria  
{ 
    Estante[] miedo  = new Estante[6]; 
    Estante[] comedia = new Estante[6]; 
    Estante[] novelas = new Estante[6]; 
    Estante[] romance = new Estante[6]; 
    Estante[] comics = new Estante[6];  
} 

package com.manzacatesSAS; 

/** 
* Created by deborah on 7/09/16. 
*/ 
public class Estante { 

    //attributes of Estante 
    private int maxNumLibros = 6; 
    //I guess this isn't necesay because if i use array,this only has 6 spaces 
    private int maxPages = 3000; 
    private String categoria = ""; 
    private int currentLibros = 0; 
    private int currentPages = 0; 

    public int getNumLibros() { 
     return maxNumLibros; 
    } 

    public void setNumLibros(int maxNumLibros) { 
     this.maxNumLibros = maxNumLibros; 
    } 

    public int getMaxPages() { 
     return maxPages; 
    } 

    public void setMaxPages(int maxPages) { 
     this.maxPages = maxPages; 
    } 

    public String getCategoria() { 
     return categoria; 
    } 

    public void setCategoria(String categoria) { 
     this.categoria = categoria; 
    } 

    /** 
    * Constructor de Estantes 
    * 
    * @param categoria categoria de los libros 
    */ 
    //No añadi los parametros restantes dado que al crear uno nuevo sus atributos deben estar vacios, o definidos por los atributos comunes. 
    public Estante(String categoria) { 
     this.categoria = categoria; 
    } 

     public void addBook(Libros x){ 
      if (currentPages + x.getNumPages() < maxPages && currentLibros < maxNumLibros) { 
       currentLibros++; 
      //I guess the array of the categorie to add the book would be here, and i think that use a for to add the book in the correct array. 
      } 
     } 
    } 
+2

申し訳ありませんが、何を求めているのですか。 – GhostCat

+0

だからあなたのライブラリーは、30段の棚の合計、棚の5列、6つの棚のための部屋でそれぞれを保持します。私はどこかの本( 'Libros')の配列を見て期待していたが、それを見ていないのですか?また、配列が実際に使用されているどのように多くのスペースを追跡するためにあなたに残し、それは 'ArrayList'を使用する方が簡単ですが、これはアレイと経験を得るのであれば、もちろん、それが動作するはずです。道による –

+0

ニース、オブジェクト指向の考え方、。 –

答えて

0

下記のクラス設計を参照してください(クラスを別々のファイルに入れてpublicにする場合)。あなたのようなものを行うことができ、あなたのメインプログラムで

:ここ

Library library = new Library(); 
Shelf shelf1 = new Shelf("Shelf 1"); 
library.addShelfToLibrary(shelf1); 
Category fiction = new Category("fiction"); 
Category literature = new Category("literature"); 
Book book1 = new Book("Don Quixote"); 
book1.addBookToCategory(fiction);  // category for this book 
book1.addBookToCategory(literature); // another category for same book 
shelf1.addBookToShelf(book1); 

は、クラスのデザインです。メソッドを追加する必要があります。これはスケルトンです。

class Library { 
    private ArrayList<Shelf> shelves = new ArrayList<>(); 

    public void addShelfToLibrary(Shelf shelf) { 
     if (!this.shelves.contains(shelf)) { 
      this.shelves.add(shelf); 
     } 
    } 
} 

class Category { 
    private String catname; 

    public Category(String catname) { 
     this.catname = catname; 
    } 

    public String getCatname() { 
     return catname; 
    } 
} 

class Book { 
    private String title; 
    private ArrayList<Category> categories = new ArrayList<>(); 

    public Book(String title) { 
     this.title = title; 
    } 

    public ArrayList<Category> getCategories() { 
     return categories; 
    } 
    public void addBookToCategory(Category category) { 
     if (!this.categories.contains(category)) { 
      this.categories.add(category); 
     } 
    } 
} 

class Shelf { 
    private String shelfId; 
    private ArrayList<Book> booksOnShelf = new ArrayList<>(); 

    public Shelf(String shelfId) { 
     this.shelfId = shelfId; 
    } 

    public void addBookToShelf(Book book) { 
     if (!this.booksOnShelf.contains(book)) { 
      this.booksOnShelf.add(book); 
     } 
    } 
} 
関連する問題