Skip to content

Commit

Permalink
Codigo consultar terminado
Browse files Browse the repository at this point in the history
  • Loading branch information
Sara Saiz committed Oct 21, 2024
1 parent 247293b commit bbe9c61
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 113 deletions.
2 changes: 1 addition & 1 deletion AndroidProject/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
android:name=".activities.RegistrarRepostajeMenu.RegistrarView"
android:exported="false" />
<activity
android:name=".activities.ConsultarRepostaje.ConsultarRepostaje"
android:name=".activities.ConsultarRepostaje.ConsultarView"
android:exported="false" />
<activity
android:name=".activities.details.DetailsView"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@

@HiltAndroidApp
public class GasolinerasApp extends Application {
@Getter
private static AppDatabase database;

@Override
public void onCreate() {
super.onCreate();
database = Room.databaseBuilder(getApplicationContext(),
AppDatabase.class, "database-name").allowMainThreadQueries().build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package es.unican.gasolineras.activities.ConsultarRepostaje;

import android.content.Context;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

import es.unican.gasolineras.activities.main.IMainContract;
import es.unican.gasolineras.model.Gasolinera;
import es.unican.gasolineras.model.Repostaje;
import es.unican.gasolineras.repository.AppDatabase;
import es.unican.gasolineras.repository.DatabaseFunction;
import es.unican.gasolineras.repository.RepostajeDAO;

public class ConsultarPresenter implements IConsultar.Presenter {

private IConsultar.View view;
private RepostajeDAO repostajeDAO;

public ConsultarPresenter(RepostajeDAO repostajeDAO) {
this.repostajeDAO = repostajeDAO;
}


/**
* @see IConsultar.Presenter#init(IConsultar.View)
* @param view the view to control
*/
@Override
public void init(IConsultar.View view) {

this.view = view;
this.view.init();
load();
}


@Override
public double calcularPrecioMedioLitro() {
List<Repostaje> repostajesMesAnterior = obtenerRepostajesDelMes();

if(repostajesMesAnterior.isEmpty()) {
return 0;
}

double sumaPreciosPonderados = 0;
double sumaLitros = 0;

for (Repostaje repostaje : repostajesMesAnterior) {
double precioPorLitro = repostaje.getPrecioTotal() / repostaje.getLitros();
sumaPreciosPonderados += precioPorLitro * repostaje.getLitros();
sumaLitros += repostaje.getLitros();
}
return sumaPreciosPonderados / sumaLitros;
}

@Override
public List<Repostaje> obtenerRepostajesDelMes() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 1); // Primer día del mes actual
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
String inicioDelMes = sdf.format(calendar.getTime());

// Establecer el último día del mes actual
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
String finDelMes = sdf.format(calendar.getTime());

// Obtener los repostajes del mes actual desde el DAO utilizando el rango de fechas
return repostajeDAO.repostajesPorRangoDeFechas(inicioDelMes, finDelMes);

}

@Override
public double calcularAcumuladoMes() {
List<Repostaje> repostajesMes = obtenerRepostajesDelMes();

if(repostajesMes.isEmpty()) {
return 0;
}

double totalAcumulado = 0;

for (Repostaje repostaje : repostajesMes) {
totalAcumulado += repostaje.getPrecioTotal();
}

return totalAcumulado;
}

private void load() {


List<Repostaje> repostajes = repostajeDAO.repostajes();

try {
view.showRepostajes(repostajes);
view.showLoadCorrect(repostajes.size());
} catch (Throwable e) {
view.showLoadError();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,27 @@
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.room.Room;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import es.unican.gasolineras.GasolinerasApp;
import es.unican.gasolineras.R;

import es.unican.gasolineras.activities.main.IMainContract;
import es.unican.gasolineras.activities.main.MainView;
import es.unican.gasolineras.model.Gasolinera;
import es.unican.gasolineras.model.Repostaje;
import es.unican.gasolineras.repository.AppDatabase;
import es.unican.gasolineras.repository.DatabaseFunction;
import es.unican.gasolineras.repository.RepostajeDAO;

public class ConsultarRepostaje extends AppCompatActivity implements IConsultar {
public class ConsultarView extends AppCompatActivity implements IConsultar.View {


private ConsultarPresenter presenter;
public AppDatabase db;
/**
* @see AppCompatActivity#onCreate(Bundle)
* @param savedInstanceState
Expand All @@ -41,87 +44,39 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_consulta_repostaje_view);
} catch (Exception e) {
// Si ocurre una excepción, muestra el mensaje de error
new AlertDialog.Builder(ConsultarRepostaje.this)
new AlertDialog.Builder(ConsultarView.this)
.setTitle("Error")
.setMessage(getString(R.string.error_acceso_bbdd))
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Puedes agregar alguna acción adicional si es necesario
Intent intent = new Intent(ConsultarRepostaje.this, MainView.class);
Intent intent = new Intent(ConsultarView.this, MainView.class);
startActivity(intent);
}
})
.show();
}
db = DatabaseFunction.getDatabase(this);

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar bar = getSupportActionBar();
//assert bar != null; // to avoid warning in the line below
//bar.setDisplayHomeAsUpEnabled(true); // show back button in action bar

TextView repostajesMes = findViewById(R.id.tvRepostajesMes);
TextView precioMedioLitro = findViewById(R.id.tvPrecioMedioLitro);
TextView acumuladoMes = findViewById(R.id.tvAcumuladoMes);

repostajesMes.setText(String.valueOf(obtenerRepostajesDelMes()));
precioMedioLitro.setText(String.valueOf(calcularPrecioMedioLitro()));
acumuladoMes.setText(String.valueOf(calcularAcumuladoMes()));

load();

}

@Override
public double calcularPrecioMedioLitro() {
List<Repostaje> repostajesMesAnterior = obtenerRepostajesDelMes();

double sumaPreciosPonderados = 0;
double sumaLitros = 0;

for (Repostaje repostaje : repostajesMesAnterior) {
double precioPorLitro = repostaje.getPrecioTotal() / repostaje.getLitros();
sumaPreciosPonderados += precioPorLitro * repostaje.getLitros();
sumaLitros += repostaje.getLitros();
}

// Evitar división por cero
if (sumaLitros == 0) {
return 0;
}
presenter = new ConsultarPresenter(db.repostajeDao());
presenter.init(this);

// Retornar el precio medio ponderado por los litros repostados
return sumaPreciosPonderados / sumaLitros;
}

@Override
public double calcularAcumuladoMes() {
// Obtener la lista de repostajes del mes anterior
List<Repostaje> repostajes = obtenerRepostajesDelMes(); // Método que devuelve los repostajes del mes anterior
double totalAcumulado = 0;

// Iterar sobre los repostajes del mes anterior para sumar el precio total de cada repostaje
for (Repostaje repostaje : repostajes) {
totalAcumulado += repostaje.getPrecioTotal();
}

// Retorna el acumulado total del mes
return totalAcumulado;
}

@Override
public List<Repostaje> obtenerRepostajesDelMes() {
// Obtener la fecha actual
Calendar calendar = Calendar.getInstance();

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM", Locale.getDefault());
public void init() {
TextView repostajesMes = findViewById(R.id.tvRepostajesMes);
TextView precioMedioLitro = findViewById(R.id.tvPrecioMedioLitro);
TextView acumuladoMes = findViewById(R.id.tvAcumuladoMes);

// Obtener el mes en formato 'YYYY-MM'
String mesAnterior = sdf.format(calendar.getTime());
repostajesMes.setText(String.valueOf(presenter.obtenerRepostajesDelMes().size()));
precioMedioLitro.setText(String.format(Locale.getDefault(),"%.2f", presenter.calcularPrecioMedioLitro()));
acumuladoMes.setText(String.format(Locale.getDefault(),"%.2f", presenter.calcularAcumuladoMes()));

// Obtener los repostajes del mes anterior desde el DAO
return GasolinerasApp.getDatabase().repostajeDao().repostajesPorMes(mesAnterior);
}

/**
Expand Down Expand Up @@ -152,16 +107,4 @@ public void showLoadError() {
Toast.makeText(this, "Error cargando los repostajes", Toast.LENGTH_SHORT).show();
}

private void load() {

RepostajeDAO repostajeDao = GasolinerasApp.getDatabase().repostajeDao();
List<Repostaje> repostajes = repostajeDao.repostajes();

try {
showRepostajes(repostajes);
showLoadCorrect(repostajes.size());
} catch (Throwable e) {
showLoadError();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,71 @@
package es.unican.gasolineras.activities.ConsultarRepostaje;

import java.util.List;

import es.unican.gasolineras.model.Repostaje;


public interface IConsultar {

/**
* Calcula el precio medio por litro pagado en el mes actual
* @return
* Methods that must be implemented in the Main Presenter.
* Only the View should call these methods.
*/
public double calcularPrecioMedioLitro();
public interface Presenter {
/**
* Links the presenter with its view.
* Only the View should call this method
* @param view
*/
public void init(View view);

public List<Repostaje> obtenerRepostajesDelMes();
/**
* Muestra los repostajes realizados
* @param repostajes the list of charging stations
*/
public void showRepostajes(List<Repostaje> repostajes);

public void showLoadCorrect(int repostajes);
public void showLoadError();
/**
*
* @return
*/
public double calcularAcumuladoMes();
/**
* Calcula el precio medio por litro pagado en el mes actual
* teniendo en cuenta el peso de cada repostajes
* @return el precio medio
*/
public double calcularPrecioMedioLitro();

/**
* Obtiene los repostajes realizados en el mes actual
* @return la lista de repostajes del mes
*/
public List<Repostaje> obtenerRepostajesDelMes();

/**
* Calcula el precio total invertido en repostajes en el mes.
* @return el precio total gastado en repostajes en el mes actual
*/
public double calcularAcumuladoMes();
}

public interface View {
/**
* Initialize the view. Typically this should initialize all the listeners in the view.
* Only the Presenter should call this method
*/
public void init();

/**
* Muestra los repostajes realizados
* @param repostajes the list of charging stations
*/
public void showRepostajes(List<Repostaje> repostajes);

/**
* The view is requested to display a notification indicating that the refueling
* were loaded correctly.
* Only the Presenter should call this method
* @param repostajes
*/
public void showLoadCorrect(int repostajes);

/**
* The view is requested to display a notificacion indicating that the refueling
* were not loaded correctly.
* Only the Presenter should call this method
*/
public void showLoadError();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,31 @@ public View getView(int position, View convertView, ViewGroup parent) {

//precio por litro
{
TextView tvLabel = convertView.findViewById(R.id.tvPrecioLitro);
String label = "Precio por litro: ";
tvLabel.setText(String.format("%s:", label));

TextView tv = convertView.findViewById(R.id.tvPrecioPorLitroNum);
tv.setText(String.valueOf(repostaje.getPrecioTotal() / repostaje.getLitros()));

}

//litros
{
TextView tvLabel = convertView.findViewById(R.id.litrosTexto);
String label = "Litros: ";
tvLabel.setText(String.format("%s:", label));

TextView tv = convertView.findViewById(R.id.tvLitrosNum);
tv.setText(String.valueOf(repostaje.getLitros()));
}

//precio total
{
TextView tvLabel = convertView.findViewById(R.id.precioTotal);
String label = "Precio total: ";
tvLabel.setText(String.format("%s:", label));

TextView tv = convertView.findViewById(R.id.tvPrecioTotalNum);
tv.setText(String.valueOf(repostaje.getPrecioTotal()));
}
Expand Down
Loading

0 comments on commit bbe9c61

Please sign in to comment.