I am building a book store application using a Firebase database
. I am trying to use the facade design pattern to check if a book is in stock before a customer buys it. I have an InventoryServices
class that has a Book
object passed into it, it then checks the stock level and return either true or false depending on the stock levels.
I have a OrderServiceFacadeImp
class that creates a new book object and implements the method in the InventoryServices
inStock
method. There is also an interface OrderServiceFacade
that is implemented by OrderServiceFacadeImp
. The interface has boolean checkStock
which takes in the title
of the Book
.
The title is passed all the way to the OrderServiceFacadeImp
class but becomes null
when I try to set the title=book.getTitle()
.
OrderServiceFacadeImp.java
import com.example.cianm.bookstore.entity.Book;
public class OrderServiceFacadeImp implements OrderServiceFacade {
@Override
public boolean checkStock(String title) {
boolean orderFulfilled=false;
Book book = new Book();
title = book.getTitle();
if(InventoryService.inStock(book)){
//System.out.println("Book" + book.getTitle() + "is availible");
orderFulfilled = true;
} else {
orderFulfilled = false;
}
return orderFulfilled;
}
}
I just don't understand why this is happening and it. Ultimately I want to display a message to the user saying if the book is in stock or not when they try to add it to their cart. Any help would be greatly appreciated.
InventoryService.java
import android.util.Log;
import com.example.cianm.bookstore.entity.Book;
public class InventoryService {
public static boolean inStock(Book book){
String TAG = "Book stock: ";
boolean inStock = false;
int stock = book.getStock();
if (stock == 0){
inStock = false;
Log.v(TAG, "Book not in stock" + stock);
} else {
inStock = true;
Log.v(TAG, "Book in stock" + stock);
}
return inStock;
}
}
OrderServiceFacade.java
public interface OrderServiceFacade {
boolean checkStock(String title);
}
OrderFulfillmentController.java
public class OrderFulfillmentController {
public OrderServiceFacade facade;
public boolean orderFulfilled=false;
public void orderProduct(String title){
orderFulfilled = facade.checkStock(title);
}
}
This is the method that is called when a user tries to add a Book
to their cart.
public void addToCart(){
OrderFulfillmentController controller = new OrderFulfillmentController();
controller.facade=new OrderServiceFacadeImp();
mCartRef = FirebaseDatabase.getInstance().getReference("Cart");
String cartID = mCartRef.push().getKey();
String title = mTitle.getText().toString();
String author = mAuthor.getText().toString();
String category = mCategory.getText().toString();
Double price = Double.parseDouble(mPrice.getText().toString());
int quantity = Integer.parseInt(mQuantity.getText().toString());
int stock = Integer.parseInt(mStock.getText().toString());
Double total = price*quantity;
controller.orderProduct(bookID);
boolean result = controller.orderFulfilled;
if(result){
Toast.makeText(getApplicationContext(), "Book: " + title + "is out of stock" , Toast.LENGTH_LONG).show();
} else {
Cart cart = new Cart.CartBuilder(title, quantity, userName, image)
.setAuthor(author)
.setCategory(category)
.setStock(stock)
.setTotal(total)
.setBookID(bookID)
.buildCart();
mCartRef.child(fbUser.getUid()).child(cartID).setValue(cart);
Toast.makeText(getApplicationContext(), "Added " + cart.getTitle() + " to cart", Toast.LENGTH_LONG).show();
}
}
Aucun commentaire:
Enregistrer un commentaire