-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDNode.java
More file actions
300 lines (245 loc) · 8.55 KB
/
Copy pathDNode.java
File metadata and controls
300 lines (245 loc) · 8.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package code;
import com.opencsv.CSVReader;
import com.opencsv.CSVWriter;
import com.opencsv.bean.StatefulBeanToCsvBuilder;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;
public class DNode<E> {
String element;
String name;
DNode<E> next;
DNode<E> prev;
static Scanner input = new Scanner(System.in);
static ArrayList<ArrayList<Integer>> twoD = new ArrayList<ArrayList<Integer>>();
public static void main(String[] args) throws IOException {
System.out.println("Welcome to order of magnitude pro!");
LinkedList<DNode> a = new LinkedList<DNode>();
//reads in data
readIn(a);
//set bounds
// setBounds(a);
// add vocab
addVocab(a);
multiplier(a, true, 1);
//reads out data
readOut(a);
/*
//add powers to the list
addPowers(a);
//check powers by for looping through
systemCheck(a);
*/
}
static int questionnaire(LinkedList<DNode> nodeList, int mid, int left, int right, String object){
//if min
if((right < left)){
return left;
}
//if bigger than min less than min+1
if((left > mid)){
return left;
}
//ask user for relative size comparison with middle of nodelist
System.out.println("Is "+ object + " bigger than or smaller than: " + nodeList.get(mid).name + "?");
String answer = "";
answer = input.nextLine();
//if bigger than middle of nodelist, recursively check upper half
if(answer.equals("bigger")){
return questionnaire(nodeList, ((mid+1) + right)/2, mid+1, right, object);
}
//if middle
else if((left==right)){
return mid;
}
//if smaller than middle of nodelist, recursively check lower half
else{
return questionnaire(nodeList, ((left) + mid-1)/2, left, mid-1, object);
}
}
//input powers value in element field of nodelist based on user input
static void addPowers(LinkedList<DNode> nodeList){
for(int i = 0 ; i < nodeList.size() ; i++){
System.out.println("What's the value of " + nodeList.get(i).name + "?");
nodeList.get(i).element = input.nextLine();
}
}
//take three bounds values from user input
static void setBounds(LinkedList<DNode> a){
//Set lower bound
DNode memberLow = new DNode();
System.out.println("What's your lower bound?");
String nameLow = input.nextLine();
memberLow.name = nameLow;
a.add(0, memberLow);
//Set mid bound
DNode memberMid = new DNode();
System.out.println("What's your mid bound?");
String nameMid = input.nextLine();
memberMid.name = nameMid;
a.add(1, memberMid);
//Set upper bound
DNode memberUpper = new DNode();
System.out.println("What's your upper bound?");
String nameUpper = input.nextLine();
memberUpper.name = nameUpper;
a.add(2, memberUpper);
}
//add to vocabulary list
static void addVocab(LinkedList<DNode> a) throws FileNotFoundException{
//boolean for if new vocabulary
boolean addVocabBool = true;
//Add vocabulary
while(true){
addVocabBool = true;
//ask user for new vocabulary
System.out.println("Add your vocabulary: ");
String object = input.nextLine();
//if user says done adding vocabulary, exit function
if(object.equals("done")){
break;
}
//check if added vocabulary already exists, don't add if so
for(int i = 0 ; i < a.size() ; i++){
if(object.equals(a.get(i).name)){
addVocabBool = false;
}
}
//case where vocabulary exists and user is informed
if (addVocabBool == false){
System.out.println("Already in the list!");
continue;
}
//if new vocabulary, add to vocabulary after asking user for comparative size
else{
DNode newNode= new DNode();
newNode.name = object;
int insert = questionnaire(a, (a.size()-1)/2, 0, a.size()-1, object);
a.add(insert, newNode);
}
//Print list on each run
for(int j = 0 ; j < a.size() ; j++){
System.out.println(a.get(j).name);
}
}
}
//print all element names with corresponding elements in DNode linked list to check if correct
static void systemCheck(LinkedList<DNode> a){
for(int k=0 ; k < a.size() ; k++){
System.out.println(a.get(k).name + " is of magnitude " + a.get(k).element);
}
}
//take values for how many objects inside object larger than it from a specified number of users
static void multiplier(LinkedList<DNode> a, boolean demo, int column) {
String object, container;
int containerIndex = 0;
int objectIndex = 0;
int twoDColumn = 0;
int twoDrow = 0;
ArrayList<Integer> valueArray = new ArrayList<Integer>();
//user inputs their preferred calculation from object to container
if (demo == false){
System.out.println("What is your object?");
object = input.nextLine();
System.out.println("What is your container?");
container = input.nextLine();
System.out.println("Your Question: How many "+ object + "'s fit inside a "+container+"?");
containerIndex = 0;
objectIndex = 0;
for (int i = 0; i < a.size(); i++){
DNode tempNode1 = a.get(i);
if (object.equals(tempNode1.name)) {
objectIndex = i;
}
for (int j = i; j < a.size(); j++){
DNode tempNode2 = a.get(j);
if (container.equals(tempNode2.name)) {
containerIndex = j;
}
}
}
}
//user is forced to give values from smallest in list to largest
else{
object = a.get(0).name;
container = a.get(a.size()-1).name;
}
//loop through for numerous inputs from users
while(column > 0){
valueArray = new ArrayList<Integer>();
containerIndex = a.size()-1;
objectIndex = 0;
BigInteger orderOfMagnitude = BigInteger.valueOf(1);
//loop through ordered list and ask from smallest to largest how many objects in larger object
while(objectIndex < containerIndex){
System.out.println("how many "+ a.get(objectIndex).name + "s fit inside a " + a.get(objectIndex+1).name + "?");
long temp = input.nextLong();
int value = (int) (Math.round(Math.log10(temp)));
valueArray.add(value);
long pow = (long) (Math.pow(10, value));
BigInteger powValue = BigInteger.valueOf(pow);
orderOfMagnitude = (orderOfMagnitude.multiply(powValue));
objectIndex++;
twoDColumn++;
}
//add to twoD array
twoD.add(twoDrow, valueArray);
System.out.println(orderOfMagnitude + " " + object + "'s fit inside of a " + container);
column--;
twoDrow++;
}
//print results
for(int p = 0 ; p < twoDrow; p++){
System.out.println("");
for(int k =0 ; k < objectIndex ; k++){
System.out.print(twoD.get(p).get(k) + " ");
}
}
}
//read in a database
static void readIn(LinkedList<DNode> a) throws IOException{
final String SAMPLE_CSV_FILE_PATH = "C:\\Users\\Huzaifa\\Documents\\Hackathon\\inputText.txt\\";
try (
Reader reader = Files.newBufferedReader(Paths.get(SAMPLE_CSV_FILE_PATH));
CSVReader csvReader = new CSVReader(reader);
) {
//Reading Records One by One in a String array
String[] nextRecord;
int index= 0;
while ((nextRecord = csvReader.readNext()) != null) {
DNode d = new DNode();
// d.element = nextRecord[1];
d.name = nextRecord[0];
a.add(d);
System.out.println("Name : " + nextRecord[0]);
index++;
}
}
}
//export out a database
static void readOut(LinkedList<DNode> a) throws IOException{
String csv = "C:\\Users\\Huzaifa\\Documents\\Hackathon\\outputText.txt\\";
CSVWriter writer = new CSVWriter(new FileWriter(csv),CSVWriter.NO_QUOTE_CHARACTER,
CSVWriter.NO_ESCAPE_CHARACTER,
System.getProperty("line.separator"));
String[] data = new String[twoD.get(0).size()+1];
for(int twoDSize = 0 ; twoDSize < twoD.size() ; twoDSize++){
for(int nameSize = 0; nameSize < twoD.get(twoDSize).size()-1 ; nameSize++){
data[nameSize]= (twoD.get(twoDSize).get(nameSize) +"");
}
data[twoD.get(0).size()] =",";
writer.writeNext(data);
}
writer.close();
}
}