java - 我需要在不使用实例变量的情况下重构这个类

标签 java parameters instance-variables tui

所以我正在做一个 TUI,这是我的第一次迭代。

package bulb.classes;

import java.util.Scanner;
import java.util.ArrayList;

public class RoomTUI {

private ArrayList<Room> rooms;
Scanner scan = new Scanner (System.in);
private int userNumber;
private String userAnswer;

public void run() {
    rooms = new ArrayList<Room>();
    introduction();
    userNumber = 0;
    options();
    while(userNumber < 5) {
        if(userNumber == 1) {
            newRoom();
        }
        if(userNumber == 2) {
            addBulbToRoom();
        }
        if(userNumber == 3) {
            clickAllBulbsInRoom();
        }
        if(userNumber == 4) {
            printDescriptionOfBulbs();
        }
    }
    System.out.println("Goodbye");
}

public int getUserInt(String aString) {
    System.out.println(aString);
    userAnswer = scan.nextLine();
    userNumber = Integer.parseInt(userAnswer);
    return userNumber;
}

public void displayRooms() {
    System.out.println("Possible rooms to choose from.");
    String tempString = "";
    int roomIndex = 0;
    for (int i = 0; i < rooms.size(); i++) {
        tempString = tempString + "Room " + roomIndex++ + ": " + rooms.get(i).getDescription() + "\n";
    }
    System.out.println(tempString);
}

public void introduction() {
    System.out.println("Welcome! With this program you can make rooms and design and place the light bulbs for each room you create.");
}

public void options() {
    System.out.println("1 : Create a new Room");
    System.out.println("2 : Add a bulb to an existing room");
    System.out.println("3 : Click all of the bulbs in a particular room");
    System.out.println("4 : Display a description of all bulbs in a particular room");
    System.out.println("5 : Quit");
    getUserInt("What would you like to do?");
}

public void newRoom() {
    System.out.println("Please enter a name for your room");
    String name = scan.nextLine();
    Room aRoom = new Room(name);
    rooms.add(aRoom);
    System.out.println("You have added the " + name + ".");
    options();
}

public void addBulbToRoom() {
    displayRooms();
    System.out.println("Which room do you want the bulb in?");
    String choice = scan.nextLine();
    int choiceNumber = Integer.parseInt(choice);
    System.out.println("Please enter the blub's color.");
    String color = scan.nextLine();
    System.out.println("Please enter the blub's increment amount.");
    String incrementS = scan.nextLine();
    int incrementI = Integer.parseInt(incrementS);
    ThreeWayBulb aBulb = new ThreeWayBulb(color, incrementI);
    rooms.get(choiceNumber).addBulb(aBulb);
    System.out.println("A " + color + " bulb with and increment of " + incrementI + " was added.");
    options();
}

public void clickAllBulbsInRoom() {
    displayRooms();
    System.out.println("Which room do you want the bulbs clicked?");
    String choice = scan.nextLine();
    int choiceNumber = Integer.parseInt(choice);
    rooms.get(choiceNumber).clickAllBulbs();
    System.out.println("The bulbs in " + rooms.get(choiceNumber).getDescription() + " have been clicked.");
    options();
}

public void printDescriptionOfBulbs() {
    displayRooms();
    System.out.println("Please enter a room number.");
    String choice = scan.nextLine();
    int choiceNumber = Integer.parseInt(choice);
    System.out.println(rooms.get(choiceNumber).getDescription() + " with " + rooms.get(choiceNumber).returnSize() + " bulbs: " + "\n" + rooms.get(choiceNumber).toString());
    options();
 }
}

我的导师希望我在没有实例变量的情况下执行此操作,他说如果一个方法需要 ArrayList,我应该将其作为参数,并且在我的 TUI 中没有实例变量。我一辈子都不知道该怎么做。另外,也可以使其静态工作飞行。感谢您提供的任何帮助。

最佳答案

他希望您从中心位置(例如主线程)声明 ArrayList,然后将其作为参数传递给使用它的函数。这样,如果您要采用方法并将它们放入不同的类中,那么它就不会中断,因为它们不依赖于此类。

例如,如果我们采用您的 newRoom 类:

public void newRoom(List<Room> roomList) {
    System.out.println("Please enter a name for your room");
    String name = scan.nextLine();
    Room aRoom = new Room(name);
    roomList.add(aRoom);
    System.out.println("You have added the " + name + ".");
    options();
}

编辑:实现此目的的最简单方法可能是将 rooms 声明移动到 run 方法中。现在,对于代码中报告“未知变量房间”的每个位置,您可以修改函数以采用 ArrayList 作为参数。

关于java - 我需要在不使用实例变量的情况下重构这个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7960480/

相关文章:

java - 如何向 Java Web 应用程序添加 Windows 安全对话框

java - 从地址簿中检索家庭联系电话

java - 从 EJB 访问 SipFactory

ios - 如何在 Swift 中传递带有未知参数并返回值作为参数的函数

CSS类名参数?

java - Azure 上的 Windows Server 2012 上 JVM 64 最多使用 2GB

C++ 参数是指向常量对象的指针,但未返回更新的对象?

ruby - Ruby 的 attr_accessor 如何产生类变量或类实例变量而不是实例变量?

原型(prototype)中具有数字属性的 Javascript 对象

objective-c - Objective-C 方法如何访问被调用者的 ivars?