-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from marcus-talbot42/Reservation
Reservation
- Loading branch information
Showing
10 changed files
with
377 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 new string GetId() => Username; | ||
|
||
public Guest(string username, DateOnly validForDate, string usernameParam) : base(username, UserRole.Guest) | ||
{ | ||
_validForDate = validForDate; | ||
this.Username = username; | ||
} | ||
|
||
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 GetId() == other.GetId(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace ProjectB.Models | ||
{ | ||
public 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<Guest> 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<Guest>(); // Initialize participant collection | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using Newtonsoft.Json; | ||
using ProjectB.IO; | ||
using ProjectB.Models; | ||
|
||
public class Reservation | ||
{ | ||
public static List<Tour> tours = new List<Tour>(); // List to store available tours | ||
static string jsonFilePath = "signups.json"; | ||
|
||
static void Main(string[] args) | ||
{ | ||
LoadParticipantsFromJson(); // Load sign-up data from JSON file, if exists | ||
|
||
// 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); | ||
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 | ||
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(); // Save sign-up data on application exit | ||
} | ||
|
||
public static void SignUpForTour() | ||
{ | ||
Console.WriteLine("Available Tours:"); | ||
for (int i = 0; i < tours.Count; i++) | ||
{ | ||
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"); | ||
|
||
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; // Go back to main menu | ||
} | ||
|
||
Tour selectedTour = tours[tourNumber - 1]; | ||
|
||
if (selectedTour.Participants.Count >= selectedTour.Capacity) | ||
{ | ||
Console.WriteLine($"Sorry, the tour at {selectedTour.Time.ToString("HH:mm")} is already fully booked."); | ||
return; | ||
} | ||
|
||
Console.Write("Enter your username: "); | ||
string? username = Console.ReadLine(); | ||
|
||
if (selectedTour.Participants.Any(p => p.GetId() == username)) | ||
{ | ||
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.GetId() == username); | ||
if (guestToRemove != null) | ||
{ | ||
selectedTour.Participants.Remove(guestToRemove); // Remove existing sign-up | ||
} | ||
} | ||
else | ||
{ | ||
return; // Go back to main menu | ||
} | ||
} | ||
|
||
Guest guest = new Guest(username!, DateOnly.FromDateTime(DateTime.Today), username!); | ||
selectedTour.Participants.Add(guest); // Add 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")}."); | ||
} | ||
|
||
public static void DeleteSignUpForTour() | ||
{ | ||
Console.Write("Enter your username to delete your sign-up: "); | ||
string? usernameToDelete = Console.ReadLine(); | ||
|
||
foreach (var tour in tours) | ||
{ | ||
var guestToRemove = tour.Participants.FirstOrDefault(p => p.GetId() == usernameToDelete); | ||
if (guestToRemove != null) | ||
{ | ||
tour.Participants.Remove(guestToRemove); // Remove sign-up | ||
|
||
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($"No sign-up found for username {usernameToDelete}."); | ||
} | ||
|
||
|
||
static void LoadParticipantsFromJson() | ||
{ | ||
if (File.Exists(jsonFilePath)) | ||
{ | ||
string json = File.ReadAllText(jsonFilePath); | ||
tours = JsonConvert.DeserializeObject<List<Tour>>(json)!; | ||
|
||
} | ||
} | ||
|
||
|
||
static void SaveParticipantsToJson() | ||
{ var filewriter = new JsonFileWriter<Tour>(); | ||
|
||
filewriter.WriteObjects(jsonFilePath, tours); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,52 @@ | ||
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<Tour> _jsonFileReader; // Specify Tour as the type argument | ||
|
||
public RondleidingService(JsonFileReader jsonFileReader) | ||
public RondleidingService(JsonFileReader<Tour> jsonFileReader) // Adjust the constructor parameter | ||
{ | ||
_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<Tour> | ||
var tourData = _jsonFileReader.ReadJsonFile(jsonFilePath); | ||
|
||
// Display the data in the console | ||
Console.WriteLine(jsonData); | ||
Console.WriteLine(tourData); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"An error occurred: {ex.Message}"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public class JsonFileReader<T> | ||
{ | ||
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<T>(jsonData)!; | ||
return deserializedData!; | ||
} | ||
} | ||
} |
Oops, something went wrong.