import java.time.LocalDate;
import java.util.List;
public class Main {
public static void main(String[] args) {
SpesaManager revolut = new SpesaManager();
revolut.addSpesa(new Spese("Trasporto", LocalDate.of(2024, 5, 10), 12.80, "Carta")); // maledetto carnet 8 corse
revolut.addSpesa(new Spese("Alimentari", LocalDate.of(2024, 5, 12), 42.30, "Contanti")); // in nero sotto casa...
revolut.addSpesa(new Spese(7.50, "Bar", "Contanti")); // uno sprits...
revolut.addSpesa(new Spese(120.00, "Tecnologia", "Bonifico")); // questa era una truffa
stampaElenco(revolut.getSpese());
stampaStatistiche(revolut);
}
private static void stampaElenco(List<Spese> spese) {
System.out.println("Spese inserite:");
for (int i = 0; i < spese.size(); i++) {
Spese spesa = spese.get(i);
System.out.printf(
"%d) %s - %.2f (%s) | %s%n",
i + 1,
spesa.getCategory(),
spesa.getMoneyyy(),
spesa.getPayMethod(),
spesa.getDate() == null ? "data non disponibile" : spesa.getDate()
);
}
System.out.println();
}
private static void stampaStatistiche(SpesaManager manager) {
List<Spese> spese = manager.getSpese();
double totale = 0;
double max = Double.NEGATIVE_INFINITY;
double min = Double.POSITIVE_INFINITY;
for (Spese spesa : spese) {
double importo = spesa.getMoneyyy();
totale += importo;
max = Math.max(max, importo);
min = Math.min(min, importo);
}
double media = spese.isEmpty() ? 0 : totale / spese.size();
System.out.printf("Totale: %.2f%n", totale);
System.out.printf("Media: %.2f%n", media);
System.out.printf("Massimo: %.2f%n", max);
System.out.printf("Minimo: %.2f%n", min);
String categoriaTop = manager.calcolaCategoriaMaggiore();
String metodoTop = manager.calcolaPayMethodMaggiore();
double mediaCategoriaTop = categoriaTop.isEmpty() ? 0 : mediaPerCategoria(spese, categoriaTop);
double totaleMetodoTop = metodoTop.isEmpty() ? 0 : totalePerMetodo(spese, metodoTop);
System.out.println();
System.out.printf("Categoria con media piu alta: %s (%.2f)%n", categoriaTop, mediaCategoriaTop);
System.out.printf("Metodo con totale piu alto: %s (%.2f)%n", metodoTop, totaleMetodoTop);
}
private static double mediaPerCategoria(List<Spese> spese, String categoria) {
double totale = 0;
int count = 0;
for (Spese spesa : spese) {
if (spesa.getCategory().equals(categoria)) {
totale += spesa.getMoneyyy();
count++;
}
}
return count == 0 ? 0 : totale / count;
}
private static double totalePerMetodo(List<Spese> spese, String metodo) {
double totale = 0;
for (Spese spesa : spese) {
if (spesa.getPayMethod().equals(metodo)) {
totale += spesa.getMoneyyy();
}
}
return totale;
}
}
import java.util.ArrayList;
public class SpesaManager {
private final ArrayList<Spese> spese;
public SpesaManager() {
spese = new ArrayList<>();
}
public void addSpesa(Spese spesa) {
spese.add(spesa);
}
public ArrayList<Spese> getSpese() {
return spese;
}
public double spesaTotale() {
double totale = 0;
for (Spese spesa : spese) {
totale += spesa.getMoneyyy();
}
return totale;
}
public double spesaMedia() {
return spese.isEmpty() ? 0 : spesaTotale() / spese.size();
}
public double spesaMax() {
if (spese.isEmpty()) {
return 0;
}
double max = spese.get(0).getMoneyyy();
for (Spese spesa : spese) {
if (spesa.getMoneyyy() > max) {
max = spesa.getMoneyyy();
}
}
return max;
}
public double spesaMin() {
if (spese.isEmpty()) {
return 0;
}
double min = spese.get(0).getMoneyyy();
for (Spese spesa : spese) {
if (spesa.getMoneyyy() < min) {
min = spesa.getMoneyyy();
}
}
return min;
}
private ArrayList<String> getCategory() {
ArrayList<String> categories = new ArrayList<>();
for (Spese spesa : spese) {
String category = spesa.getCategory();
if (!categories.contains(category)) {
categories.add(category);
}
}
return categories;
}
private ArrayList<String> getPayMethod() {
ArrayList<String> methods = new ArrayList<>();
for (Spese spesa : spese) {
String method = spesa.getPayMethod();
if (!methods.contains(method)) {
methods.add(method);
}
}
return methods;
}
private double spesaMediaPerCategoria(String categoria) {
double totale = 0;
int count = 0;
for (Spese spesa : spese) {
if (spesa.getCategory().equals(categoria)) {
totale += spesa.getMoneyyy();
count++;
}
}
return count == 0 ? 0 : totale / count;
}
private double spesaTotPerPayMethod(String method) {
double totale = 0;
for (Spese spesa : spese) {
if (spesa.getPayMethod().equals(method)) {
totale += spesa.getMoneyyy();
}
}
return totale;
}
public String calcolaPayMethodMaggiore() {
ArrayList<String> methods = this.getPayMethod();
if (methods.isEmpty()) {
return "";
}
String migliore = methods.get(0);
double totaleMigliore = this.spesaTotPerPayMethod(migliore);
for (String metodo : methods) {
double totaleMetodo = this.spesaTotPerPayMethod(metodo);
if (totaleMetodo > totaleMigliore) {
migliore = metodo;
totaleMigliore = totaleMetodo;
}
}
return migliore;
}
public String calcolaPayMethodMinore() {
ArrayList<String> methods = this.getPayMethod();
if (methods.isEmpty()) {
return "";
}
String peggiore = methods.get(0);
double totalePeggiore = this.spesaTotPerPayMethod(peggiore);
for (String metodo : methods) {
double totaleMetodo = this.spesaTotPerPayMethod(metodo);
if (totaleMetodo < totalePeggiore) {
peggiore = metodo;
totalePeggiore = totaleMetodo;
}
}
return peggiore;
}
public String calcolaCategoriaMaggiore() {
ArrayList<String> categorie = this.getCategory();
if (categorie.isEmpty()) {
return "";
}
String migliore = categorie.get(0);
double mediaMigliore = this.spesaMediaPerCategoria(migliore);
for (String categoria : categorie) {
double mediaCategoria = this.spesaMediaPerCategoria(categoria);
if (mediaCategoria > mediaMigliore) {
migliore = categoria;
mediaMigliore = mediaCategoria;
}
}
return migliore;
}
public String calcolaCategoriaMinore() {
ArrayList<String> categorie = this.getCategory();
if (categorie.isEmpty()) {
return "";
}
String peggiore = categorie.get(0);
double mediaPeggiore = this.spesaMediaPerCategoria(peggiore);
for (String categoria : categorie) {
double mediaCategoria = this.spesaMediaPerCategoria(categoria);
if (mediaCategoria < mediaPeggiore) {
peggiore = categoria;
mediaPeggiore = mediaCategoria;
}
}
return peggiore;
}
}
import java.time.LocalDate;
/**
* La classe {@code Spese} rappresenta una singola spesa con attributi relativi alla data,
* importo, categoria e metodo di pagamento.
* @author giordii.dev
*/
public class Spese {
private LocalDate date;
private double Moneyyy;
private String Category;
private String PayMethod;
/**
* Costruttore completo per l'inizializzazione completa degli oggetti.
* @param date data della spesa
*/
public Spese(String Category, LocalDate date, double Moneyyy, String PayMethod) {
this.Category = Category;
this.date = date;
this.Moneyyy = Moneyyy;
this.PayMethod = PayMethod;
}
/**
* Costruttore senza Date per spese incognite
* @hidden per i soldi in nero :|
*/
public Spese(double moneyyy, String category, String payMethod) {
this.Moneyyy = moneyyy;
this.Category = category;
this.PayMethod = payMethod;
}
// getter e setter
public void setDate(LocalDate date) {
this.date = date;
}
public LocalDate getDate() {
return date;
}
public void setMoneyyy(double Moneyyy) {
this.Moneyyy = Moneyyy;
}
public void setCategory(String Category) {
this.Category = Category;
}
public void setPayMethod(String PayMethod) {
this.PayMethod = PayMethod;
}
public double getMoneyyy() {
return Moneyyy;
}
public String getCategory() {
return Category;
}
public String getPayMethod() {
return PayMethod;
}
@Override
public String toString() {
return "Spese [date=" + date + ", Moneyyy=" + Moneyyy + ", Category=" + Category + ", PayMethod=" + PayMethod
+ "]";
}
/**
* Metodo per applcare l'iva alle spese con scontrino
* @return Spesa+iva 22%
*/
public double applicaIva() {
return this.Moneyyy * 1.22;
}
}