import java.util.*; /** * This is my own Free Response question do not be intimidated * the first question is of harder dificulty than the second one. * * @author Carlos Vega * @version 05/03/22 */ public class Restaurant { private ArrayList tableList; private int currentPlace; public Restaurant() { tableList = new ArrayList(); currentPlace = 0; } /* * Part A: * You will create a method that will find a reservation and return its * index which will be stored in current place. * If it cannot find the reservation it will return a -1 as currentPlace * make sure to make it lowercase */ public int findReservation(String name, int seats) { name.toLowerCase(); if (tableList.isEmpty() == false) { for(int i = 0; i < tableList.size(); i++) { currentPlace = i; if (tableList.get(i) != null && tableList.get(i).getName().equals(name) && tableList.get(i).getSeats() == seats) { return currentPlace; } else { currentPlace = -1; } } } else if(tableList.isEmpty() == true) { currentPlace = -1; } return currentPlace; } /* * Part B: * You will update the list with reservations, * the person will check in for a reservation if the person already * has a reservation you will update the list by removing them * if they dont you will add their reservation. * make it lowercase */ public ArrayList newListUpdated(String name, int seats) { name.toLowerCase(); if(findReservation(name, seats) >= 0) { tableList.remove(currentPlace); } else if(findReservation(name, seats) < 0) { tableList.add(new Reservation(name, seats)); } return tableList; } public void add(Reservation r) { tableList.add(r); } public void remove(int i) { tableList.remove(i); } public int getCurrentPlace() { return currentPlace; } public int getLength() { return tableList.size(); } public String toString() { return "Reservation list: " + tableList; } }