Reputation: 111
Ok, i have this class Libray that uses another class named Book. I want to make a method that erase an object book in the array inventary, but i dont how to declare the return value of the method, i want to return the object libray to use it in another class, where i will display all the books in it. The num variable its given by the user in another class, its represents the number of the book to erase. The array inventary starts from 0 to 9.
public class Library {
private Book[] inventary;
private int booksquantity;
public Library eraseBook(int num){
for(int i=0 ; i<booksquantity ; i++){
if(i == num-1){
for(int j = i ; j<booksquantity ; j++){
inventary[j] = inventary[j+1];}
}
}return ***;
}
}
//In the other class i would make something like this to use this method eraseBook, in a //switch
case 6:
AppLibrary.cleanscreen();//Static method to erase the screen
System.out.println("What book do u wish to delete?");
String inventary = ghandi.generateInventory();//this makes the //inventory to the user
if(inventary.equals("")){
System.out.println("No books available in the inventary");
}
else{
System.out.println(inventary);
}
int num = Integer.parseInt(s.nextLine());//here i read from the //keyboard the number of book the user wants to delete
//Here the object libary is caled "ghandi"
ghandi.eraseBook(num);//here i use the method
System.out.println("Book erase, please display inventary again");
s.nextLine();
break;
Thanks!!
Upvotes: 1
Views: 170
Reputation: 44459
Use void
if you don't want to return anything (which I assume is the case here).
If you want to return the object you're in (so, the current library that you just deleted from), use the this
keyword.
Upvotes: 2
Reputation: 2238
You can use void as the method signature:
public void eraseBook(int num){}
Or you could return null at the end of method, but that is not a good practice.
Upvotes: 0
Reputation: 2752
add a constructor Library(Book[] inventary, int booksquantity) and call it in the return method so.
public class Library {
private Book[] inventary;
private int booksquantity;
public Library(Book[] inventary, int booksquantity){
this.inventary = inventary;
this.booksquantity = booksquantity;
}
public Library eraseBook(int num){
for(int i = 0; i<booksquantity ; i++){
if((inventary[i] == inventary[num-1]) && (inventary[i+1] != null)){
inventary[i+1]= inventary[i];
} else if(inventary[i] == inventary[num-1]){
inventary[i] = null;
}
}
return new Library(inventary, booksquantity);
}
}
if you just want to erase a book and not make the whole class immutable.
public class Library {
private Book[] inventary;
private int booksquantity;
public Library(Book[] inventary, int booksquantity){
this.inventary = inventary;
this.booksquantity = booksquantity;
}
public void eraseBook(int num){
for(int i = 0; i<booksquantity ; i++){
if((inventary[i] == inventary[num-1]) && (inventary[i+1] != null)){
inventary[i+1]= inventary[i];
} else if(inventary[i] == inventary[num-1]){
inventary[i] = null;
}
}
}
}
Upvotes: 0