import java.util.Arrays; /** * A classroom with seats arranged in rows and columns for students to sit in, * which tracks which students are in the room for contact tracing. * * Due to physical distancing rules, capacity limits are strictly enforced. * * @author Ted Bauer, Ashneel Das, Lucia Gomez, Shirley Huang, Annabel Lin, * Changyuan Lin, Sam Sorenson, Bryan Tabor, Michael Xing, Vivi Ye */ public class Room { /** * The number of rows of seats. */ private final int numRows; /** * The number of seats in each row. */ private final int numCols; /** * The number of people currently in the room. * * * Class Invariant: 0 <= numberOfPeople <= numRows * numCols */ private int numberOfPeople = 0; /** * The names of people seated at each seat in this Room, represented by row, * then by column. Null represents an empty seat. * * * Class Invariant: A row is completely filled before the next is used, and all * empty seats in a row are at the end. * More formally, if people[x][y] == null, then for all z where y < z < * people[x].length, people[x][z] == null, and for all a where x < a < * people.length, people[a][b] == null for all b. * * * Class Invariant: There are numRows rows, each with numCols seats. * More formally, people.length == numRows and people[x].length == numCols for * all valid x. */ private String[][] people; /** * Constructor: a Room with n rows and m columns of seats. * * @param n Number of rows of seats * @param m Number of columns of seats * @throws IllegalArgumentException if n or m are negative */ public Room(int n, int m) { if (n < 0 || m < 0) { throw new IllegalArgumentException(); } people = new String[n][m]; numRows = n; numCols = m; } /** * Puts a person in the room. * * @param name The name of the person entering * @throws IllegalStateException if the room is full */ public void enter(String name) { if (numberOfPeople == numRows * numCols) { throw new IllegalStateException(); } people[numberOfPeople / numCols][numberOfPeople % numCols] = name; numberOfPeople++; } // TODO: Write documentation for this method public boolean isPersonInRoom(String name) { for (String[] row : people) { for (String person : row) { if (person == name) { return true; } } } return false; } // TODO: Write documentation for this method public void leave(String name) { numberOfPeople--; for (int i = 0; i < numRows; i++) { for (int j = 0; j < numCols; j++) { if (people[i][j] == name) { people[i][j] = null; } } } } /** * Move everyone in the current room to another identical room. * * @return A new Room with all the people from this room. */ public Room moveToOtherRoom() { // Clone this room Room newRoom = new Room(numRows, numCols); newRoom.people = people.clone(); // Remove people currently in this room for (String[] row : people) { Arrays.fill(row, null); } return newRoom; } }