Skip to content

Commit

Permalink
Real code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
jpalaz committed Feb 26, 2015
1 parent 142572e commit 1d84c58
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 54 deletions.
6 changes: 3 additions & 3 deletions Fruits/Task 1/src/jan/juice/control/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ public Component(String name) {

@Override
public String toString() {
return name;
return this.name;
}

@Override
public int compareTo(Component component) {
return name.compareTo(component.getName());
return this.name.compareTo(component.getName());
}

@Override
Expand All @@ -34,6 +34,6 @@ public void setName(String name) {
}

public int length() {
return name.length();
return this.name.length();
}
}
16 changes: 9 additions & 7 deletions Fruits/Task 1/src/jan/juice/control/Juice.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
package jan.juice.control;

import java.io.*;
import java.util.ArrayList;
import java.util.Set;
import java.util.TreeSet;

public class Juice implements Comparable<Juice> {
private TreeSet<Component> components;
public class Juice implements Comparable<Juice> {
private Set<Component> components;

public Juice() {
this.components = new TreeSet<Component>();
}

public TreeSet<Component> getComponents() {
public Set<Component> getComponents() {
return components;
}

public void addComponent(Component component) {
components.add(component);
this.components.add(component);
}

public boolean isSubJuiceOf(Juice juice) {
return this.getComponents().containsAll(juice.getComponents());
}

/**
* @param juice to compare
* @return value to sort by increase
*/
public int compareTo(Juice juice) {
return juice.components.size() - components.size();
return juice.getComponents().size() - this.components.size();
}
}
2 changes: 1 addition & 1 deletion Fruits/Task 1/src/jan/juice/control/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static void main(String[] args) {
mixer.writeAllComponents("juice1.out");
mixer.countMinWashings();

if(sorting.isAlive()) {
if (sorting.isAlive()) {
try {
sorting.join();
} catch(InterruptedException e) { e.printStackTrace(); }
Expand Down
33 changes: 15 additions & 18 deletions Fruits/Task 1/src/jan/juice/control/Mixer.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package jan.juice.control;

import java.io.*;
import java.util.ArrayList;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
import java.util.*;

/**
* Created by Jan on 17.02.15.
*/
public class Mixer {
private ArrayList<Juice> juices;
private ArrayList<Component> allComponents;
private List<Juice> juices;
private List<Component> allComponents;
private int minWashes;

public Mixer(String inputFile) {
Expand All @@ -30,18 +27,19 @@ private void readFile(String fileName) {
Juice newJuice;
String line;

while( (line = inputFile.readLine()) != null) {
while ((line = inputFile.readLine()) != null) {
tokenizer = new StringTokenizer(line);

if(tokenizer.hasMoreElements()) {
if (tokenizer.hasMoreElements()) {
newJuice = new Juice();
while(tokenizer.hasMoreTokens()) {
while (tokenizer.hasMoreTokens()) {
newComponent = new Component(tokenizer.nextToken());
if(newComponent.length() > 0) {
if (newComponent.length() > 0) {
newJuice.addComponent(newComponent);

if(!allComponents.contains(newComponent))
if (!allComponents.contains(newComponent)) {
allComponents.add(newComponent);
}
}
}

Expand All @@ -62,22 +60,21 @@ private void readFile(String fileName) {
public void writeAllComponents(String fileName) {
try {
PrintWriter outFile = new PrintWriter(new FileWriter(fileName));
for(Component component: allComponents) {
for (Component component : allComponents) {
outFile.println(component);
}
outFile.flush();
outFile.close();
}
catch (IOException e) {
} catch (IOException e) {
e.printStackTrace();
System.out.println("Problem with output file.");
}
}

public void writeSortedComponents(String fileName, ArrayList<Component> sortedComponents) {
public void writeSortedComponents(String fileName, List<Component> sortedComponents) {
try {
PrintWriter outFile = new PrintWriter(new FileWriter(fileName));
for(Component component: sortedComponents) {
for (Component component : sortedComponents) {
outFile.println(component);
}
outFile.flush();
Expand All @@ -104,7 +101,7 @@ public void countMinWashings() {
setMinWashes(washings.solve());
}

public ArrayList<Juice> getJuices() {
public List<Juice> getJuices() {
return juices;
}

Expand All @@ -120,7 +117,7 @@ public void setMinWashes(int minWashes) {
this.minWashes = minWashes;
}

public ArrayList<Component> getAllComponents() {
public List<Component> getAllComponents() {
return allComponents;
}
}
10 changes: 5 additions & 5 deletions Fruits/Task 1/src/jan/juice/control/SortingThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.Set;
import java.util.List;

/**
* Created by Jan on 10.02.15.
*/
public class SortingThread extends Thread {

private ArrayList<Component> components;
private ArrayList<Component> sortedComponents;
private List<Component> components;
private List<Component> sortedComponents;

public SortingThread(ArrayList<Component> components) {
public SortingThread(List<Component> components) {
this.components = components;
}

Expand All @@ -22,7 +22,7 @@ public void run() {
Collections.sort(sortedComponents);
}

public ArrayList<Component> getSortedComponents() {
public List<Component> getSortedComponents() {
return sortedComponents;
}
}
46 changes: 26 additions & 20 deletions Fruits/Task 1/src/jan/juice/control/Washings.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,28 @@

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

/**
* Created by Jan on 17.02.15.
*/
public class Washings {
private int size;
int[] matching;
boolean[] used;
boolean[] used1;
private int[] matching;
private boolean[] used;
private boolean[] used1;

ArrayList<ArrayList<Integer>> edges;
ArrayList<Juice> juices;
private List<List<Integer>> edges;
private List<Juice> juices;

public Washings(ArrayList<Juice> juices) {
public Washings(List<Juice> juices) {
this.juices = juices;
edges = new ArrayList<ArrayList<Integer>>();
size = juices.size();
this.edges = new ArrayList<List<Integer>>();
this.size = juices.size();

matching = new int[size];
used = new boolean[size];
used1 = new boolean[size];
this.matching = new int[size];
this.used = new boolean[size];
this.used1 = new boolean[size];
}

public int solve() {
Expand All @@ -33,12 +34,12 @@ public int solve() {

private void makeGraph() {
Collections.sort(juices);
edges = new ArrayList<ArrayList<Integer>>();
edges.clear();

for (int i = 0; i < juices.size(); i++) {
edges.add(new ArrayList<Integer>());
for (int j = i + 1; j < juices.size(); j++) {
if(juices.get(i).isSubJuiceOf(juices.get(j))) {
if (juices.get(i).isSubJuiceOf(juices.get(j))) {
edges.get(i).add(j);
}
}
Expand All @@ -53,7 +54,7 @@ private void makeMaxMatching() {

for (int i = 0; i < edges.size(); i++) {
for (int j = 0; j < edges.get(i).size(); j++) {
if(matching[edges.get(i).get(j)] == -1) {
if (matching[edges.get(i).get(j)] == -1) {
matching[edges.get(i).get(j)] = i;
used1[i] = true;
break;
Expand All @@ -62,8 +63,10 @@ private void makeMaxMatching() {
}

for (int i = 0; i < size; i++) {
if(used1[i])
if (used1[i]) {
continue;
}

for (int j = 0; j < size; j++) {
used[j] = false;
}
Expand All @@ -72,13 +75,15 @@ private void makeMaxMatching() {
}

private boolean tryKuhn(int vertex) {
if(used[vertex])
if (used[vertex]) {
return false;
}

used[vertex] = true;
for (int i = 0; i < edges.get(vertex).size(); i++) {
int to = edges.get(vertex).get(i);
if(matching[to] == -1 || tryKuhn(matching[to])) {
List<Integer> g = edges.get(vertex);
for (int i = 0; i < g.size(); i++) {
int to = g.get(i);
if (matching[to] == -1 || tryKuhn(matching[to])) {
matching[to] = vertex;
return true;
}
Expand All @@ -89,8 +94,9 @@ private boolean tryKuhn(int vertex) {
private int countMinWashing() {
int edgesInMatching = 0;
for (int i = 0; i < size; i++) {
if(matching[i] != -1)
if (matching[i] != -1) {
edgesInMatching++;
}
}

return size - edgesInMatching;
Expand Down

0 comments on commit 1d84c58

Please sign in to comment.