-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dodano drugi algorytm. Wybór algorytmu przy pomocy argumentu wywołania.
- Loading branch information
1 parent
899aade
commit 2eedd42
Showing
8 changed files
with
256 additions
and
31 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,48 @@ | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class DebtTable { | ||
|
||
private Double[][] debtTable; | ||
private Map<String, Integer> nameIndex = new HashMap<>(); | ||
private ArrayList<Person> persons; | ||
|
||
public DebtTable(ArrayList<Person> p) { | ||
this.persons = p; | ||
this.debtTable = new Double[p.size()][p.size()]; | ||
fillTable(); | ||
fillNameIndex(p); | ||
} | ||
|
||
private void fillTable() { | ||
for (int i = 0; i < debtTable.length; i++) | ||
for (int j = 0; j < debtTable.length; j++) | ||
debtTable[i][j] = 0.; | ||
} | ||
|
||
private void fillNameIndex(ArrayList<Person> persons) { | ||
for (int i = 0; i < persons.size(); i++) { | ||
nameIndex.put(persons.get(i).getName(), i); | ||
} | ||
} | ||
|
||
public void addDebt(String reciver, String giver, double x){ | ||
debtTable[nameIndex.get(reciver)][nameIndex.get(giver)] += x; | ||
} | ||
|
||
public ArrayList<Debt> getDebts(){ | ||
ArrayList<Debt> debtArray = new ArrayList<>(); | ||
for (int i = 0; i < debtTable.length; i++) { | ||
for (int j = i + 1; j < debtTable.length; j++) { | ||
double amount = debtTable[i][j] - debtTable[j][i]; | ||
if (amount > 0) { | ||
debtArray.add(new Debt(persons.get(i).getName(), persons.get(j).getName(), amount)); | ||
} else if(amount < 0){ | ||
debtArray.add(new Debt(persons.get(j).getName(), persons.get(i).getName(), Math.abs(amount))); | ||
} | ||
} | ||
} | ||
return debtArray; | ||
} | ||
} |
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
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.