Я создаю библиотечную программу, в которой есть 4 класса:
- Книги ==> содержит название книги
- Жанр ==> содержит имя жанр и массив объектов книг
- Библиотека ==> содержит массив объектов жанров
- App ==> содержит диалог и сканер
Вы будете быть в состоянии создавать новые жанровые массивы и новые книжные массивы из класса приложения.
public class App{
Library library = new Library();
//the other stuff
private void run(){
library.addGenres(insertGenreNameHere);
}
}
public class Library{
private Genres[] genres = new Genres[5]; //Obj. Array of genres
private Int nrOfGenres = 0; //number of how many genres there are in an array
public void addGenres(String genreName){ //adds a new genre to the array
if (nrOfGenres < genres.length) {
genres[nrOfGenres] = new Genres(genreName);
nrOfGenres++;
}
else {
System.out.println("You already have the maximum of " + genres.length + " genres!");
}
}
public class Genres {
private String name;
private Books[] books = new Books[5]; //Obj. Array of books
private int nrOfBooks = 0; //number of how many books there are in an array
public Genres(String name) { //Constructor
this.name = name;
}
//getter & setter for the name of the genre
public void addBooks(String titel){ //adds new book to the array
if (nrOfBooks < books.length) {
books[nrOfBooks] = new Books(titel);
nrOfBooks++;
}
else {
System.out.println("You already have the maximum of " + books.length + " books!");
}
}
public void showBooks(){ //prints the books line by line
int x = 0;
while(x < books.length && books[x] != null) {
System.out.println(books[x].getTitle());
x++;
}
}
}
public class Books(){
private String title;
public Books(String title){ //Constructor
this.title = title;
}
//getter & setter for the title
}
Однако я пока не знаю, как добавить книгу в жанр или даже как мне «связаться» ( ?) книга
Если я не ошибаюсь, я не могу просто сделать Жанр жанра = новый жанр (); или Книги книги = new Book (); , потому что он должен быть в массиве (?)
Я был бы очень рад, если бы кто-нибудь мог мне помочь, и был бы рад поделиться дополнительной информацией, если это необходимо
Ура Мартин