0% found this document useful (0 votes)
5 views10 pages

Ilagan

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

Ilagan

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Ilagan, John Caleb A.

Dueñas, Kim Harold

BSIT-3C2

4/20/24

Point of Sales with SQLite Database


1.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Point of Sale"
android:textSize="24sp"
android:textStyle="bold"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product Category"
android:textSize="18sp"
android:textStyle="bold"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"/>

<Spinner
android:id="@+id/spinnerCategory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product List"
android:textSize="18sp"
android:textStyle="bold"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"/>

<Spinner
android:id="@+id/productList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"/>

<EditText
android:id="@+id/etQuantity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Quantity"
android:inputType="number"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"/>
<TextView
android:id="@+id/txtprice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Price: "
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"/>

<TextView
android:id="@+id/txtTotal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Total: "
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"/>

<Button
android:id="@+id/btnAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"/>

<Button
android:id="@+id/btnViewTransaction"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="View Transaction"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"/>
</LinearLayout>

Output

Java

package com.example.poswithdatabase;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

String[] categories = {"","Foods", "Miscs", "Furniture"};


String[][] products = { {" ", " ", " ", " "},
{"Hotdog", "Chicken", "Canned Food", "Pork"},
{"Drill", "Gluegun", "Scissors", "Bondpaper"},
{"Chair", "Table", "Vase", "Mat"}
};
double[][] prices = { {0.0, 0.0, 0.0, 0.0},
{10.0, 20.0, 30.0, 40.0},
{50.0, 60.0, 70.0, 80.0},
{90.0, 100.0, 110.0, 120.0}
};

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

Spinner spinnerCategory = findViewById(R.id.spinnerCategory);


Spinner spinnerProduct = findViewById(R.id.productList);
TextView txtPrice = findViewById(R.id.txtprice);
TextView txtTotal = findViewById(R.id.txtTotal);
EditText etQuantity = findViewById(R.id.etQuantity);
Button btnAdd = findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String category =
spinnerCategory.getSelectedItem().toString();
String product = spinnerProduct.getSelectedItem().toString();
double price =
prices[spinnerCategory.getSelectedItemPosition()]
[spinnerProduct.getSelectedItemPosition()];
double tax = price * 0.08; // Assuming tax is 8%
double grandTotal = price + tax;

DatabaseHelper dbHelper = new


DatabaseHelper(MainActivity.this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("category", category);
values.put("product_name", product);
values.put("price", price);
values.put("tax", tax);
values.put("grand_total", grandTotal);
db.insert("sales", null, values);
db.close();

Intent intent = new Intent(MainActivity.this, records.class);


startActivity(intent);
}
});

spinnerProduct.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View
selectedItemView, int position, long id) {
double price =
prices[spinnerCategory.getSelectedItemPosition()][position];
txtPrice.setText("Price: ₱" + String.format("%.2f", price));
calculateTotal(price, etQuantity.getText().toString(),
txtTotal);
}

@Override
public void onNothingSelected(AdapterView<?> parentView) {

}
});

ArrayAdapter<String> categoryAdapter = new ArrayAdapter<>(this,


android.R.layout.simple_spinner_item, categories);

categoryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropd
own_item);
spinnerCategory.setAdapter(categoryAdapter);

spinnerCategory.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View
selectedItemView, int position, long id) {
String[] selectedProducts = products[position];
double[] selectedPrices = prices[position];
CustomAdapter newProductAdapter = new
CustomAdapter(MainActivity.this, android.R.layout.simple_spinner_item,
selectedProducts, selectedPrices);

newProductAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dro
pdown_item);
spinnerProduct.setAdapter(newProductAdapter);
}

@Override
public void onNothingSelected(AdapterView<?> parentView) {

}
});

etQuantity.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) {
double price =
prices[spinnerCategory.getSelectedItemPosition()]
[spinnerProduct.getSelectedItemPosition()];
calculateTotal(price, s.toString(), txtTotal);
}

@Override
public void afterTextChanged(Editable s) {

}
});
}

private void calculateTotal(double price, String quantityString, TextView


txtTotal) {
double quantity = 0.0;
if (!TextUtils.isEmpty(quantityString)) {
quantity = Double.parseDouble(quantityString);
}
double total = price * quantity;
txtTotal.setText("Total: ₱" + String.format("%.2f", total));
}
}

2.activity_records
<?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=".records">

<ListView
android:id="@+id/lvRecords"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@android:color/black"
android:dividerHeight="1dp" />

</RelativeLayout>
Output

Java

package com.example.poswithdatabase;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import androidx.appcompat.app.AppCompatActivity;

public class records extends AppCompatActivity {

private static final String TAG = "RecordsActivity";


private DatabaseHelper dbHelper;
private SQLiteDatabase db;

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

dbHelper = new DatabaseHelper(this);


db = dbHelper.getReadableDatabase();

String[] columns = {"_id", "product_name", "grand_total"};


Cursor cursor = db.query("sales", columns, null, null, null, null,
null);

if (cursor != null && cursor.getCount() > 0) {


ListView lvRecords = findViewById(R.id.lvRecords);

SimpleCursorAdapter adapter = new SimpleCursorAdapter(


this,
android.R.layout.simple_list_item_2,
cursor,
new String[]{"product_name", "grand_total"},
new int[]{android.R.id.text1, android.R.id.text2},
0);

lvRecords.setAdapter(adapter);
} else {
Log.d(TAG, "No data found in the cursor.");
}
}

@Override
protected void onDestroy() {
super.onDestroy();
if (db != null) {
db.close();
}
if (dbHelper != null) {
dbHelper.close();
}
}
}

3.DatabaseHelper.java
package com.example.poswithdatabase;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {


private static final String DATABASE_NAME = "pos_database";
private static final int DATABASE_VERSION = 1;

public DatabaseHelper(Context context) {


super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE products (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"category TEXT," +
"product_name TEXT," +
"price REAL)");

db.execSQL("CREATE TABLE sales (" +


"_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"product_name TEXT," +
"price REAL," +
"tax REAL," +
"grand_total REAL," +
"quantity INTEGER)");
}

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

public void insertInitialProducts() {


String[] categories = {"Foods", "Miscs", "Furniture"};
String[][] products = {
{"Hotdog", "Chicken", "Canned Food", "Pork"},
{"Drill", "Gluegun", "Scissors", "Bondpaper"},
{"Chair", "Table", "Vase", "Mat"}
};
double[][] prices = {
{10.0, 20.0, 30.0, 40.0},
{50.0, 60.0, 70.0, 80.0},
{90.0, 100.0, 110.0, 120.0}
};

SQLiteDatabase db = this.getWritableDatabase();

for (int i = 0; i < categories.length; i++) {


for (int j = 0; j < products[i].length; j++) {
ContentValues values = new ContentValues();
values.put("category", categories[i]);
values.put("product_name", products[i][j]);
values.put("price", prices[i][j]);
db.insert("products", null, values);
}
}

db.close();
}
}

4.CustomAdapter.java
package com.example.poswithdatabase;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class CustomAdapter extends ArrayAdapter<String> {

private Context mContext;


private String[] mProducts;
private double[] mPrices;

public CustomAdapter(Context context, int resource, String[] products,


double[] prices) {
super(context, resource, products);
this.mContext = context;
this.mProducts = products;
this.mPrices = prices;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
return createCustomView(position, convertView, parent);
}

@Override
public View getDropDownView(int position, View convertView, ViewGroup
parent) {
return createCustomView(position, convertView, parent);
}

private View createCustomView(int position, View convertView, ViewGroup


parent) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View row = inflater.inflate(android.R.layout.simple_spinner_item,
parent, false);

TextView textView = row.findViewById(android.R.id.text1);


textView.setText(mProducts[position] + " - ₱" + mPrices[position]);

return row;
}
}

You might also like