How to construct an android application on Shopping Cart using android studio 3.0.1 ?

INTRODUCTION :

Hii all, here we are going to construct an application for Shopping store. The programmer should be very particular that what he or she is wanted to make.So keep an eye on the details to understand this stuff well.

Description :
  • Components Taken: In the given application we will take a switch, a plain text, 4 checkboxes, 4 edit text, 2 text views, a rating bar and a button.
  • Description Of the Components: Switch is taken so that if the switch is closed then the title bar will represent Store Closed, the switch is open then the store will open and the switch is connected to the button which is represented as GO.CheckBoxes is taken for the product mentioned in the application.Edit Text is taken for the quantity to be mentioned and one text view is used to print the bill and the other is used for the rating bar.The button is used to will be work on function given in the main activity.
  • Relative Layout Design:
  • main.xml CODE :
  • <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#44cccc"
        tools:context="com.example.sakshi.shoppingcart.MainActivity">
    
    
        <Switch
            android:id="@+id/s1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginEnd="31dp"
            android:layout_marginRight="31dp"
            android:layout_marginTop="19dp"
            android:text="Store" />
    
        <EditText
            android:id="@+id/t1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/s1"
            android:layout_marginEnd="74dp"
            android:layout_marginRight="74dp"
            android:layout_toLeftOf="@+id/s1"
            android:layout_toStartOf="@+id/s1"
            android:background="#ffffff"
            android:ems="10"
            android:hint="Name"
            android:inputType="textPersonName" />
    
        <CheckBox
            android:id="@+id/cb1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/t1"
            android:layout_marginLeft="18dp"
            android:layout_marginStart="18dp"
            android:layout_marginTop="30dp"
            android:text="Notebook"
            android:textSize="18sp" />
    
        <CheckBox
            android:id="@+id/cb2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/cb1"
            android:layout_alignStart="@+id/cb1"
            android:layout_below="@+id/cb1"
            android:layout_marginTop="15dp"
            android:text="Pen"
            android:textSize="18sp" />
    
        <CheckBox
            android:id="@+id/cb3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/cb2"
            android:layout_alignStart="@+id/cb2"
            android:layout_below="@+id/cb2"
            android:layout_marginTop="18dp"
            android:text="Pencil"
            android:textSize="18sp" />
    
        <CheckBox
            android:id="@+id/cb4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/cb3"
            android:layout_alignStart="@+id/cb3"
            android:layout_below="@+id/cb3"
            android:layout_marginTop="17dp"
            android:text="Eraser"
            android:textSize="18sp" />
    
        <EditText
            android:id="@+id/t2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/cb1"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:background="#ffbb11"
            android:ems="10"
            android:hint="Quantity"
            android:inputType="numberSigned" />
    
        <EditText
            android:id="@+id/t3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/cb3"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:background="#ffbb11"
            android:ems="10"
            android:hint="Quantity"
            android:inputType="numberSigned" />
    
        <EditText
            android:id="@+id/t4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/cb3"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:background="#ffbb11"
            android:ems="10"
            android:hint="Quantity"
            android:inputType="numberSigned" />
    
        <EditText
            android:id="@+id/t5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/cb4"
            android:layout_alignBottom="@+id/cb4"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:background="#ffbb11"
            android:ems="10"
            android:hint="Quantity"
            android:inputType="numberSigned" />
    
        <Button
            android:id="@+id/bttn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/cb4"
            android:layout_marginTop="26dp"
            android:layout_toEndOf="@+id/cb1"
            android:layout_toRightOf="@+id/cb1"
            android:onClick="Shop"
            android:background="#ff1100"
            android:text="GO" />
    
        <TextView
            android:id="@+id/tv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignEnd="@+id/bttn"
            android:layout_alignRight="@+id/bttn"
            android:layout_below="@+id/bttn"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_marginTop="17dp"
            android:background="#ffffff"
            android:text="Bill"
            android:textSize="18sp" />
    
        <RatingBar
            android:id="@+id/rb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv1"
            android:background="#ccccff"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="13dp" />
    
        <TextView
            android:id="@+id/tv2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/rb"
            android:layout_centerHorizontal="true"
            android:layout_alignParentRight="true"
            android:background="#ffffff"
            android:layout_alignParentLeft="true"
            android:layout_marginTop="14dp"
            android:text="Feedback"
            android:textSize="18sp" />
    
    </RelativeLayout>
  • Main Activity:The main strategy on our programming is when the switch is OFF then the user won’t be able to access the application,  so we have used !s1.ischecked moving forward we will give the required condition .And then we will take a class for the rating bar.
  • package com.example.sakshi.shoppingcart;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.EditText;
    import android.widget.RatingBar;
    import android.widget.Switch;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity {
        EditText t1, t2, t3, t4, t5;
        TextView tv1, tv2;
        CheckBox cb1, cb2, cb3, cb4;
        Switch s1;
        RatingBar rb;
        Button bttn;
        int x,y,z,m;
        double sum;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            t1 = (EditText) findViewById(R.id.t1);
            t2 = (EditText) findViewById(R.id.t2);
            t3 = (EditText) findViewById(R.id.t3);
            t4 = (EditText) findViewById(R.id.t4);
            t5 = (EditText) findViewById(R.id.t5);
            tv1 = (TextView) findViewById(R.id.tv1);
            tv2 = (TextView) findViewById(R.id.tv2);
            s1 = (Switch) findViewById(R.id.s1);
            cb1 = (CheckBox) findViewById(R.id.cb1);
            cb2 = (CheckBox) findViewById(R.id.cb2);
            cb3 = (CheckBox) findViewById(R.id.cb3);
            cb4 = (CheckBox) findViewById(R.id.cb4);
            rb = (RatingBar) findViewById(R.id.rb);
            bttn = (Button) findViewById(R.id.bttn);
            rate r=new rate();
            rb.setOnRatingBarChangeListener(r);
        }
        public void Shop(View v) {
            try {
                if (!s1.isChecked()) {
                    setTitle("Store Closed");
                }
                int x=0,y=0,z=0,m=0;
                int a =Utilities.convertToInteger(t2);
                int b = Utilities.convertToInteger(t3);
                int c = Utilities.convertToInteger(t4);
                int d = Utilities.convertToInteger(t5);
                if (s1.isChecked()) {
                    {
                        setTitle("Store OPEN");
                    }
                            if (cb1.isChecked()) {
                                x = 30 * a;
                            } if (cb2.isChecked()) {
                                y = 10 * b;
                            } if (cb3.isChecked()) {
                                z = 20 * c;
                            }  if (cb4.isChecked()) {
                                m = 40 * d;
                            }
                            sum = (x + y + z + m) * 1.0;
                        {
                            if (sum < 200) {
                                tv1.setText("Thank You for Shopping. Your BILL=" + sum);
                            } else if (sum >= 200 && sum < 500) {
                                sum = sum - sum * 0.1;
                                tv1.setText("Congratulations! You got 10% OFF. Your BILL=" + sum);
                            } else if (sum >= 500 && sum < 1000) {
                                sum = sum - sum * 0.2;
                                tv1.setText("Congratulations! you got 20% OFF. Your BILL=" + sum);
                            } else if (sum >= 1000) {
                                sum = sum - sum * 0.3;
                                tv1.setText("Congratulations! you got 30% OFF. Your Bill=" + sum);
                            }
                        }
                    }
                }
    
            catch (Exception ex){
                System.out.println(ex);
                tv1.setText(ex.getMessage());
            }
        }
        class rate implements RatingBar.OnRatingBarChangeListener{
            @Override
            public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
                if(v>=4){
                    tv2.setText("Wow! Your Rating is " + v + " .Glad you like it");
                }
                else
                if(v>=2){
                    tv2.setText("Your rating is " + v + " .Please shop again to check the updation");
                }
                else
                    if(v>=0){
                    tv2.setText("Your rating is " + v + " .Sorry for the inconvenience" );
                    }
            }
        }
    
    }

     

    package com.example.sakshi.shoppingcart;
    
    import android.widget.EditText;
    
    /**
     * Created by Sakshi on 23-11-2017.
     */
    
    public class Utilities {
        public static int convertToInteger(EditText txt)
        {
            try
            {
                int x=Integer.parseInt("" + txt.getText());
                return x;
            }
            catch (Exception ex)
            {
                System.out.println(ex);
                return 0;
            }
        }
    }
  • OUTPUT:
  • THANK YOU!

Leave a Reply