-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwitches.java
More file actions
191 lines (172 loc) · 7.55 KB
/
Switches.java
File metadata and controls
191 lines (172 loc) · 7.55 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
/**
* Switches.java
* A program to filter a list of keyboard switches from a properly-formatted .csv file
* To use, place a .csv file of the switch chart in the same directory as this file and run. Output will be to terminal.
* @author Jordan Rudman
* @version 2.1
*/
import java.io.*;
import java.util.*;
public class Switches {
static String actuForceMin;
static String actuForceMax;
static String preTravelMin;
static String preTravelMax;
static String totalTravelMin;
static String totalTravelMax;
static String feel;
static String mount;
/**
* Main method, runs the program.
*/
public static void main(String[] args) {
try {
Scanner input = new Scanner(System.in);
// Actuation force
System.out.println("Enter desired minimum actuation force in g (or press Enter for no minimum):");
String actuForceMinInput = input.nextLine();
actuForceMin = actuForceMinInput.isEmpty() ? "0" : actuForceMinInput;
System.out.println("Enter desired maximum actuation force in g (or press Enter for no maximum):");
String actuForceMaxInput = input.nextLine();
actuForceMax = actuForceMaxInput.isEmpty() ? "999" : actuForceMaxInput;
// Pre-travel
System.out.println("Enter desired minimum pre-travel in mm (or press Enter for no minimum):");
String preTravelMinInput = input.nextLine();
preTravelMin = preTravelMinInput.isEmpty() ? "0" : preTravelMinInput;
System.out.println("Enter desired maximum pre-travel in mm (or press Enter for no maximum):");
String preTravelMaxInput = input.nextLine();
preTravelMax = preTravelMaxInput.isEmpty() ? "999" : preTravelMaxInput;
// Total travel
System.out.println("Enter desired minimum total travel in mm (or press Enter for no minimum):");
String totalTravelMinInput = input.nextLine();
totalTravelMin = totalTravelMinInput.isEmpty() ? "0" : totalTravelMinInput;
System.out.println("Enter desired maximum total travel in mm (or press Enter for no maximum):");
String totalTravelMaxInput = input.nextLine();
totalTravelMax = totalTravelMaxInput.isEmpty() ? "999" : totalTravelMaxInput;
// Feel
System.out.println("Enter desired switch feel (tactile, clicky, linear, silent tactile, silent linear, or press Enter for any):");
feel = input.nextLine();
if (feel.isEmpty()) {
feel = "none"; // Set to "none" if no preference
}
// Mount
System.out.println("Enter desired mount type (plate, PCB, or press Enter for any):");
mount = input.nextLine();
if (mount.isEmpty()) {
mount = "none"; // Set to "none" if no preference
}
input.close();
List<String> list = new ArrayList<String>(); // the list to hold matching switches
File file = new File("./switchesformatted.csv");
Scanner sc = new Scanner(file);
sc.useDelimiter(",");
String line = sc.nextLine(); // skip first line with column names
while(sc.hasNextLine()) { // run until EOF
line = sc.nextLine();
String[] arr = line.split(",");
boolean feelCheck = feelCheck(arr[1]);
boolean forceCheck = actuationCheck(arr[2]);
boolean preTCheck = preTCheck(arr[3]);
boolean totalTCheck = totalTCheck(arr[4]);
boolean mountCheck = mountCheck(arr[5]);
if(forceCheck && preTCheck && mountCheck && feelCheck && totalTCheck) {
list.add("\n" + arr[0]);
}
}
// sort alphabetically and print list + size
System.out.println(list);
System.out.println("Switch count: " + list.size());
sc.close();
}
catch(FileNotFoundException e) {
System.out.println("File not found! Make sure the file is in the same directory as this program and that your .csv file is named 'switchesformatted.csv' unless you've edited this program to change the file name input!");
}
}
/**
* Filters switches by feel
* @param String The feel type to check
* @return True if feel type matches, false otherwise
*/
public static boolean feelCheck(String str) {
if(feel.equalsIgnoreCase("none")) {
return true;
}
if(str.equalsIgnoreCase(feel)){
return true;
}
else if(str.contains("/")) {
String[] splitFeels = str.split("/");
for(String aFeel : splitFeels) {
if(aFeel.equalsIgnoreCase(feel)) {
return true;
}
}
}
return false;
}
/**
* Filters switches by actuation force
* @param String The actuation force to check
* @return True if actuation force is within range, false otherwise
*/
public static boolean actuationCheck(String str) {
String force = str.replaceAll("g", "");
if(force.contains("/")) {
String[] splitForces = force.split("/");
for (String aForce : splitForces) {
if(Double.parseDouble(aForce) >= Double.parseDouble(actuForceMin) && Double.parseDouble(aForce) <= Double.parseDouble(actuForceMax)) {
return true;
}
}
}
else if(Double.parseDouble(force) >= Double.parseDouble(actuForceMin) && Double.parseDouble(force) <= Double.parseDouble(actuForceMax)) {
return true;
}
return false;
}
/**
* Filters switches by pre-travel distance
* @param String The pre-travel distance to check
* @return True if pre-travel distance is within range, false otherwise
*/
public static boolean preTCheck(String str) {
String force = str.replaceAll("mm", "");
if(Double.parseDouble(force) >= Double.parseDouble(preTravelMin) && Double.parseDouble(force) <= Double.parseDouble(preTravelMax)) {
return true;
}
return false;
}
/**
* Filters switches by total travel distance before bottoming out
* @param String The total travel distance to check
* @return True if total travel distance is within range, false otherwise
*/
public static boolean totalTCheck(String str) {
String force = str.replaceAll("mm", "");
if(Double.parseDouble(force) >= Double.parseDouble(totalTravelMin) && Double.parseDouble(force) <= Double.parseDouble(totalTravelMax)) {
return true;
}
return false;
}
/**
* Filters switches by mount type
* @param String The mount type to check
* @return True if mount type matches, false otherwise
*/
public static boolean mountCheck(String str) {
if(mount.equalsIgnoreCase("none")) {
return true;
}
if(str.equalsIgnoreCase(mount)){
return true;
}
else if(str.contains("/")) {
String[] splitTypes = str.split("/");
for(String aType : splitTypes) {
if(aType.equalsIgnoreCase(mount)) {
}
}
}
return false;
}
}