Question

So I'm doing a TUI and this was my first iteration.

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();
 }
}

My instructor wants me to do this without instance variables He said if a method needs the ArrayList that I should make it a parameter and have no instance variables in my TUI. I can't for the life of me figure out how to do that. Also, making it static work fly either. Thanks for any help you can give.

Was it helpful?

Solution

He wants you to declare the ArrayList from a central location (such as the main thread) and then pass it as an argument to the functions that use it. This way if you were to take methods and put them in different classes then it wouldn't break because they're not dependent on this class.

For example if we take your newRoom class:

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();
}

EDIT: The easiest way to achieve this is to probably move the declaration of rooms to within your run method. Now for each location in the code that reports "unknown variable rooms" you can modify the function to take an ArrayList as a parameter.

OTHER TIPS

Well, eliminating userNumber and userAnswer as members is trivial; their usage is very localized.

For the list, just pass it around after creating it in your main loop.

The scanner is used multiple places; it could also be passed around, I suppose.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top