-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaveLoad.java
More file actions
79 lines (74 loc) · 3.47 KB
/
SaveLoad.java
File metadata and controls
79 lines (74 loc) · 3.47 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
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class SaveLoad {
public static void UpdateOptions(int topbound, int bottombound, ArrayList<Equation> missedequations)
{
try {
FileWriter writer = new FileWriter("options.txt");
writer.write("bottombound:" + bottombound + "\ntopbound:" + topbound);
writer.write("\nmissedequations:");
for (int i = 0; i < missedequations.size(); i++) {
writer.write(missedequations.get(i).SerializeEquation());
if (i < missedequations.size() - 1) {
writer.write(";");
}
}
writer.write("\n");
writer.close();
} catch (IOException e) {
System.out.println("An error occurred when attempting to write to 'options.txt'. Please let me know if you see this!");
e.printStackTrace();
}
}
public static void ReadOptions()
{
try {
File options = new File("options.txt");
Scanner scanner_file = new Scanner(options);
while (scanner_file.hasNextLine()) {
String line = scanner_file.nextLine();
String[] parts = line.split(":");
if (parts.length == 2) {
String key = parts[0].trim();
if (key.equals("bottombound")) {
int value = Integer.parseInt(parts[1].trim());
Equation.SetBottomBound(value);
} else if (key.equals("topbound")) {
int value = Integer.parseInt(parts[1].trim());
Equation.SetTopBound(value);
} else if (key.equals("missedequations")) {
if (parts[1] != null)
{
int equationCount = 0;
String[] serializedEquations = parts[1].trim().split(";");
for (String s : serializedEquations) {
if (!s.isBlank()) {
Equation equation = Equation.DeserializeEquation(s);
Guesser.AddToMissedEquations(equation);
equationCount++;
}
}
String result = (equationCount == 1) ? "Successfully loaded " + equationCount + " missed equation!" : "Successfully loaded " + equationCount + " missed equations!";
System.out.println(result);
}
}
} else if (parts.length == 1 || parts[0] == "missedequations") {
continue;
} else {
System.out.println("Invalid line format in 'options.txt': " + line + ". If you see this, please let me know!");
}
}
scanner_file.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred; 'options.txt' couldn't be found. If you see this, please let me know!");
e.printStackTrace();
} catch (NumberFormatException e) {
System.out.println("Error parsing number from 'options.txt.'' If you see this, please let me know!");
e.printStackTrace();
}
}
}