diff --git a/C/C3 Watch/.~lock.Watch-Registers-Assignment.docx# b/C/C3 Watch/.~lock.Watch-Registers-Assignment.docx# deleted file mode 100644 index ab60b5b..0000000 --- a/C/C3 Watch/.~lock.Watch-Registers-Assignment.docx# +++ /dev/null @@ -1 +0,0 @@ -,rens,hp-arch,05.06.2025 15:24,file:///home/rens/.config/libreoffice/4; \ No newline at end of file diff --git a/CS/CS2B shipping company/CS2B shipping company/Company.cs b/CS/CS2B shipping company/CS2B shipping company/Company.cs index da5cdee..fb02f3a 100644 --- a/CS/CS2B shipping company/CS2B shipping company/Company.cs +++ b/CS/CS2B shipping company/CS2B shipping company/Company.cs @@ -1,6 +1,58 @@ +using System.Text; + namespace CS2B_shipping_company; +// Company.cs public class Company { + private readonly List containers = new List(); + public void Add(BaseContainer container) + { + containers.Add(container); + } + + public string GenerateReport() + { + var report = new StringBuilder(); + report.AppendLine("| Id | Weight | Volume | Refrid | Fee |"); + + // Process Full Containers + var fullContainers = containers.OfType().ToList(); + if (fullContainers.Any()) + { + foreach (var container in fullContainers) + { + report.AppendLine($"| Full Size {container.Id} | {container.Weight}kg | | {(container.IsRefrigerated ? "Y" : "N")} | €{container.Fee():F2} |"); + } + report.AppendLine($"| Total | | | €{fullContainers.Sum(c => c.Fee()):F2} |"); + } + + // Process Half Containers + var halfContainers = containers.OfType().ToList(); + if (halfContainers.Any()) + { + foreach (var container in halfContainers) + { + report.AppendLine($"| Half Size {container.Id} | | {container.Volume}m3 | N/A | €{container.Fee():F2} |"); + } + report.AppendLine($"| Total | | | €{halfContainers.Sum(c => c.Fee()):F2} |"); + } + + // Process Quarter Containers + var quarterContainers = containers.OfType().ToList(); + if (quarterContainers.Any()) + { + foreach (var container in quarterContainers) + { + report.AppendLine($"| Quarter Size {container.Id} | | | N/A | €{container.Fee():F2} |"); + } + report.AppendLine($"| Total | | | €{quarterContainers.Sum(c => c.Fee()):F2} |"); + } + + // Grand Total + report.AppendLine($"| Grand Total | | | €{containers.Sum(c => c.Fee()):F2} |"); + + return report.ToString(); + } } \ No newline at end of file diff --git a/CS/CS2B shipping company/CS2B shipping company/Program.cs b/CS/CS2B shipping company/CS2B shipping company/Program.cs index 4ccaf61..402e9b7 100644 --- a/CS/CS2B shipping company/CS2B shipping company/Program.cs +++ b/CS/CS2B shipping company/CS2B shipping company/Program.cs @@ -1,9 +1,19 @@ namespace CS2B_shipping_company; -class Program +public class Program { - static void Main(string[] args) + public static async Task Main(string[] args) { - Console.WriteLine("Hello, World!"); + var company = new Company(); + var server = new Server(company); + + server.SetupServer(); + Console.WriteLine("Server started. Press any key to stop..."); + + var serverTask = server.ServerLoop(); + + Console.ReadKey(); + server.StopServer(); + await serverTask; } } \ No newline at end of file diff --git a/CS/CS2B shipping company/CS2B shipping company/Server.cs b/CS/CS2B shipping company/CS2B shipping company/Server.cs index 97a1cd5..7d73faf 100644 --- a/CS/CS2B shipping company/CS2B shipping company/Server.cs +++ b/CS/CS2B shipping company/CS2B shipping company/Server.cs @@ -1,6 +1,140 @@ +using System.Net; +using System.Net.Sockets; + namespace CS2B_shipping_company; public class Server { + private readonly Company company; + private TcpListener listener; + private bool serverRunning; -} \ No newline at end of file + public Server(Company company) + { + this.company = company; + } + + public void SetupServer(int port = 23) + { + listener = new TcpListener(IPAddress.Any, port); + listener.Start(); + serverRunning = true; + } + + public async Task ServerLoop() + { + while (serverRunning) + { + try + { + using (var client = await listener.AcceptTcpClientAsync()) + using (var stream = client.GetStream()) + using (var reader = new StreamReader(stream)) + using (var writer = new StreamWriter(stream) { AutoFlush = true }) + { + await HandleClient(reader, writer); + } + } + catch (Exception ex) + { + Console.WriteLine($"Error: {ex.Message}"); + } + } + } + + private async Task HandleClient(StreamReader reader, StreamWriter writer) + { + try + { + await writer.WriteLineAsync("WELCOME"); + + var command = await reader.ReadLineAsync(); + if (command == "STOP") + { + await writer.WriteLineAsync("ACK"); + return; + } + + if (command != "START") + { + await writer.WriteLineAsync("ERR;Invalid command"); + return; + } + + await writer.WriteLineAsync("TYPE"); + var type = await reader.ReadLineAsync(); + + BaseContainer container = null; + + switch (type?.ToUpper()) + { + case "FULL": + container = await ProcessFullContainer(reader, writer); + break; + case "HALF": + container = await ProcessHalfContainer(reader, writer); + break; + case "QUART": + container = new QuarterContainer("Unknown", "Unknown"); + break; + default: + await writer.WriteLineAsync("ERR;Invalid Type"); + return; + } + + if (container != null) + { + company.Add(container); + await writer.WriteLineAsync("ACK"); + await writer.WriteLineAsync(company.GenerateReport()); + } + } + catch (ShippingException ex) + { + await writer.WriteLineAsync($"ERR;{ex.Message}"); + } + catch (Exception ex) + { + await writer.WriteLineAsync($"ERR;{ex.Message}"); + } + } + + private async Task ProcessFullContainer(StreamReader reader, StreamWriter writer) + { + await writer.WriteLineAsync("FRIDGE"); + var fridgeResponse = await reader.ReadLineAsync(); + var isRefrigerated = fridgeResponse?.ToUpper() == "YES"; + + await writer.WriteLineAsync("WEIGHT"); + if (!int.TryParse(await reader.ReadLineAsync(), out int weight)) + throw new InvalidInputException("Invalid weight"); + + var container = new FullContainer("Unknown", "Unknown") + { + IsRefrigerated = isRefrigerated, + Weight = weight + }; + + return container; + } + + private async Task ProcessHalfContainer(StreamReader reader, StreamWriter writer) + { + await writer.WriteLineAsync("VOLUME"); + if (!int.TryParse(await reader.ReadLineAsync(), out int volume)) + throw new InvalidInputException("Invalid volume"); + + var container = new HalfContainer("Unknown", "Unknown") + { + Volume = volume + }; + + return container; + } + + public void StopServer() + { + serverRunning = false; + listener?.Stop(); + } +} diff --git a/CS/CS2B shipping company/CS2B shipping company/bin/Debug/net9.0/CS2B shipping company.dll b/CS/CS2B shipping company/CS2B shipping company/bin/Debug/net9.0/CS2B shipping company.dll index 91275f6..e77185d 100644 Binary files a/CS/CS2B shipping company/CS2B shipping company/bin/Debug/net9.0/CS2B shipping company.dll and b/CS/CS2B shipping company/CS2B shipping company/bin/Debug/net9.0/CS2B shipping company.dll differ diff --git a/CS/CS2B shipping company/CS2B shipping company/bin/Debug/net9.0/CS2B shipping company.pdb b/CS/CS2B shipping company/CS2B shipping company/bin/Debug/net9.0/CS2B shipping company.pdb index e79e069..4198410 100644 Binary files a/CS/CS2B shipping company/CS2B shipping company/bin/Debug/net9.0/CS2B shipping company.pdb and b/CS/CS2B shipping company/CS2B shipping company/bin/Debug/net9.0/CS2B shipping company.pdb differ diff --git a/CS/CS2B shipping company/CS2B shipping company/obj/Debug/net9.0/CS2B shipping company.csproj.CoreCompileInputs.cache b/CS/CS2B shipping company/CS2B shipping company/obj/Debug/net9.0/CS2B shipping company.csproj.CoreCompileInputs.cache index e53f192..054dbe8 100644 --- a/CS/CS2B shipping company/CS2B shipping company/obj/Debug/net9.0/CS2B shipping company.csproj.CoreCompileInputs.cache +++ b/CS/CS2B shipping company/CS2B shipping company/obj/Debug/net9.0/CS2B shipping company.csproj.CoreCompileInputs.cache @@ -1 +1 @@ -e5784bd8f6c60153fa7304c9656481c7b428255fd00993a6dce0cafe9bcc8366 +5d169557bd2a42faab7745e4d74a6575a9fbb85ef47e3589bb83f45afbd68f86 diff --git a/CS/CS2B shipping company/CS2B shipping company/obj/Debug/net9.0/CS2B shipping company.dll b/CS/CS2B shipping company/CS2B shipping company/obj/Debug/net9.0/CS2B shipping company.dll index 91275f6..e77185d 100644 Binary files a/CS/CS2B shipping company/CS2B shipping company/obj/Debug/net9.0/CS2B shipping company.dll and b/CS/CS2B shipping company/CS2B shipping company/obj/Debug/net9.0/CS2B shipping company.dll differ diff --git a/CS/CS2B shipping company/CS2B shipping company/obj/Debug/net9.0/CS2B shipping company.pdb b/CS/CS2B shipping company/CS2B shipping company/obj/Debug/net9.0/CS2B shipping company.pdb index e79e069..4198410 100644 Binary files a/CS/CS2B shipping company/CS2B shipping company/obj/Debug/net9.0/CS2B shipping company.pdb and b/CS/CS2B shipping company/CS2B shipping company/obj/Debug/net9.0/CS2B shipping company.pdb differ diff --git a/CS/CS2B shipping company/CS2B shipping company/obj/Debug/net9.0/ref/CS2B shipping company.dll b/CS/CS2B shipping company/CS2B shipping company/obj/Debug/net9.0/ref/CS2B shipping company.dll index ceb2546..618bad2 100644 Binary files a/CS/CS2B shipping company/CS2B shipping company/obj/Debug/net9.0/ref/CS2B shipping company.dll and b/CS/CS2B shipping company/CS2B shipping company/obj/Debug/net9.0/ref/CS2B shipping company.dll differ diff --git a/CS/CS2B shipping company/CS2B shipping company/obj/Debug/net9.0/refint/CS2B shipping company.dll b/CS/CS2B shipping company/CS2B shipping company/obj/Debug/net9.0/refint/CS2B shipping company.dll index ceb2546..618bad2 100644 Binary files a/CS/CS2B shipping company/CS2B shipping company/obj/Debug/net9.0/refint/CS2B shipping company.dll and b/CS/CS2B shipping company/CS2B shipping company/obj/Debug/net9.0/refint/CS2B shipping company.dll differ