Skip to content

Commit c81e9f2

Browse files
Merge pull request #19 from thehighestmath/Separation_GUI_and_logic
Separation gui and logic
2 parents 12697a9 + 7eb7db9 commit c81e9f2

File tree

14 files changed

+1152
-190
lines changed

14 files changed

+1152
-190
lines changed

build.gradle

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ repositories {
2222
mavenCentral()
2323
}
2424

25+
26+
test {
27+
useJUnitPlatform()
28+
}
29+
2530
dependencies {
26-
testCompile group: 'junit', name: 'junit', version: '4.12'
31+
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'
32+
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.2'
2733
}
17.1 KB
Binary file not shown.

build/resources/test/test1.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
7
2+
e d 7
3+
e c 6
4+
a e 1
5+
c d 2
6+
a b 3
7+
b e 4
8+
b c 5
9+
10+
11+
--- Result ---
12+
From To Distance
13+
a e 1
14+
c d 2
15+
a b 3
16+
b c 5
17+
a b c d e

build/tmp/fatJar/MANIFEST.MF

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Manifest-Version: 1.0
2+
Implementation-Title: Gradle Jar File Example
3+
Main-Class: ru.etu.practice.Main
4+

build/tmp/jar/MANIFEST.MF

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Manifest-Version: 1.0
2+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package ru.etu.practice;
2+
3+
public class Edge {
4+
@Override
5+
public String toString() {
6+
return "Edge " + " " + from + "—" + to + " " + distance;
7+
}
8+
9+
/**
10+
* way from -> to == way to -> to
11+
*/
12+
public Node from;
13+
public Node to;
14+
public int distance;
15+
16+
public Edge(Node from, Node to, int distance) {
17+
this.from = from;
18+
this.to = to;
19+
this.distance = distance;
20+
21+
}
22+
23+
@Override
24+
public int hashCode() {
25+
return super.hashCode();
26+
}
27+
28+
@Override
29+
public boolean equals(Object o) {
30+
if (this == o) {
31+
return true;
32+
}
33+
if (o == null) {
34+
return false;
35+
}
36+
Edge other = (Edge) o;
37+
return this.to.getName() == other.to.getName() && this.from.getName() == other.from.getName() && this.distance == other.distance ||
38+
this.from.getName() == other.to.getName() && this.to.getName() == other.from.getName() && this.distance == other.distance;
39+
}
40+
41+
public boolean equalsWay(Edge other){
42+
if (this == other) {
43+
return true;
44+
}
45+
if (other == null) {
46+
return false;
47+
}
48+
return this.to.getName() == other.to.getName() && this.from.getName() == other.from.getName() ||
49+
this.from.getName() == other.to.getName() && this.to.getName() == other.from.getName();
50+
}
51+
52+
}

src/main/java/ru/etu/practice/Graph.java

Lines changed: 34 additions & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -2,199 +2,56 @@
22

33
import java.util.*;
44

5-
class Edge {
6-
/**
7-
* way from -> to == way to -> to
8-
*/
9-
public char from;
10-
public char to;
11-
public int distance;
12-
13-
public Edge(char from, char to, int distance) {
14-
this.from = from;
15-
this.to = to;
16-
this.distance = distance;
17-
18-
}
19-
}
20-
215
public class Graph {
22-
private static final int SIZE = 26;
23-
24-
private int countVertexes;
25-
26-
private final int[][] inputGraph;
27-
private final int[][] outputGraph;
28-
29-
30-
private final List<Edge> inputEdges;
31-
private final List<Edge> outputEdges;
32-
33-
private final Set<Character> inputVertices;
34-
private final List<Set<Character>> outputVertices;
35-
36-
37-
public Graph() {
38-
inputGraph = new int[SIZE][SIZE];
39-
outputGraph = new int[SIZE][SIZE];
40-
inputEdges = new LinkedList<>();
41-
outputEdges = new LinkedList<>();
42-
inputVertices = new HashSet<>();
43-
outputVertices = new LinkedList<>();
44-
}
45-
46-
public void readGraph() {
47-
// TODO adapter
48-
System.out.println("Ready to enter");
49-
try (Scanner scanner = new Scanner(System.in)) {
50-
// scanner.useDelimiter("[\n ]");
51-
int n = Integer.parseInt(scanner.nextLine());
52-
// System.out.println(n);
53-
for (int i = 0; i < n; i++) {
54-
String[] parts = scanner.nextLine().split(" ");
55-
char from = parts[0].toLowerCase().toCharArray()[0];
56-
char to = parts[1].toLowerCase().toCharArray()[0];
57-
int distance = Integer.parseInt(parts[2]);
58-
59-
assert (from >= 'a' && from <= 'z');
60-
assert (to >= 'a' && to <= 'z');
61-
assert (distance > 0);
62-
63-
while (inputGraph[from - 'a'][to - 'a'] != 0) {
64-
parts = scanner.nextLine().split(" ");
65-
from = parts[0].toLowerCase().toCharArray()[0];
66-
to = parts[1].toLowerCase().toCharArray()[0];
67-
distance = Integer.parseInt(parts[2]);
68-
}
6+
// private static final int SIZE = 26;
697

70-
Edge edge = new Edge(from, to, distance);
71-
inputEdges.add(edge);
8+
private List<Edge> inputEdges;
9+
private List<Node> inputVertices;
10+
public boolean isModified = false;
7211

73-
Character vertex1 = from;
74-
Character vertex2 = to;
75-
inputVertices.add(vertex1);
76-
inputVertices.add(vertex2);
12+
// public
7713

78-
// System.out.println(from + " " + to + " " + distance);
79-
inputGraph[from - 'a'][to - 'a'] = distance;
80-
inputGraph[to - 'a'][from - 'a'] = distance;
81-
}
82-
}
83-
countVertexes = inputVertices.size();
14+
public List<Edge> getInputEdges() {
15+
return inputEdges;
8416
}
8517

86-
public void printGraph() {
87-
for (int i = 0; i < SIZE; i++) {
88-
for (int j = 0; j < SIZE; j++) {
89-
System.out.print(inputGraph[i][j] + " ");
90-
}
91-
System.out.println();
92-
}
18+
public List<Node> getInputVertices() {
19+
return inputVertices;
9320
}
9421

95-
private void printEdges(List<Edge> edges) {
96-
System.out.println("From\tTo\t\tDistance");
97-
// System.out.println("F\tT\tD");
98-
for (Edge edge : edges) {
99-
System.out.println(
100-
edge.from + "\t\t" +
101-
edge.to + "\t\t" +
102-
edge.distance
103-
);
104-
}
105-
}
106-
107-
public void sortEdges() {
108-
inputEdges.sort(new Comparator<Edge>() {
109-
@Override
110-
public int compare(Edge edge1, Edge edge2) {
111-
int distance1 = edge1.distance;
112-
int distance2 = edge2.distance;
113-
return Integer.compare(distance1, distance2);
114-
}
115-
});
116-
System.err.println(State.SORT);
117-
}
118-
119-
private State nextStep(Edge edge) {
120-
State state = null;
121-
Set<Character> tempVertexes = new HashSet<>();
122-
Character vertex1 = edge.from;
123-
Character vertex2 = edge.to;
124-
tempVertexes.add(vertex1);
125-
tempVertexes.add(vertex2);
126-
127-
boolean hasFound = false;
128-
for (Set<Character> currently : outputVertices) {
129-
if (isOld(currently, tempVertexes)) {
130-
return State.LOOP;
131-
}
132-
if (isCommon(currently, tempVertexes)) {
133-
hasFound = true;
134-
currently.addAll(tempVertexes);
135-
state = State.APPEND;
136-
}
137-
}
138-
139-
for (int i = 0; i < outputVertices.size(); i++) {
140-
for (int j = i + 1; j < outputVertices.size(); j++) {
141-
Set<Character> first = outputVertices.get(i);
142-
Set<Character> second = outputVertices.get(j);
143-
if (isCommon(first, second)) {
144-
state = State.CONNECT_COMPONENTS;
145-
first.addAll(second);
146-
second.clear();
147-
}
148-
}
149-
}
150-
151-
outputEdges.add(edge);
152-
if (!hasFound) {
153-
state = State.NEW_COMPONENT;
154-
outputVertices.add(tempVertexes);
155-
}
156-
if (outputVertices.get(0).size() == countVertexes) {
157-
state = State.END;
158-
}
159-
// assert state != null;
160-
return state;
22+
public Graph() {
23+
this.inputEdges = new LinkedList<>();
24+
this.inputVertices = new LinkedList<>();
16125
}
16226

163-
public void kraskal() {
164-
for (Edge edge : inputEdges) {
165-
State state = nextStep(edge);
166-
System.err.println(state);
167-
if (state == State.END) {
168-
break;
169-
}
170-
}
171-
}
27+
// public void initGraph(List<Edge> inputEdges, List<Node> inputVertices) {
28+
// this.inputEdges = new LinkedList<>(inputEdges);
29+
// this.inputVertices = new LinkedList<>(inputVertices);
30+
// }
17231

173-
private boolean isCommon(Set<Character> first, Set<Character> second) {
174-
Set<Character> all = new HashSet<>();
175-
all.addAll(first);
176-
all.addAll(second);
177-
return all.size() != (first.size() + second.size());
32+
public void addEdge(Node from, Node to, int distance) {
33+
Edge newEdge = new Edge(from, to, distance);
34+
for (Edge edge : inputEdges)
35+
if (edge.equalsWay(newEdge))
36+
return;
37+
inputEdges.add(newEdge);
38+
isModified = true;
17839
}
17940

180-
private boolean isOld(Set<Character> first, Set<Character> second) {
181-
Set<Character> all = new HashSet<>();
182-
all.addAll(first);
183-
all.addAll(second);
184-
return all.size() == first.size();
41+
public void addVertex(Node node) {
42+
if (!inputVertices.contains(node))
43+
inputVertices.add(node);
44+
isModified = true;
18545
}
18646

187-
public void printResult() {
188-
printEdges(outputEdges);
189-
for (Set<Character> vertices : outputVertices) {
190-
for (Character vertex : vertices) {
191-
System.out.print(vertex + " ");
192-
}
193-
System.out.println();
194-
}
47+
public void addVertex() {
48+
int len = inputVertices.size();
49+
inputVertices.add(new Node((char)('a' + len)));
19550
}
19651

197-
public void printInputEdges() {
198-
printEdges(inputEdges);
52+
public void clear() {
53+
inputVertices.clear();
54+
inputEdges.clear();
55+
isModified = false;
19956
}
20057
}

0 commit comments

Comments
 (0)