commit 3e062fc73681fe8e1803f235c370f2cc0e16af0b Author: AndrewTrieu Date: Tue Oct 4 12:54:29 2022 +0300 First commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aa724b7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild +.cxx +local.properties diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..fb7f4a8 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..a2d7c21 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,19 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..bdd9278 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,10 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..f455560 --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,39 @@ +plugins { + id 'com.android.application' +} + +android { + namespace 'com.example.getuserinput' + compileSdk 32 + + defaultConfig { + applicationId "com.example.getuserinput" + minSdk 21 + targetSdk 32 + versionCode 1 + versionName "1.0" + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } +} + +dependencies { + + implementation 'androidx.appcompat:appcompat:1.4.1' + implementation 'com.google.android.material:material:1.5.0' + implementation 'androidx.constraintlayout:constraintlayout:2.1.3' + testImplementation 'junit:junit:4.13.2' + androidTestImplementation 'androidx.test.ext:junit:1.1.3' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' +} \ No newline at end of file diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/app/src/androidTest/java/com/example/getuserinput/ExampleInstrumentedTest.java b/app/src/androidTest/java/com/example/getuserinput/ExampleInstrumentedTest.java new file mode 100644 index 0000000..b6340b4 --- /dev/null +++ b/app/src/androidTest/java/com/example/getuserinput/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package com.example.getuserinput; + +import android.content.Context; + +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumented test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); + assertEquals("com.example.getuserinput", appContext.getPackageName()); + } +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..35bda4e --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/example/getuserinput/DatabaseHelper.java b/app/src/main/java/com/example/getuserinput/DatabaseHelper.java new file mode 100644 index 0000000..12f8442 --- /dev/null +++ b/app/src/main/java/com/example/getuserinput/DatabaseHelper.java @@ -0,0 +1,66 @@ +package com.example.getuserinput; + +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; + + +public class DatabaseHelper extends SQLiteOpenHelper{ + public static final String DATABASE_NAME = "data.db"; + public static final String TABLE_NAME = "student"; + public static final String COL1 = "id"; + public static final String COL2 = "name"; + public static final String COL3 = "email"; + public static final String COL4 = "degree"; + + public DatabaseHelper(Context context) { + super(context, DATABASE_NAME, null, 1); + } + + @Override + public void onCreate(SQLiteDatabase db) { + String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, EMAIL TEXT, DEGREE TEXT)"; + db.execSQL(createTable); + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); + onCreate(db); + } + + public boolean addData(String name, String email, String degree) { + SQLiteDatabase db = this.getWritableDatabase(); + ContentValues contentValues = new ContentValues(); + contentValues.put(COL2, name); + contentValues.put(COL3, email); + contentValues.put(COL4, degree); + long result = db.insert(TABLE_NAME, null, contentValues); + + return result != -1; + } + + public Cursor showData() { + SQLiteDatabase db = this.getWritableDatabase(); + Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME, null); + return data; + } + + public boolean updateData(String id, String name, String email, String degree) { + SQLiteDatabase db = this.getWritableDatabase(); + ContentValues contentValues = new ContentValues(); + contentValues.put(COL1, id); + contentValues.put(COL2, name); + contentValues.put(COL3, email); + contentValues.put(COL4, degree); + db.update(TABLE_NAME, contentValues, "ID = ?", new String[] {id}); + return true; + } + + public Integer deleteData(String id) { + SQLiteDatabase db = this.getWritableDatabase(); + return db.delete(TABLE_NAME, "ID = ?", new String[] {id}); + } +} diff --git a/app/src/main/java/com/example/getuserinput/MainActivity.java b/app/src/main/java/com/example/getuserinput/MainActivity.java new file mode 100644 index 0000000..137be83 --- /dev/null +++ b/app/src/main/java/com/example/getuserinput/MainActivity.java @@ -0,0 +1,124 @@ +package com.example.getuserinput; + +import androidx.appcompat.app.AlertDialog; +import androidx.appcompat.app.AppCompatActivity; + +import android.database.Cursor; +import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.Toast; + +public class MainActivity extends AppCompatActivity { + DatabaseHelper database; + Button addData, viewData, updateData, deleteData; + EditText eName, eEmail, eDegree, eID; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + database = new DatabaseHelper(this); + eID = (EditText) findViewById(R.id.inputID); + eName = (EditText) findViewById(R.id.inputName); + eEmail = (EditText) findViewById(R.id.inputEmail); + eDegree = (EditText) findViewById(R.id.inputDegree); + addData = (Button) findViewById(R.id.addBtn); + viewData = (Button) findViewById(R.id.viewBtn); + updateData = (Button) findViewById(R.id.updateBtn); + deleteData = (Button) findViewById(R.id.deleteBtn); + addData(); + viewData(); + updateData(); + deleteData(); + } + + public void addData() { + addData.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + String name = eName.getText().toString(); + String email = eEmail.getText().toString(); + String degree = eDegree.getText().toString(); + + boolean insertData = database.addData(name, email, degree); + + if (insertData) { + Toast.makeText(MainActivity.this, "Successfully inserted", Toast.LENGTH_LONG).show(); + } else { + Toast.makeText(MainActivity.this, "Failed to insert", Toast.LENGTH_LONG).show(); + } + } + }); + } + + public void viewData() { + viewData.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + Cursor data = database.showData(); + + if (data.getCount() == 0) { + display("Error", "No data to display"); + return; + } + StringBuffer buffer = new StringBuffer(); + while (data.moveToNext()) { + buffer.append("ID " + data.getString(0) + "\n"); + buffer.append("Name: " + data.getString(1) + "\n"); + buffer.append("Email: " + data.getString(2) + "\n"); + buffer.append("Degree: " + data.getString(3) + "\n"); + display("All stored data:",buffer.toString()); + } + } + }); + } + + public void display(String title, String message) { + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setCancelable(true); + builder.setTitle(title); + builder.setMessage(message); + builder.show(); + } + + public void updateData() { + updateData.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + int temp = eID.getText().toString().length(); + if (temp > 0) { + boolean update = database.updateData(eID.getText().toString(), eName.getText().toString(), eEmail.getText().toString(), eDegree.getText().toString()); + if (update) { + Toast.makeText(MainActivity.this, "Data updated", Toast.LENGTH_LONG).show(); + } else { + Toast.makeText(MainActivity.this, "Failed to update", Toast.LENGTH_LONG).show(); + } + } else { + Toast.makeText(MainActivity.this, "Please enter ID", Toast.LENGTH_LONG).show(); + } + } + }); + } + + public void deleteData() { + deleteData.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + int temp = eID.getText().toString().length(); + if (temp > 0) { + Integer delete = database.deleteData(eID.getText().toString()); + if (delete > 0) { + Toast.makeText(MainActivity.this, "Data deleted", Toast.LENGTH_LONG).show(); + } else { + Toast.makeText(MainActivity.this, "Failed to delete", Toast.LENGTH_LONG).show(); + } + } else { + Toast.makeText(MainActivity.this, "Please enter ID", Toast.LENGTH_LONG).show(); + } + } + }); + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..2b068d1 --- /dev/null +++ b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..07d5da9 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..c703237 --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,174 @@ + + + + + + + + + + + + + + + + + + + +