我最近完成了一个我一直在做的小项目,除了我想弄清楚的一个小问题。
该项目使用SOAP客户端以及请求和响应类来使用C#Web服务。该项目旨在保存输入的数据。它的作用是保留一个在电影院中排成一排的座位,当我预订该座位并尝试再次选择该座位以找到该行中的另一个可用座位(在表格内)时,它们都显示为“0” ”。但是,当我在另一行中预订另一个席位时,它会成功地将该数据保存在缓存中,但它不会在代码的“else if”语句中显示任何其他席位和行。
以下是Web服务的Webmethod
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;
这是我从Webservice调用的方法中坚持的部分
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));
}
}
}
答案 0 :(得分:2)
我发现您的代码存在一些问题。
<强>了Serverside:强>
reservedRows
从未使用过。它的目的是什么?您可以定义常量,即maxRows
,但不要使用它们。写
private bool[,]reservedSeats = new bool[maxRows, maxSeats]
在两张支票中(座位是否拍摄)你都使用相同的条件 - 它不会起作用。只需写下:
if (!reservedSeats[req.RowNumber, req.SeatNumber]) {
// Seat is free
} else {
// Seat is taken
}
如果你想区分返回的座位是否可以替代,你应该在你的回复中包含一个布尔标志,或者更好的是这样的枚举:
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;
}
答案 1 :(得分:0)
我在你的代码中发现了2个错误! 首先,在以下情况之后,我无法看到二维数组的初始化:
`if (abc == null)
{
//creates a blank multidimensional array
reservedSeats = new bool[maxRows,maxSeats];
}`
其次,你的if else条件(reservedSeats [req.RowNumber,req.SeatNumber]!= true)完全一样!
希望这有帮助