MainActivity.java:

package com.example.appone;

import android.database.Cursor;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    EditText etBookId, etBookName, etPublisher, etAuthor;
    EditText etPublishedYear, etPrice, etStock;
    Button btnAdd, btnView;
    TextView tvDetails;

    DatabaseHelper databaseHelper;

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

        etBookId = findViewById(R.id.etBookId);
        etBookName = findViewById(R.id.etBookName);
        etPublisher = findViewById(R.id.etPublisher);
        etAuthor = findViewById(R.id.etAuthor);
        etPublishedYear = findViewById(R.id.etPublishedYear);
        etPrice = findViewById(R.id.etPrice);
        etStock = findViewById(R.id.etStock);

        btnAdd = findViewById(R.id.btnAdd);
        btnView = findViewById(R.id.btnView);
        tvDetails = findViewById(R.id.tvDetails);

        databaseHelper = new DatabaseHelper(this);


        btnAdd.setOnClickListener(v -> {

            String bookIdText = etBookId.getText().toString().trim();
            String bookName = etBookName.getText().toString().trim();
            String publisher = etPublisher.getText().toString().trim();
            String author = etAuthor.getText().toString().trim();
            String yearText = etPublishedYear.getText().toString().trim();
            String priceText = etPrice.getText().toString().trim();
            String stockText = etStock.getText().toString().trim();

            if (bookIdText.isEmpty()) {
                etBookId.setError("Enter book ID");
                etBookId.requestFocus();
                return;
            }

            if (bookName.isEmpty()) {
                etBookName.setError("Enter book name");
                etBookName.requestFocus();
                return;
            }

            if (publisher.isEmpty()) {
                etPublisher.setError("Enter publisher");
                etPublisher.requestFocus();
                return;
            }

            if (author.isEmpty()) {
                etAuthor.setError("Enter author");
                etAuthor.requestFocus();
                return;
            }

            if (yearText.isEmpty()) {
                etPublishedYear.setError("Enter published year");
                etPublishedYear.requestFocus();
                return;
            }

            if (priceText.isEmpty()) {
                etPrice.setError("Enter price");
                etPrice.requestFocus();
                return;
            }



            if (stockText.isEmpty()) {
                etStock.setError("Enter stock");
                etStock.requestFocus();
                return;
            }

            int bookId;
            int publishedYear;
            double price;
            int stock;

            // FIX: guard against overflow / invalid numbers
            try {
                bookId = Integer.parseInt(bookIdText);
                publishedYear = Integer.parseInt(yearText);
                price = Double.parseDouble(priceText);
                stock = Integer.parseInt(stockText);
            } catch (NumberFormatException e) {
                Toast.makeText(this, "Please enter valid numbers",
                        Toast.LENGTH_SHORT).show();
                return;
            }

            boolean inserted = databaseHelper.insertBook(
                    bookId,
                    bookName,
                    publisher,
                    author,
                    publishedYear,
                    price,
                    stock
            );

            if (inserted) {
                Toast.makeText(this, "Book added successfully",
Toast.LENGTH_SHORT).show();

                etBookId.setText("");
                etBookName.setText("");
                etPublisher.setText("");
                etAuthor.setText("");
                etPublishedYear.setText("");
                etPrice.setText("");
                etStock.setText("");
            } else {
                Toast.makeText(this, "Book insertion failed (Book ID may already exist)", Toast.LENGTH_SHORT).show();
            }
        });

        btnView.setOnClickListener(v -> {

            Cursor cursor = databaseHelper.getAllBooks();

            if (cursor.getCount() == 0) {
                tvDetails.setText("No book details found");
                cursor.close();
                return;
            }


            StringBuilder details = new StringBuilder();

            while (cursor.moveToNext()) {

                int bookId = cursor.getInt(0);
                String bookName = cursor.getString(1);
                String publisher = cursor.getString(2);
                String author = cursor.getString(3);
                int publishedYear = cursor.getInt(4);
                double price = cursor.getDouble(5);
                int stock = cursor.getInt(6);

                details.append("Book ID : ").append(bookId).append("\n");
                details.append("Book Name : ").append(bookName).append("\n");
                details.append("Publisher : ").append(publisher).append("\n");
                details.append("Author : ").append(author).append("\n");
                details.append("Published Year : ").append(publishedYear).append("\n");
                details.append("Price : ₹").append(price).append("\n");
                details.append("Stock : ").append(stock).append("\n");
                details.append("------------------------------\n");
            }
            cursor.close();
            tvDetails.setText(details.toString());
        });
    }
}


activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="20dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="LIBRARY MANAGEMENT"
            android:textSize="26sp"

            android:textStyle="bold"
            android:textColor="?android:attr/textColorPrimary"
            android:gravity="center"
            android:layout_marginBottom="25dp" />

        <EditText
            android:id="@+id/etBookId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Book ID"
            android:inputType="number" />

        <EditText
            android:id="@+id/etBookName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Book Name"
            android:inputType="text"
            android:layout_marginTop="10dp" />

        <EditText
            android:id="@+id/etPublisher"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Publisher"
            android:inputType="text"
            android:layout_marginTop="10dp" />

        <EditText
            android:id="@+id/etAuthor"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Author"
            android:inputType="text"
            android:layout_marginTop="10dp" />

        <EditText
            android:id="@+id/etPublishedYear"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Published Year"
            android:inputType="number"
            android:layout_marginTop="10dp" />

        <EditText
            android:id="@+id/etPrice"


            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Price"
            android:inputType="numberDecimal"
            android:layout_marginTop="10dp" />

        <EditText
            android:id="@+id/etStock"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Stock"
            android:inputType="number"
            android:layout_marginTop="10dp" />

        <Button
            android:id="@+id/btnAdd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="ADD BOOK"
            android:layout_marginTop="25dp" />

        <Button
            android:id="@+id/btnView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="VIEW DETAILS"
            android:layout_marginTop="10dp" />

        <TextView
            android:id="@+id/tvDetails"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="17sp"
            android:padding="10dp"
            android:layout_marginTop="20dp" />

    </LinearLayout>
</ScrollView>


DatabaseHelper.java:

package com.example.appone;

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 DatabaseHelper(Context context) {
        super(context, "LibraryDB", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE books (" +
                "book_id INTEGER PRIMARY KEY, " +
                "book_name TEXT, " +
                "publisher TEXT, " +
                "author TEXT, " +
                "published_year INTEGER, " +
                "price REAL, " +
                "stock INTEGER)");
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS books");
        onCreate(db);
    }
    public boolean insertBook(int bookId, String bookName, String publisher,
                              String author, int publishedYear,
                              double price, int stock) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("book_id", bookId);
        values.put("book_name", bookName);
        values.put("publisher", publisher);
        values.put("author", author);
        values.put("published_year", publishedYear);
        values.put("price", price);
        values.put("stock", stock);
        long result = db.insert("books", null, values);
        return result != -1;
    }
    public Cursor getAllBooks() {
        SQLiteDatabase db = this.getReadableDatabase();
        return db.rawQuery("SELECT * FROM books", null);
    }
}
