会议排期管理
题目
最近看到一个ThoughtWorks的面试题,这边实现一下,问题如下:
Problem Two: Conference Track Management
You are planning a big programming conference and have received many proposals which have passed the initial screen process but you're having trouble fitting them into the time constraints of the day -- there are so many possibilities! So you write a program to do it for you.
- The conference has multiple tracks each of which has a morning and afternoon session.
- Each session contains multiple talks.
- Morning sessions begin at 9am and must finish before 12 noon, for lunch.
- Afternoon sessions begin at 1pm and must finish in time for the networking event.
- The networking event can start no earlier than 4:00 and no later than 5:00.
- No talk title has numbers in it.
- All talk lengths are either in minutes (not hours) or lightning (5 minutes).
- Presenters will be very punctual; there needs to be no gap between sessions.
Note that depending on how you choose to complete this problem, your solution may give a different ordering or combination of talks into tracks. This is acceptable; you don’t need to exactly duplicate the sample output given here.
Test input:
1 | Writing Fast Tests Against Enterprise Rails 60min |
Test output:
1 | Track 1: |
代码介绍
实体类
会议管理系统包含多个实体类
- Track : 负责记录每一天的行程,所有的排期,可以包含多个Track,也就是多天。
- Session : 负责记录每一天上午,或者下午(可以扩展比如晚上),记录了总共可以分配的时间,剩余可用的时间,开始时间(早上是9点,下午是1点)以及Talk列表。
- Talk : 一次会议,包含的是会议标题,会议耗时。
对于使用者来说,只需要进行一次new Track()
操作,即可创建出一个包含上午
和下午
两个Session的Track,上午可用时间为180分钟,下午可用时间为240分钟,每一个Session生成了一个空的Talk List。
枚举类
- SessionType : 定义一个Session,包括Session总共可分配时间以及Session开始时间,使用LocalTime定义时分秒。
总流程类
- TrackService : 处理传递过来的Talk列表,分配时间,安排会议,翻译一个Track列表。
工具类
- FileUtils : 读取文件每一行,返回一个List<String>
调用流程
- 读取文件:
1 | List<String> list = FileUtils.read(new File(FileLaction.TEXT_FILE)); |
- 生成Talk列表
1 | TreeSet<Talk> talks = new TreeSet<>(); //使用TreeSet是可以让列表以时间排序 |
- 调用Service
1 | TrackService trackService = TrackService.getInstance(); //TrackService是单例的 |
- 格式化输出
1 | for (int i = 0; i < tracks.size(); i++) { |
- 得到结果
1 | Track 1: |
我靠这份代码拿到了面试机会。