-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatystyki.java
More file actions
232 lines (211 loc) · 7.92 KB
/
Statystyki.java
File metadata and controls
232 lines (211 loc) · 7.92 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import greenfoot.*;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Comparator;
import java.util.Collections;
/**
* Write a description of class Statystyki here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Statystyki
{
private static Statystyki instancja = null;
private Map<String,String> wszystkieWyniki = new HashMap<String, String>();
private Map<String, Integer> gracze = new HashMap<String, Integer>();
private String nazwaPliku = "GameStats.txt";
private String sciezkaPliku = System.getProperty("user.home");
//constructor
private Statystyki() throws IOException {
czytajStatystykiZPliku(nazwaPliku);
}
// singleton - return instancja of class Statystyki
public static Statystyki getInstance() {
if(instancja == null) {
try {
instancja = new Statystyki();
} catch (IOException e) {
System.out.println("IOException caught during file operations");
}
}
return instancja;
}
/**
* Read stats from file.
*
* @param nazwaPliku the file name
* @throws IOException Signals that an I/O exception has occurred.
*/
private void czytajStatystykiZPliku(String nazwaPliku) throws IOException{
List<String> lista = new ArrayList<String>();
BufferedReader br = null;
FileReader czytajPlik = null;
File file = null;
try {
file = new File(sciezkaPliku + File.separator + nazwaPliku);
if (file.exists() && file.isFile()) {
czytajPlik = new FileReader(file);
} else {
if (file.createNewFile()) {
wszystkieWyniki.put("PlayerWithHighestScore", "none");
wszystkieWyniki.put("HighestScore", "0");
return;
}
}
br = new BufferedReader(czytajPlik);
String linia;
//Read first line - overal Statystyki
if ((linia = br.readLine()) != null) {
//put wszystkieWyniki to temp ArrayList
lista = Arrays.asList(linia.split(";"));
}
//put overal stats to HashMap Key:Value
for (int i = 0; i < lista.size(); ++i) {
wszystkieWyniki.put((lista.get(i).split("="))[0], (lista.get(i).split("="))[1]);
}
while((linia = br.readLine()) != null) {
gracze.put(linia.split("=")[0], Integer.parseInt(linia.split("=")[1]));
}
} finally {
if (br != null) {
br.close();
}
}
}
/**
* Gets the player wynik.
*
* @param nazwaGracza the player name
* @return the player wynik
*/
public int wezWynikGracza(String nazwaGracza) {
Integer wynik = gracze.get(nazwaGracza);
if (wynik != null) {
return wynik.intValue();
}
return 0;
}
/**
* Sets the player wynik.
*
* @param nazwaGracza the player name
* @param wynik the wynik
*/
public void ustawWynikGracza(String nazwaGracza, int nowyWynik) {
// if new wynik is lover than old wynik leave old one
int staryWynik = wezWynikGracza(nazwaGracza);
if (nowyWynik > staryWynik ) {
gracze.put(nazwaGracza, nowyWynik);
}
}
/**
* Gets the highest wynik.
*
*/
public String wezNajwyzszyWynik() {
return wszystkieWyniki.get("PlayerWithHighestScore") + ":" + wszystkieWyniki.get("HighestScore");
}
/**
* Save stats to file.
*
* @throws IOException Signals that an I/O exception has occurred.
*/
public void zapiszStatystykiDoPliku() throws IOException{
BufferedWriter bw = null;
FileWriter fileWriter = null;
File file = null;
// before saving statistics to file update high wynik
aktualizujNajwyzszyWynik();
try {
file = new File(sciezkaPliku + File.separator + nazwaPliku);
if (file.exists() && file.isFile()) {
fileWriter = new FileWriter(file);
}
bw = new BufferedWriter(fileWriter);
StringBuilder stringBuilder = new StringBuilder();
// write first line to file with overal statistics
for (String key : wszystkieWyniki.keySet()) {
if (stringBuilder.length() > 0) {
stringBuilder.append(";");
}
String value = wszystkieWyniki.get(key);
stringBuilder.append(key);
stringBuilder.append("=");
stringBuilder.append(value);
}
bw.write(stringBuilder.toString());
bw.newLine();
stringBuilder.setLength(0);
// save individual gracze wynik to file (one line per one player and wynik)
for (String key : gracze.keySet()) {
if (stringBuilder.length() > 0) {
stringBuilder.append(";");
}
String value = gracze.get(key).toString();
stringBuilder.append(key);
stringBuilder.append("=");
stringBuilder.append(value);
bw.write(stringBuilder.toString());
bw.newLine();
stringBuilder.setLength(0);
}
} finally {
if (bw != null) {
bw.close();
}
}
}
/**
* Update high wynik in overal statistics.
*/
private void aktualizujNajwyzszyWynik() {
Map<String, Integer> map = sortujPoWynikach(gracze);
// sorted hashmap
Set<Entry<String, Integer>> set = map.entrySet();
Iterator<Entry<String, Integer>> iterator = set.iterator();
if (iterator.hasNext()) {
Map.Entry<String, Integer> me2 = iterator.next();
wszystkieWyniki.put("PlayerWithHighestScore", me2.getKey());
wszystkieWyniki.put("HighestScore", me2.getValue().toString());
}
}
/**
* Sort by scores.
*
* @param gracze the gracze
* @return the hash map
*/
private HashMap<String, Integer> sortujPoWynikach(Map<String, Integer> gracze) {
Set<Entry<String, Integer>> zbior = gracze.entrySet();
//convert set () of Entry to LinkedList
List<Entry<String, Integer>> lista = new LinkedList<Entry<String, Integer>>(zbior);
// Defined Custom Comparator
Collections.sort(lista, new Comparator<Entry<String, Integer>>() {
@Override
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
return ((Comparable<Integer>) (o2).getValue())
.compareTo((o1).getValue());
}
});
//sorted list -> HasMap
// using LinkedHashMap to preserve the insertion order
HashMap<String, Integer> posortowanaMapa = new LinkedHashMap<String, Integer>();
Iterator<Entry<String, Integer>> iterator = lista.iterator();
while(iterator.hasNext()) {
Map.Entry<String, Integer> wpis = iterator.next();
posortowanaMapa.put(wpis.getKey(), wpis.getValue());
}
return posortowanaMapa;
}
}