2011-10-31 44 views
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没有实例变量。我不能为我的生活找出如何做到这一点。此外,使其静态工作飞行。谢谢你提供的所有帮助。

回答

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方法中最简单的方法。现在,对于报告“未知变量空间”的代码中的每个位置,您都可以修改函数以将ArrayList作为参数。

+0

我觉得这是类似的东西。所以我必须让我的run方法返回ArrayList?这是我能想到让它通过的唯一途径。 –

+0

是的,你需要让函数返回List类。 – Grambot

1

那么,取消userNumber和userAnswer作为成员是微不足道的;他们的使用非常本地化。

有关列表,只需在主循环中创建它即可将其传递。

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

+0

我觉得这是类似的东西。所以我必须让我的run方法返回ArrayList?这是我能想到让它通过的唯一途径。 –

+0

@LordCanti不,这是你的“事件循环”,因为缺少一个更好的单词。它会将其传递给所有需要访问它的方法。 –

+0

非常感谢。我明白我现在需要怎么做。 –

相关问题