-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
183 lines (151 loc) · 5.01 KB
/
Main.java
File metadata and controls
183 lines (151 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import java.util.Scanner;
class Main {
/**
* Asks an integer in the range [from, to]
*/
public static int askIntBtw(int from, int to) {
Scanner scan = new Scanner(System.in);
int answer = -1;
while (answer == -1) {
System.out.print("Votre reponse : ");
try {
answer = scan.nextInt();
if (answer < from || answer > to) {
System.out.println("Veuillez entrer un nombre entre " + from + " et " + to + ".");
answer = -1;
}
} catch (RuntimeException e) {
System.out.println("Veuillez entrer un nombre.");
scan.nextLine(); // Makes the buffer empty
answer = -1;
}
}
System.out.println(); // Break line before results
return answer;
}
/**
* Displays menu and asks for an action
*/
public static int menuMetro() {
System.out.println("-------------------------------------------------");
System.out.println("Menu :");
System.out.println("\t0- Quitter.");
System.out.println("\t1- Trouver les 10 elements les plus connectes.");
System.out.println("\t2- Trouver le plus court chemin d'un element a un autre.");
System.out.println("\t3- Trouver le plus court chemin d'un element a un autre en passant par tous les groupes/composantes.");
System.out.println("Que voulez-vous faire ?");
return Main.askIntBtw(0, 3);
}
public static int menuPerson() {
System.out.println("-------------------------------------------------");
System.out.println("Menu :");
System.out.println("\t0- Quitter.");
System.out.println("\t1- Trouver les 10 elements les plus connectes.");
System.out.println("\t2- Trouver le plus court chemin d'un element a un autre.");
System.out.println("\t3- Verifier les six degrees de separation.");
System.out.println("Que voulez-vous faire ?");
return Main.askIntBtw(0, 3);
}
/**
* Ask the user to enter a node name.
*/
public static <T extends Linkable> String askName(Network<T> net) {
Scanner scan = new Scanner(System.in);
String answer = null;
Linkable elementFound = null;
while (elementFound == null) {
System.out.print("Entrer un nom : ");
answer = scan.nextLine().trim();
elementFound = net.get(answer);
}
System.out.println("La reponse \"" + answer + "\" a ete enregistree.");
return answer;
}
/**
* Showcase for metro
*/
public static void showMetroCase(String fileMetro) {
boolean wantToContinue = true;
String startName;
String endName;
Node itinerary;
// Loading the network with the given file
Network<Station> net = new Network<>(new Station());
if (!net.fill(fileMetro)) return;
System.out.println("Les donnees sont chargees.");
// Interaction with the user
while (wantToContinue) {
int choice = Main.menuMetro();
switch(choice) {
case 0: // Quit
wantToContinue = false;
break;
case 1: // Showing the 10 most connected Linkables
net.showTop10();
break;
case 2: // Ask for two names to find the shortest path
startName = Main.<Station>askName(net);
endName = Main.<Station>askName(net);
System.out.println(net.getItinerary(startName, endName));
break;
case 3: // Ask for two names to find the shortest path
startName = Main.<Station>askName(net);
endName = Main.<Station>askName(net);
itinerary = net.getBestItineraryWithAllComponents(startName, endName);
System.out.println(itinerary + "\nLongueur : " + itinerary.size());
break;
}
}
}
/**
* Showcase for social network
*/
public static void showSocialNetCase(String filePerson) {
boolean wantToContinue = true;
String startName;
String endName;
Node itinerary;
// Loading the network with the given file
Network<Person> net = new Network<>(new Person());
if (!net.fill(filePerson)) return;
System.out.println("Les donnees sont chargees.");
// Interaction with the user
while (wantToContinue) {
int choice = Main.menuPerson();
switch(choice) {
case 0: // Quit
wantToContinue = false;
break;
case 1: // Showing the 10 most connected Linkables
net.showTop10();
break;
case 2: // Ask for two names to find the shortest path
startName = Main.<Person>askName(net);
endName = Main.<Person>askName(net);
System.out.println(net.getItinerary(startName, endName));
break;
case 3: // Ask for two names to find the shortest path
net.checkSeparationDegree();
break;
}
}
}
public static void main(String [] args) {
System.out.println("Selectionnez :");
System.out.println("\t0- metro-wl.txt");
System.out.println("\t1- sonet-200.txt");
System.out.println("\t2- sonet-1000.txt");
int mode = Main.askIntBtw(0, 2);
switch (mode) {
case 0:
Main.showMetroCase("metro-wl.txt");
break;
case 1:
Main.showSocialNetCase("sonet-200.txt");
break;
case 2:
Main.showSocialNetCase("sonet-1000.txt"); // Can change the filename for social network case
break;
}
}
}