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