2016-12-06 60 views
0

我試圖重構這個類,並且對於java來說相當新,我害怕破壞它。我的意思是它已經可以工作,但我正在努力改進它。我正在研究的主要問題是處理代碼中命令大小的重複,並且我正在考慮爲這些顏色製作散列圖,但我不相信這會縮短代碼。我已經開始從代碼中提取方法,例如,help()曾用於開關的情況下,但我已經將其拉出以使事情更清晰。我試圖按照這一脈絡。重構援助

import java.awt.*; 
import java.util.HashMap; 
import java.util.LinkedList; 

public class DrawingTool { 
    Color color; 
    // The drawing pen. 
    HashMap<String, Pen> pcase = new HashMap<String, Pen>(); 
    // Where to draw. 
    private Canvas canvas; 
    // Source of user commands. 
    private InputReader reader; 
    private Pen pen; 

    /** 
    * Prepare to draw on a canvas of default size. 
    * The pen starts up at position (0, 0) and its 
    * colour is black. 
    */ 
    public DrawingTool() { 
     this(500, 400); 
    } 

    /** 
    * Prepare to draw on a canvas of specified size. 
    * The pen starts up at position (0, 0) and its 
    * colour is black. 
    * 
    * @param width The canvas width. 
    * @param height The canvas height. 
    */ 
    public DrawingTool(int width, int height) { 
     canvas = new Canvas("Drawing Program", width, height); 
     reader = new InputReader(); 
    } 

    private void noPen() { 
     if (pcase.isEmpty()) { 
      System.out.println("Pen name not recognized."); 
     } 
    } 

    public void printHelp() { 
     System.out.println("The available commands are:   move,  movetto, turn, turnto and colour."); 
     System.out.println(""); 
     System.out.println(
       "Please not that commands like 'move' and 'turn'should be followed by an integer i.e.(move 10)."); 
     System.out.println(""); 
     System.out.println(
       "Similarly, commands such as moveto and turnto must be followed by two integers, the first being for the horizontal"); 
     System.out.println("positioning and the second to the vertical. i.e.(moveto 10 20)."); 
     System.out.println(""); 
     System.out.println("The available colours are: red, blue, yellow, magenta, green and black."); 
     System.out.println("To choose a colour, simply type 'colour' followed by any of the aforementioned colours."); 
    } 

    /** 
    * Allow the user to draw on the canvas by typing commands. 
    */ 
    public void draw() { 
     System.out.println("Welcome to the drawing tool."); 

     System.out.println(); 

     System.out.println("Type bye to exit."); 

     boolean finished = false; 

     int distance = 0; 

     int xPosition = 0; 

     int yPosition = 0; 

     int angle = 0; 

     int degrees = 0; 


     String name; 

     while (!finished) { 
      LinkedList<String> command = reader.getInput(); 
      if (!command.isEmpty()) { 
       String firstWord = command.get(0); 
       if (firstWord.equals("move")) { 
        if (command.size() >= 2) { 
         String secondWord = command.get(1); 
         if (reader.isAnInteger(secondWord)) { 
          distance = reader.convertToInteger(secondWord); 
         } 
        } 
        else { 
         System.out.println("Second word missing"); 
        } 
       } 
       if (firstWord.equals("moveto")) { 
        if (command.size() >= 3) { 
         String secondWord = command.get(1); 
         if (reader.isAnInteger(secondWord)) { 
          xPosition = reader.convertToInteger(secondWord); 
         } 
        } 
        else { 
         System.out.println("Unrecognised command:" + distance); 
        } 
        if (command.size() >= 3) { 
         String secondWord = command.get(1); 
         String thirdWord = command.get(2); 
         if (reader.isAnInteger(secondWord) || reader.isAnInteger(thirdWord)) { 
          xPosition = reader.convertToInteger(secondWord); 
          yPosition = reader.convertToInteger(thirdWord); 
         } 
        } 
        else { 
         System.out.println("Unrecognised command:" + xPosition); 
         System.out.println("Unrecognised command:" + yPosition); 
        } 
       } 
       if (firstWord.equals("turn")) { 
        if (command.size() >= 2) { 
         String secondWord = command.get(1); 
         if (reader.isAnInteger(secondWord)) { 
          degrees = reader.convertToInteger(secondWord); 
         } 
        } 
        else { 
         System.out.println("Unrecognised command:" + degrees); 
         ; 
        } 
       } 
       if (firstWord.equals("turnto")) { 
        if (command.size() >= 2) { 
         String secondWord = command.get(1); 
         if (reader.isAnInteger(secondWord)) { 
          angle = reader.convertToInteger(secondWord); 
         } 
        } 
        else { 
         System.out.println("Unrecognised command:" + angle); 
         ; 
        } 
       } 
       if (firstWord.equals("colour")) { 
        if (command.size() >= 2) { 
         String secondWord = command.get(1); 
         if (secondWord.equals("red")) { 
          color = Color.red; 
          pen.setColor(color); 
         } 
        } 
        else { 
         System.out.println("Second word missing"); 
        } 
       } 
       if (firstWord.equals("colour")) { 
        if (command.size() >= 2) { 
         String secondWord = command.get(1); 
         if (secondWord.equals("blue")) { 
          color = Color.blue; 
          pen.setColor(color); 
         } 
        } 
        else { 
         System.out.println("Second word missing"); 
        } 
       } 
       if (firstWord.equals("colour")) { 
        if (command.size() >= 2) { 
         String secondWord = command.get(1); 
         if (secondWord.equals("yellow")) { 
          color = Color.yellow; 
          pen.setColor(color); 
         } 
        } 
        else { 
         System.out.println("Second word missing"); 
        } 
       } 
       if (firstWord.equals("colour")) { 
        if (command.size() >= 2) { 
         String secondWord = command.get(1); 
         if (secondWord.equals("magenta")) { 
          color = Color.magenta; 
          pen.setColor(color); 
         } 
        } 
        else { 
         System.out.println("Second word missing"); 
        } 
       } 
       if (firstWord.equals("colour")) { 
        if (command.size() >= 2) { 
         String secondWord = command.get(1); 
         if (secondWord.equals("green")) { 
          color = Color.green; 
          pen.setColor(color); 
         } 
        } 
        else { 
         System.out.println("Second word missing"); 
        } 
       } 
       if (firstWord.equals("colour")) { 
        if (command.size() >= 2) { 
         String secondWord = command.get(1); 
         if (secondWord.equals("black")) { 
          color = Color.black; 
          pen.setColor(color); 
         } 
        } 
        else { 
         System.out.println("Second word missing"); 
        } 
       } 
       switch (firstWord) { 
        case "bye": 
         finished = true; 
         break; 

        case "check": 
         pen.isPenDown(); 
         System.out.println("The pen is " + (pen.isPenDown() ? "down" : "up") + "."); 
         break; 

        case "down": 
         pen.penDown(); 
         break; 

        case "up": 
         pen.penUp(); 
         break; 

        case "move": 
         pen.move(distance); 
         break; 

        case "moveto": 
         pen.moveTo(xPosition, yPosition); 
         break; 

        case "turn": 
         pen.turn(degrees); 
         break; 

        case "turnto": 
         pen.turnTo(angle); 
         break; 

        case "colour": 
         pen.setColor(color); 
         break; 

        case "pen": 
         if (firstWord.equals("pen")) { 
          String secondWord = command.get(1); 
          if (command.size() >= 2) { 
           pcase.put(secondWord, new Pen(0, 0, canvas)); 
           pen = pcase.get(secondWord); 
          } 
          else { 
           noPen(); 
          } 
         } 
         break; 

        case "select": 
         if (firstWord.equals("select")) { 
          String secondWord = command.get(1); 
          if (command.size() >= 2) { 
           pen = pcase.get(secondWord); 

          } 
          else { 
           System.out.println("Second word missing"); 
          } 
         } 
         break; 

        case "delete": 
         if (firstWord.equals("delete")) { 
          String secondWord = command.get(1); 
          if (command.size() >= 2) { 
           pcase.remove(secondWord); 
          } 
          else { 
           System.out.println("Second word missing"); 
          } 
         } 
         break; 
        case "help": 
         printHelp(); 
         break; 

        default: 
         System.out.println("Unrecognised command: " + firstWord); 
         break; 
       } 
      } 
     } 
     System.out.println("Goodbye."); 
    } 
} 
+2

_ 「它已經工作,但我正在努力完善它」 _。這聽起來像這篇文章屬於[代碼評論](http://codereview.stackexchange.com)。 – byxor

+0

另外,「有效地使用遺留代碼」書聽起來很適合你。 – byxor

回答

2

解決這個的唯一辦法是

  1. 收購這是用於生產代碼的任何規範和文檔。

  2. 與代碼的作者以及任何用戶(如果可用)交談。

  3. 以代碼爲中心構建一組全面的單元測試。旨在實現100%的代碼覆蓋率。

  4. 進行更改一個接一個,確保你不會打破任何的測試。

  5. 保留單元測試 - 它們總是有用的。

步驟(3),見https://en.wikipedia.org/wiki/JUnit