// Objects for the bookstore I created
BookStore bookStore = new BookStore("Subu's Book Store","Perambur");
bookStore.addItem(new Item("Animal Farm",20));
bookStore.addItem(new Item("Animal Farm",20));
Output if I used the original working method:
Animal Farm has been bought
Animal Farm is already bought
Output for the method that doesn't work using contains()
Animal Farm has been bought
Animal Farm has been bought
Output for the method that doesn't work using indexof()
-1
Animal Farm has been bought
-1
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 1
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372)
at java.base/java.util.ArrayList.get(ArrayList.java:459)
at com.company.subusproject.BookStore.searchItem(BookStore.java:41)
at com.company.subusproject.BookStore.addItem(BookStore.java:16)
at com.company.subusproject.Main.main(Main.java:11)
This is the original method that works
public boolean searchItem(Item item) {
for (int i = 0; i < this.myStocks.size(); i++) {
Item foundItem = this.myStocks.get(i);
if (foundItem.getName().equals((item.getName()))) {
return true;
}
}
return false;
}
This is the method I don't know why it doesn't work, how I intended this method to work is, if the variable searchingItem returns true then the method searchItem will return if not it returns false
public boolean searchItem(Item item) {
// this method returns the boolean value true if found, if not returns false
boolean searchingItem = this.myStocks.contains(item);
if(searchingItem){
return true;
} else {
return false;
}
}
This is also doesn't work as I intended. The way I intended this method to work is, when i found the index position of the element i am looking for and then compare it with the elements that are available in the list myStocks and that isn't the case in this scenario as well.
public boolean searchItem(Item item) {
// this method returns the index number of the founded element, if not returns -1
int itemPosition = this.myStocks.indexOf(item); // this line always returns -1 in this scenario
for(int i=0; i<this.myStocks.size(); i++){
if(myStocks.get(itemPosition).getName().equals(myStocks.get(i).getName())){
return true;
}
}
return false;
}
Store class to create a any store class
public abstract class Store {
private String name;
private String address;
public Store(String name, String address) {
this.name = name;
this.address = address;
}
public abstract boolean addItem(Item item);
public abstract boolean removeItem(Item item);
public abstract boolean searchItem(Item item);
}
Basic items class to add any kind of items
public class Item {
private String name;
private int price;
public Item(String name, int price){
this.name = name;
this.price = price;
}
public String getName(){
return this.name;
}
public int getPrice(){
return this.price;
}
}
Actual book store class where it has the methods to add and search
public class BookStore extends Store {
private List<Item> myStocks;
public BookStore(String name, String address) {
super(name, address);
this.myStocks = new ArrayList<>();
}
public boolean addItem(Item item) {
if (!searchItem(item)) {
this.myStocks.add(item);
System.out.println(item.getName() + " has been bought");
return true;
} else {
System.out.println(item.getName() + " is already bought");
return false;
}
}
public boolean searchItem(Item item) {
for (int i = 0; i < this.myStocks.size(); i++) {
Item foundItem = this.myStocks.get(i);
if(foundItem.getName().equals((this.myStocks.get(i).getName()))) {
return true;
}
}
return false;
}
}
Clearly, I know the assumption I am making about how it should work is wrong and the problem is with those inbuild methods and how I wanted them to work but that isn't the case.