-
Notifications
You must be signed in to change notification settings - Fork 0
/
Options.java
197 lines (170 loc) · 4.7 KB
/
Options.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
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
/**
* @version 1.0.0, 26 May 2023
* @author Andrew Kim and Dylan Nguyen
*
* Manages stockbroker options and stores counts of each share
*/
import java.awt.Font;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class Options {
private static Hashtable<String, String> options = new Hashtable<>();
private static Hashtable<String, Integer> shareCounts = new Hashtable<>();
private static boolean safeToClose = true;
/**
* Loads options from file resources/options.txt
* Follows the format <option>:<value>
*/
public static void loadOptions() {
String filepath = "./resources/options.txt";
try {
Scanner scanner = new Scanner(new File(filepath));
scanner.useDelimiter("\n");
int count = 0;
while (scanner.hasNext()) {
String next = scanner.next();
String key = next.split(":")[0].split("\r")[0];
String value = next.split(":")[1].split("\r")[0];
if(count < 5) {
options.put(key, value);
}else{
shareCounts.put(key, Integer.parseInt(value));
}
count++;
}
scanner.close();
// print hashtables
// System.out.println(options.toString());
// System.out.println(shareCounts.toString());
} catch (Exception e) {
System.out.println("Could not read file: " + e);
}
}
/**
* Writes values of hashtables to txt
*/
public static void saveOptions() {
safeToClose = false;
options.put("networth", "" + Portfolio.getTotalNetworth());
ArrayList<String> optionsList = new ArrayList<String>();
// go through options
for (int i = 0; i < options.size(); i++) {
String keyName = options.keySet().toArray()[i].toString();
String keyValue = options.get(keyName);
optionsList.add(keyName + ":" + keyValue);
}
// go through shareCounts
for(int i = 0; i < shareCounts.size(); i++) {
String keyName = shareCounts.keySet().toArray()[i].toString();
String keyValue = "" + shareCounts.get(keyName);
optionsList.add(keyName + ":" + keyValue);
}
FileWriter writer;
try {
writer = new FileWriter("./resources/options.txt");
for (String line : optionsList) {
writer.append(line + "\n");
}
writer.close();
} catch (IOException e) {
System.out.println("Error writing CSV file: " + e);
}
safeToClose = true;
}
/**
* Gets the number of shares of given stocks in hashtable
* @param ticker ticker of stock
* @return number of shares of stock
*/
public static int getStockCount(String ticker) {
return shareCounts.get(ticker);
}
/**
* Sets the number of shares for a given stock in hashtable
* @param stock ticker of stock
* @param shares number of shares of stock
*/
public static void setStockCount(String stock, int shares) {
shareCounts.put(stock, shares);
}
/**
* Returns the font.
*
* @return the font.
*/
public static Font getFont() {
return new Font("Arial", Font.PLAIN, Integer.valueOf(options.get("fontsize")));
}
/**
* Returns the font used with a custom font size
*
* @param size the size of the font.
* @return the font used by the program.
*/
public static Font getFont(int size) {
return new Font("Arial", Font.PLAIN, size);
}
/**
* Sets the font used by the program.
*
* @param f the font to be used by the program.
*/
public static void setFont(Font f) {
options.put("fontsize", "" + f.getSize());
saveOptions();
}
/**
* Sets the status of popups.
*
* @param b whether or not popups should be enabled.
*/
public static void setPopups(boolean b) {
String s = b ? "true" : "false";
options.put("popups", s);
saveOptions();
}
/**
* Returns whether or not popups are enabled.
*
* @return whether or not popups are enabled.
*/
public static boolean popupsEnabled() {
return options.get("popups").equals("true");
}
/**
* Sets the fullscreen option
*
* @param b whether or not the program should start in fullscreen.
*/
public static void setStartFullscreen(boolean b) {
String s = b ? "true" : "false";
options.put("startfullscreen", s);
saveOptions();
}
/**
* Returns the fullscreen option
*
* @return whether or not the program should start in fullscreen.
*/
public static boolean getStartFullscreen() {
return options.get("startfullscreen").equals("true");
}
public static void setSimulationSpeed(int speed){
options.put("simulationspeed", ""+speed);
saveOptions();
}
public static int getSimulationSpeed(){
return Integer.parseInt(options.get("simulationspeed"));
}
public static int getSimulationSpeedScaled(){
return 2500/Integer.parseInt(options.get("simulationspeed"));
}
public static double getNetWorth() {
return Double.parseDouble(options.get("networth"));
}
public static boolean isSafeToClose(){
return safeToClose;
}
}