Search in sources :

Example 1 with Cart

use of com.example.asus.onlinecanteen.model.Cart in project OnlineCanteen by josephgunawan97.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.AppTheme);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Initialize Navigation View
    DrawerLayout drawerLayout = findViewById(R.id.main_drawer_layout);
    ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.navigation_open, R.string.navigation_close);
    drawerLayout.addDrawerListener(drawerToggle);
    drawerToggle.syncState();
    NavigationView navigationView = findViewById(R.id.main_navigation_view);
    navigationView.setNavigationItemSelectedListener(new MainNavigationListener());
    // Get User
    firebaseAuth = FirebaseAuth.getInstance();
    user = firebaseAuth.getCurrentUser();
    // Initialize References
    databaseUsers = FirebaseDatabase.getInstance().getReference("users");
    databaseProducts = FirebaseDatabase.getInstance().getReference("products");
    databaseStore = FirebaseDatabase.getInstance().getReference("store");
    databaseWallet = FirebaseDatabase.getInstance().getReference("wallet");
    // Product List
    ArrayList<Product> productArrayList = new ArrayList<>();
    menuListAdapter = new MenuListAdapter(this, productArrayList);
    // Initialize ListView
    productListView = findViewById(R.id.list);
    productListView.setAdapter(menuListAdapter);
    // Cart Button and Click Handler
    placeOrder = findViewById(R.id.OrderButton);
    placeOrder.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            cart = menuListAdapter.getList();
            // Check empty cart
            boolean emptyCart = true;
            for (Cart c : cart) {
                if (c.getQuantity() != 0) {
                    emptyCart = false;
                    break;
                }
            }
            if (emptyCart == true) {
                // Alert dialog if there are no items in cart
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setMessage("You don't have any items in your cart!").setCancelable(false).setNegativeButton("Ok", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });
                AlertDialog alert = builder.create();
                alert.setTitle("Error");
                alert.show();
            } else {
                // Remove item if qty is 0
                ArrayList<Cart> toRemove = new ArrayList<>();
                for (Cart c : cart) {
                    if (c.getQuantity() == 0) {
                        toRemove.add(c);
                    }
                }
                cart.removeAll(toRemove);
                // Go to cart
                Intent intent = new Intent(MainActivity.this, CartActivity.class);
                intent.putExtra("Cart", cart);
                intent.putExtra("Seller", menuListAdapter.getSeller());
                startActivity(intent);
            }
        }
    });
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) NavigationView(android.support.design.widget.NavigationView) DialogInterface(android.content.DialogInterface) ActionBarDrawerToggle(android.support.v7.app.ActionBarDrawerToggle) ArrayList(java.util.ArrayList) Product(com.example.asus.onlinecanteen.model.Product) Intent(android.content.Intent) NavigationView(android.support.design.widget.NavigationView) View(android.view.View) TextView(android.widget.TextView) ListView(android.widget.ListView) DrawerLayout(android.support.v4.widget.DrawerLayout) MenuListAdapter(com.example.asus.onlinecanteen.adapter.MenuListAdapter) Cart(com.example.asus.onlinecanteen.model.Cart)

Example 2 with Cart

use of com.example.asus.onlinecanteen.model.Cart in project OnlineCanteen by josephgunawan97.

the class UserOrderProductAdapter method addProduct.

public void addProduct(Product product) {
    if (product == null)
        return;
    if (products == null) {
        products = new ArrayList<>();
    }
    if (orders == null) {
        orders = new ArrayList<>();
    }
    products.add(product);
    orders.add(new Cart(product.getName(), product.getPrice(), 0));
    notifyDataSetChanged();
}
Also used : Cart(com.example.asus.onlinecanteen.model.Cart)

Example 3 with Cart

use of com.example.asus.onlinecanteen.model.Cart in project OnlineCanteen by josephgunawan97.

the class CartActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cart);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    cart = (ArrayList<Cart>) getIntent().getSerializableExtra("Cart");
    cartActivityAdapter = new CartActivityAdapter(this, cart);
    mAuth = FirebaseAuth.getInstance();
    user = mAuth.getCurrentUser();
    walletUtil = new WalletUtil();
    notificationRef = FirebaseDatabase.getInstance().getReference().child("notifications");
    // Calculating the grand total
    for (Cart c : cart) {
        total += c.getTotalPrice(c);
    }
    // Initialize ListView
    cartList = findViewById(R.id.cartList);
    grandTotal = findViewById(R.id.grandTotal);
    deliveryFee = findViewById(R.id.deliveryFee);
    orderButton = findViewById(R.id.OrderButton);
    locationEditText = findViewById(R.id.userLocation);
    // Set views
    grandTotal.setText("TOTAL: Rp " + total);
    deliveryFee.setText("Delivery Fee : Rp ");
    cartList.setAdapter(cartActivityAdapter);
    orderButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (!locationEditText.getText().toString().isEmpty()) {
                // Make new transaction
                walletRef = FirebaseDatabase.getInstance().getReference().child("wallet");
                walletRef.child(user.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {

                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if (dataSnapshot.exists()) {
                            String valueString = dataSnapshot.getValue().toString();
                            int walletCash = Integer.parseInt(valueString);
                            // if wallet money >= total
                            if (walletCash >= total) {
                                ArrayList<PurchasedItem> items = new ArrayList<>();
                                for (Cart c : cart) {
                                    PurchasedItem item = new PurchasedItem(c.getProductName(), c.getProductPrice(), c.getQuantity());
                                    items.add(item);
                                }
                                Intent intent = getIntent();
                                Transaction transaction = new Transaction(intent.getStringExtra("Seller"), FirebaseAuth.getInstance().getUid(), items, locationEditText.getText().toString());
                                TransactionUtil.insert(transaction);
                                Toast.makeText(getApplicationContext(), "Transcation done", Toast.LENGTH_SHORT).show();
                                setResult(RESULT_OK);
                                // Make notification to the seller
                                HashMap<String, String> notificationData = new HashMap<>();
                                notificationData.put("from", transaction.getUid());
                                notificationData.put("type", "new order");
                                notificationRef.child(transaction.getSid()).push().setValue(notificationData);
                                // Go back to main menu
                                finish();
                            } else {
                                Toast.makeText(getApplicationContext(), "Not enough wallet cash, please top-up at our counter", Toast.LENGTH_SHORT).show();
                                finish();
                            }
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                    }
                });
            } else {
                // Alert dialog if the location is not filled in
                AlertDialog.Builder builder = new AlertDialog.Builder(CartActivity.this);
                builder.setMessage("Please enter your location!").setCancelable(false).setNegativeButton("Ok", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });
                AlertDialog alert = builder.create();
                alert.setTitle("Error");
                alert.show();
            }
        }
    });
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) HashMap(java.util.HashMap) DialogInterface(android.content.DialogInterface) ArrayList(java.util.ArrayList) Intent(android.content.Intent) DataSnapshot(com.google.firebase.database.DataSnapshot) View(android.view.View) TextView(android.widget.TextView) ListView(android.widget.ListView) CartActivityAdapter(com.example.asus.onlinecanteen.adapter.CartActivityAdapter) DatabaseError(com.google.firebase.database.DatabaseError) Transaction(com.example.asus.onlinecanteen.model.Transaction) ValueEventListener(com.google.firebase.database.ValueEventListener) WalletUtil(com.example.asus.onlinecanteen.utils.WalletUtil) PurchasedItem(com.example.asus.onlinecanteen.model.PurchasedItem) Cart(com.example.asus.onlinecanteen.model.Cart)

Example 4 with Cart

use of com.example.asus.onlinecanteen.model.Cart in project OnlineCanteen by josephgunawan97.

the class UserOrderProductActivity method onClickPlaceOrder.

@Override
public void onClickPlaceOrder(ArrayList<Cart> carts) {
    // Check empty cart
    boolean emptyCart = true;
    for (Cart c : carts) {
        if (c.getQuantity() != 0) {
            emptyCart = false;
            break;
        }
    }
    if (emptyCart == true) {
        // Alert dialog if there are no items in cart
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("You don't have any items in your cart!").setCancelable(false).setNegativeButton("Ok", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });
        AlertDialog alert = builder.create();
        alert.setTitle("Error");
        alert.show();
    } else {
        // Remove item if qty is 0
        ArrayList<Cart> toRemove = new ArrayList<>();
        for (Cart c : carts) {
            if (c.getQuantity() == 0) {
                toRemove.add(c);
            }
        }
        // Go to cart
        Intent intent = new Intent(this, CartActivity.class);
        ArrayList<Cart> intentCart = new ArrayList<>();
        intentCart.addAll(carts);
        intentCart.removeAll(toRemove);
        intent.putExtra("Cart", intentCart);
        intent.putExtra("Seller", currentStore.getStoreId());
        Log.d(TAG, currentStore.getStoreId());
        intent.putExtra("SellerEmail", currentStore.getEmail());
        startActivityForResult(intent, PLACE_ORDER_CODE);
    }
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) DialogInterface(android.content.DialogInterface) ArrayList(java.util.ArrayList) Intent(android.content.Intent) Cart(com.example.asus.onlinecanteen.model.Cart)

Example 5 with Cart

use of com.example.asus.onlinecanteen.model.Cart in project OnlineCanteen by josephgunawan97.

the class MenuListAdapter method getView.

public View getView(final int position, View view, ViewGroup parent) {
    final OrderHolder holder;
    if (view == null) {
        LayoutInflater inflater = ((Activity) getContext()).getLayoutInflater();
        view = inflater.inflate(R.layout.menu_adapter_list, parent, false);
        holder = new OrderHolder();
        // Initialize views
        holder.txtTitle = view.findViewById(R.id.itemName);
        holder.imageView = view.findViewById(R.id.icon);
        holder.extratxt = view.findViewById(R.id.price);
        holder.seller = view.findViewById(R.id.seller);
        holder.quantityOrder = view.findViewById(R.id.quantityOrder);
        holder.increaseOrder = view.findViewById(R.id.increaseOrder);
        holder.decreaseOrder = view.findViewById(R.id.decreaseOrder);
        holder.increaseOrder.setTag(position);
        view.setTag(holder);
    } else {
        holder = (OrderHolder) view.getTag();
    }
    // Get product
    product = getItem(position);
    // Initialize HashMap for Order Quantity
    if (Order.get(product.getName()) == null) {
        Order.put(product.getName(), 0);
    }
    // Make Array List for Cart
    cartItem = new Cart(product.getName(), product.getPrice(), 0);
    if (!cart.contains(cartItem)) {
        cart.add(cartItem);
    }
    // Set Texts
    holder.txtTitle.setText(product.getName());
    holder.price = product.getPrice();
    holder.extratxt.setText("Rp " + product.getPrice());
    holder.quantityOrder.setText(String.valueOf(Order.get(holder.txtTitle.getText().toString())));
    // Set seller Text
    String id = product.getTokoId();
    storeDatabase = FirebaseDatabase.getInstance().getReference();
    storeDatabase.child("store").child(id).child("storeName").addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot snapshot) {
            value = (String) snapshot.getValue();
            holder.seller.setText("Seller : " + value);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
    // Get image
    if (product.getImageUrl() != null) {
        Glide.with(holder.imageView.getContext()).load(product.getImageUrl()).into(holder.imageView);
    }
    // Increase Order
    holder.increaseOrder.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // Change item's quantity in array list
            int qty = Order.get(holder.txtTitle.getText().toString()) + 1;
            Cart cartItem = new Cart(holder.txtTitle.getText().toString(), holder.price, qty);
            cart.set(position, cartItem);
            // Change views
            Order.put(cartItem.getProductName(), cartItem.getQuantity());
            holder.quantityOrder.setText(String.valueOf(Order.get(cartItem.getProductName())));
        }
    });
    // Decrease Order
    holder.decreaseOrder.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            if (Order.get(holder.txtTitle.getText().toString()) > 0) {
                // Change item's quantity in array list
                int qty = Order.get(holder.txtTitle.getText().toString()) - 1;
                Cart cartItem = new Cart(holder.txtTitle.getText().toString(), holder.price, qty);
                cart.set(position, cartItem);
                // Change views
                Order.put(cartItem.getProductName(), cartItem.getQuantity());
                holder.quantityOrder.setText(String.valueOf(Order.get(cartItem.getProductName())));
            }
        }
    });
    return view;
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) LayoutInflater(android.view.LayoutInflater) Activity(android.app.Activity) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot) ImageView(android.widget.ImageView) TextView(android.widget.TextView) View(android.view.View) Cart(com.example.asus.onlinecanteen.model.Cart)

Aggregations

Cart (com.example.asus.onlinecanteen.model.Cart)6 DialogInterface (android.content.DialogInterface)3 Intent (android.content.Intent)3 AlertDialog (android.support.v7.app.AlertDialog)3 View (android.view.View)3 TextView (android.widget.TextView)3 ArrayList (java.util.ArrayList)3 ListView (android.widget.ListView)2 Product (com.example.asus.onlinecanteen.model.Product)2 DataSnapshot (com.google.firebase.database.DataSnapshot)2 DatabaseError (com.google.firebase.database.DatabaseError)2 ValueEventListener (com.google.firebase.database.ValueEventListener)2 Activity (android.app.Activity)1 NavigationView (android.support.design.widget.NavigationView)1 DrawerLayout (android.support.v4.widget.DrawerLayout)1 ActionBarDrawerToggle (android.support.v7.app.ActionBarDrawerToggle)1 LayoutInflater (android.view.LayoutInflater)1 ImageView (android.widget.ImageView)1 CartActivityAdapter (com.example.asus.onlinecanteen.adapter.CartActivityAdapter)1 MenuListAdapter (com.example.asus.onlinecanteen.adapter.MenuListAdapter)1