This commit is contained in:
2023-12-20 10:00:51 +02:00
6 changed files with 166 additions and 35 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; package com.example.myapp_1;
import android.annotation.SuppressLint;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
@@ -11,6 +10,8 @@ import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import com.example.myapp_1.DB.DBOperations;
public class LoginActivity extends AppCompatActivity { public class LoginActivity extends AppCompatActivity {
EditText username; EditText username;
@@ -19,8 +20,9 @@ public class LoginActivity extends AppCompatActivity {
Button login; Button login;
Button signup; Button signup;
Button guestLogin;
DBOperations dbOperations = new DBOperations(this);
Button guestLogin;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@@ -66,11 +68,20 @@ public class LoginActivity extends AppCompatActivity {
if(username_data.isEmpty() || password_data.isEmpty()){ if(username_data.isEmpty() || password_data.isEmpty()){
Toast.makeText(LoginActivity.this,"Please fill your data!", Toast.LENGTH_LONG).show(); Toast.makeText(LoginActivity.this,"Please fill your data!", Toast.LENGTH_LONG).show();
}else{ }else{
// Check for user input if in database if (dbOperations.checkLoginCredentials(username_data, password_data)) {
// if found go to home page // Login successful
// else keep in same page bitch // 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); Intent newScreen = new Intent(getApplicationContext(), HomeActivity.class);
startActivity(newScreen); 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; package com.example.myapp_1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.myapp_1.DB.DBOperations;
public class SigninActivity extends AppCompatActivity { public class SigninActivity extends AppCompatActivity {
EditText username; EditText username;
@@ -18,6 +20,7 @@ public class SigninActivity extends AppCompatActivity {
Button register; Button register;
DBOperations dbOperations = new DBOperations(this);
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@@ -32,37 +35,46 @@ public class SigninActivity extends AppCompatActivity {
register.setOnClickListener(new View.OnClickListener() { register.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
String username_data = username.getText().toString(); String username_data = username.getText().toString();
String phonenumber_data = username.getText().toString(); String phonenumber_data = phonenumber.getText().toString(); // Corrected here
String email_data = username.getText().toString(); String email_data = email.getText().toString(); // Corrected here
String password_data = username.getText().toString(); String password_data = password.getText().toString(); // Corrected here
boolean checked = false; 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[0] = username_data;
data[1] = phonenumber_data; data[1] = phonenumber_data;
data[2] = email_data; data[2] = email_data;
data[3] = password_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(); Toast.makeText(SigninActivity.this, "Please fill your data!", Toast.LENGTH_LONG).show();
checked = false; checked = false;
break; break;
}else{ } else {
checked = true; 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); Intent newScreen = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(newScreen); startActivity(newScreen);
} }
} }
}); });
} }
} }

View File

@@ -4,7 +4,7 @@
-- last_name => User's Last Name -- last_name => User's Last Name
-- U_email => User's Registered Email -- U_email => User's Registered Email
-- U_password => User's Password -- U_password => User's Password
CREATE TABLE users(U_ID SERIAL PRIMARY KEY, CREATE TABLE users(u_id SERIAL PRIMARY KEY,
first_name varchar(255) NOT NULL, first_name varchar(255) NOT NULL,
last_name varchar(255) NOT NULL, last_name varchar(255) NOT NULL,
U_email varchar(255) NOT NULL UNIQUE, U_email varchar(255) NOT NULL UNIQUE,
@@ -54,12 +54,13 @@ CREATE TABLE prefers(pref_UID SERIAL,
-- time_of_day => Time object, nanosecond accuracy, y'all handle it idk -- time_of_day => Time object, nanosecond accuracy, y'all handle it idk
-- week_day => varchar as {Mon, Tue, Wed}, Could probs just encode with 0 -> 6 -- week_day => varchar as {Mon, Tue, Wed}, Could probs just encode with 0 -> 6
-- tram_line => int ranging 1 -> 4 -- tram_line => int ranging 1 -> 4
-- fill_status => Boolean, 1 full, 0 not as full -- direction => Boolean, 1 full, 0 not as full
CREATE TABLE arrival(time_of_day TIME, CREATE TABLE arrivals(
a_id SERIAL PRIMARY KEY,
time_of_day TIME,
week_day INTEGER, week_day INTEGER,
tram_line INTEGER, tram_line INTEGER,
fill_status BOOLEAN direction BOOLEAN,
CONSTRAINT ck PRIMARY KEY (week_day,tram_line) CONSTRAINT in_week CHECK (week_day >= 0 AND week_day <= 6),
CONSTRAINT in_week CHECK (week_day >= 0 AND week_day <= 6)
CONSTRAINT valid_line CHECK (tram_line >= 1 AND tram_line <= 4) CONSTRAINT valid_line CHECK (tram_line >= 1 AND tram_line <= 4)
); );

View File

@@ -1,8 +0,0 @@
## This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# 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 20 09:27:23 EET 2023
sdk.dir=C\:\\Users\\ahmed\\AppData\\Local\\Android\\Sdk