From 60d0ad4e9450a12cab572780e2d7cceb61111c7f Mon Sep 17 00:00:00 2001 From: KaiHog <161350865+KaiHog@users.noreply.github.com> Date: Sat, 30 Mar 2024 13:35:42 +0100 Subject: [PATCH 01/22] Update Reservation tour.cs --- ProjectB/Program.cs | 236 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 228 insertions(+), 8 deletions(-) diff --git a/ProjectB/Program.cs b/ProjectB/Program.cs index 5508d9c..61e0b43 100644 --- a/ProjectB/Program.cs +++ b/ProjectB/Program.cs @@ -1,16 +1,236 @@ -using System; -using ProjectB.Services; // Import the namespace where IService is defined +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text.Json; + +class Tour +{ + public DateTime Time { get; set; } + public string Location { get; set; } + public int Capacity { get; set; } + public int ParticipantsCount { get; set; } + + public Tour(DateTime time, string location, int capacity) + { + Time = time; + Location = location; + Capacity = capacity; + ParticipantsCount = 0; + } +} + +class Participant +{ + public int TicketNumber { get; set; } + public DateTime TourTime { get; set; } + + public Participant(int ticketNumber, DateTime tourTime) + { + TicketNumber = ticketNumber; + TourTime = tourTime; + } +} class Program { + static List tours = new List(); + static Dictionary> signupsByTourTime = new Dictionary>(); + static string jsonFilePath = "signups.json"; + static void Main(string[] args) { - // Create an instance of the service - IService service = new Service(); + LoadParticipantsFromJson(); + + for (int hour = 9; hour <= 20; hour++) + { + DateTime tourTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, hour, 0, 0); + tours.Add(new Tour(tourTime, "Your Tour Location", 13)); + if (!signupsByTourTime.ContainsKey(tourTime)) + { + signupsByTourTime.Add(tourTime, new List()); + } + } + + AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit); + + bool exit = false; + while (!exit) + { + Console.WriteLine("Welcome to our museum!"); + Console.WriteLine("Please choose an option:"); + Console.WriteLine("1. Sign up for a tour"); + Console.WriteLine("2. Delete your sign-up for a tour"); + Console.WriteLine("3. Exit"); + + int option; + while (!int.TryParse(Console.ReadLine(), out option) || (option < 1 || option > 3)) + { + Console.WriteLine("Invalid input. Please enter a number between 1 and 3."); + } + + switch (option) + { + case 1: + SignUpForTour(); + break; + case 2: + DeleteSignUpForTour(); + break; + case 3: + Console.WriteLine("Thank you for visiting our museum! Goodbye."); + exit = true; + break; + } + } + } + + static void CurrentDomain_ProcessExit(object sender, EventArgs e) + { + SaveParticipantsToJson(); + } + + static void SignUpForTour() + { + Console.WriteLine("Available Tours:"); + for (int i = 0; i < tours.Count; i++) + { + int spotsLeft = tours[i].Capacity - tours[i].ParticipantsCount; + Console.WriteLine($"{i + 1}. Tour at {tours[i].Time.ToString("HH:mm")}, {spotsLeft} spots left"); + } + Console.WriteLine("0. Go back"); + + Console.Write("Enter the number of the tour you want to sign up for (or 0 to go back): "); + int tourNumber; + while (!int.TryParse(Console.ReadLine(), out tourNumber) || (tourNumber < 0 || tourNumber > tours.Count)) + { + Console.WriteLine($"Invalid input. Please enter a number between 0 and {tours.Count}."); + } + + if (tourNumber == 0) + { + return; + } + + Tour selectedTour = tours[tourNumber - 1]; + + if (selectedTour.ParticipantsCount >= selectedTour.Capacity) + { + Console.WriteLine($"Sorry, the tour at {selectedTour.Time.ToString("HH:mm")} is already fully booked."); + return; + } + + Console.Write("Enter your ticket number: "); + int ticketNumber; + while (!int.TryParse(Console.ReadLine(), out ticketNumber) || ticketNumber <= 0) + { + Console.WriteLine("Invalid input. Ticket number must be a positive integer."); + Console.Write("Enter your ticket number: "); + } + + Participant existingParticipant = signupsByTourTime[selectedTour.Time].FirstOrDefault(p => p.TicketNumber == ticketNumber); + if (existingParticipant != null) + { + Console.WriteLine($"Ticket number {ticketNumber} is already signed up for a tour."); + + Console.Write("Do you want to change your sign-up (Y/N)? "); + string response = Console.ReadLine().Trim().ToUpper(); + if (response == "Y") + { + signupsByTourTime[selectedTour.Time].Remove(existingParticipant); + selectedTour.ParticipantsCount--; // Decrement count for the old sign-up + } + else + { + return; + } + } + + Participant participant = new Participant(ticketNumber, selectedTour.Time); + signupsByTourTime[selectedTour.Time].Add(participant); + selectedTour.ParticipantsCount++; // Increment count for the new sign-up + + SaveParticipantsToJson(); // Save immediately after sign-up + + Console.WriteLine($"You have successfully signed up for the tour at {selectedTour.Time.ToString("HH:mm")}."); + } + + static void DeleteSignUpForTour() + { + Console.Write("Enter your ticket number to delete your sign-up: "); + int ticketNumberToDelete; + while (!int.TryParse(Console.ReadLine(), out ticketNumberToDelete) || ticketNumberToDelete <= 0) + { + Console.WriteLine("Invalid input. Ticket number must be a positive integer."); + } + + foreach (var tourTime in signupsByTourTime.Keys) + { + Participant participantToDelete = signupsByTourTime[tourTime].FirstOrDefault(p => p.TicketNumber == ticketNumberToDelete); + if (participantToDelete != null) + { + signupsByTourTime[tourTime].Remove(participantToDelete); + + var tourToUpdate = tours.FirstOrDefault(t => t.Time == tourTime); + if (tourToUpdate != null) + { + tourToUpdate.ParticipantsCount--; + } + + SaveParticipantsToJson(); // Save immediately after deletion + + Console.WriteLine($"Your sign-up for the tour at {tourTime.ToString("HH:mm")} has been deleted."); + return; + } + } + + Console.WriteLine($"No sign-up found for ticket number {ticketNumberToDelete}."); + } + + static void LoadParticipantsFromJson() + { + if (File.Exists(jsonFilePath)) + { + string json = File.ReadAllText(jsonFilePath); + var dictionary = JsonSerializer.Deserialize>>(json); + + foreach (var kvp in dictionary) + { + foreach (var participant in kvp.Value) + { + if (signupsByTourTime.ContainsKey(kvp.Key)) + { + signupsByTourTime[kvp.Key].Add(participant); + var tourToUpdate = tours.FirstOrDefault(t => t.Time == kvp.Key); + if (tourToUpdate != null) + { + tourToUpdate.ParticipantsCount++; + } + } + else + { + signupsByTourTime[kvp.Key] = new List { participant }; + var tourToUpdate = tours.FirstOrDefault(t => t.Time == kvp.Key); + if (tourToUpdate != null) + { + tourToUpdate.ParticipantsCount++; + } + } + } + } + } + } + + static void SaveParticipantsToJson() + { + var dataToSerialize = new Dictionary>(); - // Call methods on the service - service.DoSomething(); + foreach (var kvp in signupsByTourTime) + { + dataToSerialize.Add(kvp.Key, kvp.Value); + } - // Rest of your code... + string json = JsonSerializer.Serialize(dataToSerialize); + File.WriteAllText(jsonFilePath, json); } -} \ No newline at end of file +} From 31e8fd706ea6aafb4e870883755f4ea21528d3ee Mon Sep 17 00:00:00 2001 From: KaiHog <161350865+KaiHog@users.noreply.github.com> Date: Sat, 30 Mar 2024 13:35:59 +0100 Subject: [PATCH 02/22] Rename Program.cs to Reservation.cs --- ProjectB/{Program.cs => Reservation.cs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename ProjectB/{Program.cs => Reservation.cs} (100%) diff --git a/ProjectB/Program.cs b/ProjectB/Reservation.cs similarity index 100% rename from ProjectB/Program.cs rename to ProjectB/Reservation.cs From 820d80727a968000cc3c46e17acbd6ff57567c83 Mon Sep 17 00:00:00 2001 From: KaiHog <161350865+KaiHog@users.noreply.github.com> Date: Sat, 30 Mar 2024 14:20:03 +0100 Subject: [PATCH 03/22] Update Reservation.cs --- ProjectB/Reservation.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ProjectB/Reservation.cs b/ProjectB/Reservation.cs index 61e0b43..6513a1b 100644 --- a/ProjectB/Reservation.cs +++ b/ProjectB/Reservation.cs @@ -148,9 +148,9 @@ static void SignUpForTour() Participant participant = new Participant(ticketNumber, selectedTour.Time); signupsByTourTime[selectedTour.Time].Add(participant); - selectedTour.ParticipantsCount++; // Increment count for the new sign-up + selectedTour.ParticipantsCount++; - SaveParticipantsToJson(); // Save immediately after sign-up + SaveParticipantsToJson(); Console.WriteLine($"You have successfully signed up for the tour at {selectedTour.Time.ToString("HH:mm")}."); } @@ -177,7 +177,7 @@ static void DeleteSignUpForTour() tourToUpdate.ParticipantsCount--; } - SaveParticipantsToJson(); // Save immediately after deletion + SaveParticipantsToJson(); Console.WriteLine($"Your sign-up for the tour at {tourTime.ToString("HH:mm")} has been deleted."); return; From 1b2f51dcd73c050a20227fa8e4ea4d3940d4fd34 Mon Sep 17 00:00:00 2001 From: KaiHog <161350865+KaiHog@users.noreply.github.com> Date: Sat, 30 Mar 2024 14:20:43 +0100 Subject: [PATCH 04/22] Update Reservation.cs --- ProjectB/Reservation.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ProjectB/Reservation.cs b/ProjectB/Reservation.cs index 6513a1b..1a00183 100644 --- a/ProjectB/Reservation.cs +++ b/ProjectB/Reservation.cs @@ -138,7 +138,7 @@ static void SignUpForTour() if (response == "Y") { signupsByTourTime[selectedTour.Time].Remove(existingParticipant); - selectedTour.ParticipantsCount--; // Decrement count for the old sign-up + selectedTour.ParticipantsCount--; } else { From d2fbc583ce1d1283484bfff69b5c90453c416b56 Mon Sep 17 00:00:00 2001 From: KaiHog <161350865+KaiHog@users.noreply.github.com> Date: Mon, 1 Apr 2024 19:52:09 +0200 Subject: [PATCH 05/22] Tour --- ProjectB/Tour | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 ProjectB/Tour diff --git a/ProjectB/Tour b/ProjectB/Tour new file mode 100644 index 0000000..b39bb2b --- /dev/null +++ b/ProjectB/Tour @@ -0,0 +1,15 @@ +class Tour +{ + public DateTime Time { get; set; } // Time of the tour + public string Location { get; set; } // Location of the tour + public int Capacity { get; set; } // Maximum capacity of the tour + public int ParticipantsCount { get; set; } // Number of participants signed up for the tour + + public Tour(DateTime time, string location, int capacity) + { + Time = time; + Location = location; + Capacity = capacity; + ParticipantsCount = 0; // Initially, no participants signed up + } +} From b69b9205835283c80dc8165b1958c01305fb5e9e Mon Sep 17 00:00:00 2001 From: KaiHog <161350865+KaiHog@users.noreply.github.com> Date: Mon, 1 Apr 2024 19:52:55 +0200 Subject: [PATCH 06/22] Participant --- ProjectB/Participant | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 ProjectB/Participant diff --git a/ProjectB/Participant b/ProjectB/Participant new file mode 100644 index 0000000..8f7af9a --- /dev/null +++ b/ProjectB/Participant @@ -0,0 +1,11 @@ +class Participant +{ + public int TicketNumber { get; set; } // Unique ticket number of the participant + public DateTime TourTime { get; set; } // Time of the tour the participant signed up for + + public Participant(int ticketNumber, DateTime tourTime) + { + TicketNumber = ticketNumber; + TourTime = tourTime; + } +} From 89bb28675d4b593c24e9bc44bf62b85c7a39f596 Mon Sep 17 00:00:00 2001 From: KaiHog <161350865+KaiHog@users.noreply.github.com> Date: Mon, 1 Apr 2024 19:53:35 +0200 Subject: [PATCH 07/22] Reservation.cs --- ProjectB/Reservation.cs | 75 +++++++++++++---------------------------- 1 file changed, 24 insertions(+), 51 deletions(-) diff --git a/ProjectB/Reservation.cs b/ProjectB/Reservation.cs index 1a00183..f8a5138 100644 --- a/ProjectB/Reservation.cs +++ b/ProjectB/Reservation.cs @@ -4,54 +4,28 @@ using System.Linq; using System.Text.Json; -class Tour -{ - public DateTime Time { get; set; } - public string Location { get; set; } - public int Capacity { get; set; } - public int ParticipantsCount { get; set; } - - public Tour(DateTime time, string location, int capacity) - { - Time = time; - Location = location; - Capacity = capacity; - ParticipantsCount = 0; - } -} - -class Participant -{ - public int TicketNumber { get; set; } - public DateTime TourTime { get; set; } - - public Participant(int ticketNumber, DateTime tourTime) - { - TicketNumber = ticketNumber; - TourTime = tourTime; - } -} - class Program { - static List tours = new List(); - static Dictionary> signupsByTourTime = new Dictionary>(); - static string jsonFilePath = "signups.json"; + static List tours = new List(); // List to store available tours + static Dictionary> signupsByTourTime = new Dictionary>(); // Dictionary to store sign-ups for each tour time + static string jsonFilePath = "signups.json"; // File path to store sign-up data in JSON format static void Main(string[] args) { - LoadParticipantsFromJson(); + LoadParticipantsFromJson(); // Load sign-up data from JSON file, if exists + // Create tours for each hour from 9 AM to 8 PM for (int hour = 9; hour <= 20; hour++) { DateTime tourTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, hour, 0, 0); - tours.Add(new Tour(tourTime, "Your Tour Location", 13)); + tours.Add(new Tour(tourTime, "Your Tour Location", 13)); // Initialize tours with default capacity of 13 if (!signupsByTourTime.ContainsKey(tourTime)) { - signupsByTourTime.Add(tourTime, new List()); + signupsByTourTime.Add(tourTime, new List()); // Initialize sign-up list for each tour time } } + // Register event handler to save sign-up data on application exit AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit); bool exit = false; @@ -87,7 +61,7 @@ static void Main(string[] args) static void CurrentDomain_ProcessExit(object sender, EventArgs e) { - SaveParticipantsToJson(); + SaveParticipantsToJson(); // Save sign-up data on application exit } static void SignUpForTour() @@ -109,7 +83,7 @@ static void SignUpForTour() if (tourNumber == 0) { - return; + return; // Go back to main menu } Tour selectedTour = tours[tourNumber - 1]; @@ -137,20 +111,20 @@ static void SignUpForTour() string response = Console.ReadLine().Trim().ToUpper(); if (response == "Y") { - signupsByTourTime[selectedTour.Time].Remove(existingParticipant); - selectedTour.ParticipantsCount--; + signupsByTourTime[selectedTour.Time].Remove(existingParticipant); // Remove existing sign-up + selectedTour.ParticipantsCount--; // Decrement count for the old sign-up } else { - return; + return; // Go back to main menu } } Participant participant = new Participant(ticketNumber, selectedTour.Time); - signupsByTourTime[selectedTour.Time].Add(participant); - selectedTour.ParticipantsCount++; + signupsByTourTime[selectedTour.Time].Add(participant); // Add new sign-up + selectedTour.ParticipantsCount++; // Increment count for the new sign-up - SaveParticipantsToJson(); + SaveParticipantsToJson(); // Save immediately after sign-up Console.WriteLine($"You have successfully signed up for the tour at {selectedTour.Time.ToString("HH:mm")}."); } @@ -169,15 +143,14 @@ static void DeleteSignUpForTour() Participant participantToDelete = signupsByTourTime[tourTime].FirstOrDefault(p => p.TicketNumber == ticketNumberToDelete); if (participantToDelete != null) { - signupsByTourTime[tourTime].Remove(participantToDelete); - + signupsByTourTime[tourTime].Remove(participantToDelete); // Remove sign-up var tourToUpdate = tours.FirstOrDefault(t => t.Time == tourTime); if (tourToUpdate != null) { - tourToUpdate.ParticipantsCount--; + tourToUpdate.ParticipantsCount--; // Decrement count for the deleted sign-up } - SaveParticipantsToJson(); + SaveParticipantsToJson(); // Save immediately after deletion Console.WriteLine($"Your sign-up for the tour at {tourTime.ToString("HH:mm")} has been deleted."); return; @@ -200,20 +173,20 @@ static void LoadParticipantsFromJson() { if (signupsByTourTime.ContainsKey(kvp.Key)) { - signupsByTourTime[kvp.Key].Add(participant); + signupsByTourTime[kvp.Key].Add(participant); // Add participant to existing tour time var tourToUpdate = tours.FirstOrDefault(t => t.Time == kvp.Key); if (tourToUpdate != null) { - tourToUpdate.ParticipantsCount++; + tourToUpdate.ParticipantsCount++; // Increment count for loaded sign-up } } else { - signupsByTourTime[kvp.Key] = new List { participant }; + signupsByTourTime[kvp.Key] = new List { participant }; // Initialize new tour time var tourToUpdate = tours.FirstOrDefault(t => t.Time == kvp.Key); if (tourToUpdate != null) { - tourToUpdate.ParticipantsCount++; + tourToUpdate.ParticipantsCount++; // Increment count for loaded sign-up } } } @@ -231,6 +204,6 @@ static void SaveParticipantsToJson() } string json = JsonSerializer.Serialize(dataToSerialize); - File.WriteAllText(jsonFilePath, json); + File.WriteAllText(jsonFilePath, json); // Write sign-up data to JSON file } } From 1d9b58dd914b538367f4843f6f567a88a08cc670 Mon Sep 17 00:00:00 2001 From: KaiHog <161350865+KaiHog@users.noreply.github.com> Date: Mon, 1 Apr 2024 19:53:58 +0200 Subject: [PATCH 08/22] Rename Tour to Tour.cs --- ProjectB/{Tour => Tour.cs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename ProjectB/{Tour => Tour.cs} (100%) diff --git a/ProjectB/Tour b/ProjectB/Tour.cs similarity index 100% rename from ProjectB/Tour rename to ProjectB/Tour.cs From cb73dab4187b29f9aa22eb4ee214b9b32147cbe9 Mon Sep 17 00:00:00 2001 From: KaiHog <161350865+KaiHog@users.noreply.github.com> Date: Mon, 1 Apr 2024 19:54:16 +0200 Subject: [PATCH 09/22] Rename Participant to Participant.cs --- ProjectB/{Participant => Participant.cs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename ProjectB/{Participant => Participant.cs} (100%) diff --git a/ProjectB/Participant b/ProjectB/Participant.cs similarity index 100% rename from ProjectB/Participant rename to ProjectB/Participant.cs From 77933c25243eb0be89de7586ac9b40363f38f54d Mon Sep 17 00:00:00 2001 From: KaiHog <161350865+KaiHog@users.noreply.github.com> Date: Mon, 1 Apr 2024 20:19:56 +0200 Subject: [PATCH 10/22] Update Participant.cs From 8371ad06d52eb70656eea30887a0e64eb35ff9ac Mon Sep 17 00:00:00 2001 From: KaiHog <161350865+KaiHog@users.noreply.github.com> Date: Mon, 1 Apr 2024 20:21:06 +0200 Subject: [PATCH 11/22] Update Reservation.cs Nu kan je alleen de tours zien die later zijn dan de tijd van nu. --- ProjectB/Reservation.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ProjectB/Reservation.cs b/ProjectB/Reservation.cs index f8a5138..fdc61d8 100644 --- a/ProjectB/Reservation.cs +++ b/ProjectB/Reservation.cs @@ -14,8 +14,11 @@ static void Main(string[] args) { LoadParticipantsFromJson(); // Load sign-up data from JSON file, if exists - // Create tours for each hour from 9 AM to 8 PM - for (int hour = 9; hour <= 20; hour++) + // Get the current hour + int currentHour = DateTime.Now.Hour; + + // Create tours for each hour from the current hour to 8 PM + for (int hour = currentHour; hour <= 20; hour++) { DateTime tourTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, hour, 0, 0); tours.Add(new Tour(tourTime, "Your Tour Location", 13)); // Initialize tours with default capacity of 13 From 56ad952c30b439e0538c817772d0559400ffa5af Mon Sep 17 00:00:00 2001 From: KaiHog <161350865+KaiHog@users.noreply.github.com> Date: Mon, 1 Apr 2024 20:21:59 +0200 Subject: [PATCH 12/22] Update Tour.cs From e8a791b26757a8ed1254f7ebf0477b7de610c0f6 Mon Sep 17 00:00:00 2001 From: KaiHog <161350865+KaiHog@users.noreply.github.com> Date: Tue, 2 Apr 2024 17:43:04 +0200 Subject: [PATCH 13/22] Update Reservation.cs Changes class program to Reservation --- ProjectB/Reservation.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ProjectB/Reservation.cs b/ProjectB/Reservation.cs index fdc61d8..d89843b 100644 --- a/ProjectB/Reservation.cs +++ b/ProjectB/Reservation.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text.Json; -class Program +class Reservation { static List tours = new List(); // List to store available tours static Dictionary> signupsByTourTime = new Dictionary>(); // Dictionary to store sign-ups for each tour time From fda29a2cf2c036f9c3ae827a8fbf41d677939b1e Mon Sep 17 00:00:00 2001 From: KaiHog <161350865+KaiHog@users.noreply.github.com> Date: Tue, 2 Apr 2024 18:29:13 +0200 Subject: [PATCH 14/22] Update Tour.cs changed participantcount to Icollection --- ProjectB/Tour.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ProjectB/Tour.cs b/ProjectB/Tour.cs index b39bb2b..688ef2b 100644 --- a/ProjectB/Tour.cs +++ b/ProjectB/Tour.cs @@ -3,13 +3,13 @@ class Tour public DateTime Time { get; set; } // Time of the tour public string Location { get; set; } // Location of the tour public int Capacity { get; set; } // Maximum capacity of the tour - public int ParticipantsCount { get; set; } // Number of participants signed up for the tour + public ICollection Participants { get; set; } // Collection of participants signed up for the tour public Tour(DateTime time, string location, int capacity) { Time = time; Location = location; Capacity = capacity; - ParticipantsCount = 0; // Initially, no participants signed up + Participants = new List(); // Initialize participant collection } } From db28b10c763247059cfded25f647fae5a32507c9 Mon Sep 17 00:00:00 2001 From: KaiHog <161350865+KaiHog@users.noreply.github.com> Date: Tue, 2 Apr 2024 18:30:14 +0200 Subject: [PATCH 15/22] Update Participant.cs Removed datetime from participants --- ProjectB/Participant.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ProjectB/Participant.cs b/ProjectB/Participant.cs index 8f7af9a..6c8cf68 100644 --- a/ProjectB/Participant.cs +++ b/ProjectB/Participant.cs @@ -1,11 +1,9 @@ class Participant { public int TicketNumber { get; set; } // Unique ticket number of the participant - public DateTime TourTime { get; set; } // Time of the tour the participant signed up for - public Participant(int ticketNumber, DateTime tourTime) + public Participant(int ticketNumber) { TicketNumber = ticketNumber; - TourTime = tourTime; } } From ab4e40c0ed75d30f6d3f7fedd071716152295591 Mon Sep 17 00:00:00 2001 From: KaiHog <161350865+KaiHog@users.noreply.github.com> Date: Tue, 2 Apr 2024 18:31:13 +0200 Subject: [PATCH 16/22] Update Reservation.cs --- ProjectB/Reservation.cs | 70 ++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 46 deletions(-) diff --git a/ProjectB/Reservation.cs b/ProjectB/Reservation.cs index d89843b..e60bf3a 100644 --- a/ProjectB/Reservation.cs +++ b/ProjectB/Reservation.cs @@ -7,7 +7,6 @@ class Reservation { static List tours = new List(); // List to store available tours - static Dictionary> signupsByTourTime = new Dictionary>(); // Dictionary to store sign-ups for each tour time static string jsonFilePath = "signups.json"; // File path to store sign-up data in JSON format static void Main(string[] args) @@ -21,11 +20,8 @@ static void Main(string[] args) for (int hour = currentHour; hour <= 20; hour++) { DateTime tourTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, hour, 0, 0); - tours.Add(new Tour(tourTime, "Your Tour Location", 13)); // Initialize tours with default capacity of 13 - if (!signupsByTourTime.ContainsKey(tourTime)) - { - signupsByTourTime.Add(tourTime, new List()); // Initialize sign-up list for each tour time - } + Tour tour = new Tour(tourTime, "Your Tour Location", 13); // Initialize tours with default capacity of 13 + tours.Add(tour); } // Register event handler to save sign-up data on application exit @@ -72,7 +68,7 @@ static void SignUpForTour() Console.WriteLine("Available Tours:"); for (int i = 0; i < tours.Count; i++) { - int spotsLeft = tours[i].Capacity - tours[i].ParticipantsCount; + int spotsLeft = tours[i].Capacity - tours[i].Participants.Count; Console.WriteLine($"{i + 1}. Tour at {tours[i].Time.ToString("HH:mm")}, {spotsLeft} spots left"); } Console.WriteLine("0. Go back"); @@ -91,7 +87,7 @@ static void SignUpForTour() Tour selectedTour = tours[tourNumber - 1]; - if (selectedTour.ParticipantsCount >= selectedTour.Capacity) + if (selectedTour.Participants.Count >= selectedTour.Capacity) { Console.WriteLine($"Sorry, the tour at {selectedTour.Time.ToString("HH:mm")} is already fully booked."); return; @@ -105,8 +101,7 @@ static void SignUpForTour() Console.Write("Enter your ticket number: "); } - Participant existingParticipant = signupsByTourTime[selectedTour.Time].FirstOrDefault(p => p.TicketNumber == ticketNumber); - if (existingParticipant != null) + if (selectedTour.Participants.Any(p => p.TicketNumber == ticketNumber)) { Console.WriteLine($"Ticket number {ticketNumber} is already signed up for a tour."); @@ -114,8 +109,8 @@ static void SignUpForTour() string response = Console.ReadLine().Trim().ToUpper(); if (response == "Y") { - signupsByTourTime[selectedTour.Time].Remove(existingParticipant); // Remove existing sign-up - selectedTour.ParticipantsCount--; // Decrement count for the old sign-up + var participantToRemove = selectedTour.Participants.FirstOrDefault(p => p.TicketNumber == ticketNumber); + selectedTour.Participants.Remove(participantToRemove); // Remove existing sign-up } else { @@ -123,9 +118,8 @@ static void SignUpForTour() } } - Participant participant = new Participant(ticketNumber, selectedTour.Time); - signupsByTourTime[selectedTour.Time].Add(participant); // Add new sign-up - selectedTour.ParticipantsCount++; // Increment count for the new sign-up + Participant participant = new Participant(ticketNumber); + selectedTour.Participants.Add(participant); // Add new sign-up SaveParticipantsToJson(); // Save immediately after sign-up @@ -141,21 +135,16 @@ static void DeleteSignUpForTour() Console.WriteLine("Invalid input. Ticket number must be a positive integer."); } - foreach (var tourTime in signupsByTourTime.Keys) + foreach (var tour in tours) { - Participant participantToDelete = signupsByTourTime[tourTime].FirstOrDefault(p => p.TicketNumber == ticketNumberToDelete); - if (participantToDelete != null) + var participantToRemove = tour.Participants.FirstOrDefault(p => p.TicketNumber == ticketNumberToDelete); + if (participantToRemove != null) { - signupsByTourTime[tourTime].Remove(participantToDelete); // Remove sign-up - var tourToUpdate = tours.FirstOrDefault(t => t.Time == tourTime); - if (tourToUpdate != null) - { - tourToUpdate.ParticipantsCount--; // Decrement count for the deleted sign-up - } + tour.Participants.Remove(participantToRemove); // Remove sign-up SaveParticipantsToJson(); // Save immediately after deletion - Console.WriteLine($"Your sign-up for the tour at {tourTime.ToString("HH:mm")} has been deleted."); + Console.WriteLine($"Your sign-up for the tour at {tour.Time.ToString("HH:mm")} has been deleted."); return; } } @@ -168,29 +157,18 @@ static void LoadParticipantsFromJson() if (File.Exists(jsonFilePath)) { string json = File.ReadAllText(jsonFilePath); - var dictionary = JsonSerializer.Deserialize>>(json); + var dictionary = JsonSerializer.Deserialize>>(json); foreach (var kvp in dictionary) { - foreach (var participant in kvp.Value) + DateTime tourTime = DateTime.Parse(kvp.Key); + Tour tour = tours.FirstOrDefault(t => t.Time == tourTime); + if (tour != null) { - if (signupsByTourTime.ContainsKey(kvp.Key)) - { - signupsByTourTime[kvp.Key].Add(participant); // Add participant to existing tour time - var tourToUpdate = tours.FirstOrDefault(t => t.Time == kvp.Key); - if (tourToUpdate != null) - { - tourToUpdate.ParticipantsCount++; // Increment count for loaded sign-up - } - } - else + foreach (var ticketNumber in kvp.Value) { - signupsByTourTime[kvp.Key] = new List { participant }; // Initialize new tour time - var tourToUpdate = tours.FirstOrDefault(t => t.Time == kvp.Key); - if (tourToUpdate != null) - { - tourToUpdate.ParticipantsCount++; // Increment count for loaded sign-up - } + Participant participant = new Participant(ticketNumber); + tour.Participants.Add(participant); // Add participant to existing tour } } } @@ -199,11 +177,11 @@ static void LoadParticipantsFromJson() static void SaveParticipantsToJson() { - var dataToSerialize = new Dictionary>(); + var dataToSerialize = new Dictionary>(); - foreach (var kvp in signupsByTourTime) + foreach (var tour in tours) { - dataToSerialize.Add(kvp.Key, kvp.Value); + dataToSerialize.Add(tour.Time.ToString(), tour.Participants.Select(p => p.TicketNumber).ToList()); } string json = JsonSerializer.Serialize(dataToSerialize); From 142fb22df432d2818f2097ecedb360054787ad5e Mon Sep 17 00:00:00 2001 From: KaiHog Date: Wed, 3 Apr 2024 20:22:56 +0200 Subject: [PATCH 17/22] Participant > Guest --- ProjectB/Tour.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ProjectB/Tour.cs b/ProjectB/Tour.cs index 688ef2b..b658d77 100644 --- a/ProjectB/Tour.cs +++ b/ProjectB/Tour.cs @@ -1,15 +1,16 @@ +using ProjectB.Models; class Tour { public DateTime Time { get; set; } // Time of the tour public string Location { get; set; } // Location of the tour public int Capacity { get; set; } // Maximum capacity of the tour - public ICollection Participants { get; set; } // Collection of participants signed up for the tour + public ICollection Participants { get; set; } // Collection of participants signed up for the tour public Tour(DateTime time, string location, int capacity) { Time = time; Location = location; Capacity = capacity; - Participants = new List(); // Initialize participant collection + Participants = new List(); // Initialize participant collection } } From 531404f79488575390d4e169602e336fcb1d9073 Mon Sep 17 00:00:00 2001 From: KaiHog Date: Fri, 5 Apr 2024 13:47:18 +0200 Subject: [PATCH 18/22] Troubleshooting --- ProjectB/Models/AbstractUser.cs | 45 ++++++++++++------ ProjectB/Models/Guest.cs | 36 +++++++++------ ProjectB/Participant.cs | 9 ---- ProjectB/Reservation.cs | 61 ++++++++++++++----------- ProjectB/Services/RondleidingService.cs | 33 +++++++++---- ProjectB/Tour.cs | 29 +++++++----- 6 files changed, 129 insertions(+), 84 deletions(-) delete mode 100644 ProjectB/Participant.cs diff --git a/ProjectB/Models/AbstractUser.cs b/ProjectB/Models/AbstractUser.cs index 146fb13..bd15d15 100644 --- a/ProjectB/Models/AbstractUser.cs +++ b/ProjectB/Models/AbstractUser.cs @@ -1,19 +1,22 @@ -using Newtonsoft.Json; - -namespace ProjectB.Models; - using BCrypt.Net; +using Newtonsoft.Json; +using ProjectB.Models; +using System; /// /// Blueprint for users, containing the fields shared by all user types. This class should be implemented by sub-classes, /// which specify the type of user, and implement unique functionality and fields based on that type. /// -/// The username assigned to the user. -/// The role assigned to a user, used to determine what functionality should be available to said user. -public abstract class AbstractUser(string username, UserRole role) : IEquatable, IEntity +public abstract class AbstractUser : IEquatable, IEntity { - [JsonProperty] protected readonly string Username = username; - [JsonProperty] protected readonly UserRole Role = role; + [JsonProperty] protected readonly string Username; + [JsonProperty] protected readonly UserRole Role; + + public AbstractUser(string username, UserRole role) + { + Username = username; + Role = role; + } /// /// Gets the role assigned to the user. @@ -36,7 +39,7 @@ public UserRole GetUserRole() /// The object which will be checked for equality with the object this method is called on. /// True if the objects are equal, false if not. public abstract bool Equals(AbstractUser? other); - + /// /// Provides a more generic way to check for equality between an AbstractUser-instance and an instance of an unknown /// type. @@ -65,9 +68,10 @@ public override int GetHashCode() private class Builder { private string? _username; - private string? _password; + private int _ticketNumber; // Add ticketNumber field private UserRole _role; private DateOnly _validForDate; + private string? _password; // Add password field /// /// Sets the username that will be set, when the Builder.Build()-method is called. @@ -80,16 +84,27 @@ public Builder WithUsername(string username) return this; } + /// + /// Sets the ticket number for the guest user. + /// + /// The ticket number of the guest user. + /// This instance of the Builder-pattern. + public Builder WithTicketNumber(int ticketNumber) + { + _ticketNumber = ticketNumber; + return this; + } + /// /// Sets the password that will be set, when the Builder.Build()-method is called. Before setting the password, - /// the given value is hashed, using BCrypt's EnhancedHashPassword-method. If the role of the user is + /// the given value is hashed, using BCrypt's HashPassword-method. If the role of the user is /// UserRole.Guest, the password will be ignored. /// /// The password that will be set. /// This instance of the Builder. public Builder WithPassword(string password) { - _password = BCrypt.EnhancedHashPassword(password); + _password = BCrypt.HashPassword(password); // Change to BCrypt.HashPassword return this; } @@ -125,10 +140,10 @@ public AbstractUser Build() { if (_role == UserRole.Guest) { - return new Guest(_username!, _validForDate); + return new Guest(_username!, _validForDate, _ticketNumber); // Include ticketNumber } return new Employee(_username!, _role, _password!); } } -} \ No newline at end of file +} diff --git a/ProjectB/Models/Guest.cs b/ProjectB/Models/Guest.cs index 1319794..e82af01 100644 --- a/ProjectB/Models/Guest.cs +++ b/ProjectB/Models/Guest.cs @@ -1,18 +1,28 @@ using Newtonsoft.Json; -namespace ProjectB.Models; - -public class Guest(string username, DateOnly validForDate) : AbstractUser(username, UserRole.Guest) +namespace ProjectB.Models { - [JsonProperty] private readonly DateOnly _validForDate = validForDate; - - public bool IsValid() => _validForDate.CompareTo(DateTime.Today) == 0; - - public override bool Equals(AbstractUser? other) + public class Guest : AbstractUser { - if (ReferenceEquals(other, this)) return true; - if (ReferenceEquals(other, null)) return false; - if (other.GetType() != GetType()) return false; - return Username == other.GetId(); + [JsonProperty] + private readonly DateOnly _validForDate; + + public int TicketNumber { get; set; } + + public Guest(string username, DateOnly validForDate, int ticketNumber) : base(username, UserRole.Guest) + { + _validForDate = validForDate; + TicketNumber = ticketNumber; + } + + public bool IsValid() => _validForDate.CompareTo(DateTime.Today) == 0; + + public override bool Equals(AbstractUser? other) + { + if (ReferenceEquals(other, this)) return true; + if (ReferenceEquals(other, null)) return false; + if (other.GetType() != GetType()) return false; + return Username == other.GetId(); + } } -} \ No newline at end of file +} diff --git a/ProjectB/Participant.cs b/ProjectB/Participant.cs deleted file mode 100644 index 6c8cf68..0000000 --- a/ProjectB/Participant.cs +++ /dev/null @@ -1,9 +0,0 @@ -class Participant -{ - public int TicketNumber { get; set; } // Unique ticket number of the participant - - public Participant(int ticketNumber) - { - TicketNumber = ticketNumber; - } -} diff --git a/ProjectB/Reservation.cs b/ProjectB/Reservation.cs index e60bf3a..0f1fbd5 100644 --- a/ProjectB/Reservation.cs +++ b/ProjectB/Reservation.cs @@ -2,7 +2,8 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using System.Text.Json; +using Newtonsoft.Json; +using ProjectB.Models; class Reservation { @@ -58,7 +59,7 @@ static void Main(string[] args) } } - static void CurrentDomain_ProcessExit(object sender, EventArgs e) + static void CurrentDomain_ProcessExit(object? sender, EventArgs e) { SaveParticipantsToJson(); // Save sign-up data on application exit } @@ -109,8 +110,11 @@ static void SignUpForTour() string response = Console.ReadLine().Trim().ToUpper(); if (response == "Y") { - var participantToRemove = selectedTour.Participants.FirstOrDefault(p => p.TicketNumber == ticketNumber); - selectedTour.Participants.Remove(participantToRemove); // Remove existing sign-up + var guestToRemove = selectedTour.Participants.FirstOrDefault(p => p.TicketNumber == ticketNumber); + if (guestToRemove != null) + { + selectedTour.Participants.Remove(guestToRemove); // Remove existing sign-up + } } else { @@ -118,8 +122,9 @@ static void SignUpForTour() } } - Participant participant = new Participant(ticketNumber); - selectedTour.Participants.Add(participant); // Add new sign-up + // Modify this line to create a Guest object instead of a Participant + Guest guest = new Guest("username", DateOnly.FromDateTime(DateTime.Today), ticketNumber); + selectedTour.Participants.Add(guest); // Add new sign-up SaveParticipantsToJson(); // Save immediately after sign-up @@ -127,37 +132,38 @@ static void SignUpForTour() } static void DeleteSignUpForTour() +{ + Console.Write("Enter your ticket number to delete your sign-up: "); + int ticketNumberToDelete; + while (!int.TryParse(Console.ReadLine(), out ticketNumberToDelete) || ticketNumberToDelete <= 0) { - Console.Write("Enter your ticket number to delete your sign-up: "); - int ticketNumberToDelete; - while (!int.TryParse(Console.ReadLine(), out ticketNumberToDelete) || ticketNumberToDelete <= 0) - { - Console.WriteLine("Invalid input. Ticket number must be a positive integer."); - } + Console.WriteLine("Invalid input. Ticket number must be a positive integer."); + } - foreach (var tour in tours) + foreach (var tour in tours) + { + var guestToRemove = tour.Participants.FirstOrDefault(p => p.TicketNumber == ticketNumberToDelete); + if (guestToRemove != null) { - var participantToRemove = tour.Participants.FirstOrDefault(p => p.TicketNumber == ticketNumberToDelete); - if (participantToRemove != null) - { - tour.Participants.Remove(participantToRemove); // Remove sign-up + tour.Participants.Remove(guestToRemove); // Remove sign-up - SaveParticipantsToJson(); // Save immediately after deletion + SaveParticipantsToJson(); // Save immediately after deletion - Console.WriteLine($"Your sign-up for the tour at {tour.Time.ToString("HH:mm")} has been deleted."); - return; - } + Console.WriteLine($"Your sign-up for the tour at {tour.Time.ToString("HH:mm")} has been deleted."); + return; } - - Console.WriteLine($"No sign-up found for ticket number {ticketNumberToDelete}."); } + Console.WriteLine($"No sign-up found for ticket number {ticketNumberToDelete}."); +} + + static void LoadParticipantsFromJson() { if (File.Exists(jsonFilePath)) { string json = File.ReadAllText(jsonFilePath); - var dictionary = JsonSerializer.Deserialize>>(json); + var dictionary = JsonConvert.DeserializeObject>>(json); // Use JsonConvert.DeserializeObject from Newtonsoft.Json foreach (var kvp in dictionary) { @@ -167,8 +173,9 @@ static void LoadParticipantsFromJson() { foreach (var ticketNumber in kvp.Value) { - Participant participant = new Participant(ticketNumber); - tour.Participants.Add(participant); // Add participant to existing tour + // Modify this line to create a Guest object instead of a Participant + Guest guest = new Guest("username", DateOnly.FromDateTime(DateTime.Today), ticketNumber); + tour.Participants.Add(guest); // Add guest to existing tour } } } @@ -184,7 +191,7 @@ static void SaveParticipantsToJson() dataToSerialize.Add(tour.Time.ToString(), tour.Participants.Select(p => p.TicketNumber).ToList()); } - string json = JsonSerializer.Serialize(dataToSerialize); + string json = JsonConvert.SerializeObject(dataToSerialize); // Use JsonConvert.SerializeObject from Newtonsoft.Json File.WriteAllText(jsonFilePath, json); // Write sign-up data to JSON file } } diff --git a/ProjectB/Services/RondleidingService.cs b/ProjectB/Services/RondleidingService.cs index 195a857..545fb10 100644 --- a/ProjectB/Services/RondleidingService.cs +++ b/ProjectB/Services/RondleidingService.cs @@ -1,14 +1,16 @@ using System; using ProjectB.IO; +using ProjectB.Models; +using System.IO; +using Newtonsoft.Json; namespace ProjectB.Services { - // Naam van de serve moet nog aangepast worden public class RondleidingService { - private readonly JsonFileReader _jsonFileReader; + private readonly JsonFileReader _jsonFileReader; // Specify Tour as the type argument - public RondleidingService(JsonFileReader jsonFileReader) + public RondleidingService(JsonFileReader jsonFileReader) // Adjust the constructor parameter { _jsonFileReader = jsonFileReader; } @@ -16,15 +18,15 @@ public RondleidingService(JsonFileReader jsonFileReader) public void ShowRondleidingData() { // Replace "path/to/your/json/file.json" with the actual path to your JSON file - string jsonFilePath = "path/to/your/json/file.json"; + string jsonFilePath = "C:\\Bureaublad\\C# Main\\Project-B\\ProjectB\\Database\\database.json"; try { - // Read the JSON file using the JsonFileReader - var jsonData = _jsonFileReader.ReadJsonFile(jsonFilePath); + // Read the JSON file using the JsonFileReader + var tourData = _jsonFileReader.ReadJsonFile(jsonFilePath); // Display the data in the console - Console.WriteLine(jsonData); + Console.WriteLine(tourData); } catch (Exception ex) { @@ -32,4 +34,19 @@ public void ShowRondleidingData() } } } -} \ No newline at end of file + + public class JsonFileReader + { + public T ReadJsonFile(string filePath) + { + if (!File.Exists(filePath)) + { + throw new FileNotFoundException("JSON file not found.", filePath); + } + + string jsonData = File.ReadAllText(filePath); + T deserializedData = JsonConvert.DeserializeObject(jsonData); + return deserializedData; + } + } +} diff --git a/ProjectB/Tour.cs b/ProjectB/Tour.cs index b658d77..5e204ba 100644 --- a/ProjectB/Tour.cs +++ b/ProjectB/Tour.cs @@ -1,16 +1,21 @@ -using ProjectB.Models; -class Tour -{ - public DateTime Time { get; set; } // Time of the tour - public string Location { get; set; } // Location of the tour - public int Capacity { get; set; } // Maximum capacity of the tour - public ICollection Participants { get; set; } // Collection of participants signed up for the tour +using System; +using System.Collections.Generic; - public Tour(DateTime time, string location, int capacity) +namespace ProjectB.Models +{ + public class Tour { - Time = time; - Location = location; - Capacity = capacity; - Participants = new List(); // Initialize participant collection + public DateTime Time { get; set; } // Time of the tour + public string Location { get; set; } // Location of the tour + public int Capacity { get; set; } // Maximum capacity of the tour + public ICollection Participants { get; set; } // Collection of participants signed up for the tour + + public Tour(DateTime time, string location, int capacity) + { + Time = time; + Location = location; + Capacity = capacity; + Participants = new List(); // Initialize participant collection + } } } From 0d66cf834f7148503b46061ff68f49e1d918c080 Mon Sep 17 00:00:00 2001 From: KaiHog Date: Fri, 5 Apr 2024 14:46:32 +0200 Subject: [PATCH 19/22] klaar --- ProjectB/Models/AbstractUser.cs | 4 ++-- ProjectB/Reservation.cs | 8 ++++---- ProjectB/Services/RondleidingService.cs | 4 ++-- ProjectBTest/Fixtures/GuestFixtures.cs | 26 +++++++++++++++---------- ProjectBTest/ProjectBTest.csproj | 5 +++++ 5 files changed, 29 insertions(+), 18 deletions(-) diff --git a/ProjectB/Models/AbstractUser.cs b/ProjectB/Models/AbstractUser.cs index bd15d15..c89bc94 100644 --- a/ProjectB/Models/AbstractUser.cs +++ b/ProjectB/Models/AbstractUser.cs @@ -1,4 +1,4 @@ -using BCrypt.Net; +using static BCrypt.Net.BCrypt; using Newtonsoft.Json; using ProjectB.Models; using System; @@ -104,7 +104,7 @@ public Builder WithTicketNumber(int ticketNumber) /// This instance of the Builder. public Builder WithPassword(string password) { - _password = BCrypt.HashPassword(password); // Change to BCrypt.HashPassword + _password = HashPassword(password); // Change to BCrypt.HashPassword return this; } diff --git a/ProjectB/Reservation.cs b/ProjectB/Reservation.cs index 0f1fbd5..af19853 100644 --- a/ProjectB/Reservation.cs +++ b/ProjectB/Reservation.cs @@ -8,7 +8,7 @@ class Reservation { static List tours = new List(); // List to store available tours - static string jsonFilePath = "signups.json"; // File path to store sign-up data in JSON format + static string jsonFilePath = "signups.json"; static void Main(string[] args) { @@ -107,7 +107,7 @@ static void SignUpForTour() Console.WriteLine($"Ticket number {ticketNumber} is already signed up for a tour."); Console.Write("Do you want to change your sign-up (Y/N)? "); - string response = Console.ReadLine().Trim().ToUpper(); + string? response = Console.ReadLine()!.Trim().ToUpper(); if (response == "Y") { var guestToRemove = selectedTour.Participants.FirstOrDefault(p => p.TicketNumber == ticketNumber); @@ -165,10 +165,10 @@ static void LoadParticipantsFromJson() string json = File.ReadAllText(jsonFilePath); var dictionary = JsonConvert.DeserializeObject>>(json); // Use JsonConvert.DeserializeObject from Newtonsoft.Json - foreach (var kvp in dictionary) + foreach (var kvp in dictionary!) { DateTime tourTime = DateTime.Parse(kvp.Key); - Tour tour = tours.FirstOrDefault(t => t.Time == tourTime); + Tour? tour = tours.FirstOrDefault(t => t.Time == tourTime); if (tour != null) { foreach (var ticketNumber in kvp.Value) diff --git a/ProjectB/Services/RondleidingService.cs b/ProjectB/Services/RondleidingService.cs index 545fb10..1499c5b 100644 --- a/ProjectB/Services/RondleidingService.cs +++ b/ProjectB/Services/RondleidingService.cs @@ -45,8 +45,8 @@ public T ReadJsonFile(string filePath) } string jsonData = File.ReadAllText(filePath); - T deserializedData = JsonConvert.DeserializeObject(jsonData); - return deserializedData; + T deserializedData = JsonConvert.DeserializeObject(jsonData)!; + return deserializedData!; } } } diff --git a/ProjectBTest/Fixtures/GuestFixtures.cs b/ProjectBTest/Fixtures/GuestFixtures.cs index bb31692..528f6fc 100644 --- a/ProjectBTest/Fixtures/GuestFixtures.cs +++ b/ProjectBTest/Fixtures/GuestFixtures.cs @@ -1,17 +1,23 @@ using ProjectB.Models; +using System; +using System.Collections.Generic; -namespace ProjectBTest.Fixtures; - -public class GuestFixtures +namespace ProjectBTest.Fixtures { - public static ICollection GenerateCollection(int amount) + public class GuestFixtures { - ICollection userList = new List(); - for (int i = 0; i < amount; i++) + public static ICollection GenerateCollection(int amount) { - userList.Add(new Guest(Guid.NewGuid().ToString(), DateOnly.FromDateTime(DateTime.Today))); - } + ICollection userList = new List(); + for (int i = 0; i < amount; i++) + { + // Generate a unique ticket number for each guest + int ticketNumber = i + 1; + + userList.Add(new Guest(Guid.NewGuid().ToString(), DateOnly.FromDateTime(DateTime.Today), ticketNumber)); + } - return userList; + return userList; + } } -} \ No newline at end of file +} diff --git a/ProjectBTest/ProjectBTest.csproj b/ProjectBTest/ProjectBTest.csproj index ab65b93..ab35ce3 100644 --- a/ProjectBTest/ProjectBTest.csproj +++ b/ProjectBTest/ProjectBTest.csproj @@ -20,6 +20,11 @@ runtime; build; native; contentfiles; analyzers; buildtransitive + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + From cc28e225b3b4d2cb5cedd6837dedd6ce32942e2b Mon Sep 17 00:00:00 2001 From: KaiHog Date: Fri, 5 Apr 2024 19:37:01 +0200 Subject: [PATCH 20/22] LoadParticipants moet nog --- ProjectB/Models/AbstractUser.cs | 32 +++--------- ProjectB/Models/Guest.cs | 10 ++-- ProjectB/{ => Models}/Tour.cs | 0 ProjectB/Reservation.cs | 77 +++++++++++------------------ ProjectBTest/UnitTestReservation.cs | 54 ++++++++++++++++++++ ProjectBTest/UnitTestTour.cs | 54 ++++++++++++++++++++ 6 files changed, 151 insertions(+), 76 deletions(-) rename ProjectB/{ => Models}/Tour.cs (100%) create mode 100644 ProjectBTest/UnitTestReservation.cs create mode 100644 ProjectBTest/UnitTestTour.cs diff --git a/ProjectB/Models/AbstractUser.cs b/ProjectB/Models/AbstractUser.cs index c89bc94..5a5df84 100644 --- a/ProjectB/Models/AbstractUser.cs +++ b/ProjectB/Models/AbstractUser.cs @@ -7,16 +7,13 @@ /// Blueprint for users, containing the fields shared by all user types. This class should be implemented by sub-classes, /// which specify the type of user, and implement unique functionality and fields based on that type. /// -public abstract class AbstractUser : IEquatable, IEntity +/// The username assigned to the user. +/// The role assigned to a user, used to determine what functionality should be available to said user. +public abstract class AbstractUser(string username, UserRole role) : IEquatable, IEntity { - [JsonProperty] protected readonly string Username; - [JsonProperty] protected readonly UserRole Role; - public AbstractUser(string username, UserRole role) - { - Username = username; - Role = role; - } + [JsonProperty] protected string Username = username; + [JsonProperty] protected UserRole Role = role; /// /// Gets the role assigned to the user. @@ -68,10 +65,9 @@ public override int GetHashCode() private class Builder { private string? _username; - private int _ticketNumber; // Add ticketNumber field private UserRole _role; private DateOnly _validForDate; - private string? _password; // Add password field + private string? _password; /// /// Sets the username that will be set, when the Builder.Build()-method is called. @@ -83,18 +79,6 @@ public Builder WithUsername(string username) _username = username; return this; } - - /// - /// Sets the ticket number for the guest user. - /// - /// The ticket number of the guest user. - /// This instance of the Builder-pattern. - public Builder WithTicketNumber(int ticketNumber) - { - _ticketNumber = ticketNumber; - return this; - } - /// /// Sets the password that will be set, when the Builder.Build()-method is called. Before setting the password, /// the given value is hashed, using BCrypt's HashPassword-method. If the role of the user is @@ -104,7 +88,7 @@ public Builder WithTicketNumber(int ticketNumber) /// This instance of the Builder. public Builder WithPassword(string password) { - _password = HashPassword(password); // Change to BCrypt.HashPassword + _password = EnhancedHashPassword(password); return this; } @@ -140,7 +124,7 @@ public AbstractUser Build() { if (_role == UserRole.Guest) { - return new Guest(_username!, _validForDate, _ticketNumber); // Include ticketNumber + return new Guest(_username!, _validForDate, _username!); } return new Employee(_username!, _role, _password!); diff --git a/ProjectB/Models/Guest.cs b/ProjectB/Models/Guest.cs index e82af01..e0e8c24 100644 --- a/ProjectB/Models/Guest.cs +++ b/ProjectB/Models/Guest.cs @@ -6,13 +6,13 @@ public class Guest : AbstractUser { [JsonProperty] private readonly DateOnly _validForDate; - - public int TicketNumber { get; set; } - public Guest(string username, DateOnly validForDate, int ticketNumber) : base(username, UserRole.Guest) + public new string GetId() => Username; + + public Guest(string username, DateOnly validForDate, string usernameParam) : base(username, UserRole.Guest) { _validForDate = validForDate; - TicketNumber = ticketNumber; + this.Username = username; } public bool IsValid() => _validForDate.CompareTo(DateTime.Today) == 0; @@ -22,7 +22,7 @@ public override bool Equals(AbstractUser? other) if (ReferenceEquals(other, this)) return true; if (ReferenceEquals(other, null)) return false; if (other.GetType() != GetType()) return false; - return Username == other.GetId(); + return GetId() == other.GetId(); } } } diff --git a/ProjectB/Tour.cs b/ProjectB/Models/Tour.cs similarity index 100% rename from ProjectB/Tour.cs rename to ProjectB/Models/Tour.cs diff --git a/ProjectB/Reservation.cs b/ProjectB/Reservation.cs index af19853..163a925 100644 --- a/ProjectB/Reservation.cs +++ b/ProjectB/Reservation.cs @@ -3,11 +3,12 @@ using System.IO; using System.Linq; using Newtonsoft.Json; +using ProjectB.IO; using ProjectB.Models; -class Reservation +public class Reservation { - static List tours = new List(); // List to store available tours + public static List tours = new List(); // List to store available tours static string jsonFilePath = "signups.json"; static void Main(string[] args) @@ -64,7 +65,7 @@ static void CurrentDomain_ProcessExit(object? sender, EventArgs e) SaveParticipantsToJson(); // Save sign-up data on application exit } - static void SignUpForTour() + public static void SignUpForTour() { Console.WriteLine("Available Tours:"); for (int i = 0; i < tours.Count; i++) @@ -94,23 +95,18 @@ static void SignUpForTour() return; } - Console.Write("Enter your ticket number: "); - int ticketNumber; - while (!int.TryParse(Console.ReadLine(), out ticketNumber) || ticketNumber <= 0) - { - Console.WriteLine("Invalid input. Ticket number must be a positive integer."); - Console.Write("Enter your ticket number: "); - } + Console.Write("Enter your username: "); // Changed prompt from "Enter your ticket number" to "Enter your username" + string? username = Console.ReadLine(); // Changed variable name from ticketNumber to username - if (selectedTour.Participants.Any(p => p.TicketNumber == ticketNumber)) + if (selectedTour.Participants.Any(p => p.GetId() == username)) { - Console.WriteLine($"Ticket number {ticketNumber} is already signed up for a tour."); + Console.WriteLine($"Username {username} is already signed up for a tour."); Console.Write("Do you want to change your sign-up (Y/N)? "); string? response = Console.ReadLine()!.Trim().ToUpper(); if (response == "Y") { - var guestToRemove = selectedTour.Participants.FirstOrDefault(p => p.TicketNumber == ticketNumber); + var guestToRemove = selectedTour.Participants.FirstOrDefault(p => p.GetId() == username); if (guestToRemove != null) { selectedTour.Participants.Remove(guestToRemove); // Remove existing sign-up @@ -122,8 +118,7 @@ static void SignUpForTour() } } - // Modify this line to create a Guest object instead of a Participant - Guest guest = new Guest("username", DateOnly.FromDateTime(DateTime.Today), ticketNumber); + Guest guest = new Guest(username!, DateOnly.FromDateTime(DateTime.Today), username!); selectedTour.Participants.Add(guest); // Add new sign-up SaveParticipantsToJson(); // Save immediately after sign-up @@ -131,39 +126,35 @@ static void SignUpForTour() Console.WriteLine($"You have successfully signed up for the tour at {selectedTour.Time.ToString("HH:mm")}."); } - static void DeleteSignUpForTour() -{ - Console.Write("Enter your ticket number to delete your sign-up: "); - int ticketNumberToDelete; - while (!int.TryParse(Console.ReadLine(), out ticketNumberToDelete) || ticketNumberToDelete <= 0) + public static void DeleteSignUpForTour() { - Console.WriteLine("Invalid input. Ticket number must be a positive integer."); - } + Console.Write("Enter your username to delete your sign-up: "); // Changed prompt from "Enter your ticket number to delete your sign-up" to "Enter your username to delete your sign-up" + string? usernameToDelete = Console.ReadLine(); - foreach (var tour in tours) - { - var guestToRemove = tour.Participants.FirstOrDefault(p => p.TicketNumber == ticketNumberToDelete); - if (guestToRemove != null) + foreach (var tour in tours) { - tour.Participants.Remove(guestToRemove); // Remove sign-up + var guestToRemove = tour.Participants.FirstOrDefault(p => p.GetId() == usernameToDelete); + if (guestToRemove != null) + { + tour.Participants.Remove(guestToRemove); // Remove sign-up - SaveParticipantsToJson(); // Save immediately after deletion + SaveParticipantsToJson(); // Save immediately after deletion - Console.WriteLine($"Your sign-up for the tour at {tour.Time.ToString("HH:mm")} has been deleted."); - return; + Console.WriteLine($"Your sign-up for the tour at {tour.Time.ToString("HH:mm")} has been deleted."); + return; + } } - } - Console.WriteLine($"No sign-up found for ticket number {ticketNumberToDelete}."); -} + Console.WriteLine($"No sign-up found for username {usernameToDelete}."); + } - static void LoadParticipantsFromJson() + static void LoadParticipantsFromJson() { if (File.Exists(jsonFilePath)) { string json = File.ReadAllText(jsonFilePath); - var dictionary = JsonConvert.DeserializeObject>>(json); // Use JsonConvert.DeserializeObject from Newtonsoft.Json + var dictionary = JsonConvert.DeserializeObject>>(json); foreach (var kvp in dictionary!) { @@ -171,10 +162,9 @@ static void LoadParticipantsFromJson() Tour? tour = tours.FirstOrDefault(t => t.Time == tourTime); if (tour != null) { - foreach (var ticketNumber in kvp.Value) + foreach (var username in kvp.Value) { - // Modify this line to create a Guest object instead of a Participant - Guest guest = new Guest("username", DateOnly.FromDateTime(DateTime.Today), ticketNumber); + Guest guest = new Guest(username, DateOnly.FromDateTime(DateTime.Today), username); tour.Participants.Add(guest); // Add guest to existing tour } } @@ -183,15 +173,8 @@ static void LoadParticipantsFromJson() } static void SaveParticipantsToJson() - { - var dataToSerialize = new Dictionary>(); - - foreach (var tour in tours) - { - dataToSerialize.Add(tour.Time.ToString(), tour.Participants.Select(p => p.TicketNumber).ToList()); - } + { var filewriter = new JsonFileWriter(); - string json = JsonConvert.SerializeObject(dataToSerialize); // Use JsonConvert.SerializeObject from Newtonsoft.Json - File.WriteAllText(jsonFilePath, json); // Write sign-up data to JSON file + filewriter.WriteObjects(jsonFilePath, tours); } } diff --git a/ProjectBTest/UnitTestReservation.cs b/ProjectBTest/UnitTestReservation.cs new file mode 100644 index 0000000..24f5304 --- /dev/null +++ b/ProjectBTest/UnitTestReservation.cs @@ -0,0 +1,54 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using ProjectB.Models; +using ProjectB; + +namespace ProjectB.Tests +{ + [TestClass] + public class ReservationTests + { + [TestMethod] + public void SignUpForTour_UserAlreadySignedUp_UserNotAddedToTour() + { + // Test scenario: Attempting to sign up a user who is already signed up for a tour + // Arrange: Create a mock tour with a participant + var mockTour = new Tour(DateTime.Today, "Test Location", 10) + { + Participants = new List + { + new Guest("testUser", DateOnly.FromDateTime(DateTime.Today), "testUser") + } + }; + Reservation.tours = new List { mockTour }; + var input = new StringReader("1\n"); + + // Act: Try to sign up the same user again + Console.SetIn(input); + Reservation.SignUpForTour(); + } + + [TestMethod] + public void DeleteSignUpForTour_UserSignedUp_UserRemovedFromTour() + { + // Test scenario: Deleting the sign-up of a user from a tour + // Arrange: Create a mock tour with a participant + var mockTour = new Tour(DateTime.Today, "Test Location", 10) + { + Participants = new List + { + new Guest("testUser", DateOnly.FromDateTime(DateTime.Today), "testUser") + } + }; + Reservation.tours = new List { mockTour }; + var input = new StringReader("testUser\n"); + + // Act: Delete the sign-up of the user + Console.SetIn(input); + Reservation.DeleteSignUpForTour(); + } + } +} diff --git a/ProjectBTest/UnitTestTour.cs b/ProjectBTest/UnitTestTour.cs new file mode 100644 index 0000000..fc4a2a8 --- /dev/null +++ b/ProjectBTest/UnitTestTour.cs @@ -0,0 +1,54 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using ProjectB.Models; +using System; + +namespace ProjectB.Tests +{ + [TestClass] + public class TourTests + { + // Test to ensure that the Tour constructor initializes properties correctly + [TestMethod] + public void Tour_Constructor_InitializesPropertiesCorrectly() + { + DateTime time = new DateTime(2024, 4, 5, 10, 0, 0); + string location = "Test Location"; + int capacity = 20; + + Tour tour = new Tour(time, location, capacity); + + Assert.AreEqual(time, tour.Time); + Assert.AreEqual(location, tour.Location); + Assert.AreEqual(capacity, tour.Capacity); + Assert.IsNotNull(tour.Participants); + Assert.AreEqual(0, tour.Participants.Count); + } + + // Test to ensure that a participant can be added to the participants list + [TestMethod] + public void Tour_AddParticipant_ParticipantAddedToParticipantsList() + { + Tour tour = new Tour(DateTime.Now, "Test Location", 10); + Guest guest = new Guest("testUser", DateOnly.FromDateTime(DateTime.Today), "testUser"); + + tour.Participants.Add(guest); + + Assert.AreEqual(1, tour.Participants.Count); + Assert.IsTrue(tour.Participants.Contains(guest)); + } + + // Test to ensure that a participant can be removed from the participants list + [TestMethod] + public void Tour_RemoveParticipant_ParticipantRemovedFromParticipantsList() + { + Tour tour = new Tour(DateTime.Now, "Test Location", 10); + Guest guest = new Guest("testUser", DateOnly.FromDateTime(DateTime.Today), "testUser"); + tour.Participants.Add(guest); + + tour.Participants.Remove(guest); + + Assert.AreEqual(0, tour.Participants.Count); + Assert.IsFalse(tour.Participants.Contains(guest)); + } + } +} From deecd871b7865f6bd20c2149961b05091cb1cc4b Mon Sep 17 00:00:00 2001 From: KaiHog Date: Fri, 5 Apr 2024 19:48:49 +0200 Subject: [PATCH 21/22] changed GuestFixtures --- ProjectBTest/Fixtures/GuestFixtures.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/ProjectBTest/Fixtures/GuestFixtures.cs b/ProjectBTest/Fixtures/GuestFixtures.cs index 528f6fc..c6eda54 100644 --- a/ProjectBTest/Fixtures/GuestFixtures.cs +++ b/ProjectBTest/Fixtures/GuestFixtures.cs @@ -1,6 +1,7 @@ -using ProjectB.Models; +using Newtonsoft.Json; using System; using System.Collections.Generic; +using ProjectB.Models; namespace ProjectBTest.Fixtures { @@ -11,10 +12,8 @@ public static ICollection GenerateCollection(int amount) ICollection userList = new List(); for (int i = 0; i < amount; i++) { - // Generate a unique ticket number for each guest int ticketNumber = i + 1; - - userList.Add(new Guest(Guid.NewGuid().ToString(), DateOnly.FromDateTime(DateTime.Today), ticketNumber)); + userList.Add(new Guest(Guid.NewGuid().ToString(), DateOnly.FromDateTime(DateTime.Today), ticketNumber.ToString())); } return userList; From d8a6ff8d6223c3edbe865e7645c559d19a770068 Mon Sep 17 00:00:00 2001 From: KaiHog Date: Sat, 6 Apr 2024 14:12:01 +0200 Subject: [PATCH 22/22] LoadParticipants ready --- ProjectB/Reservation.cs | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/ProjectB/Reservation.cs b/ProjectB/Reservation.cs index 163a925..60ea363 100644 --- a/ProjectB/Reservation.cs +++ b/ProjectB/Reservation.cs @@ -95,8 +95,8 @@ public static void SignUpForTour() return; } - Console.Write("Enter your username: "); // Changed prompt from "Enter your ticket number" to "Enter your username" - string? username = Console.ReadLine(); // Changed variable name from ticketNumber to username + Console.Write("Enter your username: "); + string? username = Console.ReadLine(); if (selectedTour.Participants.Any(p => p.GetId() == username)) { @@ -128,7 +128,7 @@ public static void SignUpForTour() public static void DeleteSignUpForTour() { - Console.Write("Enter your username to delete your sign-up: "); // Changed prompt from "Enter your ticket number to delete your sign-up" to "Enter your username to delete your sign-up" + Console.Write("Enter your username to delete your sign-up: "); string? usernameToDelete = Console.ReadLine(); foreach (var tour in tours) @@ -154,23 +154,11 @@ static void LoadParticipantsFromJson() if (File.Exists(jsonFilePath)) { string json = File.ReadAllText(jsonFilePath); - var dictionary = JsonConvert.DeserializeObject>>(json); - - foreach (var kvp in dictionary!) - { - DateTime tourTime = DateTime.Parse(kvp.Key); - Tour? tour = tours.FirstOrDefault(t => t.Time == tourTime); - if (tour != null) - { - foreach (var username in kvp.Value) - { - Guest guest = new Guest(username, DateOnly.FromDateTime(DateTime.Today), username); - tour.Participants.Add(guest); // Add guest to existing tour - } - } + tours = JsonConvert.DeserializeObject>(json)!; + } } - } + static void SaveParticipantsToJson() { var filewriter = new JsonFileWriter();