mirror of
https://github.com/Nezumi-2711/PRM392.git
synced 2026-09-22 05:41:45 +00:00
Update Quantity Cart
This commit is contained in:
@@ -57,7 +57,7 @@ public class Cart extends AppCompatActivity {
|
||||
FirebaseDatabase database;
|
||||
DatabaseReference requests;
|
||||
|
||||
TextView txtTotalPrice;
|
||||
public TextView txtTotalPrice;
|
||||
FButton btnPlace;
|
||||
|
||||
List<Order> cart = new ArrayList<>();
|
||||
|
||||
@@ -13,8 +13,9 @@ import java.util.List;
|
||||
|
||||
public class Database extends SQLiteAssetHelper {
|
||||
|
||||
private static final String DB_NAME="EatItDB.db";
|
||||
private static final int DB_VER=1;
|
||||
private static final String DB_NAME = "EatItDB.db";
|
||||
private static final int DB_VER = 1;
|
||||
|
||||
public Database(Context context) {
|
||||
super(context, DB_NAME, null, DB_VER);
|
||||
}
|
||||
@@ -23,22 +24,24 @@ public class Database extends SQLiteAssetHelper {
|
||||
SQLiteDatabase db = getReadableDatabase();
|
||||
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
|
||||
|
||||
String[] sqlSelect = {"ProductName", "ProductId", "Quantity", "Price", "Discount"};
|
||||
String sqlTable="OrderDetail";
|
||||
String[] sqlSelect = {"ID", "ProductName", "ProductId", "Quantity", "Price", "Discount"};
|
||||
String sqlTable = "OrderDetail";
|
||||
|
||||
qb.setTables(sqlTable);
|
||||
Cursor c = qb.query(db, sqlSelect, null, null, null, null, null);
|
||||
|
||||
final List<Order> result = new ArrayList<>();
|
||||
if(c.moveToFirst()) {
|
||||
if (c.moveToFirst()) {
|
||||
do {
|
||||
result.add(new Order(c.getString(c.getColumnIndexOrThrow("ProductId")),
|
||||
result.add(new Order(
|
||||
c.getInt(c.getColumnIndexOrThrow("ID")),
|
||||
c.getString(c.getColumnIndexOrThrow("ProductId")),
|
||||
c.getString(c.getColumnIndexOrThrow("ProductName")),
|
||||
c.getString(c.getColumnIndexOrThrow("Quantity")),
|
||||
c.getString(c.getColumnIndexOrThrow("Price")),
|
||||
c.getString(c.getColumnIndexOrThrow("Discount"))
|
||||
));
|
||||
} while(c.moveToNext());
|
||||
} while (c.moveToNext());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -61,23 +64,23 @@ public class Database extends SQLiteAssetHelper {
|
||||
}
|
||||
|
||||
//Favorites
|
||||
public void addToFavorites(String foodId){
|
||||
public void addToFavorites(String foodId) {
|
||||
SQLiteDatabase db = getReadableDatabase();
|
||||
String query = String.format("INSERT INTO Favorites(FoodId) VALUES('%s');", foodId);
|
||||
db.execSQL(query);
|
||||
}
|
||||
|
||||
public void removeFromFavorites(String foodId){
|
||||
public void removeFromFavorites(String foodId) {
|
||||
SQLiteDatabase db = getReadableDatabase();
|
||||
String query = String.format("DELETE FROM Favorites WHERE FoodId='%s';", foodId);
|
||||
db.execSQL(query);
|
||||
}
|
||||
|
||||
public boolean isFavorite(String foodId){
|
||||
public boolean isFavorite(String foodId) {
|
||||
SQLiteDatabase db = getReadableDatabase();
|
||||
String query = String.format("SELECT * FROM Favorites WHERE FoodId='%s'", foodId);
|
||||
Cursor cursor = db.rawQuery(query, null);
|
||||
if(cursor.getCount() <= 0){
|
||||
if (cursor.getCount() <= 0) {
|
||||
cursor.close();
|
||||
return false;
|
||||
}
|
||||
@@ -90,12 +93,18 @@ public class Database extends SQLiteAssetHelper {
|
||||
SQLiteDatabase db = getReadableDatabase();
|
||||
String query = String.format("SELECT COUNT(*) FROM OrderDetail");
|
||||
Cursor cursor = db.rawQuery(query, null);
|
||||
if(cursor.moveToFirst()){
|
||||
if (cursor.moveToFirst()) {
|
||||
do {
|
||||
count = cursor.getInt(0);
|
||||
}while (cursor.moveToNext());
|
||||
} while (cursor.moveToNext());
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
public void updateCart(Order order) {
|
||||
SQLiteDatabase db = getReadableDatabase();
|
||||
String query = String.format("UPDATE OrderDetail SET Quantity = %s WHERE ID = %d", order.getQuantity(), order.getID());
|
||||
db.execSQL(query);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.waterbase.foodify.Model;
|
||||
|
||||
public class Order {
|
||||
|
||||
private int ID;
|
||||
private String ProductId;
|
||||
private String ProductName;
|
||||
private String Quantity;
|
||||
@@ -19,6 +20,23 @@ public class Order {
|
||||
Discount = discount;
|
||||
}
|
||||
|
||||
public Order(int ID, String productId, String productName, String quantity, String price, String discount) {
|
||||
this.ID = ID;
|
||||
ProductId = productId;
|
||||
ProductName = productName;
|
||||
Quantity = quantity;
|
||||
Price = price;
|
||||
Discount = discount;
|
||||
}
|
||||
|
||||
public int getID() {
|
||||
return ID;
|
||||
}
|
||||
|
||||
public void setID(int ID) {
|
||||
this.ID = ID;
|
||||
}
|
||||
|
||||
public String getProductId() {
|
||||
return ProductId;
|
||||
}
|
||||
|
||||
@@ -13,7 +13,10 @@ import androidx.annotation.NonNull;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.amulyakhare.textdrawable.TextDrawable;
|
||||
import com.cepheuen.elegantnumberbutton.view.ElegantNumberButton;
|
||||
import com.waterbase.foodify.Cart;
|
||||
import com.waterbase.foodify.Common.Common;
|
||||
import com.waterbase.foodify.Database.Database;
|
||||
import com.waterbase.foodify.Interface.ItemClickListener;
|
||||
import com.waterbase.foodify.Model.Order;
|
||||
import com.waterbase.foodify.R;
|
||||
@@ -28,7 +31,7 @@ import java.util.Locale;
|
||||
class CartViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener {
|
||||
|
||||
public TextView txt_card_item, txt_price;
|
||||
public ImageView img_cart_count;
|
||||
public ElegantNumberButton btn_quantity;
|
||||
|
||||
private ItemClickListener itemClickListener;
|
||||
|
||||
@@ -40,7 +43,7 @@ class CartViewHolder extends RecyclerView.ViewHolder implements View.OnClickList
|
||||
super(itemView);
|
||||
txt_card_item = (TextView) itemView.findViewById(R.id.cart_item_name);
|
||||
txt_price = (TextView) itemView.findViewById(R.id.cart_item_Price);
|
||||
img_cart_count = (ImageView) itemView.findViewById(R.id.cart_item_count);
|
||||
btn_quantity = (ElegantNumberButton) itemView.findViewById(R.id.btn_quantity);
|
||||
itemView.setOnCreateContextMenuListener(this);
|
||||
}
|
||||
|
||||
@@ -59,25 +62,47 @@ class CartViewHolder extends RecyclerView.ViewHolder implements View.OnClickList
|
||||
public class CartAdapter extends RecyclerView.Adapter<CartViewHolder>{
|
||||
|
||||
private List<Order> listData = new ArrayList<>();
|
||||
private Context context;
|
||||
private Cart cart;
|
||||
|
||||
public CartAdapter(List<Order> listData, Context context) {
|
||||
public CartAdapter(List<Order> listData, Cart cart) {
|
||||
this.listData = listData;
|
||||
this.context = context;
|
||||
this.cart = cart;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public CartViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
LayoutInflater inflater = LayoutInflater.from(context);
|
||||
LayoutInflater inflater = LayoutInflater.from(cart);
|
||||
View itemView = inflater.inflate(R.layout.cart_layout, parent, false);
|
||||
return new CartViewHolder(itemView);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull CartViewHolder holder, int position) {
|
||||
TextDrawable drawable = TextDrawable.builder().buildRound("" + listData.get(position).getQuantity(), Color.RED);
|
||||
holder.img_cart_count.setImageDrawable(drawable);
|
||||
public void onBindViewHolder(@NonNull CartViewHolder holder, final int position) {
|
||||
// TextDrawable drawable = TextDrawable.builder().buildRound("" + listData.get(position).getQuantity(), Color.RED);
|
||||
// holder.img_cart_count.setImageDrawable(drawable);
|
||||
|
||||
holder.btn_quantity.setNumber(listData.get(position).getQuantity());
|
||||
holder.btn_quantity.setOnValueChangeListener(new ElegantNumberButton.OnValueChangeListener() {
|
||||
@Override
|
||||
public void onValueChange(ElegantNumberButton view, int oldValue, int newValue) {
|
||||
Order order = listData.get(position);
|
||||
order.setQuantity(String.valueOf(newValue));
|
||||
new Database(cart).updateCart(order);
|
||||
|
||||
//Calculate total price
|
||||
float total = 0;
|
||||
List<Order> orders = new Database(cart).getCarts();
|
||||
for (Order item : orders)
|
||||
total += (Float.parseFloat(order.getPrice())) * (Float.parseFloat(item.getQuantity()));
|
||||
Locale locale = new Locale("vi", "VN");
|
||||
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
|
||||
float price = newValue*(Float.parseFloat(order.getPrice()));
|
||||
holder.txt_price.setText(fmt.format(price));
|
||||
|
||||
cart.txtTotalPrice.setText(fmt.format(total));
|
||||
}
|
||||
});
|
||||
|
||||
Locale locale = new Locale("vi", "VN");
|
||||
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
|
||||
|
||||
@@ -50,20 +50,22 @@
|
||||
|
||||
<info.hoang8f.widget.FButton
|
||||
android:id="@+id/btnPlaceOrder"
|
||||
android:text="Place Order"
|
||||
android:textColor="@android:color/white"
|
||||
android:layout_marginRight="8dp"
|
||||
android:layout_marginLeft="8dp"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="8dp"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:layout_marginStart="8dp"
|
||||
android:layout_marginLeft="8dp"
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:layout_marginRight="8dp"
|
||||
android:layout_marginBottom="8dp"
|
||||
android:text="Place Order"
|
||||
android:textColor="@android:color/white"
|
||||
app:cornerRadius="4dp"
|
||||
app:fButtonColor="@color/btnSignActive"
|
||||
app:shadowColor="@android:color/black"
|
||||
app:shadowEnabled="true"
|
||||
app:shadowHeight="5dp"
|
||||
app:cornerRadius="4dp"
|
||||
/>
|
||||
app:shadowHeight="5dp" />
|
||||
</RelativeLayout>
|
||||
</androidx.cardview.widget.CardView>
|
||||
|
||||
|
||||
@@ -156,10 +156,9 @@
|
||||
android:id="@+id/ratingBar"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:rating="0"
|
||||
android:max="5"
|
||||
android:isIndicator="true"
|
||||
/>
|
||||
android:max="5"
|
||||
android:rating="0" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/food_description"
|
||||
@@ -171,6 +170,7 @@
|
||||
android:text="Description"
|
||||
android:textColor="@color/black"
|
||||
android:textSize="14sp" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</androidx.cardview.widget.CardView>
|
||||
|
||||
@@ -43,12 +43,17 @@
|
||||
android:layout_height="wrap_content" />
|
||||
</LinearLayout>
|
||||
|
||||
<ImageView
|
||||
android:layout_gravity="center_vertical|end"
|
||||
android:layout_marginRight="16dp"
|
||||
android:id="@+id/cart_item_count"
|
||||
android:layout_width="20dp"
|
||||
android:layout_height="20dp" />
|
||||
<com.cepheuen.elegantnumberbutton.view.ElegantNumberButton
|
||||
android:id="@+id/btn_quantity"
|
||||
android:layout_width="130dp"
|
||||
android:layout_height="47dp"
|
||||
android:layout_margin="10dp"
|
||||
app:textSize="8sp"
|
||||
app:backGroundColor="@color/colorAccent"
|
||||
app:initialNumber="1"
|
||||
app:finalNumber="20"
|
||||
|
||||
/>
|
||||
|
||||
</LinearLayout>
|
||||
</com.google.android.material.card.MaterialCardView>
|
||||
Reference in New Issue
Block a user