-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathV1-WorkSpots.java
190 lines (153 loc) · 4.44 KB
/
V1-WorkSpots.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
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Random;
import java.time.LocalDate;
import java.time.ZoneId;
public claws WorkSpots{
static Scanner input;
static File responsibilities;
static File workspots;
static ArrayList<String> rList;
static ArrayList<String> wsList;
public static void main(String[] args){
initialize();
System.out.println("Welcome! Working on different responsibilities in different places may enhance your focus.");
System.out.println("Here, you can pair your responsibilities with places to work on them.");
while(true){
int option = menu();
switch(option){
case 1:
addResponsibilities();
break;
case 2:
addWorkSpots();
break;
case 3:
viewData();
break;
case 4:
pair();
break;
case 5:
quit();
break;
}
}
}
private static void initialize(){
input = new Scanner(System.in);
responsibilities = new File("responsibilities.txt");
workspots = new File("workspots.txt");
rList = new ArrayList<>();
wsList = new ArrayList<>();
try {
if (!responsibilities.createNewFile())
fillList(rList, responsibilities);
if (!workspots.createNewFile())
fillList(wsList, workspots);
}
catch (IOException e) {
System.out.println("An error occurred creating files.");
e.printStackTrace();
}
}
private static void fillList(ArrayList<String> list, File file) throws IOException{
BufferedReader br = new BufferedReader(new FileReader(file));
while (br.ready()){
list.add(br.readLine());
}
br.close();
}
private static int menu(){
int result;
System.out.println("\nMenu:");
System.out.println("1) Add responsibilities");
System.out.println("2) Add work spots");
System.out.println("3) View your responsibilities and work spots");
System.out.println("4) Pair responsibilities with work spots");
System.out.println("5) Quit");
System.out.print("\nEnter an option number: ");
while(true){
try{
result = input.nextInt();
if (result<1 || result>5)
throw new RuntimeException();
break;
}
catch (RuntimeException e){
System.out.println("Error: Enter a number from 1-4, or 5 to quit");
}
}
input.nextLine();
return result;
}
private static void addResponsibilities(){
System.out.println("Enter your responsibilities one at a time. Enter \"END\" to finish: ");
while(true){
String r = input.nextLine();
if (r.toUpperCase().equals("END"))
break;
rList.add(r);
}
writeToFile(rList.toArray(new String[0]), responsibilities);
}
private static void addWorkSpots(){
System.out.println("\nEnter your potential work spots one at a time. Enter \"END\" to finish: ");
while(true){
String ws = input.nextLine();
if (ws.toUpperCase().equals("END"))
break;
wsList.add(ws);
}
writeToFile(wsList.toArray(new String[0]), workspots);
}
private static void viewData(){
System.out.println("#################################################");
System.out.println("Responsibilities:");
for (String r : rList)
System.out.println(r);
System.out.println("#################################################");
System.out.println("Work Spots:");
for (String ws : wsList)
System.out.println(ws);
System.out.println("#################################################");
}
private static void pair(){
String[] rArray = rList.toArray(new String[0]);
String[] wsArray = wsList.toArray(new String[0]);
shuffleArray(rArray);
shuffleArray(wsArray);
for (int i=0; i<rArray.length; i++){
System.out.println("Work on " + rArray[i] + " at " + wsArray[i%wsArray.length]);
}
}
// Fisher Yates Shuffle
private static void shuffleArray(String[] arr){
LocalDate date = LocalDate.now(ZoneId.systemDefault());
int day = date.getDayOfYear()/14;
Random r = new Random(day);
for (int i=arr.length-1; i>0; i--){
int index = r.nextInt(i+1);
String temp = arr[index];
arr[index] = arr[i];
arr[i] = temp;
}
}
private static void writeToFile(String[] input, File file){
try {
FileWriter w = new FileWriter(file);
for (String r : input)
w.write(r+System.getProperty("line.separator"));
w.close();
}
catch (IOException e) {
System.out.println("An error occurred writing to " + file.getName() + ".");
e.printStackTrace();
}
}
private static void quit(){
input.close();
System.exit(0);
}
}