我最近完成了一个我一直在做的小项目,除了有一个小问题我正在努力解决。
该项目使用 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));
}
}
}