Пробував зробити це через GPT але не допомогло(була помилка "4 Не запущен"), можливо через те, що там старі інструкції(не знаю). Потрібно написати 3 NUnit, або Xunit тести до цієї проги:
using System;
using System.Collections.Generic;
using System.IO;
// Main Application Class
public class TableReservationApp
{
static void Main(string[] args)
{
ReservationManagerClass m = new ReservationManagerClass();
m.AddRestaurantMethod("A", 10);
m.AddRestaurantMethod("B", 5);
Console.WriteLine(m.BookTable("A", new DateTime(2023, 12, 25), 3)); // True
Console.WriteLine(m.BookTable("A", new DateTime(2023, 12, 25), 3)); // False
}
}
// Reservation Manager Class
public class ReservationManagerClass
{
// res
public List<RestaurantClass> Restaurants { get; private set; }
public ReservationManagerClass()
{
Restaurants = new List<RestaurantClass>();
}
// Add Restaurant Method
public void AddRestaurantMethod(string name, int tableCount)
{
try
{
RestaurantClass restaurant = new RestaurantClass();
restaurant.Name = name;
restaurant.Tables = new RestaurantTableClass[tableCount];
for (int i = 0; i < tableCount; i++)
{
restaurant.Tables[i] = new RestaurantTableClass();
}
Restaurants.Add(restaurant);
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
// Load Restaurants From
// File
private void LoadRestaurantsFromFileMethod(string filePath)
{
try
{
string[] lines = File.ReadAllLines(filePath);
foreach (string line in lines)
{
var parts = line.Split(',');
if (parts.Length == 2 && int.TryParse(parts[1], out int tableCount))
{
AddRestaurantMethod(parts[0], tableCount);
}
else
{
Console.WriteLine($"Invalid line: {line}");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
//Find All Free Tables
public List<string> FindAllFreeTables(DateTime date)
{
try
{
List<string> freeTables = new List<string>();
foreach (var restaurant in Restaurants)
{
for (int i = 0; i < restaurant.Tables.Length; i++)
{
if (!restaurant.Tables[i].IsBooked(date))
{
freeTables.Add($"{restaurant.Name} - Table {i + 1}");
}
}
}
return freeTables;
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
return new List<string>();
}
}
public bool BookTable(string restaurantName, DateTime date, int tableNumber)
{
foreach (var restaurant in Restaurants)
{
if (restaurant.Name == restaurantName)
{
if (tableNumber < 0 || tableNumber >= restaurant.Tables.Length)
{
throw new Exception("Invalid table number");//Invalid table number
}
return restaurant.Tables[tableNumber].Book(date);
}
}
throw new Exception("Restaurant not found");//Restaurant not found
}
public void SortRestaurantsByAvailabilityForUsersMethod(DateTime date)
{
try
{
bool swapped;
do
{
swapped = false;
for (int i = 0; i < Restaurants.Count - 1; i++)
{
int avTc = CountAvailableTablesForRestaurantClassAndDateTimeMethod(Restaurants[i], date);
int avTn = CountAvailableTablesForRestaurantClassAndDateTimeMethod(Restaurants[i + 1], date);
if (avTc < avTn)
{
// Swap restaurants
var temp = Restaurants[i];
Restaurants[i] = Restaurants[i + 1];
Restaurants[i + 1] = temp;
swapped = true;
}
}
} while (swapped);
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
// count available tables in a restaurant
public int CountAvailableTablesForRestaurantClassAndDateTimeMethod(RestaurantClass restaurant, DateTime date)
{
try
{
int count = 0;
foreach (var table in restaurant.Tables)
{
if (!table.IsBooked(date))
{
count++;
}
}
return count;
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
return 0;
}
}
}
// Restaurant Class
public class RestaurantClass
{
public string Name { get; set; }//name
public RestaurantTableClass[] Tables { get; set; }// tables
}
// // Table Class
public class RestaurantTableClass
{
private List<DateTime> bookedDates;
public RestaurantTableClass()
{
bookedDates = new List<DateTime>();
}
// book
public bool Book(DateTime date)
{
try
{
if (bookedDates.Contains(date))
{
return false;
}
//add to bd
bookedDates.Add(date);
return true;
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
return false;
}
}
// is booked
public bool IsBooked(DateTime date)
{
return bookedDates.Contains(date);
}
}
0 комментариев
Добавить комментарий