-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
183 lines (153 loc) · 5.85 KB
/
Program.cs
File metadata and controls
183 lines (153 loc) · 5.85 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
using NLog;
namespace Week4NetAssignment;
class Program
{
static Logger logger;
static void Main()
{
// NLog setup
string path = Directory.GetCurrentDirectory() + "//nlog.config";
logger = LogManager.Setup().LoadConfigurationFromFile(path).GetCurrentClassLogger();
logger.Info("Program started");
string file = "mario.csv";
Console.WriteLine(Directory.GetCurrentDirectory());
try
{
if (!File.Exists(file))
{
logger.Error("File does not exist: {File}", file);
Console.WriteLine("File does not exist");
return;
}
while (true)
{
Menu();
if (!int.TryParse(Console.ReadLine(), out int answer))
{
Console.WriteLine("Invalid input. Please enter a number.");
Task.Delay(1000).Wait();
continue;
}
switch (answer)
{
case 1:
ViewCharacters(file);
break;
case 2:
AddCharacter(file);
break;
case 3:
Console.WriteLine("Exiting the program. Goodbye!");
Task.Delay(1000).Wait();
logger.Info("Program ended by user");
return;
default:
Console.WriteLine("Invalid option. Please select 1, 2, or 3.");
Task.Delay(1000).Wait();
break;
}
}
}
catch (Exception ex)
{
logger.Error(ex, "Unhandled exception in Main");
Console.WriteLine("An unexpected error occurred. Check log_file.txt for details.");
}
finally
{
logger.Info("Program ended");
}
}
static void Menu()
{
Console.Clear();
Console.WriteLine("-----------------------------");
Console.WriteLine("- Mario Bros Character Data -");
Console.WriteLine("-----------------------------");
Console.WriteLine("1. View all characters");
Console.WriteLine("2. Add a character");
Console.WriteLine("3. Exit");
Console.Write("Please select an option: ");
}
static void ViewCharacters(string file)
{
Console.Clear();
Console.WriteLine("Viewing all characters...\n");
try
{
string[] lines = File.ReadAllLines(file);
if (lines.Length <= 1)
{
Console.WriteLine("No characters found.");
}
else
{
// optional: show header once
Console.WriteLine("File header: " + lines[0]);
Console.WriteLine(new string('-', 40));
// each remaining line is one character
for (int i = 1; i < lines.Length; i++)
{
string line = lines[i];
if (string.IsNullOrWhiteSpace(line))
continue;
string[] parts = line.Split(','); // id,name,description,species,first-appearance,year-created [web:42][web:45][web:48]
if (parts.Length < 6)
{
Console.WriteLine("Invalid line: " + line);
Console.WriteLine();
continue;
}
Console.WriteLine($"id: {parts[0]}");
Console.WriteLine($"name: {parts[1]}");
Console.WriteLine($"description: {parts[2]}");
Console.WriteLine($"species: {parts[3]}");
Console.WriteLine($"first-appearance: {parts[4]}");
Console.WriteLine($"year-created: {parts[5]}");
Console.WriteLine(new string('-', 40));
}
logger.Info("Viewed characters with labeled output from file {File}", file);
}
}
catch (Exception ex)
{
logger.Error(ex, "Error viewing characters from {File}", file);
Console.WriteLine("Error reading file. Check log for details.");
}
Console.Write("\nPress Enter to return to menu...");
Console.ReadLine();
}
static void AddCharacter(string file)
{
Console.Clear();
Console.WriteLine("Adding a new character...\n");
try
{
Console.Write("Id: ");
string? id = Console.ReadLine();
Console.Write("Name: ");
string? name = Console.ReadLine();
Console.Write("Description: ");
string? description = Console.ReadLine();
Console.Write("Species: ");
string? species = Console.ReadLine();
Console.Write("First appearance (game): ");
string? firstAppearance = Console.ReadLine();
Console.Write("Year created: ");
string? yearCreated = Console.ReadLine();
// Build CSV line in same order as header
string newLine = $"{id},{name},{description},{species},{firstAppearance},{yearCreated}";
// Append with newline
File.AppendAllText(file, Environment.NewLine + newLine);
logger.Info("Added character {Name} with id {Id} to {File}", name, id, file);
Console.WriteLine("\nCharacter added successfully!");
}
catch (Exception ex)
{
logger.Error(ex, "Error adding character to {File}", file);
Console.WriteLine("Error writing to file. Check log for details.");
}
Console.Write("\nPress Enter to return to menu...");
Console.ReadLine();
}
}