-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRanking.java
More file actions
105 lines (85 loc) · 3.01 KB
/
Copy pathRanking.java
File metadata and controls
105 lines (85 loc) · 3.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
import java.io.*;
public class Ranking {
private static int[] pontos = new int[5];
private static String[] nomes = new String[5];
public static int getMenor(){
return pontos[4];
}
private static String nomeArquivo = "ranking.txt";
public static synchronized void rankingRead() {
try {
BufferedReader br = new BufferedReader(new FileReader(nomeArquivo));
String linha;
int i = 0;
while ((linha = br.readLine()) != null && i < 5) {
String[] partes = linha.split(" ");
if (partes.length == 2) {
nomes[i] = partes[0];
pontos[i] = Integer.parseInt(partes[1]);
i++;
}
}
br.close();
} catch (IOException e) {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(nomeArquivo));
String[] dadosIniciais = {"AJR 100", "PED 100", "XXD 100", "PAO 100", "CAP 100"};
for (String dado : dadosIniciais) {
bw.write(dado);
bw.newLine();
}
bw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
private static synchronized void rankingWrite(){
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(nomeArquivo));
for (int i = 0; i < nomes.length; i++) {
if (nomes[i] != null && !nomes[i].isEmpty()) {
bw.write(nomes[i] + " " + pontos[i]);
bw.newLine();
}
}
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static synchronized boolean adicionar(String novoNome, int novoPontos) {
int posicaoInsercao = -1;
for (int i = 0; i < nomes.length; i++) {
if (novoPontos > pontos[i]) {
posicaoInsercao = i;
break;
}
}
if(posicaoInsercao >= 0 ) {
for (int i = nomes.length - 1; i > posicaoInsercao; i--) {
nomes[i] = nomes[i - 1];
pontos[i] = pontos[i - 1];
}
nomes[posicaoInsercao] = novoNome;
pontos[posicaoInsercao] = novoPontos;
rankingWrite();
return true;
}
else
return false;
}
public static synchronized void imprimirRanking (DataInputStream in, DataOutputStream out) throws IOException, InterruptedException {
mode.read(out);
out.writeUTF("Ranking:");
for (int i = 0; i < nomes.length; i++) {
mode.read(out);
out.writeUTF("Nome: " + nomes[i] + ", Pontos: " + pontos[i]);
}
Thread.sleep(3000);
mode.read(out);
out.writeUTF("\n\n\n\n\nVocê será retornado ao menu\n\n\n\n\n");
Thread.sleep(5000);
protocolServer.menu(in,out);
}
}