-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
518 lines (508 loc) · 18.1 KB
/
Form1.cs
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Game_Collection
{
public partial class GameCollectionLibrary : Form
{
bool dontRunHandler = false;
private const int GAMELIMIT = 20; /* This is here to allow other developers to use this code with greater limits. */
private static String[] names = new String[GAMELIMIT];
private static String[] genres = new String[GAMELIMIT];
private static String[] platforms = new String[GAMELIMIT];
private static int count = 0; /* Start At 0 */
public GameCollectionLibrary()
{
createInputDir();
try
{
readFile("mygames");
}
catch (IOException exc)
{
MessageBox.Show(exc.Message);
}
InitializeComponent();
displayGames();
}
private void gameListBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (dontRunHandler == false)
{
int ind = gameListBox.SelectedIndex;
dontRunHandler = true;
updateTitles(ind);
}
//gameListBox.SelectedIndex = ind;
}
private void add_Click(object sender, EventArgs e)
{
try
{
updateCollections(nameText.Text, platformText.Text, genreText.Text);
displayGames();
}
catch (ArgumentException exc)
{
MessageBox.Show(exc.Message);
}
}
private void reset_Click(object sender, EventArgs e)
{
clearGames();
displayGames();
}
private void clear_Click(object sender, EventArgs e)
{
nameText.Text = "";
genreText.Text = "";
platformText.Text = "";
}
private void delete_Click(object sender, EventArgs e)
{
removeGame(gameListBox.SelectedIndex);
shuffleGames();
displayGames();
}
private void update_Click(object sender, EventArgs e)
{
try
{
updateGame(gameListBox.SelectedIndex, nameText.Text, platformText.Text, genreText.Text);
shuffleGames(); /* If they updated a empty record. */
displayGames();
}
catch (ArgumentException exc)
{
MessageBox.Show(exc.Message);
}
}
private void sort_Click(object sender, EventArgs e)
{
bubbleSort();
displayGames();
}
private void open_Click(object sender, EventArgs e)
{
try
{
readFile(filenameText.Text);
displayGames();
}
catch(IOException exc)
{
MessageBox.Show(exc.Message);
}
}
private void save_Click(object sender, EventArgs e)
{
saveFile();
}
private void binaryS_Click(object sender, EventArgs e)
{
binarySearchWrapper(nameText.Text);
}
private void searchL_Click(object sender, EventArgs e)
{
linearSearchWrapper(nameText.Text);
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
saveFile();
}
/******************************************************************
* NAME: displayGames
* PURPOSE: To update the display gameListBox with all games in arrays.
* IMPORTS: EventHandler onchangedindex
* EXPORTS: None
* ***************************************************************/
private void displayGames()
{
bubbleSort();
int ind = gameListBox.SelectedIndex;
gameListBox.Items.Clear();
for (int i = 0; i < 20; i++)
{
if (names[i] == null)
{ /* No More Games. */
Console.Out.Write("At value: " + i + " there are no more games, now filling with emptys.");
gameListBox.Items.Add("Empty");
}
else
{ /* Games */
gameListBox.Items.Add(String.Format("{0} {1} {2}", names[i], platforms[i].ToUpper(), genres[i]));
}
}
countBox.Text = count.ToString();
if (ind >= 0 && ind < GAMELIMIT)
{
gameListBox.SelectedIndex = ind;
}
dontRunHandler = false;
}
/******************************************************************
* NAME: updateCollections
* PURPOSE: To update the game collection
* IMPORTS: String inGame, String inPlatform, String inGenre
* EXPORTS: None
* ***************************************************************/
private void updateCollections(String inGame, String inPlatform, String inGenre)
{
if (inGame == null || inGame.Length < 1)
{
throw new ArgumentException("inGame is invalid/empty!");
}
else if (inPlatform == null || inPlatform.Length < 1)
{
throw new ArgumentException("Platform is invalid/empty!");
}
else if (inGenre == null || inGenre.Length < 1)
{
throw new ArgumentException("Genre is invalid/empty!");
}
else if (count == 20)
{
throw new ArgumentException("Cannot add anymore games!");
}
else
{ /* Inputs Valid */
names[count] = inGame;
platforms[count] = inPlatform;
genres[count] = inGenre;
count++;
Console.Out.Write(String.Format("Added Game: {0}, Genre: {1}, Platform: {2}, to relevant arrays, count now at {3}", inGame, inGenre, inPlatform, count));
}
}
/******************************************************************
* NAME: clearGames
* PURPOSE: To clear the game collection.
* IMPORTS: None
* EXPORTS: None
* ***************************************************************/
private void clearGames()
{
Array.Clear(names, 0, GAMELIMIT);
Array.Clear(platforms, 0, GAMELIMIT);
Array.Clear(genres, 0, GAMELIMIT);
count = 0;
}
/******************************************************************
* NAME: removeGame
* PURPOSE: remove selected game
* IMPORTS: int index
* EXPORTS: None
* ***************************************************************/
private void removeGame(int index)
{
if (index < 0 || index > (GAMELIMIT - 1))
{
Console.Out.Write("Strange Index Input, Ignoring Selection.");
}
else
{
names[index] = null;
genres[index] = null;
platforms[index] = null;
if (count != 0)
{
count--;
}
}
}
/******************************************************************
* NAME: shuffleGames
* PURPOSE: shuffle game arrays.
* IMPORTS: None
* EXPORTS: None
* ***************************************************************/
private void shuffleGames()
{
names = shuffle(names);
genres = shuffle(genres);
platforms = shuffle(platforms);
}
private String[] shuffle(String[] selectedArray)
{
String[] temp = new String[selectedArray.Length];
int k = 0;
for (int i = 0; i < selectedArray.Length; i++)
{
if (selectedArray[i] != null)
{
temp[k] = selectedArray[i];
k++;
}
}
return temp;
}
/******************************************************************
* NAME: updateGame
* PURPOSE: update selected game with input values.
* IMPORTS: int selected, String name, String platform, String genre
* EXPORTS: None
* ***************************************************************/
private void updateGame(int selected, String name, String platform, String genre)
{
if (selected < 0 || selected > GAMELIMIT) { throw new ArgumentException("Invalid Selection To Update."); }
else if (name == null || name.Length < 1) { throw new ArgumentException("Invalid Name input."); }
else if (platform == null || platform.Length < 1) { throw new ArgumentException("Invalid Platform input."); }
else if (genre == null || genre.Length < 1) { throw new ArgumentException("Invalid Genre input."); }
else
{
if (names[selected] == null)
{ /* Updating A Empty Record. */
names[selected] = name;
platforms[selected] = platform;
genres[selected] = genre;
count++;
}
else
{ /* Updating a normal record. */
names[selected] = name;
platforms[selected] = platform;
genres[selected] = genre;
}
}
}
/******************************************************************
* NAME: bubbleSort
* PURPOSE: sort arrays based off name using bubbleSort algorithm. (can not finish early, bad bubblesort algorithm, can be improved!)
* IMPORTS: None
* EXPORTS: None
* ***************************************************************/
private void bubbleSort()
{
for (int i = 0; i < (GAMELIMIT - 1); i++)
{
for (int k = i + 1; k < GAMELIMIT; k++)
{
if (names[i] != null && names[k] != null && names[i].CompareTo(names[k]) > 0)
{
String tempName = names[i];
String tempGenre = genres[i];
String tempPlatform = platforms[i];
names[i] = names[k];
platforms[i] = platforms[k];
genres[i] = genres[k];
names[k] = tempName;
genres[k] = tempGenre;
platforms[k] = tempPlatform;
}
}
}
}
/******************************************************************
* NAME: createInputDir
* PURPOSE: create directory inputFiles for .dat Files if it doesnt exist.
* IMPORTS: None
* EXPORTS: None
* ***************************************************************/
private void createInputDir()
{
try
{
// If the directory doesn't exist, create it.
if (!Directory.Exists(System.Environment.CurrentDirectory + "\\InputFiles"))
{
Directory.CreateDirectory(System.Environment.CurrentDirectory + "\\InputFiles");
}
}
catch (Exception)
{
// Directory Exists.
}
}
/******************************************************************
* NAME: readFile
* PURPOSE: reads input file and saves information.
* IMPORTS: String filename
* EXPORTS: None
* ***************************************************************/
private void readFile(String filename)
{
if (filename == null || filename.Length < 1)
{
throw new IOException("Inputted Filename is invalid!");
}
else if (filename.Contains("."))
{
throw new IOException("input filename should not contain a . (dont include fileExtension.)");
}
else if (!File.Exists(System.Environment.CurrentDirectory + "\\InputFiles\\" + filename + ".dat"))
{
throw new IOException("file " + filename + ".dat does not exist");
}
else
{
beginRead(System.Environment.CurrentDirectory + "\\InputFiles\\" + filename + ".dat");
}
}
private void beginRead(String path)
{
clearGames();
StreamReader sr = File.OpenText(path);
try
{ /* Lines Should be: Name,Platform,Genre\n */
String line = sr.ReadLine();
while (line != null && line != "")
{
String[] lines = line.Split(',');
if (lines.Length != 3)
{
throw new IOException("Expected Line Format To Be Name,Platform,Genre");
}
try
{
updateCollections(lines[0], lines[1], lines[2]);
}
catch (ArgumentException exc2)
{
MessageBox.Show(exc2.Message);
}
line = sr.ReadLine();
}
sr.Close(); // Close Stream.
}
catch (IOException exc)
{
throw exc;
}
}
/******************************************************************
* NAME: saveFile
* PURPOSE: saves games to mygames.dat
* IMPORTS: None
* EXPORTS: None
* ***************************************************************/
private void saveFile()
{ /* Saves to mygames.dat */
try
{
StreamWriter sw = File.CreateText(System.Environment.CurrentDirectory + "\\InputFiles\\" + "mygames.dat");
for (int i = 0; i < count; i++)
{
sw.WriteLine(String.Format("{0},{1},{2}", names[i], platforms[i], genres[i]));
}
sw.Close();
MessageBox.Show("Successfully Saved Game");
}
catch (IOException exc)
{
MessageBox.Show(exc.Message);
}
}
/******************************************************************
* NAME: updateTitles
* PURPOSE: titles updated to selected ind
* IMPORTS: int ind
* EXPORTS: None
* ***************************************************************/
private void updateTitles(int ind)
{
if (ind >= 0 && ind < GAMELIMIT)
{
if (names[ind] != null)
{
nameText.Text = names[ind];
platformText.Text = platforms[ind];
genreText.Text = genres[ind];
displayGames();
}
else
{
nameText.Text = "";
platformText.Text = "";
genreText.Text = "";
displayGames();
}
}
}
/******************************************************************
* NAME: binarySearch
* PURPOSE: Performs a recursive binary search for input string.
* IMPORTS: String name
* EXPORTS: None
* ***************************************************************/
private void binarySearchWrapper(String name)
{
if (name != null)
{
binarySearch(0, count, name);
}
else
{
MessageBox.Show("Please enter a name to search");
}
}
private void binarySearch(int left, int right, String name)
{
if (right >= left)
{
int mid = left + (right - left) / 2;
int comparision = name.CompareTo(names[mid]);
if (comparision == 0)
{
genreText.Text = genres[mid];
platformText.Text = platforms[mid];
}
else if (comparision < 0)
{
binarySearch(left, mid - 1, name); // recurse left
}
else
{
binarySearch(mid+1, right, name); // recurse right
}
}
else
{
nameText.Text = "";
genreText.Text = "";
platformText.Text = "";
MessageBox.Show("Cannot find game " + name);
}
}
/******************************************************************
* NAME: linearSearch
* PURPOSE: Performs a linear search for input string.
* IMPORTS: String name
* EXPORTS: None
* ***************************************************************/
private void linearSearchWrapper(String name)
{
int foundInd = -1;
for (int i = 0; i < count; i++)
{
if (name.Equals(names[i]))
{
foundInd = i;
}
}
if (foundInd >= 0)
{
genreText.Text = genres[foundInd];
platformText.Text = platforms[foundInd];
}
else
{
nameText.Text = "";
genreText.Text = "";
platformText.Text = "";
MessageBox.Show("Cannot find game " + name);
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void GameCollectionLibrary_Load(object sender, EventArgs e)
{
}
}
}