-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOS.cs
119 lines (111 loc) · 4.15 KB
/
OS.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
using PrivateOS.Business;
using PrivateOS.DataAccess;
namespace PrivateOS
{
public class OS
{
private HWStorage Storage { get; }
private Prompter Prompter { get; set; }
private IHWStorageRepository Repository { get; set; }
public OS()
{
Prompter = new Prompter();
Repository = new HWStorageRepository();
Storage = Repository.GetStorage();
}
public void Run()
{
Prompter.WelcomeUser();
while (true)
{
Prompter.BeginNewLine();
try // Sistem centralizat pentru error handling.
{
//Preluam comanda de la user
string userInput = Prompter.AskForCommand();
//Impartim comanda in doua: nume comanda, argumente
string commandName;
List<string> arguments;
SplitCommand(userInput, out commandName, out arguments);
//Cerem 'gestionarului de dependinte' sa ne dea comanda dorita
var commandInstance = CommandResolver.ResolveCommand(commandName, arguments);
ICommand command = commandInstance;
commandInstance.Execute(Storage);
if(command is CopyCommand ||
command is CreateCommand ||
command is DeleteCommand ||
command is RenameCommand)
{
Repository.SaveStorage(Storage);
}
}
catch (ShutDownException e)
{
Console.WriteLine(e.Message);
if (Prompter.WantsToExit())
break;
}
catch (CommandNotFoundException e)
{
Console.WriteLine(e.Message);
Console.WriteLine("Try 'help' command.");
}
catch (ArgumentNotFoundException e)
{
Console.WriteLine(e.Message);
Console.WriteLine("Try 'help' command.");
}
catch (ArgumentNullException e)
{
Console.WriteLine(e.Message);
Console.WriteLine("Try 'help' command.");
}
catch (ArgumentNotValidException e) when (e is ArgumentNotValidException)
{
Console.WriteLine(e.Message);
Console.WriteLine("Try 'help' command.");
}
catch (CreateCommandParametersMissingException e)
{
Console.WriteLine(e.Message);
Console.WriteLine("Try 'help' command.");
}
catch (FatIsFullException e)
{
Console.WriteLine(e.Message);
}
catch (RoomIsFullException e)
{
Console.WriteLine(e.Message);
}
catch (FileNameTakenException e)
{
Console.WriteLine(e.Message);
}
catch (NameIsTooLongException e)
{
Console.WriteLine(e.Message);
}
catch(FileDoesNotExistsException e)
{
Console.WriteLine(e.Message);
}
catch (Exception e)
{
Console.WriteLine("Error happend, message: \n" + e.Message + "\n" + e.StackTrace);
Console.WriteLine("Try 'help' command.");
}
finally
{
Console.WriteLine();
}
}
}
private static void SplitCommand(string command, out string commandName, out List<string> arguments)
{
string[] splittedCommand = command.ToLower().Split(" ");
commandName = splittedCommand.First().ToLower();
arguments = splittedCommand.Skip(1).ToList();
}
}
}