Andriod_Slip_7B

1. Create the layout file activity_main.xml):
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
    android:id="@+id/etName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Name"/>

    <EditText
    android:id="@+id/etAddress"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/etName"
    android:layout_marginTop="16dp"
    android:hint="Address"/>

    <EditText
    android:id="@+id/etPhno"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/etAddress"
    android:layout_marginTop="16dp"
    android:inputType="phone"
    android:hint="Phone Number"/>

    <Button
    android:id="@+id/btnInsert"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/etPhno"
    android:layout_marginTop="16dp"
    android:text="Insert"/>

    <Button
    android:id="@+id/btnShowDetails"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/btnInsert"
    android:layout_marginTop="16dp"
    android:text="Show Details"/>
    </RelativeLayout>


2. Customer Class:
    public class Customer {
    private int id;
    private String name;
    private String address;
    private String phno;

    public Customer() {
    // Default constructor
    }

    // Constructors, getters, and setters
    }


3. Database Helper Class:
    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.widget.Toast;

    import java.util.ArrayList;
    import java.util.List;

    public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "customer_db";
    private static final int DATABASE_VERSION = 1;

    private static final String TABLE_CUSTOMER = "customer";
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
    private static final String KEY_ADDRESS = "address";
    private static final String KEY_PHNO = "phno";

    public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    String CREATE_CUSTOMER_TABLE = "CREATE TABLE " + TABLE_CUSTOMER + "("
    + KEY_ID + " INTEGER PRIMARY KEY,"
    + KEY_NAME + " TEXT,"
    + KEY_ADDRESS + " TEXT,"
    + KEY_PHNO + " TEXT)";
    db.execSQL(CREATE_CUSTOMER_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CUSTOMER);
    onCreate(db);
    }

    public void insertCustomer(Customer customer) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_NAME, customer.getName());
    values.put(KEY_ADDRESS, customer.getAddress());
    values.put(KEY_PHNO, customer.getPhno());

    db.insert(TABLE_CUSTOMER, null, values);
    db.close();
    }

    public List <Customer> getAllCustomers() {
    List<Customer> customerList = new ArrayList<>();
    String selectQuery = "SELECT * FROM " + TABLE_CUSTOMER;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
    do {
    Customer customer = new Customer();
    customer.setId(cursor.getInt(0));
    customer.setName(cursor.getString(1));
    customer.setAddress(cursor.getString(2));
    customer.setPhno(cursor.getString(3));

    customerList.add(customer);
    } while (cursor.moveToNext());
    }

    cursor.close();
    return customerList;
    }
    }


4. MainActivity Class::
    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;

    public class MainActivity extends AppCompatActivity {
    private EditText etName, etAddress, etPhno;
    private Button btnInsert, btnShowDetails;
    private DatabaseHelper databaseHelper;

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

    etName = findViewById(R.id.etName);
    etAddress = findViewById(R.id.etAddress);
    etPhno = findViewById(R.id.etPhno);
    btnInsert = findViewById(R.id.btnInsert);
    btnShowDetails = findViewById(R.id.btnShowDetails);

    databaseHelper = new DatabaseHelper(this);

    btnInsert.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    insertCustomer();
    }
    });

    btnShowDetails.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    showCustomerDetails();
    }
    });
    }

    private void insertCustomer() {
    String name = etName.getText().toString().trim();
    String address = etAddress.getText().toString().trim();
    String phno = etPhno.getText().toString().trim();

    if (!name.isEmpty() && !address.isEmpty() && !phno.isEmpty()) {
    Customer customer = new Customer();
    customer.setName(name);
    customer.setAddress(address);
    customer.setPhno(phno);

    databaseHelper.insertCustomer(customer);

    Toast.makeText(this, "Customer details inserted", Toast.LENGTH_SHORT).show();
    } else {
    Toast.makeText(this, "Please fill in all fields", Toast.LENGTH_SHORT).show();
    }
    }

    private void showCustomerDetails() {
    List<Customer> customerList = databaseHelper.getAllCustomers();

    if (customerList.isEmpty()) {
    Toast.makeText(this, "No customer details available", Toast.LENGTH_SHORT).show();
    } else {
    StringBuilder details = new StringBuilder("Customer Details:\n");

    for (Customer customer : customerList) {
    details.append("ID: ").append(customer.getId()).append("\n");
    details.append("Name: ").append(customer.getName()).append("\n");
    details.append("Address: ").append(customer.getAddress()).append("\n");
    details.append("Phone Number: ").append(customer.getPhno()).append("\n\n");
    }

    Toast.makeText(this, details.toString(), Toast.LENGTH_LONG).show();
    }
    }
    }

No comments:

Post a Comment