2

我最近完成了一个我一直在做的小项目,除了有一个小问题我正在努力解决。

该项目使用 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));
                    }
                }


            }
4

2 回答 2

2

我发现您的代码存在一些问题。

服务器端:

  • 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;
    }
    
于 2012-11-20T08:52:30.817 回答
0

我在你的代码中发现了 2 个错误!首先,我看不到你的二维数组的初始化:

`if (abc == null)
    {
        //creates a blank multidimensional array
        reservedSeats = new bool[maxRows,maxSeats];

    }`

其次,您的 if else 条件(reservedSeats[req.RowNumber, req.SeatNumber] != true)完全相同!

希望这可以帮助

于 2012-11-20T08:27:52.620 回答