Java 在线餐厅预订系统(数据结构)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18291175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:08:11  来源:igfitidea点击:

Online restaurant reservation system (data structures)

javaoopdata-structuresobject-oriented-analysis

提问by 12rad

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.

每家餐厅可以有多个座位数不同的桌子。因此,2 张可坐 4 人的桌子和 4 张可坐 4 人的桌子。

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.

Restaurant:包含 timeofopening、timeOfClosing、totalNoOfSeatsAvailable 不确定我将如何在餐厅内存储餐桌信息。为表设置单独的类是没有意义的。我需要的所有信息是 howManytables 是免费的以及它们的大小。

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.

ReservationSystem: 包含`List checkAvailability(long time, int people)'的接口,这将如何返回这个列表?我最初想到使用 priorityQueue 来维护一个最大可用座位数的队列。但随后我将查看该列表以查看甚至进行预订的时间是否正确,然后在进行预订后更新此队列。一个问题是队列执行所有重复项。

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.
  1. 我如何在每个餐厅内存储餐桌信息。
  2. 维护此餐厅列表的最佳方法是什么,以便我可以返回列表,而不必每次都对这些信息进行排序。

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)

编辑:关于如何存储表信息的问题。我特别担心的是,存储表类意味着我正在创建不必要的对象。这是我的推理。5 张可容纳 2 人的桌子,每张桌子都有完全相同的物品 - 我的意思是没有任何有意义的信息会在它们之间有所不同。我只需要数字。没有座位/桌子。(如果我有一张 4 但 3 人的桌子,我会考虑这张桌子)

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.

我想过创建 3 个数组。让我们说表代表 1,2 等所以 int[] differentSeatingOnTable; 它的索引是表,值是允许的座位。接下来是一个带有 totalNoOfThosetable 的表数组,其中索引是表,值是此类表的总数。与空闲表 freeTables 类似,其中索引是表以及剩余多少这样的空闲表。

采纳答案by Renan

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.

1. )如果您只是在餐厅中存储座位数量,那么您就是在用脚射击。假设我需要为 16 人预订,而且他们都必须在同一张桌子上(是的,我需要一张很长的桌子)。你的系统可以把我的客人带到某个地方,他们必须坐在 8 张桌子上,每人可以坐两个人。

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).

有5张4人桌、8张2人桌、16人一张长桌。(也使用表来存储表是如此元)。

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.

2. )你的推理是正确的保留。如果它正在做重复,你应该发布一个更具体的问题来说明你是如何做的,这样我们就可以尝试帮助你找到错误。

回答by duffymo

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.

您将有两个表:RESTAURANT 和 SITTING(TABLE 是 SQL 中的保留字),它们之间存在一对多关系。

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

RESTAURANT 会有一个名字,所以你可以按名字订购。

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

回答by Vihanga Gamage

1) well..i think it makes more sense if you created the table class.its easier than trying to cramp it in the restaurant class.and you would find it easier too

1) 好吧..我认为如果你创建 table class 更有意义。它比试图在餐厅 class 中抽筋更容易。你也会发现它更容易

2)maintain a primary key field,maybe a composite key,marking out the uniques,this could keep out duplicates

2)维护一个主键字段,可能是一个复合键,标记出唯一性,这样可以防止重复

Reccomendations: Res_Table class Restaurant class

推荐: Res_Table 类 餐厅类

primary key fields with ORDERING

带 ORDERING 的主键字段