我正在尝试重构这个类并且对java很新,我害怕打破它。我的意思是它已经有效但我正在努力改进它。我正在看的主要问题是处理代码中命令大小的重复,我正在考虑为颜色制作一个hashmap,但我不相信会缩短代码。我已经开始从代码中提取方法,即曾经处于切换案例中的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.");
}
}
答案 0 :(得分:2)
解决此问题的唯一方法是
获取用于生成代码的任何规范和文档。
与代码的作者和任何用户(如果有)交谈。
以代码为中心构建一整套单元测试。旨在实现100%的代码覆盖率。
逐个进行更改,确保不会破坏任何测试。
保留单元测试 - 它们总是有用的。
对于步骤(3),请参阅https://en.wikipedia.org/wiki/JUnit