Skip to content

Commit d4c7b02

Browse files
committed
Improving solution for file_displayer project.
1 parent 171e801 commit d4c7b02

15 files changed

Lines changed: 160 additions & 83 deletions

File tree

source/code/projects/FileDisplayer/FileDisplayer/Program.cs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,70 @@ static void Main()
99
* We first create three
1010
* string variables where
1111
* our three demo files
12-
* are located.
12+
* are located, and created.
1313
*/
1414
string directoryPath = AppDomain
1515
.CurrentDomain
1616
.BaseDirectory;
1717
string ex0 = Path.Combine(directoryPath, "ex0.txt");
18+
try
19+
{
20+
StreamWriter sr = new StreamWriter(ex0);
21+
sr.WriteLine(
22+
@"This is the first demo file. It contains a long first line that will not wrap nicely on some display.
23+
It also contains another line that contains the word line also.
24+
It will be used to test our methods.
25+
"
26+
);
27+
sr.Close();
28+
}
29+
catch (Exception ex)
30+
{
31+
Console.WriteLine(
32+
"Something went wrong setting up the project.\nPlease, contact your instructor, sharing the following message:\n"
33+
+ ex.Message
34+
);
35+
}
1836
string ex1 = Path.Combine(directoryPath, "ex1.txt");
37+
try
38+
{
39+
StreamWriter sr = new StreamWriter(ex1);
40+
sr.WriteLine(
41+
@"A very short
42+
demo file.
43+
"
44+
);
45+
sr.Close();
46+
}
47+
catch (Exception ex)
48+
{
49+
Console.WriteLine(
50+
"Something went wrong setting up the project.\nPlease, contact your instructor, sharing the following message:\n"
51+
+ ex.Message
52+
);
53+
}
1954
string ex2 = Path.Combine(directoryPath, "ex2.csv");
55+
try
56+
{
57+
StreamWriter sr = new StreamWriter(ex2);
58+
sr.WriteLine(
59+
@"Index,First Name,Last Name,Lap 1, Lap 2, Lap 3
60+
1,Shelby,Terrell,78,80,82
61+
2,Phillip,Summers,76,82,91
62+
3,Kristine,Travis,78,82,83
63+
4,Marty,Gave,67,73,72
64+
5,Harley,Test,78,91,72
65+
"
66+
);
67+
sr.Close();
68+
}
69+
catch (Exception ex)
70+
{
71+
Console.WriteLine(
72+
"Something went wrong setting up the project.\nPlease, contact your instructor, sharing the following message:\n"
73+
+ ex.Message
74+
);
75+
}
2076

2177
// Testing the "Display" Method:
2278
Console.WriteLine("*****\n* Testing Display\n*****");

source/code/projects/FileDisplayer/FileDisplayer/bin/Debug/ex0.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.

source/code/projects/FileDisplayer/FileDisplayer/bin/Debug/ex1.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

source/code/projects/FileDisplayer/FileDisplayer/bin/Debug/ex2.csv

Lines changed: 0 additions & 6 deletions
This file was deleted.

source/code/projects/FileDisplayerSolution/FileDisplayerSolution/FileDisplayer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* asks the method to display an error
77
* message if something goes wrong. A more
88
* principled approach would require the
9-
* method to send an exception back to
9+
* method to send an exception back to
1010
* the main routine, and to let it handle
1111
* the error as it sees fit.
1212
*/

source/code/projects/FileSumNumbers/FileSumNumbers/Program.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ static void Main()
2626
{
2727
Console.WriteLine("Exception: " + e.Message);
2828
}
29-
29+
3030
// Beginning of solution.
3131
string line;
3232
double number;
@@ -53,12 +53,14 @@ static void Main()
5353
}
5454
finally
5555
{
56-
Console.WriteLine("Done summing the numbers in the file.");
56+
Console.WriteLine(
57+
"Done summing the numbers in the file."
58+
);
5759
}
58-
60+
5961
try
6062
{
61-
// The "true" in the following constructor allows
63+
// The "true" in the following constructor allows
6264
// us to directly append at the end of the file.
6365
StreamWriter sw = new StreamWriter(fPath, true);
6466
sw.WriteLine($"Total: {sum}");
@@ -68,28 +70,31 @@ static void Main()
6870
{
6971
Console.WriteLine("Exception: " + e.Message);
7072
}
71-
finally{
73+
finally
74+
{
7275
Console.WriteLine("Done writing in the file.");
7376
}
74-
77+
7578
// This is simply to open the file and read it back
7679
// directly from the program.
77-
try{
80+
try
81+
{
7882
StreamReader sr = new StreamReader(fPath);
7983
line = sr.ReadLine();
80-
while(line != null){
84+
while (line != null)
85+
{
8186
Console.WriteLine(line);
8287
line = sr.ReadLine();
8388
}
8489
sr.Close();
8590
}
86-
catch (Exception e)
91+
catch (Exception e)
8792
{
8893
Console.WriteLine("Exception: " + e.Message);
8994
}
90-
finally{
95+
finally
96+
{
9197
Console.WriteLine("Done displaying the file.");
9298
}
93-
9499
}
95100
}
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
class Demo : IToString
22
{
3-
public string Name{get; set;}
4-
public Demo(string nameP){Name = nameP;}
3+
public string Name { get; set; }
4+
5+
public Demo(string nameP)
6+
{
7+
Name = nameP;
8+
}
59
// public override string ToString(){return "The name attribute contains:" + Name;}
610
}

source/code/projects/InheritanceAndAccessModifiers/InheritanceAndAccessModifiers/Person.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ private void NewName(string newNameP)
2525
{
2626
name = newNameP + $" (previous name: {name})";
2727
}
28-
28+
2929
public Person() { }
3030
}

source/code/projects/InheritanceAndAccessModifiers/InheritanceAndAccessModifiers/Program.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,5 @@ public static void Main()
2020

2121
test1.commonName = "Test";
2222
FMember test2 = new FMember("Cousin", "Samuel");
23-
2423
}
2524
}

source/code/projects/RecursionDisplayH/RecursionDisplayH/Program.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@ string DisplayH(int[] aP, int currentIndex)
1111
string ret = "";
1212
if (aP.Length == currentIndex + 1)
1313
ret += " || " + aP[currentIndex].ToString();
14-
else ret+= DisplayH(aP, currentIndex+1) + ", " + aP[currentIndex].ToString();
15-
if (currentIndex == 0){ret += " ** "; }
16-
return ret;
14+
else
15+
ret +=
16+
DisplayH(aP, currentIndex + 1)
17+
+ ", "
18+
+ aP[currentIndex].ToString();
19+
if (currentIndex == 0)
20+
{
21+
ret += " ** ";
22+
}
23+
return ret;
1724
}
1825
string Display(int[] aP)
1926
{
@@ -23,7 +30,7 @@ string Display(int[] aP)
2330
return DisplayH(aP, 0);
2431
}
2532
// Examples
26-
int[] array0 = { 1, 2, 4};
33+
int[] array0 = { 1, 2, 4 };
2734
int[] array1 = { 1, 2, 4, 10, 13, 17 };
2835
int[] array2 = { 12, 98, 120, 15 };
2936
int[] array3 =
@@ -44,9 +51,6 @@ string Display(int[] aP)
4451
Console.WriteLine("Example 0: " + Display(array0));
4552
Console.WriteLine("Example 1: " + Display(array1));
4653
Console.WriteLine("Example 2: " + Display(array2));
47-
Console.WriteLine(
48-
"Example 3: " + Display(array3)
49-
);
50-
54+
Console.WriteLine("Example 3: " + Display(array3));
5155
}
5256
}

0 commit comments

Comments
 (0)