Code: Part2

MainActivity.java

package org.meicode.meibank;

import androidx.appcompat.app.AppCompatActivity;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;

import org.meicode.meibank.Database.DatabaseHelper;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = “MainActivity”;

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

        DatabaseHelper databaseHelper = new DatabaseHelper(this);
        SQLiteDatabase db = databaseHelper.getReadableDatabase();
        Cursor cursor = db.query(“items”, null, null, null, null, null, null);
        if (null != cursor) {
            if (cursor.moveToFirst()) {
                Log.d(TAG, “onCreate: name: ” + cursor.getString(cursor.getColumnIndex(“name”)));
            }
        }
    }
}

DatabaseHelper.java

package org.meicode.meibank.Database;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String TAG = “DatabaseHelper”;

    private static final String DB_NAME = “fb_mei_bank”;
    private static final int DB_VERSION = 1;

    public DatabaseHelper(@Nullable Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        Log.d(TAG, “onCreate: started”);
        String createUserTable = “CREATE TABLE users (_id INTEGER PRIMARY KEY AUTOINCREMENT, email TEXT NOT NULL, ” +
                “password TEXT NOT NULL, ” +
                “first_name TEXT, last_name TEXT, address TEXT, image_url TEXT, remained_amount DOUBLE)”;

        String createShoppingTable = “CREATE TABLE shopping (_id INTEGER PRIMARY KEY AUTOINCREMENT, item_id INTEGER, ” +
                “user_id INTEGER, transaction_id INTEGER, price DOUBLE, date DATE, description TEXT)”;


        String createInvestmentTable = “CREATE TABLE investments (_id INTEGER PRIMARY KEY AUTOINCREMENT, amount DOUBLE, ” +
                “monthly_roi DOUBLE, name TEXT, init_date DATE, finish_date DATE, user_id INTEGER, transaction_id INTEGER)”;

        String createLoansTable  = “CREATE TABLE loans (_id INTEGER PRIMARY KEY AUTOINCREMENT, init_date DATE, ” +
                “finish_date DATE, init_amount DOUBLE, remained_amount DOUBLE, monthly_payment DOUBLE, monthly_roi DOUBLE,” +
                “name TEXT, user_id INTEGER)”;

        String createTransactionTable = “CREATE TABLE transactions (_id INTEGER PRIMARY KEY AUTOINCREMENT, amount double, ” +
                “date DATE, type TEXT, user_id INTEGER, recipient TEXT, description TEXT)”;

        String createItemsTable = “CREATE TABLE items (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, image_url TEXT,” +
                “description TEXT)”;

        sqLiteDatabase.execSQL(createUserTable);
        sqLiteDatabase.execSQL(createShoppingTable);
        sqLiteDatabase.execSQL(createInvestmentTable);
        sqLiteDatabase.execSQL(createLoansTable);
        sqLiteDatabase.execSQL(createTransactionTable);
        sqLiteDatabase.execSQL(createItemsTable);

        addInitialItems(sqLiteDatabase);
    }

    private void addInitialItems (SQLiteDatabase db) {
        Log.d(TAG, “addInitialItems: started”);
        ContentValues values = new ContentValues();
        values.put(“name”, “Bike”);
        values.put(“image_url”, “https://cdn.shopify.com/s/files/1/0903/4494/products/Smashing-Pumpkin-GX-Eagle-complete-front-white.jpg”);
        values.put(“description”, “The perfect mountain bike”);

        db.insert(“items”, null, values);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}

RegisterActivit.java

package org.meicode.meibank.Authentication;

import androidx.appcompat.app.AppCompatActivity;

import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import org.meicode.meibank.Database.DatabaseHelper;
import org.meicode.meibank.R;

public class RegisterActivity extends AppCompatActivity {
    private static final String TAG = “RegisterActivity”;

    private EditText edtTxtEmail, edtTxtPassword, edtTxtAddress, edtTxtName;
    private TextView txtWarning, txtLogin, txtLicence;
    private ImageView firstImage, secondImage, thirdImage, forthImage, fifthImage;
    private Button btnRegister;

    private DatabaseHelper databaseHelper;

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

        initViews();

        databaseHelper = new DatabaseHelper(this);

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initRegister();
            }
        });
    }

    private void initRegister() {
        Log.d(TAG, “initRegister: started”);
        String email = edtTxtEmail.getText().toString();
        String password = edtTxtPassword.getText().toString();

        if (email.equals(“”) || password.equals(“”)) {
            txtWarning.setVisibility(View.VISIBLE);
            txtWarning.setText(“Please enter the password and Email”);
        }else {
            txtWarning.setVisibility(View.GONE);


        }
    }

    private class DoesUserExist extends AsyncTask<String, Void, Boolean> {
        @Override
        protected Boolean doInBackground(String… strings) {
            try{
                SQLiteDatabase db = databaseHelper.getReadableDatabase();
                Cursor cursor = db.query(“users”, new String[] {“_id”, “email”}, “email=?”,
                        new String[] {strings[0]}, null, null, null);
                if (null != cursor) {
                    if (cursor.moveToFirst()) {
                        if (cursor.getString(cursor.getColumnIndex(“email”)).equals(strings[0])) {
                            cursor.close();
                            db.close();
                            return true;
                        }else {
                            cursor.close();
                            db.close();
                            return false;
                        }
                    }else {
                        cursor.close();
                        db.close();
                        return false;
                    }
                }else {
                    db.close();
                    return true;
                }
            }catch (SQLException e) {
                e.printStackTrace();

                return true;
            }
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);

            if (aBoolean) {
                txtWarning.setVisibility(View.VISIBLE);
                txtWarning.setText(“There is user with this email, please try another email”);
            }else {
                txtWarning.setVisibility(View.GONE);

            }
        }
    }

    private void initViews() {
        Log.d(TAG, “initViews: called”);

        edtTxtEmail = (EditText) findViewById(R.id.edtTxtEmail);
        edtTxtPassword = (EditText) findViewById(R.id.edtTxtPassword);
        edtTxtAddress = (EditText) findViewById(R.id.edtTxtAddress);
        edtTxtName = (EditText) findViewById(R.id.edtTxtName);

        txtWarning = (TextView) findViewById(R.id.txtWarning);
        txtLogin  = (TextView) findViewById(R.id.txtLogin);
        txtLicence = (TextView) findViewById(R.id.txtLicense);

        firstImage = (ImageView) findViewById(R.id.firstImage);
        secondImage = (ImageView) findViewById(R.id.secondImage);
        thirdImage = (ImageView) findViewById(R.id.thirdImage);
        forthImage = (ImageView) findViewById(R.id.forthImage);
        fifthImage = (ImageView) findViewById(R.id.fifthImage);

        btnRegister = (Button) findViewById(R.id.btnRegister);
    }
}

activity_main.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<androidx.constraintlayout.widget.ConstraintLayout 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”
tools:context=”.MainActivity”>

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Hello World!”
app:layout_constraintBottom_toBottomOf=”parent”
app:layout_constraintLeft_toLeftOf=”parent”
app:layout_constraintRight_toRightOf=”parent”
app:layout_constraintTop_toTopOf=”parent” />

</androidx.constraintlayout.widget.ConstraintLayout>


activity_register.xml

<?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”
    tools:context=”.Authentication.RegisterActivity”>

    <RelativeLayout
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:layout_centerInParent=”true”>

        <Button
            android:id=”@+id/btnRegister”
            android:layout_width=”wrap_content”
            android:layout_height=”wrap_content”
            android:layout_below=”@+id/imagesRelLayout”
            android:layout_centerHorizontal=”true”
            android:layout_marginTop=”20dp”
            android:background=”@color/orange”
            android:text=”Register” />

        <EditText
            android:id=”@+id/edtTxtPassword”
            android:layout_width=”match_parent”
            android:layout_height=”wrap_content”
            android:layout_below=”@+id/edtTxtEmail”
            android:layout_marginLeft=”30dp”
            android:layout_marginTop=”10dp”
            android:layout_marginRight=”30dp”
            android:hint=”Password”
            android:inputType=”textPassword” />

        <EditText
            android:id=”@+id/edtTxtName”
            android:layout_width=”match_parent”
            android:layout_height=”wrap_content”
            android:layout_below=”@+id/edtTxtPassword”
            android:layout_marginLeft=”30dp”
            android:layout_marginTop=”10dp”
            android:layout_marginRight=”30dp”
            android:hint=”Name” />

        <EditText
            android:id=”@+id/edtTxtAddress”
            android:layout_width=”match_parent”
            android:layout_height=”wrap_content”
            android:layout_below=”@+id/edtTxtName”
            android:layout_marginLeft=”30dp”
            android:layout_marginTop=”10dp”
            android:layout_marginRight=”30dp”
            android:hint=”Address”
            android:lines=”2″ />

        <EditText
            android:id=”@+id/edtTxtEmail”
            android:layout_width=”match_parent”
            android:layout_height=”wrap_content”
            android:layout_below=”@+id/logo”
            android:layout_marginLeft=”30dp”
            android:layout_marginTop=”50dp”
            android:layout_marginRight=”30dp”
            android:hint=”Email”
            android:inputType=”textEmailAddress” />

        <ImageView
            android:id=”@+id/logo”
            android:layout_width=”100dp”
            android:layout_height=”50dp”
            android:layout_centerHorizontal=”true”
            android:src=”@mipmap/meicode” />

        <RelativeLayout
            android:id=”@+id/imagesRelLayout”
            android:layout_width=”wrap_content”
            android:layout_height=”wrap_content”
            android:layout_below=”@+id/edtTxtAddress”
            android:layout_centerHorizontal=”true”
            android:layout_marginTop=”10dp”>

            <ImageView
                android:id=”@+id/firstImage”
                android:layout_width=”50dp”
                android:layout_height=”50dp”
                android:src=”@mipmap/boy” />

            <ImageView
                android:id=”@+id/secondImage”
                android:layout_width=”50dp”
                android:layout_height=”50dp”
                android:layout_marginLeft=”10dp”
                android:layout_toRightOf=”@+id/firstImage”
                android:src=”@mipmap/girl” />

            <ImageView
                android:id=”@+id/thirdImage”
                android:layout_width=”50dp”
                android:layout_height=”50dp”
                android:layout_marginLeft=”10dp”
                android:layout_toRightOf=”@+id/secondImage”
                android:src=”@mipmap/man” />

            <ImageView
                android:id=”@+id/forthImage”
                android:layout_width=”50dp”
                android:layout_height=”50dp”
                android:layout_marginLeft=”10dp”
                android:layout_toRightOf=”@+id/thirdImage”
                android:src=”@mipmap/woman” />

            <ImageView
                android:id=”@+id/fifthImage”
                android:layout_width=”50dp”
                android:layout_height=”50dp”
                android:layout_marginLeft=”10dp”
                android:layout_toRightOf=”@+id/forthImage”
                android:src=”@mipmap/secon_girl” />

        </RelativeLayout>

        <TextView
            android:id=”@+id/txtWarning”
            android:layout_width=”wrap_content”
            android:layout_height=”wrap_content”
            android:layout_below=”@+id/btnRegister”
            android:layout_centerHorizontal=”true”
            android:layout_marginTop=”20dp”
            android:text=”Warning”
            android:textColor=”@color/colorAccent”
            android:visibility=”gone” />

        <TextView
            android:id=”@+id/txtLogin”
            android:layout_width=”wrap_content”
            android:layout_height=”wrap_content”
            android:layout_below=”@+id/txtWarning”
            android:layout_centerHorizontal=”true”
            android:layout_marginTop=”50dp”
            android:text=”Have an account? login from here”
            android:textColor=”@color/blue” />

    </RelativeLayout>

    <TextView
        android:id=”@+id/txtLicense”
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:layout_alignParentBottom=”true”
        android:layout_centerHorizontal=”true”
        android:layout_marginBottom=”10dp”
        android:text=”Developed By Meisam at meiCode.org”
        android:textStyle=”italic” />

</RelativeLayout>


AndroidManifest.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
    package=”org.meicode.meibank”>

    <application
        android:allowBackup=”true”
        android:icon=”@mipmap/ic_launcher”
        android:label=”@string/app_name”
        android:roundIcon=”@mipmap/ic_launcher_round”
        android:supportsRtl=”true”
        android:theme=”@style/AppTheme”>
        <activity android:name=”.Authentication.RegisterActivity”></activity>
        <activity android:name=”.MainActivity”>
            <intent-filter>
                <action android:name=”android.intent.action.MAIN” />

                <category android:name=”android.intent.category.LAUNCHER” />
            </intent-filter>
        </activity>
    </application>

</manifest>


Subscribe To Our Newsletter
Get updates and learn from the best

Leave a Reply

Your email address will not be published. Required fields are marked *

Andriod Course

Table of Contents