使用C sharp编写一个自习室座位预订系统
时间: 2023-09-28 08:09:49 浏览: 80
好的,下面是一个简单的自习室座位预订系统的 C# 代码示例:
```csharp
using System;
using System.Collections.Generic;
namespace SeatReservationSystem
{
class SeatReservation
{
static void Main(string[] args)
{
// 初始化座位列表
List<Seat> seats = new List<Seat>();
for (int i = 1; i <= 30; i++)
{
Seat seat = new Seat(i);
seats.Add(seat);
}
while (true)
{
Console.WriteLine("请选择操作:1. 预订座位 2. 取消预订 3. 显示座位状态 4. 退出");
int choice = Convert.ToInt32(Console.ReadLine());
switch (choice)
{
case 1:
Console.WriteLine("请输入要预订的座位号:");
int seatNumber = Convert.ToInt32(Console.ReadLine());
if (seatNumber < 1 || seatNumber > 30)
{
Console.WriteLine("座位号无效,请重新输入!");
break;
}
Seat seat = seats[seatNumber - 1];
if (seat.IsReserved)
{
Console.WriteLine("该座位已被预订,请选择其他座位!");
break;
}
Console.WriteLine("请输入预订人姓名:");
string name = Console.ReadLine();
seat.Reserve(name);
Console.WriteLine("预订成功!");
break;
case 2:
Console.WriteLine("请输入要取消预订的座位号:");
seatNumber = Convert.ToInt32(Console.ReadLine());
if (seatNumber < 1 || seatNumber > 30)
{
Console.WriteLine("座位号无效,请重新输入!");
break;
}
seat = seats[seatNumber - 1];
if (!seat.IsReserved)
{
Console.WriteLine("该座位未被预订,无需取消预订!");
break;
}
seat.CancelReservation();
Console.WriteLine("取消预订成功!");
break;
case 3:
Console.WriteLine("当前座位状态如下:");
foreach (Seat s in seats)
{
Console.WriteLine("座位号:" + s.SeatNumber + ",状态:" + (s.IsReserved ? "已预订,预订人姓名:" + s.ReservedName : "未预订"));
}
break;
case 4:
Console.WriteLine("程序已退出!");
return;
default:
Console.WriteLine("无效操作,请重新选择!");
break;
}
}
}
}
class Seat
{
public int SeatNumber { get; private set; }
public bool IsReserved { get; private set; }
public string ReservedName { get; private set; }
public Seat(int seatNumber)
{
SeatNumber = seatNumber;
IsReserved = false;
ReservedName = "";
}
public void Reserve(string name)
{
IsReserved = true;
ReservedName = name;
}
public void CancelReservation()
{
IsReserved = false;
ReservedName = "";
}
}
}
```
该系统使用了一个 `Seat` 类来表示座位,包含座位号、预订状态和预订人姓名等属性,以及 `Reserve` 和 `CancelReservation` 方法来预订和取消预订座位。主程序中使用一个 `List<Seat>` 来管理所有座位,通过不断循环读取用户输入的操作选择,来实现预订、取消预订和显示座位状态等功能。
该代码只是一个简单的示例,实际项目中需要根据需求进行更加详细的设计和开发。
阅读全文