Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sit-In Lab 2 - OOP Restaurant.  Manage a restaurant and perform these types of queries: Assign a favorite table to a specific group Assign the lexicographically-smallest.

Similar presentations


Presentation on theme: "Sit-In Lab 2 - OOP Restaurant.  Manage a restaurant and perform these types of queries: Assign a favorite table to a specific group Assign the lexicographically-smallest."— Presentation transcript:

1 Sit-In Lab 2 - OOP Restaurant

2  Manage a restaurant and perform these types of queries: Assign a favorite table to a specific group Assign the lexicographically-smallest available table to a specific group A specific group leaves the restaurant Prints the name of the table that a specific group is currently in Prints the name of the group of a specific table Problem Description

3 Restaurant Group Table Group Problem Illustration

4 Restaurant GroupTable Contains Has Uses Relationship Between Entities

5 Class Overview Restaurant Keeps a list of all tables and groups currently in the restaurant Reads and executes the queries Table Contains information about: Capacity of the table The group that is assigned to it The status of the table Its name Group Contains information about: The number of people The assigned table The group’s name

6  Class Restaurant Class Overview Restaurant -tables : ArrayList -groups : ArrayList +//methods  Class Table Table -name : String -capacity : int -isOccupied : Boolean -currentGroup : Group +getName() : String +reset() : void -toggle() : void …  Class Group Group -name : String -capacity : int -currentTable : Table +getName() : String +getCapacity() : int +getTable() : Table … No need getter-setter for Restaurant; Restaurant acts as a “Controller”

7 public void setGroup(Group g) { this.currentGroup = g; this.toggle(); } public void reset() { this.currentGroup = null; this.toggle(); } private void toggle() { this.isOccupied = !this.isOccupied; } Private Helper in Table

8 Constructors Restaurant: public class Restaurant() Group: class Group(String name, int numOfPeople, Table assignedTable) Table: class Table(String name, int capacity) Some questions to ponder: When do we create a new group?  When we can assign it a table. A group cannot “queue”, i.e. if it could not be assigned a table, it leaves the restaurant. When do we add tables (or groups) to the list in Restaurant?  After the creation of each object. When do we remove tables (or groups) from the list in Restaurant?  You remove a group from the list when it leaves the restaurant. You do not remove tables from the list.

9  Some tips for processing the queries:  Performs all logical operations in the Restaurant class  Avoid the use of static variables and methods  The methods in the table and group class are only for operations that modifies or returns their own attributes. These operations are called by the Restaurant class. E.g:  getName(): returns the name of the table (or group)  reset(): resets a table back to vacant (and toggle the status of the table)  We define helper methods in Restaurant as we go along, e.g:  getTableBasedOnName(String tableName): returns the Table with the name tableName, returns null if not found  getValidLexiSmallestTable(int numOfPeople): returns the lexicographically smallest Table that fits the group with numOfPeople. Useful Tips

10 How to avoid static In the Restaurant Class  What most of you did: public static void main(String[] args) { //your code } public static Table getTableBasedOnName(String name) { //implementation }  How to avoid static: public static void main(String[] args) { Restaurant myRestaurant = new Restaurant(); myRestaurant.run(); } public void run() { //your code (treat this as “main”) } public Table getTableBasedOnName(String name) { //implementation }

11 The Run Method (in Restaurant) public void run() { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); sc.nextLine(); readAndStoreTables(sc, N); int Q = sc.nextInt(); sc.nextLine(); executeQueries(sc, Q); }

12 The Run Method (in Restaurant) public void run() { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); sc.nextLine(); readAndStoreTables(sc, N); int Q = sc.nextInt(); sc.nextLine(); executeQueries(sc, Q); }

13 Read and Store Tables private void readAndStoreTables(Scanner sc, int N) { for (int i = 0; i < N; i++) { //reads the input and split it if //a blank space (" ") is found String[] split = sc.nextLine().split(" "); //store table information into local variables String tableName = split[0]; int capacity = Integer.parseInt(split[1]); //create new table object and store it into //the list of tables Table newTable = new Table(tableName, capacity); tables.add(newTable);} }

14 Execute Queries private void executeQueries(Scanner sc, int Q) { for (int i = 0; i < Q; i++) { String[] splittedInput = sc.nextLine().split(" "); int input = Integer.parseInt(splittedInput[0]); //execute queries based on type if (input == 1) { assignFavoriteTable(splittedInput); } else if (input == 2) { assignLexiSmallestTable(splittedInput); } else if (input == 3) { leaveRestaurant(splittedInput); } else if (input == 4) { printOccupiedTable(splittedInput); } else { printCurrentGroupName(splittedInput); }

15  Query 1 Query 1: Assign group to favorite table private void assignFavoriteTable(String[] splittedInput) { String groupName = splittedInput[1]; int numOfPeople = Integer.parseInt(splittedInput[2]); String tableName = splittedInput[3]; Table favTable = getTableBasedOnName(tableName); } If table is not null and can assign*  create group and assign table If not  print “not possible” *Can assign if: 1. The capacity of favTable is at least numOfPeople 2. favTable is currently vacant

16  Helper Method (Reusable for query 1 and 5) /* * pre-condition: The arraylist of tables has been initialized and tables * have been stored properly * * post-condition: Returns the table with name tableName and null if there * is no such table * * description: iterates through the list of tables and return the most * appropriate table based on tableName. Returns null if no such table is * found */ private Table getTableBasedOnName(String tableName) { } Helper Method: getTableBasedOnName -Loop through the list of tables -If the current table’s name matches tableName, return that Table and stop the loop. Why can we stop? -After finish iterating and no table is found: return null

17 private void assignLexiSmallestTable(String[] splittedInput) { String groupName = splittedInput[1]; int numOfPeople = Integer.parseInt(splittedInput[2]); Table assigned = getValidTable(numOfPeople); }  Query 2 Query 2: Assign group to a table If table is not null*  create group and assign table If null (we don’t find a table that fits)  print “not possible” *We don’t need to check if it is assignable as in query 1 since we do the checking inside getValidTable(). Why?  we will iterate through all tables and find the lexicographically smallest valid table. Hence, as we iterate, we also check its validity.  Why do we need to iterate through all tables? Think!

18 /* * pre-condition: ??? * post-condition: ??? * description: ??? */ private Table getValidTable(int numOfPeople) { }  Helper Method Helper Method: getValidTable -Loop through the list of tables while keeping the current lexi- smallest and valid table in a local variable -For each table: check its validity. If it’s invalid, skip. If it’s valid, compare with the current lexi-smallest table. If this new table is lexicographically smaller, update the local variable that keeps the table. -Return the lexi-smallest available table (if it exists) or null otherwise

19  Query 3 Query 3: A group leaves the restaurant private void leaveRestaurant(String[] splittedInput) { String groupName = splittedInput[1]; Group currentGroup = getGroupBasedOnName(groupName); } If currentGroup is not null  reset its table and assign null as currentGroup ’s table If null (there is no such group)  do nothing The implementation of getGroupBasedOnName is similar to getTableBasedOnName. This helper is useful for Query 3 and 4.

20  Query 4 Query 4 : Prints the current group’s table Why?  we only add groups to the list when we assign it a table  when a group leaves, we remove it from the list of groups private void printOccupiedTable(String[] splittedInput) { String groupName = splittedInput[1]; Group currentGroup = getGroupBasedOnName(groupName); } If currentGroup is not null  print its table name. We do not need to check if it has a table or not. Why*? If null  print “invalid”

21  Query 5 private void printCurrentGroupName(String[] splittedInput) { String tableName = splittedInput[1]; Table curTable = getTableBasedOnName(tableName); } Query 5 : Prints the current group of the table If curTable is not null and it is occupied  print the occupying group’s name. Else  print “invalid”

22 The End

23


Download ppt "Sit-In Lab 2 - OOP Restaurant.  Manage a restaurant and perform these types of queries: Assign a favorite table to a specific group Assign the lexicographically-smallest."

Similar presentations


Ads by Google