Skip to content

Commit

Permalink
Update Reservation.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
KaiHog committed Apr 3, 2024
1 parent db28b10 commit ab4e40c
Showing 1 changed file with 24 additions and 46 deletions.
70 changes: 24 additions & 46 deletions ProjectB/Reservation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
class Reservation
{
static List<Tour> tours = new List<Tour>(); // List to store available tours
static Dictionary<DateTime, List<Participant>> signupsByTourTime = new Dictionary<DateTime, List<Participant>>(); // 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)
Expand All @@ -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<Participant>()); // 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
Expand Down Expand Up @@ -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");
Expand All @@ -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;
Expand All @@ -105,27 +101,25 @@ 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.");

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); // 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
{
return; // Go back to main menu
}
}

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

Expand All @@ -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;
}
}
Expand All @@ -168,29 +157,18 @@ static void LoadParticipantsFromJson()
if (File.Exists(jsonFilePath))
{
string json = File.ReadAllText(jsonFilePath);
var dictionary = JsonSerializer.Deserialize<Dictionary<DateTime, List<Participant>>>(json);
var dictionary = JsonSerializer.Deserialize<Dictionary<string, List<int>>>(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> { 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
}
}
}
Expand All @@ -199,11 +177,11 @@ static void LoadParticipantsFromJson()

static void SaveParticipantsToJson()
{
var dataToSerialize = new Dictionary<DateTime, List<Participant>>();
var dataToSerialize = new Dictionary<string, List<int>>();

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);
Expand Down

0 comments on commit ab4e40c

Please sign in to comment.