Skip to content

Commit

Permalink
Dodano drugi algorytm. Wybór algorytmu przy pomocy argumentu wywołania.
Browse files Browse the repository at this point in the history
  • Loading branch information
kristoph4822 committed Aug 16, 2020
1 parent 899aade commit 2eedd42
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 31 deletions.
124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/Debt.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

public class Debt {

private final String CURRENCY = "PLN";
public static final String CURRENCY = "PLN";

private String reciver;
private String giver;
Expand All @@ -21,4 +21,5 @@ public String toString(){
NumberFormat f = new DecimalFormat("#0.00");
return this.giver + " owes " + this.reciver + " " + f.format(this.amount) + " " + CURRENCY;
}

}
63 changes: 49 additions & 14 deletions src/DebtCalculator.java
Original file line number Diff line number Diff line change
@@ -1,56 +1,90 @@
import java.util.ArrayList;
import java.util.Collections;

import static java.lang.System.exit;

public class DebtCalculator {

private ArrayList<Person> persons;
private ArrayList<Debt> debts;
private DebtTable dt;

public double sum;
public double x;

public DebtCalculator(ArrayList<Person> persons) {
this.persons = persons;
persons.sort(Person.sortByBalance);
this.debts = new ArrayList();
this.sum = 0;
}

public void resolveDebt(){
public void resolveDebt(int version) {
calculateBalance();
if(version == 1){
resolveDebt1();
}
else if(version == 2){
resolveDebt2();
}
else{
System.out.println("Podano złą wersję");
exit(-1);
}
}

private void resolveDebt1(){ //zależnie która kwota jest najmniejsza wybieramy jedną z 3 opcji
Person first;
Person last;

while(this.persons.size() > 1){
first = this.persons.get(0);
last = persons.get(persons.size() - 1);
first = this.persons.get(persons.size() - 1);
last = persons.get(0);
double lastAbsBalance = Math.abs(last.getBalance());

if(first.getBalance() > lastAbsBalance){
// wyrównujemy najgorszego
if(first.getBalance() > lastAbsBalance) {
debts.add(new Debt(first.getName(), last.getName(), lastAbsBalance));
first.addToBalance(-lastAbsBalance);
persons.remove(last);
}

else{
//wyrównujemy najlepszego
else {
debts.add(new Debt(first.getName(), last.getName(), first.getBalance()));
last.addToBalance(first.getBalance());
persons.remove(first);
}
}
}

private void resolveDebt2() {
this.dt = new DebtTable(persons);
for (Person p1: persons) {
double amount = p1.getExpense()/getRatio();
for (Person p2: persons){
if(p2.equals(p1))
continue;
dt.addDebt(p1.getName(), p2.getName(), amount * p2.getRatio());
}
}

this.debts = dt.getDebts();

}

private void calculateBalance(){
double sum = getSum();
getSum();
double ratio = getRatio();
double x = sum/ratio; //amount which each person with ratio 1 has to pay overall
this.x = sum/ratio; //amount which each person with ratio 1 has to pay overall
for (Person p: persons) {
p.setBalance(p.getExpense() - x * p.getRatio());
}
}

private double getSum(){
double sum = 0;
private void getSum(){
for (Person p: persons) {
sum += p.getExpense();
}
return sum;
}

private double getRatio(){
Expand All @@ -61,8 +95,9 @@ private double getRatio(){
return ratio;
}

public ArrayList<Debt> getDebts() {
return debts;
public void printDebts() {
for(Debt d: debts){
System.out.println(d.toString());
}
}

}
48 changes: 48 additions & 0 deletions src/DebtTable.java
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;
}
}
5 changes: 4 additions & 1 deletion src/FileReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ public static ArrayList<Person> readPersonsFromFile(String filePath) throws IOEx
String line;

while ((line = br.readLine()) != null) {
if(line.charAt(0) == '#'){ //lines starting with '#' are ignored
if(line.isEmpty() || line.charAt(0) == '#'){ //lines starting with '#' are ignored
continue;
}

String[] info = line.split(Pattern.quote("|"));
persons.add(new Person(info[0].trim(), Double.parseDouble(info[1].trim()), Double.parseDouble(info[2].trim())));


}

return persons;
Expand Down
19 changes: 12 additions & 7 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import java.io.IOException;
import java.util.ArrayList;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Main {

public static void main(String[] args){

if (args.length == 0)
throw new IllegalArgumentException("No argument was given.");
if (args.length < 2)
throw new IllegalArgumentException("Wrong arguments.\n 1st argument is text file, 2nd is choice of algorithm (1 or 2).");

DebtCalculator dc = null;

Expand All @@ -17,10 +18,14 @@ public static void main(String[] args){
System.exit(-1);
}

dc.resolveDebt();
for(Debt d: dc.getDebts()){
System.out.println(d.toString());
}
dc.resolveDebt(Integer.parseInt(args[1]));

NumberFormat f = new DecimalFormat("#0.00");
System.out.println("Sum: " + f.format(dc.sum) + " " + Debt.CURRENCY);
System.out.println("Amount each person (with ratio 1) has to pay: " + f.format(dc.x)+ " " + Debt.CURRENCY);
System.out.println("----------------------------");

dc.printDebts();

}

Expand Down
10 changes: 5 additions & 5 deletions src/Person.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import java.util.Comparator;

public class Person{
public class Person {

private final String name;
private final double expense;
private final double ratio;
private double balance;


public Person(String name, double expense, double ratio) {
this.name = name;
this.expense = expense;
this.ratio = ratio;
this.balance = 0;
}


public String getName() {
return name;
}
Expand Down Expand Up @@ -44,9 +42,11 @@ public void addToBalance(double amount) {

@Override
public int compare(Person p1, Person p2) {

int x = Double.compare(p1.getBalance(), p2.getBalance());
//sort in ascending order
return Double.compare(p1.getBalance(), p2.getBalance());
if(x == 0)
return Double.compare(p1.getExpense(), p2.getExpense());
return x;
}
};
}
Loading

0 comments on commit 2eedd42

Please sign in to comment.