-
Notifications
You must be signed in to change notification settings - Fork 1
/
StoreRunner.java
45 lines (37 loc) · 1.42 KB
/
StoreRunner.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
import java.util.*;
import java.io.*;
public class StoreRunner {
public static void main(String[] args) {
Store store = new Store();
// create, write to file, and read from file
try {
//create file
File output = new File("output.txt");
output.createNewFile();
// create writer
FileWriter writer = new FileWriter("output.txt");
// read from input
File input = new File("input.txt");
Scanner scan = new Scanner(input);
while (scan.hasNextLine()) {
String data = scan.nextLine();
// i == 0: query type
// i == 1: key
// i == 2: version or value
String[] parsedData = data.split(" ");
if (parsedData[0].equals("PUT")) {
writer.write(store.put(parsedData[1], Integer.parseInt(parsedData[2])) + "\n");
} else if (parsedData.length == 2) {
writer.write(store.get(parsedData[1]) + "\n");
} else {
writer.write(store.get(parsedData[1], Integer.parseInt(parsedData[2])) + "\n");
}
}
writer.close();
scan.close();
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}