2014-08-27 85 views
-6

我是新的編碼和我最近閱讀關於代碼重構的文章。所以我做了一個控制檯應用程序來預訂船上的房間。我認爲在我的項目中只有2個部分需要重構,如下所示。 一個是if else語句。如何使這個C#代碼很小或重構此代碼

 ship1 = new Ship("Olympic Countess"); 

     ArrayList groupA = new ArrayList(); 
     for (int i = 0; i < 10; i++) 
     { 
      groupA.Add(new room(5000, "A" + (i + 1))); 
     } 

     ArrayList groupB = new ArrayList(); 
     for (int i = 0; i < 10; i++) 
     { 
      groupB.Add(new room(4000, "B" + (i + 1))); 
     } 

     ArrayList groupC = new ArrayList(); 
     for (int i = 0; i < 30; i++) 
     { 
      groupC.Add(new room(3500, "C" + (i + 1))); 
     } 

     ArrayList groupD = new ArrayList(); 
     for (int i = 0; i < 36; i++) 
     { 
      groupD.Add(new room(3400, "D" + (i + 1))); 
     } 

     ArrayList groupE = new ArrayList(); 
     for (int i = 0; i < 40; i++) 
     { 
      groupE.Add(new room(3300, "E" + (i + 1))); 
     } 

     ArrayList groupF = new ArrayList(); 
     for (int i = 0; i < 30; i++) 
     { 
      groupF.Add(new room(3400, "F" + (i + 1))); 
     } 

     ArrayList groupG = new ArrayList(); 
     for (int i = 0; i < 36; i++) 
     { 
      groupG.Add(new room(3300, "G" + (i + 1))); 
     } 

     ArrayList groupH = new ArrayList(); 
     for (int i = 0; i < 40; i++) 
     { 
      groupH.Add(new room(3200, "H" + (i + 1))); 
     } 

     ship1.addDeck("Balcony Suite", groupA); 
     ship1.addDeck("Suite", groupB); 
     ship1.addDeck("Deck 3 - Outside Twin", groupC); 
     ship1.addDeck("Deck 2 - Outside Twin", groupD); 
     ship1.addDeck("Deck 1 - Outside Twin", groupE); 
     ship1.addDeck("Deck 3 - Inside Twin", groupF); 
     ship1.addDeck("Deck 2 - Inside Twin", groupG); 
     ship1.addDeck("Deck 1 - Inside Twin", groupH);  
    } 

,另一種是,如果else語句如下

public Reservation bookPassage(String cabinclass, Customer booker, int number) 
     { 
     ArrayList cabins; 
     if (cabinclass.Equals("a", StringComparison.OrdinalIgnoreCase)) 
      cabins = ship1.getDeck("Balcony Suite"); 
     else if (cabinclass.Equals("b", StringComparison.OrdinalIgnoreCase)) 
      cabins = ship1.getDeck("Suite"); 
     else if (cabinclass.Equals("c", StringComparison.OrdinalIgnoreCase)) 
      cabins = ship1.getDeck("Deck 3 - Outside Twin"); 
     else if (cabinclass.Equals("d", StringComparison.OrdinalIgnoreCase)) 
      cabins = ship1.getDeck("Deck 2 - Outside Twin"); 
     else if (cabinclass.Equals("e", StringComparison.OrdinalIgnoreCase)) 
      cabins = ship1.getDeck("Deck 1 - Outside Twin"); 
     else if (cabinclass.Equals("f", StringComparison.OrdinalIgnoreCase)) 
      cabins = ship1.getDeck("Deck 3 - Inside Twin"); 
     else if (cabinclass.Equals("g", StringComparison.OrdinalIgnoreCase)) 
      cabins = ship1.getDeck("Deck 2 - Inside Twin"); 
     else 
      cabins = ship1.getDeck("Deck 1 - Inside Twin"); 

我不明白的是我的參數是兩條邏輯改變。 那麼當我的艙艙每次都在變化時,我怎麼能爲這個邏輯做一個單獨的方法?

+1

你應該問這對http://codereview.stackexchange.com/ – GolfWolf 2014-08-27 08:30:03

+0

如果你的代碼工作,你的問題可能是一個更好的˚F它用於[代碼評論](http://codereview.stackexchange.com/)。 – nvoigt 2014-08-27 08:30:10

回答

1

您所有的迭代可以結合在一起,因此在這樣一個運行中執行:

ArrayList groupA = new ArrayList(); 
    ArrayList groupB = new ArrayList(); 
    ArrayList groupC = new ArrayList(); 
    ArrayList groupD = new ArrayList(); 
    ArrayList groupE = new ArrayList(); 
    ArrayList groupF = new ArrayList(); 
    ArrayList groupG = new ArrayList(); 
    ArrayList groupH = new ArrayList(); 

    for (int i = 0; i < 40; i++) 
    {   
     if (i < 10) 
     { 
      groupA.Add(new room(5000, "A" + (i + 1))); 
      groupB.Add(new room(4000, "B" + (i + 1))); 
     } 
     if (i < 30) 
     { 
      groupF.Add(new room(3400, "F" + (i + 1))); 
      groupC.Add(new room(3500, "C" + (i + 1))); 
     } 
     if (i < 40) 
     { 
      groupE.Add(new room(3300, "E" + (i + 1))); 
      groupH.Add(new room(3200, "H" + (i + 1)));   
     } 
     if (i < 36) 
     { 
      groupD.Add(new room(3400, "D" + (i + 1))); 
      groupG.Add(new room(3300, "G" + (i + 1))); 
     }    
    } 

    ship1.addDeck("Balcony Suite", groupA); 
    ship1.addDeck("Suite", groupB); 
    ship1.addDeck("Deck 3 - Outside Twin", groupC); 
    ship1.addDeck("Deck 2 - Outside Twin", groupD); 
    ship1.addDeck("Deck 1 - Outside Twin", groupE); 
    ship1.addDeck("Deck 3 - Inside Twin", groupF); 
    ship1.addDeck("Deck 2 - Inside Twin", groupG); 
    ship1.addDeck("Deck 1 - Inside Twin", groupH); 

和相互排斥的一羣IFS可以在CSE LIK被加入:

switch (cabinclass.ToLower()) 
{ 
    case "a": 
     cabins = ship1.getDeck("Balcony Suite"); 
     break; 
    case "b": 
     cabins = ship1.getDeck("Suite"); 
     break; 
    case "c": 
     cabins = ship1.getDeck("Deck 3 - Outside Twin"); 
     break; 
    case "d": 
     cabins = ship1.getDeck("Deck 2 - Outside Twin"); 
     break; 
    case "e": 
     cabins = ship1.getDeck("Deck 1 - Outside Twin"); 
     break; 
    case "f": 
     cabins = ship1.getDeck("Deck 3 - Inside Twin"); 
     break; 
    case "g": 
     cabins = ship1.getDeck("Deck 2 - Inside Twin"); 
     break; 
    default: 
     cabins = ship1.getDeck("Deck 1 - Inside Twin"); 
     break;    
} 
+1

不錯,你甚至可以減少if語句的數量,方法是反向添加到列表中並將它們分組在一起(即小於40首先添加e和h,然後小於36秒以添加d和g) – Sayse 2014-08-27 08:46:06

+0

@sayse已編輯。 .. – mg30rg 2014-08-27 09:01:41

+1

謝謝@sayse和其他人回答我認爲我有一些它,我會嘗試實現它現在再次感謝你 – 2014-08-27 13:59:14

0

使用參數化函數。

E.g. in

ArrayList groupC = new ArrayList(); 
    for (int i = 0; i < 30; i++) 
    { 
     groupC.Add(new room(3500, "C" + (i + 1))); 
    } 

項目不同的是:groupC,30,3500和「C」。

使用上述項目作爲參數,併爲每個片段調用具有正確參數的方法。

1

看看這段代碼,應該(或可能)重構代碼中最有可能有更多部分。

讓我們開始提取常量。只需將每個常量字符串(如「甲板1 - 內部雙胞胎」)放入常量中。如果您嘗試重新命名任何套牌,這將有助於您。

那麼你應該堅持常見的編碼習慣。 C#中的類名始終以大寫字母開頭(將room重命名爲Room)。

然後你可以從

new ArrayList(); 
for (int i = 0; i < 30; i++) 
{ 
    groupC.Add(new room(3500, "C" + (i + 1))); 
} 

然後,而不是提取方法的if語句,你可以使用一個類似於提取正確的甲板,這隻會刪除if-then-else的Dictionary什麼的。

可能很多更可以重構使你的代碼更好:)