Skip to content

Commit

Permalink
1. Added Checkout Button
Browse files Browse the repository at this point in the history
2. Added Total cost logic
3. Fixed Increase/Decrease quantity issue
4. Added cost formatting
  • Loading branch information
banditVedant committed Sep 11, 2023
1 parent b8f292e commit 4cab1c9
Show file tree
Hide file tree
Showing 14 changed files with 350 additions and 155 deletions.
9 changes: 6 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" >
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Expand Down Expand Up @@ -29,7 +29,10 @@
android:theme="@style/Theme.BugBazaar"
android:usesCleartextTraffic="true"
tools:replace="android:fullBackupContent"
tools:targetApi="31" >
tools:targetApi="31">
<activity
android:name=".ui.checkout.Checkout"
android:exported="false" />
<activity
android:name=".ui.cart.CartActivity"
android:exported="false" />
Expand All @@ -55,7 +58,7 @@
android:exported="true" />
<activity
android:name=".ui.NavigationDrawer_Dashboard"
android:exported="true" >
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
30 changes: 17 additions & 13 deletions app/src/main/java/com/BugBazaar/ui/DetailedProductActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.BugBazaar.R;
import com.BugBazaar.ui.cart.Cart;
Expand All @@ -23,15 +22,16 @@
import java.util.List;


public class DetailedProductActivity extends AppCompatActivity {
private List<CartItem> cartItems = new ArrayList<>();
public class DetailedProductActivity extends AppCompatActivity {

private List<CartItem> cartItems; // Declare cartItems here
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detailed_product);


CartDatabaseHelper cartDBHelper = new CartDatabaseHelper(this, "cart.db", null, 1);

cartItems = cartDBHelper.getAllRecords();
// Toolbar title set
TextView toolbarTitle = findViewById(R.id.toolbarTitle);
toolbarTitle.setText("Product Details");
Expand Down Expand Up @@ -65,37 +65,41 @@ public void onClick(View v) {
String productName = product.getName();
int productPrice = product.getPrice();
int productImage = product.getImageResId();
int quantity = 1;

// Check if the item already exists in the cart
boolean itemExists = false;
for (CartItem cartItem : cartItems) {
if (cartItem.getProductName().equals(productName)) {
// If the item exists, update its quantity
int existingQuantity = cartItem.getQuantity();
cartItem.setQuantity(existingQuantity + quantity);
itemExists = true;
cartItem.setQuantity(existingQuantity + 1);

// Update the quantity in the database
cartDBHelper.updateCartItem(cartItem);
int rowsUpdated = cartDBHelper.updateCartItem(cartItem);
itemExists = true;
break;

}
}

if (!itemExists) {
// If the item is not already in the cart, add it
CartItem cartItem = new CartItem(productName, productPrice, quantity, productImage);
cartItems.add(cartItem);
CartItem cartItem = new CartItem(productName, productPrice, 1, productImage);
// Save the product details to the SQLite database
long recordId = cartDBHelper.saveProductDetails(productName, productPrice, quantity, productImage);
long recordId = cartDBHelper.addCartItem(cartItem); // Add the new item to the database
cartItems.add(cartItem);
cartItem.setId(recordId);
cartItems.add(cartItem); // Add the item to the cartItems list
}

// Start CartActivity without sending the product details as Parcelable
Intent intent = new Intent(DetailedProductActivity.this, CartActivity.class);
startActivity(intent);
}
});
}
// Code to handle back button


// Code to handle back button
public void onBackButtonClick(View view) {
onBackPressed(); // Navigate back to the previous activity
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ protected void onCreate(Bundle savedInstanceState) {

// product data
productList = new ArrayList<>();
productList.add(new Product("Old Town Camera",getString(R.string.desc_cycle), R.drawable.item_camera,3000));
productList.add(new Product("Dumb Watch", getString(R.string.desc_cycle), R.drawable.item_watch,2400));
productList.add(new Product("Skate-Board", getString(R.string.desc_cycle), R.drawable.item_skateboard,5640));
productList.add(new Product("A Lazy BiCycle", getString(R.string.desc_cycle), R.drawable.item_cycle,19000));
productList.add(new Product("PineApple iPhone", getString(R.string.desc_cycle), R.drawable.item_iphone,69000));
productList.add(new Product("Old Town Camera",getString(R.string.desc_cycle), R.drawable.item_camera,3400));
productList.add(new Product("Dumb Watch", getString(R.string.desc_cycle), R.drawable.item_watch,2700));
productList.add(new Product("Skate-Board", getString(R.string.desc_cycle), R.drawable.item_skateboard,1600));
productList.add(new Product("A Lazy BiCycle", getString(R.string.desc_cycle), R.drawable.item_cycle,7040));
productList.add(new Product("PineApple iPhone", getString(R.string.desc_cycle), R.drawable.item_iphone,6900));
productList.add(new Product("Z Box Gaming Controller", getString(R.string.desc_cycle), R.drawable.item_gc,3400));
productList.add(new Product("A Rat", getString(R.string.desc_cycle), R.drawable.item_mouse,1200));
productList.add(new Product("Spy TWS", getString(R.string.desc_cycle), R.drawable.item_tws,4200));
productList.add(new Product("VR device", getString(R.string.desc_cycle), R.drawable.item_vr,24000));
productList.add(new Product("VR device", getString(R.string.desc_cycle), R.drawable.item_vr,8340));


// Create and set the adapter for the GridView
Expand All @@ -75,7 +75,6 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
public void onClick(View v) {
String searchText = String.valueOf(searchEditText.getText());
Log.d("amit", String.valueOf(searchText.length()));

if (searchText.length() <= 30) {

Expand All @@ -100,7 +99,7 @@ public void onClick(View v) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}

//DO NOT REMOVEIf you search for empty searchbox and app crashed, it is intentional. It is a "Improper Exception Handling" bug
String filteredList = null;
Log.d("Excpetion",filteredList);
}
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/com/BugBazaar/ui/ProductAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import com.BugBazaar.R;
import com.BugBazaar.ui.DetailedProductActivity;
import com.BugBazaar.ui.Product;
import com.BugBazaar.ui.cart.CartItem;

import java.io.Serializable;

import java.util.List;
Expand Down Expand Up @@ -69,6 +71,11 @@ public void onClick(View view) {
Intent intent = new Intent(context, DetailedProductActivity.class);
intent.putExtra("product", product);
context.startActivity(intent);

//Sending intent to CartItem class
Intent intToCartItem = new Intent(context, CartItem.class);
intent.putExtra("product", product);

}
});
return view;
Expand Down
87 changes: 78 additions & 9 deletions app/src/main/java/com/BugBazaar/ui/cart/CartActivity.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,115 @@
package com.BugBazaar.ui.cart;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.BugBazaar.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class CartActivity extends AppCompatActivity {

private RecyclerView cartRecyclerView;
private CartAdapter cartAdapter;
private List<CartItem> cartItems;
private CartDatabaseHelper cartDBHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maincart);

// Initialize your CartDatabaseHelper
CartDatabaseHelper cartDBHelper = new CartDatabaseHelper(this, "cart.db", null, 1);

TextView toolbarTitle = findViewById(R.id.toolbarTitle);
toolbarTitle.setText("My Cart");

// Find the RecyclerView and the empty cart TextView in the layout
cartRecyclerView = findViewById(R.id.cartRecyclerView);
TextView cartEmptyTextView = findViewById(R.id.cartEmptyTextView);
ImageView cartEmptyImage=findViewById(R.id.cartEmptyImage);
LinearLayout emptyLinear=findViewById(R.id.emptyLinear);
Button checkoutButton=findViewById(R.id.checkoutButton);

//TotalCost
TextView txtTotalCostText=findViewById(R.id.txtTotalCostText);
TextView txtTotalCostNumber=findViewById(R.id.txtTotalCostNumber);

// Find the RecyclerView in the layout
cartRecyclerView = findViewById(R.id.cartRecyclerView);

// Initialize your cartItems list and populate it with all items from the database
cartItems = cartDBHelper.getAllRecords();

// Create and set up the adapter
cartAdapter = new CartAdapter(this, cartItems);
cartAdapter = new CartAdapter(this, cartItems, cartDBHelper);

cartRecyclerView.setAdapter(cartAdapter);
cartRecyclerView.setLayoutManager(new LinearLayoutManager(this));

// Check if the cart is empty and show/hide the empty cart message
if (cartItems.isEmpty()) {
//Empty msg and image visible
emptyLinear.setVisibility(View.VISIBLE);
cartEmptyTextView.setVisibility(View.VISIBLE);
//Checkout and total Gone
txtTotalCostText.setVisibility(View.GONE);
txtTotalCostNumber.setVisibility(View.GONE);
checkoutButton.setVisibility(View.GONE);
cartEmptyImage.setVisibility(View.VISIBLE);

// Hide the RecyclerView
cartRecyclerView.setVisibility(View.GONE);

} else {
// Hide the "Cart is Empty" message
emptyLinear.setVisibility(View.GONE);
cartEmptyTextView.setVisibility(View.GONE);
cartEmptyImage.setVisibility(View.GONE);

//Checkout and total Visible
checkoutButton.setVisibility(View.VISIBLE);
txtTotalCostText.setVisibility(View.VISIBLE);
txtTotalCostNumber.setVisibility(View.VISIBLE);

// Show the RecyclerView
cartRecyclerView.setVisibility(View.VISIBLE);
}
// Calculate the total cart value and update the txtTotalCostNumber TextView
int totalCost = calculateTotalCost(cartItems);
String formattedTotalCost = formatTotalCost(totalCost);
txtTotalCostNumber.setText(formattedTotalCost);

checkoutButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Perform the checkout action here
Toast.makeText(CartActivity.this, "Checkout clicked", Toast.LENGTH_SHORT).show();
}
});

}

// Calculate the total cart value
private int calculateTotalCost(List<CartItem> cartItems) {
int totalCost = 0;
for (CartItem cartItem : cartItems) {
totalCost =totalCost+ cartItem.getPrice() * cartItem.getQuantity();
}
return totalCost;
}
// Format the total cart value with commas
private String formatTotalCost(int totalCost) {
return String.format("₹%,d.00", totalCost);
}

// Code to handle back button
Expand Down
Loading

0 comments on commit 4cab1c9

Please sign in to comment.