体育场馆系统场地预约缴费c#代码
时间: 2023-11-18 09:05:56 浏览: 183
以下是一个简单的场地预约和缴费的 C# 代码示例:
```csharp
using System;
using System.Collections.Generic;
namespace SportsBookingSystem
{
// 场馆类别枚举
enum VenueType
{
BasketballCourt,
BadmintonCourt,
SwimmingPool,
Gym
}
// 预约状态枚举
enum BookingStatus
{
Available,
Booked,
Paid
}
// 场地类
class Venue
{
public VenueType Type { get; set; }
public int Capacity { get; set; }
public decimal Price { get; set; }
public List<Booking> Bookings { get; set; }
public Venue(VenueType type, int capacity, decimal price)
{
Type = type;
Capacity = capacity;
Price = price;
Bookings = new List<Booking>();
}
// 预约场地
public Booking Book(DateTime startTime, DateTime endTime, int numOfPeople)
{
if (numOfPeople > Capacity) // 容量不足
{
return null;
}
foreach (Booking booking in Bookings)
{
if (startTime >= booking.StartTime && startTime < booking.EndTime ||
endTime > booking.StartTime && endTime <= booking.EndTime) // 时间冲突
{
return null;
}
}
Booking newBooking = new Booking(startTime, endTime, numOfPeople);
Bookings.Add(newBooking);
return newBooking;
}
}
// 预约类
class Booking
{
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
public int NumOfPeople { get; set; }
public BookingStatus Status { get; set; }
public Booking(DateTime startTime, DateTime endTime, int numOfPeople)
{
StartTime = startTime;
EndTime = endTime;
NumOfPeople = numOfPeople;
Status = BookingStatus.Booked;
}
// 缴费
public void Pay()
{
Status = BookingStatus.Paid;
}
}
// 场馆管理类
class VenueManager
{
private List<Venue> venues;
public VenueManager()
{
venues = new List<Venue>();
venues.Add(new Venue(VenueType.BasketballCourt, 10, 50));
venues.Add(new Venue(VenueType.BadmintonCourt, 8, 30));
venues.Add(new Venue(VenueType.SwimmingPool, 50, 100));
venues.Add(new Venue(VenueType.Gym, 20, 80));
}
// 预约场地
public Booking BookVenue(VenueType type, DateTime startTime, DateTime endTime, int numOfPeople)
{
foreach (Venue venue in venues)
{
if (venue.Type == type)
{
return venue.Book(startTime, endTime, numOfPeople);
}
}
return null;
}
// 缴费
public void PayBooking(Booking booking)
{
booking.Pay();
}
}
// 测试程序
class Program
{
static void Main(string[] args)
{
VenueManager venueManager = new VenueManager();
// 预约篮球场
Booking basketballBooking = venueManager.BookVenue(VenueType.BasketballCourt, new DateTime(2021, 6, 1, 10, 0, 0), new DateTime(2021, 6, 1, 12, 0, 0), 8);
if (basketballBooking != null)
{
Console.WriteLine("预约成功!场地类型:" + basketballBooking.Type + ",开始时间:" + basketballBooking.StartTime + ",结束时间:" + basketballBooking.EndTime + ",人数:" + basketballBooking.NumOfPeople);
venueManager.PayBooking(basketballBooking);
Console.WriteLine("缴费成功!");
}
else
{
Console.WriteLine("预约失败!");
}
// 预约游泳池
Booking swimmingBooking = venueManager.BookVenue(VenueType.SwimmingPool, new DateTime(2021, 6, 2, 14, 0, 0), new DateTime(2021, 6, 2, 16, 0, 0), 30);
if (swimmingBooking != null)
{
Console.WriteLine("预约成功!场地类型:" + swimmingBooking.Type + ",开始时间:" + swimmingBooking.StartTime + ",结束时间:" + swimmingBooking.EndTime + ",人数:" + swimmingBooking.NumOfPeople);
venueManager.PayBooking(swimmingBooking);
Console.WriteLine("缴费成功!");
}
else
{
Console.WriteLine("预约失败!");
}
Console.ReadLine();
}
}
}
```
代码中使用了类、枚举和列表等 C# 的基本语法,实现了场地预约和缴费的功能。需要注意的是,代码中的时间、人数和价格等参数都是根据实际情况进行设置的,具体实现时需要根据需求进行调整。
阅读全文