-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sara Saiz
committed
Oct 21, 2024
1 parent
247293b
commit bbe9c61
Showing
10 changed files
with
223 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
...src/main/java/es/unican/gasolineras/activities/ConsultarRepostaje/ConsultarPresenter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 59 additions & 17 deletions
76
...ect/app/src/main/java/es/unican/gasolineras/activities/ConsultarRepostaje/IConsultar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.