我正在尝试用java中的数组做一个例子,我想用一些按类别排序的书来做一个库,我想用addBook
方法在类别数组中添加这本书但是我不知道如果我有很多数组怎么做,我的目标是在同一个类别的shell中添加这本书,我的3个类称为Estante
,Libros
,libreria
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.
}
}
}
答案 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);
}
}
}