Skip to content

Commit

Permalink
Soluzione proposta
Browse files Browse the repository at this point in the history
  • Loading branch information
JIMMY authored and JIMMY committed Apr 17, 2018
1 parent fbf2e50 commit 7a41e69
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 3 deletions.
1 change: 1 addition & 0 deletions VotiNobel/bin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/it/
Empty file modified VotiNobel/bin/it/polito/tdp/dao/ConnectDB.class
100755 → 100644
Empty file.
Empty file modified VotiNobel/bin/it/polito/tdp/dao/EsameDAO.class
100755 → 100644
Empty file.
Empty file modified VotiNobel/bin/it/polito/tdp/dao/PopulateDB.class
100755 → 100644
Empty file.
Empty file modified VotiNobel/bin/it/polito/tdp/dao/TestDB.class
100755 → 100644
Empty file.
Empty file modified VotiNobel/bin/it/polito/tdp/main/Main.class
100755 → 100644
Empty file.
Empty file modified VotiNobel/bin/it/polito/tdp/main/VotiNobelController.class
100755 → 100644
Empty file.
Binary file modified VotiNobel/bin/it/polito/tdp/model/Esame.class
100755 → 100644
Binary file not shown.
Binary file modified VotiNobel/bin/it/polito/tdp/model/Model.class
100755 → 100644
Binary file not shown.
Binary file modified VotiNobel/bin/it/polito/tdp/model/TestModel.class
100755 → 100644
Binary file not shown.
74 changes: 72 additions & 2 deletions VotiNobel/src/it/polito/tdp/model/Model.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,84 @@
package it.polito.tdp.model;

import java.util.ArrayList;
import java.util.List;

import it.polito.tdp.dao.EsameDAO;

public class Model {

private List<Esame> esami;
private EsameDAO edao;
private List<Esame> soluzione;
private double bestAvg;

public Model() {
edao = new EsameDAO();
esami = edao.getTuttiEsami();
// for (Esame e: esami) {
// System.out.println(e);
// }
}

public List<Esame> calcolaSottoinsiemeEsami(int numeroCrediti) {
// inizializzazione
soluzione = new ArrayList<Esame>();
bestAvg = 0.0;

int step = 0;
List<Esame> parziale = new ArrayList<>();
recursive(step, parziale, numeroCrediti);

return soluzione;
}

public int totCrediti(List<Esame> parziale) {
int somma = 0;
for (Esame e: parziale) {
somma += e.getCrediti();
}
return somma;
}

public double avg(List<Esame> parziale) {
double avg = 0;
for (Esame e : parziale) {
avg += e.getCrediti() * e.getVoto();
}
avg /= totCrediti(parziale);
return avg;
}

private void recursive(int step, List<Esame> parziale, int numeroCrediti) {

// Debug ??
// for (Esame e : parziale) {
// System.out.print(e.getCodins() + " ");
// }
// System.out.println(" ");


// Condizione di terminazione
if (totCrediti(parziale) > numeroCrediti) {
return;
}

System.out.println("TODO!");
// Controllo se ho trovato una nuova soluzione migliore
if (totCrediti(parziale) == numeroCrediti) {
if (avg(parziale) > bestAvg) {
soluzione = new ArrayList(parziale);
bestAvg = avg(parziale);
}
}

return null;
// Generazione di una nuova soluzione parziale
for (Esame esame : esami) {
if (!parziale.contains(esame)) {
parziale.add(esame);
recursive(step+1, parziale, numeroCrediti);
parziale.remove(esame);
}
}
}

}
8 changes: 7 additions & 1 deletion VotiNobel/src/it/polito/tdp/model/TestModel.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package it.polito.tdp.model;

import java.util.List;

public class TestModel {

public static void main(String[] args) {
// TODO Auto-generated method stub

Model model = new Model();
model.calcolaSottoinsiemeEsami(20);
List<Esame> soluzione = model.calcolaSottoinsiemeEsami(22);

System.out.println("Migliore combinazione di esami: ");
for (Esame e : soluzione) {
System.out.println(e);
}
}

}

0 comments on commit 7a41e69

Please sign in to comment.