2014-06-26 23 views
-2

我正在用C#開發ASP.NET應用程序,我試圖找出實現邏輯語句的最佳方法,該語句將阻止系統允許另一個預留如果獨木舟和皮艇的拖車已滿,應採取措施。問題是拖車將舉行獨木舟和皮划艇,但有很多不同的組合。邏輯,以防止插入,如果沒有可用的插槽

拖車上有5個「行」向上計數,2個「列」在中間分割5行。我會畫一張圖來向你展示它的樣子,以及哪些船可以到達哪裏。 「C」代表獨木舟,「K」代表皮划艇。拖車看起來是這樣的:

C only|C only } 
______|______ } BOAT TRAILER 
1C\2K|1C\2K } 
______|______ }  
1C\2K|1C\2K } 
______|______ } 
1C\2K|1C\2K } 
______|______ } 
C only| C only } 
______|______ } 

所以我的問題是,什麼儘可能編碼和邏輯來講不採取任何更多的「保留」當拖車全是最好的選擇嗎?此應用程序將是一個.aspx表單,它將向SQL服務器發送一條插入命令以獲取客戶信息。

+4

迄今爲止的努力?請顯示你的代碼。 – tnw

+0

你也應該把它放在堆棧數學板上,有人可能會喜歡解決這個問題。 – Mark

+1

我目前正處於設計階段,並在做E-R圖等,但這是目前唯一阻止我回歸的東西。我目前唯一的想法是用所有可能的組合形成一個巨大的IF + Else聲明。 – user3267755

回答

1
public enum BoatType : int 
{ 
    Kayak = 1, 
    Canoe = 2 
} 

public class BoatReservation 
{ 
    public int ReservationID { get; set; } 
    public BoatType ReservationBoatType { get; set; } 
} 

public class BoatTrailer 
{ 
    public List<BoatReservation> CanoeSlots = new List<BoatReservation>(); 
    public List<BoatReservation> RegularSlots = new List<BoatReservation>(); 

    public BoatTrailer() 
    { 
    } 

    public bool AddBoat(BoatReservation b) 
    { 
     bool boatAdded = false; 
     switch (b.ReservationBoatType) 
     { 
      case BoatType.Canoe: 
       if (CanoeSlots.Count() < 4) 
       { 
        CanoeSlots.Add(b); 
        boatAdded = true; 
       } 
       else 
       { 
        var reg = RegularSlots.Sum(x => Convert.ToInt16(x.ReservationBoatType)); 
        if (reg <= 10) 
        { 
         RegularSlots.Add(b); 
         boatAdded = true; 
        } 
       } 
       break; 

      case BoatType.Kayak: 
       { 
        var reg = RegularSlots.Sum(x => Convert.ToInt16(x.ReservationBoatType)); 
        if (reg <= 11) 
        { 
         RegularSlots.Add(b); 
         boatAdded = true; 
        } 
       } 
       break; 
     } 

     return boatAdded; 
    } 

    public void RemoveBoat(BoatReservation b) 
    { 
     switch (b.ReservationBoatType) 
     { 
      case BoatType.Kayak: 
       if (RegularSlots.Contains(b)) 
       { 
        RegularSlots.Remove(b); 
       } 
       break; 

      case BoatType.Canoe: 
       if (RegularSlots.Contains(b)) 
       { 
        RegularSlots.Remove(b); 
       } 
       else 
       { 
        if (CanoeSlots.Contains(b)) 
        { 
         CanoeSlots.Remove(b); 
         if (RegularSlots.Where(fb => fb.ReservationBoatType == BoatType.Canoe).Count() > 0) 
         { 
          //Move Reservation From Regular to Canoe Only With Opening 
          BoatReservation mv = RegularSlots.FindLast(fb => fb.ReservationBoatType == BoatType.Canoe); 
          RegularSlots.Remove(mv); 
          CanoeSlots.Add(mv); 
         } 
        } 
       } 
       break; 
     } 
    } 

    public string AvailableSlots() 
    { 
     string Output = string.Empty; 

     int AvailableCanoeCnt = (4 - CanoeSlots.Count()) + ((12 - RegularSlots.Count())/2); 
     int AvailableKayakCnt = (12 - RegularSlots.Count()); 

     Output = string.Format("Canoe Slots Left: {0} Kayak Slots Left {1} ", AvailableCanoeCnt, AvailableKayakCnt); 

     return Output; 
    } 
} 

快速類,來處理獨木舟/皮艇保留(二者添加和刪除),以適應拖車。

0

不是這個網站的最佳問題,但我會爲此提供一個僞結構。

Trailer Object 
    JustCanoes int 
    CanoeKayakBlend int 

保留當...

If the reservation is for a canoe, and the JustCanoes value is < 4, then increase JustCanoes by 1 
    If JustCanoes is >= 4 
     If CanoeKayakBlend <= 10 
      increase CanoeKayakBlend by 2 
     else 
      Sorry no reservation available 


If the reservation is for a kayak 
    If CanoeKayakBlend <= 11 
      increase CanoeKayakBlend by 1 
    else 
      Sorry no reservation available 
0

一個非常快速和簡單的實現:這裏是Compartment

class Compartment 
    { 
     private readonly int _maxCanoe; 
     private readonly int _maxKayak; 
     private int _currentCanoe; 
     private int _currentKayak; 
     private readonly int _id; 
     private bool _fullCanoe; 
     private bool _fullKayak; 

     public Compartment(int id, int maxC, int maxK) 
     { 
      _id = id; 
      _maxCanoe = maxC; 
      _maxKayak = maxK; 

      _currentCanoe = _currentKayak = 0; 

      UpdateCapacityStatus(); 
     } 

     private void UpdateCapacityStatus() 
     { 
      _fullCanoe = _maxCanoe == _currentCanoe; 
      _fullKayak = _maxKayak == _currentKayak; 
     } 

     private string Status 
     { 
      get { return IsFull() ? "FULL" : "Space available"; } 
     } 

     public bool IsFull() 
     { 
      return _fullKayak && _fullCanoe; 
     } 

     public void AddCanoe() 
     { 
      _fullKayak = true; // disable adding kayak 

      _currentCanoe = _currentCanoe + 1; 

      _fullCanoe = _maxCanoe == _currentCanoe; //update canoe status 
     } 

     public void AddKayak() 
     { 
      _fullCanoe = true; //disable adding canoe 

      _currentKayak = _currentKayak + 1; 

      _fullKayak = _maxKayak == _currentKayak; //update kayak status 
     } 

     public override string ToString() 
     { 
      return string.Format("Id: {5}, Status: {0}, with {1} of {2} canoes or {3} of {4} kayaks", Status, _currentCanoe, _maxCanoe, _currentKayak, _maxKayak, _id); 
     } 

     public bool CanAddCanoe() 
     { 
      return !_fullCanoe; 
     } 

     public bool CanAddKayak() 
     { 
      return !_fullKayak; 
     } 
    } 

而這裏的司機控制檯應用程序進行測試,使用它你當然應該重構它。

`類節目 { 靜態無效的主要(字符串[]參數) { 變種拖車=新列表 { 新隔室(1,1,0), 新隔間(2,1,0) , 新隔室(3,1,2), 新隔室(4,1,2), 新隔室(5,1,2), 新隔室(6,1,2), 新隔室(7 ,1,2), 新的隔間(8,1,2), };

 foreach (var compartment in trailer) 
     { 
      Console.WriteLine(compartment.ToString()); 
     } 

     Console.WriteLine("Press c for canoe or k for kayak"); 


     var keepGoing = true; 

     while (keepGoing) 
     { 
      var input = Console.Read(); 

      if (input == 99 || input == 107) //99 c, 107 k 
      { 
       if (trailer.All(c => c.IsFull())) 
       { 
        keepGoing = false; 
       } 
       else 
       { 
        if (input == 99) 
        { 
         if(!trailer.Any(t=>t.CanAddCanoe())) 
         { 
          Console.WriteLine("Cannot add a canoe!!!!"); 
         } 
         else 
         { 
          var firstAvailable = trailer.First(c => c.CanAddCanoe()); 
          firstAvailable.AddCanoe(); 
         } 
        } 
        else if (input == 107) 
        { 
         if (!trailer.Any(t => t.CanAddKayak())) 
         { 
          Console.WriteLine("Cannot add a kayak!!!!"); 
         } 
         else 
         { 
          var firstAvailable = trailer.First(c => c.CanAddKayak()); 
          firstAvailable.AddKayak(); 
         } 
        } 
        else 
        { 
         Console.WriteLine("Press c for canoe or k for kayak"); 
        } 
       } 

       foreach (var compartment in trailer) 
       { 
        Console.WriteLine(compartment.ToString()); 
       } 
      } 
     } 
     Console.ReadKey(); 
    } 
}`