0% found this document useful (0 votes)
3 views

MainActivity

The document contains Java code for an Android application that manages products, including adding and purchasing products. It consists of three main activities: MainActivity for navigation, AddProductActivity for adding product details and saving them to Firebase, and PurchaseActivity for searching and displaying products. The Product class defines the product structure with attributes like productId, name, purchasePrice, and salePrice, which includes a 15% tax calculation.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

MainActivity

The document contains Java code for an Android application that manages products, including adding and purchasing products. It consists of three main activities: MainActivity for navigation, AddProductActivity for adding product details and saving them to Firebase, and PurchaseActivity for searching and displaying products. The Product class defines the product structure with attributes like productId, name, purchasePrice, and salePrice, which includes a 15% tax calculation.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

// MainActivity.

java

package com.nelsontorres.tallersegundobimestre;

import android.content.Intent;

import android.os.Bundle;

import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private Button btnAddProduct, btnPurchase;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btnAddProduct = findViewById(R.id.btnAddProduct);

btnPurchase = findViewById(R.id.btnPurchase);

btnAddProduct.setOnClickListener(v -> {

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

startActivity(intent);

});

btnPurchase.setOnClickListener(v -> {

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

startActivity(intent);

});

}
// Product.java

package com.nelsontorres.tallersegundobimestre;

public class Product {

private String productId;

private String name;

private double purchasePrice;

private double salePrice;

public Product() {

// Constructor vacío requerido para Firebase

public Product(String productId, String name, double purchasePrice) {

this.productId = productId;

this.name = name;

this.purchasePrice = purchasePrice;

this.salePrice = purchasePrice * 1.15; // 15% IVA

// Getters y setters

public String getProductId() { return productId; }

public void setProductId(String productId) { this.productId = productId; }

public String getName() { return name; }

public void setName(String name) { this.name = name; }

public double getPurchasePrice() { return purchasePrice; }

public void setPurchasePrice(double purchasePrice) { this.purchasePrice = purchasePrice; }

public double getSalePrice() { return salePrice; }

public void setSalePrice(double salePrice) { this.salePrice = salePrice; }

}
// AddProductActivity.java

package com.nelsontorres.tallersegundobimestre;

import android.os.Bundle;

import android.text.Editable;

import android.text.TextWatcher;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.firebase.database.DatabaseReference;

import com.google.firebase.database.FirebaseDatabase;

public class AddProductActivity extends AppCompatActivity {

private EditText etProductName, etProductId, etPurchasePrice;

private TextView tvSalePrice;

private Button btnSave;

private DatabaseReference databaseReference;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_add_product);

// Inicializar Firebase

databaseReference = FirebaseDatabase.getInstance()

.getReferenceFromUrl("https://taller2025-default-rtdb.firebaseio.com/")

.child("products");

// Inicializar vistas

etProductName = findViewById(R.id.etProductName);

etProductId = findViewById(R.id.etProductId);

etPurchasePrice = findViewById(R.id.etPurchasePrice);
tvSalePrice = findViewById(R.id.tvSalePrice);

btnSave = findViewById(R.id.btnSave);

// Calcular precio de venta automáticamente

etPurchasePrice.addTextChangedListener(new TextWatcher() {

@Override

public void afterTextChanged(Editable s) {

if (!s.toString().isEmpty()) {

try {

double purchasePrice = Double.parseDouble(s.toString());

double salePrice = purchasePrice * 1.15; // 15% IVA

tvSalePrice.setText(String.format("%.2f", salePrice));

} catch (NumberFormatException e) {

tvSalePrice.setText("0.00");

@Override

public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

@Override

public void onTextChanged(CharSequence s, int start, int before, int count) {}

});

btnSave.setOnClickListener(v -> saveProduct());

private void saveProduct() {

String name = etProductName.getText().toString().trim();

String id = etProductId.getText().toString().trim();

String purchasePriceStr = etPurchasePrice.getText().toString().trim();

if (name.isEmpty() || id.isEmpty() || purchasePriceStr.isEmpty()) {


Toast.makeText(this, "Por favor complete todos los campos", Toast.LENGTH_SHORT).show();

return;

double purchasePrice = Double.parseDouble(purchasePriceStr);

Product product = new Product(id, name, purchasePrice);

databaseReference.child(id).setValue(product)

.addOnSuccessListener(aVoid -> {

Toast.makeText(AddProductActivity.this, "Producto guardado exitosamente", Toast.LENGTH_SHORT).show();

finish();

})

.addOnFailureListener(e -> {

Toast.makeText(AddProductActivity.this, "Error al guardar: " + e.getMessage(),


Toast.LENGTH_SHORT).show();

});

// PurchaseActivity.java

package com.nelsontorres.tallersegundobimestre;

import android.os.Bundle;

import android.widget.EditText;

import android.widget.ListView;

import android.widget.SearchView;

import androidx.appcompat.app.AppCompatActivity;

import com.google.firebase.database.*;

import java.util.ArrayList;

public class PurchaseActivity extends AppCompatActivity {

private SearchView searchView;

private ListView listView;

private DatabaseReference databaseReference;


private ArrayList<Product> productList;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_purchase);

searchView = findViewById(R.id.searchView);

listView = findViewById(R.id.listView);

productList = new ArrayList<>();

databaseReference = FirebaseDatabase.getInstance()

.getReferenceFromUrl("https://taller2025-default-rtdb.firebaseio.com/")

.child("products");

// Configurar búsqueda

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

@Override

public boolean onQueryTextSubmit(String query) {

searchProducts(query);

return true;

@Override

public boolean onQueryTextChange(String newText) {

searchProducts(newText);

return true;

});

private void searchProducts(String query) {

databaseReference.orderByChild("name")

.startAt(query)
.endAt(query + "\uf8ff")

.addValueEventListener(new ValueEventListener() {

@Override

public void onDataChange(DataSnapshot dataSnapshot) {

productList.clear();

for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

Product product = snapshot.getValue(Product.class);

if (product != null) {

productList.add(product);

// Aquí deberías actualizar tu ListView con un adaptador personalizado

@Override

public void onCancelled(DatabaseError databaseError) {

// Manejar error

});

You might also like