2011-10-31 70 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

非常感謝。我明白我現在需要怎麼做。 –

相關問題