-
Notifications
You must be signed in to change notification settings - Fork 0
/
HighScore.java
64 lines (55 loc) · 2.07 KB
/
HighScore.java
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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map.Entry;
import java.util.Comparator;
import java.util.Map;
public class HighScore{
public ArrayList<Entry<String, Double>> highscoreTable = new ArrayList<>();
public void printHighscoreTable(){
System.out.println(" Name | Zeit ");
System.out.println("---------------------------------");
for(int i = 0; i < highscoreTable.size(); i++){
String space = "";
String userName = highscoreTable.get(i).getKey();
for(int j = userName.length(); j < 16; j++){
space += " ";
}
System.out.println(userName + space + "| " + highscoreTable.get(i).getValue() + " sek.");
}
System.out.println();
}
public void sortTable(){
highscoreTable.sort(Comparator.comparing(Entry::getValue));
}
public void saveHighscoreTable(){
try{
BufferedWriter bw = new BufferedWriter(new FileWriter("highscores.txt"));
for(int i = 0; i < highscoreTable.size(); i++){
bw.write(highscoreTable.get(i).getKey() + "," + highscoreTable.get(i).getValue());
bw.newLine();
}
bw.close();
}catch(IOException e){
e.printStackTrace();
}
}
public void loadHighscoreTable(){
try{
BufferedReader br = new BufferedReader(new FileReader("highscores.txt"));
String line = "";
while((line = br.readLine()) != null){
String[] KV_Pair = line.split(",");
String userName = KV_Pair[0];
Double timeNeeded = Double.parseDouble(KV_Pair[1]);
highscoreTable.add(Map.entry(userName,timeNeeded));
}
br.close();
}catch(IOException e){
e.printStackTrace();
}
}
}