我最近完成了一个我一直在做的小项目,除了我正在尝试解决一个小问题。
该项目利用 C# Web 服务,使用 SOAP 客户端以及请求类和响应类。该项目旨在保存输入的数据。它的作用是在电影院中保留一排座位,当我预订该座位并尝试再次选择该座位以查找该排中的另一个可用座位(在表单内)时,它将它们都显示为“0” ”。但是,当我预订不同行中的另一个座位时,它成功将该数据保存在缓存中,但不会在代码的“else if”语句中显示任何其他座位和行。
这是 Web 服务的 Web 方法
public string Name { get; set; } //gets and sets the Name from the ReserveSeatResponse
public int Row { get; set; } //gets and sets the Row from the ReserveSeatResponse
public int Seat { get; set; } //gets and sets the Seat from the ReserveSeatResponse
private const int maxRows = 13; //sets the max amount of Rows in the Array tried setting the rows to 12, but since the array starts at 0, I had to set the Rows to 13)
private const int maxSeats = 17; //sets the max amount of Seats in the Array (tried setting the seats at 16, but since the array starts at 0, I had to set it to 17)
private bool[,]reservedSeats = new bool[13, 17]; //same description above, but this sets a boolean in the reserved seats, to check if its taken or no)
private bool[,]reservedRows = new bool[13, 17]; //same description for the max row/seats.
[WebMethod]
public GetSeatResponse Booking (GetSeatRequest req)
{
GetSeatResponse resp = new GetSeatResponse();
// resp.Seat = req.SeatNumber;
// resp.Row = req.RowNumber;
object abc = HttpContext.Current.Cache["CinemaReservation"];
//if the cache does not exist
if (abc == null)
{
//creates a blank multidimensional array
reservedSeats = new bool[maxRows,maxSeats];
}
//if the cache exists
else
{
//using the cache object as an array
try
{
reservedSeats = (bool[,])abc;
}
catch
{
}
}
//if the seat is vacant
if (reservedSeats[req.RowNumber, req.SeatNumber] != true)
{
resp.Name = req.Name;
resp.Seat = req.SeatNumber;
resp.Row = req.RowNumber;
reservedSeats[req.RowNumber, req.SeatNumber] = true;
HttpContext.Current.Cache["CinemaReservation"] = reservedSeats;
return resp;
}
//if the seat is taken
else if (reservedSeats[req.RowNumber, req.SeatNumber] != true)
{
bool breakTest = false;
string Name = req.Name;
int row = req.RowNumber;
int seat = req.SeatNumber;
for (int i = row; i < reservedSeats.GetLength(0); i++)
{
for (int j = seat; j < reservedSeats.GetLength(1); j++)
{
if (reservedSeats[i, j] !=true )
{
resp.Name = Name;
resp.Row = i+1;
resp.Seat = j+1;
reservedSeats[i, j] = true;
breakTest = true;
break;
}
}
seat = 0;
if (breakTest == true)
{
break;
}
}
//getNextSeat(req, resp, reservedSeats);
HttpContext.Current.Cache["CinemaReservation"] = reservedSeats;
}
return resp;
这是我在从 Web 服务调用的方法中陷入困境的部分
if (resp.erroresp != true)
{
if ((resp.Row > 0) && (resp.Seat > 0))
{
MessageBox.Show(String.Format("Hi there {0}!!, you have reserved a Seat at Row: {1} in Seat Number : {2}", resp.Name, resp.Row, resp.Seat));
}
else if (resp.erroresp != true)
{
if ((resp.Row < 1) && (resp.Seat < 1))
{
MessageBox.Show(String.Format("Sorry, your selected Seat has been taken, however there is an available seat at row {0} in seat {1}", resp.Row, resp.Seat));
}
}
}
最佳答案
我发现您的代码存在一些问题。
服务器端:
reservedRows
从未使用过。它的目的是什么?您定义了常量,即
maxRows
但您不使用它们。写private bool[,]reservedSeats = new bool[maxRows, maxSeats]
在两次检查(是否有座位)中,您使用相同的条件 - 这是行不通的。只需写:
if (!reservedSeats[req.RowNumber, req.SeatNumber]) { // Seat is free } else { // Seat is taken }
如果您想区分返回的座位是否是替代座位,您应该在您的响应中包含一个 bool 标志,或者更好的是像这样的枚举:
public enum ResultEnum { Ok, // the requested seat is free Full, // no seat is available Alternative // you got another seat }
客户端:
您的检查有些多余(您检查了两次
resp.erroresp != true
)并且根本不清楚。当使用枚举方法时,您可以像这样检查:
switch (resp.Result) { case ResultEnum.Ok: break; case ResultEnum.Full: break; case ResultEnum.Alternative: break; }
关于c# - 电影院座位排c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13469039/