From 1d84c5827fd1e5ee2637cc8e458661d69c9993a7 Mon Sep 17 00:00:00 2001 From: pivs96 Date: Thu, 26 Feb 2015 20:45:29 +0300 Subject: [PATCH] Real code cleaning --- .../src/jan/juice/control/Component.java | 6 +-- .../Task 1/src/jan/juice/control/Juice.java | 16 ++++--- Fruits/Task 1/src/jan/juice/control/Main.java | 2 +- .../Task 1/src/jan/juice/control/Mixer.java | 33 ++++++------- .../src/jan/juice/control/SortingThread.java | 10 ++-- .../src/jan/juice/control/Washings.java | 46 +++++++++++-------- 6 files changed, 59 insertions(+), 54 deletions(-) diff --git a/Fruits/Task 1/src/jan/juice/control/Component.java b/Fruits/Task 1/src/jan/juice/control/Component.java index a70e9e5..bbd777e 100644 --- a/Fruits/Task 1/src/jan/juice/control/Component.java +++ b/Fruits/Task 1/src/jan/juice/control/Component.java @@ -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 @@ -34,6 +34,6 @@ public void setName(String name) { } public int length() { - return name.length(); + return this.name.length(); } } diff --git a/Fruits/Task 1/src/jan/juice/control/Juice.java b/Fruits/Task 1/src/jan/juice/control/Juice.java index 2970723..24430ae 100644 --- a/Fruits/Task 1/src/jan/juice/control/Juice.java +++ b/Fruits/Task 1/src/jan/juice/control/Juice.java @@ -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 { - private TreeSet components; +public class Juice implements Comparable { + private Set components; public Juice() { this.components = new TreeSet(); } - public TreeSet getComponents() { + public Set 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(); } } diff --git a/Fruits/Task 1/src/jan/juice/control/Main.java b/Fruits/Task 1/src/jan/juice/control/Main.java index 809971e..b74c7bf 100644 --- a/Fruits/Task 1/src/jan/juice/control/Main.java +++ b/Fruits/Task 1/src/jan/juice/control/Main.java @@ -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(); } diff --git a/Fruits/Task 1/src/jan/juice/control/Mixer.java b/Fruits/Task 1/src/jan/juice/control/Mixer.java index 3e0f856..f44a937 100644 --- a/Fruits/Task 1/src/jan/juice/control/Mixer.java +++ b/Fruits/Task 1/src/jan/juice/control/Mixer.java @@ -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 juices; - private ArrayList allComponents; + private List juices; + private List allComponents; private int minWashes; public Mixer(String inputFile) { @@ -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); + } } } @@ -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 sortedComponents) { + public void writeSortedComponents(String fileName, List sortedComponents) { try { PrintWriter outFile = new PrintWriter(new FileWriter(fileName)); - for(Component component: sortedComponents) { + for (Component component : sortedComponents) { outFile.println(component); } outFile.flush(); @@ -104,7 +101,7 @@ public void countMinWashings() { setMinWashes(washings.solve()); } - public ArrayList getJuices() { + public List getJuices() { return juices; } @@ -120,7 +117,7 @@ public void setMinWashes(int minWashes) { this.minWashes = minWashes; } - public ArrayList getAllComponents() { + public List getAllComponents() { return allComponents; } } diff --git a/Fruits/Task 1/src/jan/juice/control/SortingThread.java b/Fruits/Task 1/src/jan/juice/control/SortingThread.java index d76860d..3ddab94 100644 --- a/Fruits/Task 1/src/jan/juice/control/SortingThread.java +++ b/Fruits/Task 1/src/jan/juice/control/SortingThread.java @@ -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 components; - private ArrayList sortedComponents; + private List components; + private List sortedComponents; - public SortingThread(ArrayList components) { + public SortingThread(List components) { this.components = components; } @@ -22,7 +22,7 @@ public void run() { Collections.sort(sortedComponents); } - public ArrayList getSortedComponents() { + public List getSortedComponents() { return sortedComponents; } } diff --git a/Fruits/Task 1/src/jan/juice/control/Washings.java b/Fruits/Task 1/src/jan/juice/control/Washings.java index 9909327..4dcef18 100644 --- a/Fruits/Task 1/src/jan/juice/control/Washings.java +++ b/Fruits/Task 1/src/jan/juice/control/Washings.java @@ -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> edges; - ArrayList juices; + private List> edges; + private List juices; - public Washings(ArrayList juices) { + public Washings(List juices) { this.juices = juices; - edges = new ArrayList>(); - size = juices.size(); + this.edges = new ArrayList>(); + 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() { @@ -33,12 +34,12 @@ public int solve() { private void makeGraph() { Collections.sort(juices); - edges = new ArrayList>(); + edges.clear(); for (int i = 0; i < juices.size(); i++) { edges.add(new ArrayList()); 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); } } @@ -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; @@ -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; } @@ -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 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; } @@ -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;