I need to write a search method so that I can find a specific book from the library. I have already written a method for registering books to the library. According to the task, I have to inform the user in the register method whether the book has been added or whether there is no capacity. That's why I wrote this method like this. In the next step I need to be able to search a registered book. I have to write another method for this, namely search (string title) and I have to check whether the title is in the library or not.
The task says that you have to store the books in an array. Someone recommended me to use hash map. I'm an absolute beginner and I don't know how I can write a search method here using hashmap, so that I can also tell the user (using an if
operation) whether the title is available or not. I have to write another method outside register. put and get in hashmap is clear to me, but it is important to use them in the search method. Is there an easy way, without hashmap, without arraylist, and only by arrays?
Book class:
public class Book {
private final String title;
public Book(String title) {
this.title = title;
}
public String toString() {
return title;
}
}
Library class:
public class library {
private Book[] myBooks;
static final Map <Book, Book[]> shelves =new HashMap<Book, Book[]> ();
public library() {
myBooks = new Book[10];
System.out.println("Hello, I am a small library for at most 10 books.");
}
public void register(Book... book ) {
//int n= book.length;
for(int i=0;i<myBooks.length;i ) {
myBooks[i]=book[i];
System.out.println("an new book " book[i] " has been registed");
Book[] buch = shelves.put(book[i],book);
System.out.println(buch);
}
System.out.println("the library is full!");
}
The main class:
public class App {
public static void main(String[] args) throws Exception {
library bibliothek = new library();
Book buch1 = new Book("SWT");
Book buch2 = new Book("INt1");
Book buch3 = new Book("INt2");
Book buch4 = new Book("INt3");
Book buch5 = new Book("INt4");
Book buch6 = new Book("INt5");
Book buch7 = new Book("INt6");
Book buch8 = new Book("INt7");
Book buch9 = new Book("INt8");
Book buch10 = new Book("INt9");
Book buch11 = new Book("INt10");
Book buch12 = new Book("INt11");
bibliothek.register(buch1, buch2, buch3, buch4, buch5, buch6, buch7, buch8, buch9, buch10, buch11, buch12);
}
}
CodePudding user response:
If the task says you have to store the books in an array, then do not use hash map, use array.
public String search(String title) {
for (int i = 0; i < books.length; i ) {
if (books[i].title.equals(title)) {
return "The title is in the library!";
}
}
return "The title is not in the library!";
}
CodePudding user response:
You can do it like this i think:
public class library {
private Book[] myBooks;
public library() {
myBooks = new Book[10];
System.out.println("Hello, I am a small library for at most 10 books.");
}
public void register(Book... book ) {
//int n= book.length;
for(int i=0;i<myBooks.length;i ){
myBooks[i]=book[i];
System.out.println("an new book " book[i] " has been registed");
}
System.out.println("the library is full!");
}
public boolean isInLibary(Book aBook){
for (int i = 0; i< myBooks.length; i ){
if (myBooks[i].getTitle().equals(aBook.getTitle())) return true;
}
return false;
}
}
I just renamed your method toString to getTitle because is what it does, and i implemented a search method, type boolean, which searches all the array and if it finds the book, it returns true, otherwise it returns false. Therefore the only thing you have to do is: Go to the point that you want to check if the book already exists in the array and write if(isInLibary(book)).
Your job could have been done easier if you had used an arraylist and that is something i suggest you do generally in the exercises.
You can ask me anything you want.