2

所以我正在做一个 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 中没有实例变量。我无法为我的生活弄清楚如何做到这一点。此外,也可以让它静态工作。谢谢你提供的所有帮助。

4

2 回答 2

3

他希望您从中心位置(例如主线程)声明 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方法中移动 to 的声明。现在,对于报告“未知变量房间”的代码中的每个位置,您可以修改函数以将 ArrayList 作为参数。

于 2011-10-31T22:25:53.207 回答
1

好吧,消除 userNumber 和 userAnswer 作为成员是微不足道的;它们的使用非常本地化。

对于列表,只需在主循环中创建它后将其传递。

扫描仪多处使用;我想它也可以被传递。

于 2011-10-31T22:30:11.707 回答