Skip to content

Commit

Permalink
In working order
Browse files Browse the repository at this point in the history
  • Loading branch information
TissieVA committed Dec 12, 2020
1 parent 2c12acf commit 74b40f5
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public TicketDBPanel(Controller controller)

public void refresh()
{

listModel.clear();
Database.getTicketDB().forEach(listModel::addElement);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void extraPersonLine()
comboBox.addActionListener(this);
JScrollPane personPane = new JScrollPane(comboBox);

SpinnerNumberModel model = new SpinnerNumberModel(50, 0, Double.POSITIVE_INFINITY, 1);
SpinnerNumberModel model = new SpinnerNumberModel(15, 0, Double.POSITIVE_INFINITY, 1);
JSpinner priceField = new JSpinner(model);
spinnersArray.add(priceField);
JScrollPane pricePane = new JScrollPane(priceField);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

public class CheckWindow extends JFrame implements ActionListener
public class CheckWindow extends JFrame implements ActionListener, Observer
{

public CheckWindow()
{
super("MoneySplitter");
this.setSize(500,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Database.getTicketDB().addObserver(this);
Database.getPersonDB().addObserver(this);

initialise();
this.setVisible(true);
Expand All @@ -39,14 +43,14 @@ private void initialise()

Check check = new Check(Database.getTicketDB(), Database.getPersonDB());
JTextArea textArea = new JTextArea(check.print());
c.gridy = 1;
this.add(textArea, c);

this.add(textArea, BorderLayout.PAGE_START);


c.gridx = 0;
c.gridy = 2;
JButton backButton = new JButton("Back");
backButton.setFont(new Font("Sans", Font.PLAIN, 10));
backButton.addActionListener(this);
this.add(backButton,BorderLayout.SOUTH);
this.add(backButton, c);

}

Expand All @@ -60,4 +64,11 @@ public void actionPerformed(ActionEvent e)
this.dispose();
}
}

@Override
public void update(Observable o, Object arg)
{
this.setVisible(false);
this.dispose();
}
}
52 changes: 42 additions & 10 deletions MoneySplitter/src/main/java/ua/tijsva/sd/project/check/Check.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import ua.tijsva.sd.project.ticket.Ticket;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.UUID;

Expand All @@ -13,6 +14,7 @@ public class Check
private Database<Ticket> ticketDb;
private Database<Person> personDb;
private HashMap<UUID, Double> totalCheck;
ArrayList<String> solution = new ArrayList<>();

public Check(Database<Ticket> ticketDb, Database<Person> personDb)
{
Expand All @@ -28,37 +30,67 @@ public HashMap<UUID, Double> calculateCheck()
for(Ticket t : ticketDb)
{
if(totalCheck.containsKey(t.getPaidPerson()))
totalCheck.put(t.getPaidPerson(),totalCheck.get(t.getPaidPerson())-t.getPrice());
totalCheck.put(t.getPaidPerson(),totalCheck.get(t.getPaidPerson())+t.getPrice());
else
System.out.format("Person %s, who paid ticket %s not found", t.getPaidPerson().toString(),t.getId().toString());

for(UUID id: t.getIndebted().keySet())
{
if (totalCheck.containsKey(id))
totalCheck.put(id,totalCheck.get(id)+t.getIndebted().get(id));
totalCheck.put(id,totalCheck.get(id)-t.getIndebted().get(id));
else
System.out.format("Person %s in ticked %s not found", id.toString(), t.getId().toString());
}
}
return totalCheck;
}

public void whoOwesWho()
public void whoOwesWho(HashMap<UUID, Double> listing)
{
this.totalCheck = calculateCheck();

}
Double maxPrice = (Double) Collections.max(listing.values());
Double minPrice = (Double) Collections.min(listing.values());
if(!minPrice.equals(maxPrice))
{
UUID maxPricePerson = getKeyFromValue(listing, maxPrice);
UUID minPricePerson = getKeyFromValue(listing, minPrice);
Double result = maxPrice + minPrice;
result = Math.round(result* 100.0)/100.0;
if(result>=0.0)
{
solution.add(String.format("%s -> %s : %.2f%n",Database.getPersonDB().get(minPricePerson).getName(),Database.getPersonDB().get(maxPricePerson).getName(),Math.abs(minPrice)));

listing.put(maxPricePerson, result);
listing.put(minPricePerson, 0.0);
}
else
{
solution.add(String.format("%s -> %s : %.2f%n",Database.getPersonDB().get(minPricePerson).getName(),Database.getPersonDB().get(maxPricePerson).getName(),Math.abs(maxPrice)));
listing.put(minPricePerson, result);
listing.put(maxPricePerson, 0.0);
}
whoOwesWho(listing);
}

}
public String print()
{
this.totalCheck = calculateCheck();
StringBuilder string = new StringBuilder("Positive values are people who owe money to the people with negative value.%n");
solution.clear();
whoOwesWho(this.totalCheck);
StringBuilder string = new StringBuilder("");

solution.forEach(string::append);
return string.toString();
}

for(UUID id: totalCheck.keySet())
private UUID getKeyFromValue(HashMap<UUID,Double> hm, Double value)
{
for (UUID id : hm.keySet())
{
string.append(String.format("%s : %.2f%n", Database.getPersonDB().get(id).getName(), totalCheck.get(id)));
if(hm.get(id).equals(value))
return id;
}

return string.toString();
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,12 @@ public void setIndebted(List<Person> persons)
@Override
public String toString()
{
return "Ticket{" +
"ticketType='" + ticketType + '\'' +
", paidPerson=" + Database.getPersonDB().get(paidPerson).getName() +
", price=" + price +
'}';
String string = String.format("%s : %.2f paid by %s. Persons: ",ticketType,price,Database.getPersonDB().get(paidPerson).getName());
for (UUID id: this.indebted.keySet())
{
string += String.format("%s, ",Database.getPersonDB().get(id).getName());
}
string = string.substring(0, string.length() -2);
return string;
}
}

0 comments on commit 74b40f5

Please sign in to comment.