Online restaurant reservation system (data structures)

19,629

Solution 1

1. ) If you just store the amount of seats in a restaurant, you're shooting yourself in the foot. Suppose I need to make a reservation for 16 people, and they all must be on the same table (yes, I need a pretty long table). Your system could take my guests to someplace where they'd have to sit in 8 tables for two people each.

You do need a table class. Then your restaurants need to have collections of tables. If you want to know how many seats you have in a restaurant, you just have to iterate through its table collection and count the seats. And if you want to know if you can sit a family in a single table in a restaurant, you just have to check whether it has any table with that amount of seats.

EDIT: there is a more minimalistic way to store seats per restaurant. Use a dictionary, hash table or any other structure that holds keys and associated values. So have the key represent a type of table. The key may be an integer saying how many people the table sits. The value is the amount of tables of that type present in the restaurant. I think this is way better than my original suggestion.

So, for example, a restaurant with such a hash table:

Key | Value
 4  |   5
 2  |   8
16  |   1

Has five tables with 4 seats each, 8 tables with 2 seats each, and a single long table that sits 16 people. (Also using a table to store tables is so meta).

2. ) Your reasoning is right for the reservations. If it is doing duplicates, you should post a more especific question showing how you're doing it so we can try and help you locate the bug.

Solution 2

Relational databases make both these requirements easy.

You'll have two tables: RESTAURANT and SITTING (TABLE is a reserved word in SQL) with a one-to-many relationship between them.

A RESTAURANT will have a name, so you can ORDER BY name.

package model;

class Table {
    private int id; 
    private int numSeats; 

    public Table(int id, int numSeats) { 
        this.id = id;
        this.numSeats = numSeats; 
    }
    public int getId() { return this.id; }
    public int getNumSeats() { return this.getNumSeats; }
}

class Restaurant implements Comparable {
    private String name;
    private List<Table> tables;

    public Restaurant(String name) {
        this.name = name; 
        this.tables = new ArrayList<Table>(); 
    }

    public void addTable(Table t) { this.tables.add(t); }
    public void removeTable(int id) {
       for (Table t : this.tables) {
           if (t.getId() == id) {
               this.tables.remove(t); 
               break;
           }
       }
    }    
    public int getCapacity() { 
        int capacity = 0;
        for (Table t : this.tables) {
            capacity += t.getNumSeats();
        }
        return capacity;
    }
    public int compareTo(Restaurant r) {
        return this.name.compareTo(r.name);
    }
}
Share:
19,629
12rad
Author by

12rad

Updated on June 16, 2022

Comments

  • 12rad
    12rad almost 2 years

    I have a task to design an online reservation system. Where a user can enter zip code/ no of people/time of reservation and get a list of restaurants. Assumption (User and restaurant are always in the same city)

    Each restaurant can have multiple tables with different number of seats. So, 2 tables that seat 4 people and 4 tables that seat 4 people.

    I'm having trouble coming up with the right data structures to use.

    My classes are as follows

    Restaurant : Contains timeofopening, timeOfClosing, totalNoOfSeatsAvailable Not sure how i will store table information within the restaurant. It doesn't make sense to have a separate class for the table. All the info i need is howManytables are free and what are their sizes.

    Reservation: This maintains the actual reservation and allows to cancel reservation

    ReservationSystem : contains the interface to `List checkAvailability(long time, int people)' How will this return this list? I initially thought of using a priorityQueue to maintain a queue with max no of seats available. But then i will go through that list to see if the time is correct to even make the reservation and then once a reservation is made,update this queue. One problem is the queue does all duplicates.

    My specific questions are:

    1. How do i store the table information within each restaurant.
    2. What is the best way to maintain this list of restaurants so i can give return a list without having to sort this information everytime.

    EDIT: For the question on how to store the table information. My specific concern is that storing a table class would mean that i'm creating un necessary objects. Here's my reasoning. 5 tables that seat 2 people each with have the exact same objects - i mean there isn't any meaningful information that will be differnet among them. I just need to numbers. no of seats/table.(If i have a table of 4 but 3 peole, I will consider this table taken)

    I thought of creating 3 arrays. Lets say table represent 1,2 etc so int[] differentSeatingOnTable; its indexes are tables and values are seats allowed. Next an array of tables with totalNoOfThosetable where indexs are tables and values are total number of such table. Similary for free tables freeTables where index are table and how many of such free table are left.