Major: Software Engineering
Class: 3rd Year
Name:AFSAN MD RUBAYET
Student number: 189076015
Instructor: 陶陶
School of computer science and technology
May 29, 2021
GROUP LEADER : EDMUND OFORI 189076014
CHAPTER 1 OVERALL DESIGN
APPLICATION ARCHITECTURE
APPLICATION CONFIGURATION
Operating System: Windows 10
Environment: Android Studio.
Database: MySQL
This is the project file :
These are the codes for the signup and Login application :
register.java :
package com.example.ofori;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.ofori.MainActivity;
public class register extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState){
try{
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_register );
DatabaseHelper dbHelper;
dbHelper = new DatabaseHelper( this, "User",null,1);
Button addData = (Button) findViewById( R.id.btn_register);
EditText username=(EditText)findViewById(R.id.et_1);
EditText password=(EditText)findViewById(R.id.et_2);
EditText rpassword=(EditText)findViewById(R.id.et_3);
addData.setOnClickListener( new View.OnClickListener(){
@Override
public void onClick(View v){
try {
if(!(password.getText().toString().equals(rpassword.getText().toString())))
{
Toast.makeText(register.this,"please repeat the same
password",Toast.LENGTH_LONG).show();
return;
}
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.query("User",null,null,null,null,null,"name DESC");
boolean flag=true;
while(cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
if(name.equals(username.getText().toString())) {
flag=false;
Toast.makeText(register.this,"the username has been
registered",Toast.LENGTH_LONG).show();
break;
}
}
if(flag) {
dbHelper.add(username.getText().toString(), password.getText().toString());
Toast.makeText(register.this,"register success!!!",Toast.LENGTH_LONG).show();
Intent intent = new Intent(register.this, MainActivity.class);
startActivity(intent);
}
}
catch (Exception e)
{
System.out.println("错误如下:"+e);
}
}
});}
catch(Exception e)
{
System.out.println("error"+e);
}
}
}
DatabaseHelper.java :
package com.example.ofori;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String CREATE_User = "create table User (name text primary key,password text);";
private Context mContext;
private SQLiteDatabase db;
public DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int
version) {
super(context, name, factory, version);
mContext = context;
try {
db = getReadableDatabase();
}
catch (Exception e)
{
System.out.println("错误如下:"+e);
}
}
public void add(String name,String password ){
db.execSQL("INSERT INTO User(name,password)VALUES(?,?)",new Object[]{name,password});
}
public void onCreate(SQLiteDatabase db) {
Toast.makeText(mContext, "Create succeeded.", Toast.LENGTH_LONG).show();
//调用 SQLiteDatabase 的 execSQL()方法执行建表语句
db.execSQL(CREATE_User);
//弹出一个 Toast 提示创建成
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
activity_register :
<?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=".register">
<EditText
android:id="@+id/et_1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="16sp"
android:textColor="#FFAD33"
android:hint="username"
/>
<EditText
android:id="@+id/et_2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="16sp"
android:textColor="#FFAD33"
android:hint="password"
android:layout_marginTop="15dp"
android:layout_below="@id/et_1"
/>
<EditText
android:id="@+id/et_3"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="16sp"
android:textColor="#FFAD33"
android:hint="Repeat password"
android:layout_marginTop="15dp"
android:layout_below="@id/et_2"
/>
<Button
android:id="@+id/btn_register"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/et_3"
android:text="Register"
android:layout_marginTop="50dp"
/>
</RelativeLayout>
Success.java :
package com.example.ofori;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.View;
public class Success extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_success);
}
}
activity_success :
<?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=".Success">
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="success!!!"
></TextView>
</RelativeLayout>
activity_main:
<?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=".MainActivity">
<EditText
android:id="@+id/et_1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="16sp"
android:textColor="#FFAD33"
android:hint="username"
/>
<EditText
android:id="@+id/et_2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="16sp"
android:textColor="#FFAD33"
android:hint="password"
android:layout_marginTop="15dp"
android:inputType="textPassword"
android:layout_below="@id/et_1"
/>
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/et_2"
android:text="Login"
android:layout_marginTop="50dp"
/>
<Button
android:id="@+id/btn_register"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/btn_login"
android:text="Register"
android:layout_marginTop="50dp"
/>
</RelativeLayout>
AndroidManifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ofori">
<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/Theme.Ofori">
<activity
android:name=".register"
android:label="@string/title_activity_register"
android:theme="@style/Theme.Ofori.NoActionBar" />
<activity
android:name=".Success"
android:label="@string/title_activity_success"
android:theme="@style/Theme.Ofori.NoActionBar" />
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/Theme.Ofori.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java :
package com.example.ofori;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private Button mBtnLogin,mBtnRegister;
private EditText username,password;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String[] user = {null};
final String[] pwd = {null};
username=(EditText)findViewById(R.id.et_1);
password=(EditText)findViewById(R.id.et_2);
mBtnLogin=(Button)findViewById(R.id.btn_login);
mBtnRegister=(Button)findViewById(R.id.btn_register);
DatabaseHelper dbHelper = new DatabaseHelper( this, "User",null,1);
SQLiteDatabase db = dbHelper.getWritableDatabase();
mBtnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor cursor = db.query("User",null,null,null,null,null,"name DESC");
boolean flag=true;
while(cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
String pwd= cursor.getString(cursor.getColumnIndex("password"));
if(name.equals(username.getText().toString())&&pwd.equals(password.getText().toString())) {
flag=false;
Toast.makeText(MainActivity.this,"login
success!!!",Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainActivity.this, Success.class);
startActivity(intent);
}
}
if(flag)
Toast.makeText(MainActivity.this,"user or password is
wrong",Toast.LENGTH_LONG).show();
}
}
);
mBtnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,register.class);
startActivity(intent);
}
});
username.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
user[0] =s.toString();
}
@Override
public void afterTextChanged(Editable s) {
}
});
password.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
pwd[0]=s.toString();
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}