Usertable,login,signup

This commit is contained in:
AhmedmostafaElabdli1
2023-12-10 14:57:12 +02:00
parent ee3c28dbf1
commit 08cbd352b3
5 changed files with 159 additions and 22 deletions

View File

@@ -0,0 +1,37 @@
package com.example.myapp_1.DB;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "WITL.db";
private static final int DATABASE_VERSION = 1;
private static final String CREATE_USERS_TABLE =
"CREATE TABLE users (" +
"U_ID INTEGER PRIMARY KEY AUTOINCREMENT," +
"UserName TEXT NOT NULL," +
"phone TEXT CHECK(length(phone) = 11)," +
"U_email TEXT NOT NULL UNIQUE," +
"U_password TEXT NOT NULL);";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// Create your tables here
db.execSQL(CREATE_USERS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Handle database upgrades if needed
db.execSQL("DROP TABLE IF EXISTS users");
onCreate(db);
}
}

View File

@@ -0,0 +1,78 @@
package com.example.myapp_1.DB;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class DBOperations {
private DBHelper dbHelper;
public DBOperations(Context context) {
dbHelper = new DBHelper(context);
}
// Insert user data into the users table
public long insertUser(String UserName, String phone,String email, String password) {
try {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("UserName", UserName);
values.put("phone", phone);
values.put("U_email", email);
values.put("U_password", password);
long newRowId = db.insert("users", null, values);
return newRowId;
}
catch (Exception e){
return 0;
}
}
public boolean checkLoginCredentials(String UserName, String password) {
SQLiteDatabase db = dbHelper.getReadableDatabase();
String[] columns = {"U_ID"};
String selection = "UserName=? AND U_password=?";
String[] selectionArgs = {UserName, password};
Cursor cursor = db.query("users", columns, selection, selectionArgs, null, null, null);
boolean isCredentialsValid = cursor.moveToFirst();
cursor.close();
return isCredentialsValid;
}
// Add other methods as needed
}
// Retrieve all users from the users table
// public Cursor getAllUsers() {
// SQLiteDatabase db = dbHelper.getReadableDatabase();
//
// String[] columns = {"U_ID", "first_name", "last_name", "U_email", "phone", "U_password"};
// return db.query("users", columns, null, null, null, null, null);
// }
// Delete a user from the users table based on their email
// public int deleteUser(String email) {
// SQLiteDatabase db = dbHelper.getWritableDatabase();
//
// String selection = "U_email=?";
// String[] selectionArgs = {email};
//
// int deletedRows = db.delete("users", selection, selectionArgs);
// db.close();
//
// return deletedRows;
// }

View File

@@ -1,6 +1,5 @@
package com.example.myapp_1;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
@@ -11,6 +10,8 @@ import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.myapp_1.DB.DBOperations;
public class LoginActivity extends AppCompatActivity {
EditText username;
@@ -18,7 +19,7 @@ public class LoginActivity extends AppCompatActivity {
TextView forgetpassword;
Button login;
Button signup;
DBOperations dbOperations = new DBOperations(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -54,11 +55,20 @@ public class LoginActivity extends AppCompatActivity {
if(username_data.isEmpty() || password_data.isEmpty()){
Toast.makeText(LoginActivity.this,"Please fill your data!", Toast.LENGTH_LONG).show();
}else{
// Check for user input if in database
// if found go to home page
// else keep in same page bitch
Intent newScreen = new Intent(getApplicationContext(), HomeActivity.class);
startActivity(newScreen);
if (dbOperations.checkLoginCredentials(username_data, password_data)) {
// Login successful
// Proceed to the next screen or perform other actions
Toast.makeText(LoginActivity.this,"Welcome", Toast.LENGTH_LONG).show();
Intent newScreen = new Intent(getApplicationContext(), HomeActivity.class);
startActivity(newScreen);
} else {
// Login failed
Toast.makeText(LoginActivity.this,"Invalid Iformation", Toast.LENGTH_LONG).show();
}
}
}

View File

@@ -1,14 +1,16 @@
package com.example.myapp_1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.myapp_1.DB.DBOperations;
public class SigninActivity extends AppCompatActivity {
EditText username;
@@ -18,6 +20,7 @@ public class SigninActivity extends AppCompatActivity {
Button register;
DBOperations dbOperations = new DBOperations(this);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -32,37 +35,46 @@ public class SigninActivity extends AppCompatActivity {
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username_data = username.getText().toString();
String phonenumber_data = username.getText().toString();
String email_data = username.getText().toString();
String password_data = username.getText().toString();
String phonenumber_data = phonenumber.getText().toString(); // Corrected here
String email_data = email.getText().toString(); // Corrected here
String password_data = password.getText().toString(); // Corrected here
boolean checked = false;
//Add the user to the database
//betengano base
String [] data = new String[4];
String[] data = new String[4];
data[0] = username_data;
data[1] = phonenumber_data;
data[2] = email_data;
data[3] = password_data;
for(int i = 0; i < data.length; i++){
if(data[i].isEmpty()){
for (int i = 0; i < data.length; i++) {
if (data[i].isEmpty()) {
Toast.makeText(SigninActivity.this, "Please fill your data!", Toast.LENGTH_LONG).show();
checked = false;
break;
}else{
} else {
checked = true;
}
}
if(checked == true){
if (checked) {
long newRowId = dbOperations.insertUser(data[0], data[1], data[2], data[3]);
if (newRowId != -1) {
Toast.makeText(SigninActivity.this, "SignUp Success", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(SigninActivity.this, "SignUp failed" + newRowId, Toast.LENGTH_LONG).show();
}
Intent newScreen = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(newScreen);
}
}
});
}
}

View File

@@ -4,5 +4,5 @@
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
#Wed Dec 06 17:47:01 EET 2023
sdk.dir=C\:\\Users\\Beast\\AppData\\Local\\Android\\Sdk
#Sun Dec 10 14:55:12 EET 2023
sdk.dir=C\:\\Users\\ahmed\\AppData\\Local\\Android\\Sdk