有没有人做思特沃克的线下作业题,求讨论

有没有人做思特沃克的线下作业题,求讨论
全部评论
https://github.com/billweasley/BadmintonCourt Java写的 不会写正则,所以语句parse写的特别特别丑... 另外好多地方好像可以改进,总之求交流
点赞 回复 分享
发布于 2017-09-12 20:17
JS写的,个人觉得写得比较垃圾,应该有其他更好的方法和优化的方法,不过自己实在是想不出来了,希望有大佬指点一下 (function work() { //程序主类 class Program { constructor(line) { this.reg = /\s*(\w+)\s+(\d{4}-\d{2}-\d{2})\s+(\d{2}:00~\d{2}:00)\s+([ABCD])\s*(C)?\s*/; //匹配字符串格式 this.dateReg = /^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$/; //匹配日期字符串 this.timeReg = /(09|1[0-9]|2[0-2]):00~(09|1[0-9]|2[0-2]):00/; //匹配时间段字符串 this.A = new Court(); this.B = new Court(); this.C = new Court(); this.D = new Court(); this.total = 0; } //程序入口,进行基础判断,并调用相应场地的对应函数 start(str) { if (!str) { return; } else if (/^\s+$/.test(str)) { this.printAll(); } else if (!(this.reg.test(str))) { console.log('> ' + new Error('the booking is invalid!')); return; } else { let res = this.reg.exec(str), id = res[1], date = res[2], time = res[3], court = res[4], cancel = res[5]; //将输入字符串分割,提取信息 let courtObj = { 'A': this.A, 'B': this.B, 'C': this.C, 'D': this.D }; if (!(this.dateReg.test(date))) { //日期格式错误 console.log('> ' + new Error('the booking is invalid!')); return; } else { if (!(this.timeReg.test(time))) { //时间格式错误 console.log('> ' + new Error('the booking is invalid!')); return; } else { if (Number(RegExp.$1) >= Number(RegExp.$2)) { //订场起始时间晚于开始时间 console.log('> ' + new Error('the booking is invalid!')); return; } else { if (!cancel) { //如果没有取消标识符,调用相应的场地对象的order courtObj[court].order(id, date, time); } else { //如果有取消标识符,调用相应的场地对象的cancelOrder courtObj[court].cancelOrder(id, date, time); } } } } } } //所有信息的打印函数 printAll() { console.log('> 收入汇总'); console.log('> ---'); console.log('> 场地:A'); this.A.printItems(); console.log('>'); console.log('> 场地:B'); this.B.printItems(); console.log('>'); console.log('> 场地:C'); this.C.printItems(); console.log('>'); console.log('> 场地:D'); this.D.printItems(); console.log('> ---'); this.total = this.A.total + this.B.total + this.C.total + this.D.total; //在打印时再计算总收入 console.log('> 总计:' + this.total + '元'); } } //场地类,包含场地所有订单、收入等信息 class Court { constructor() { this.orders = []; this.total = 0; } //新增订单时调用的函数 order(id, date, time) { let newTime = time.split('~'), newStart = Number(newTime[0].split(":")[0]), newEnd = Number(newTime[1].split(":")[0]), weekDay = new Date(date).getDay(), price; for (let i = 0; i < this.orders.length; i++) { let nowOrder = this.orders[i]; let currentTime = nowOrder.time.split("~"), currentStart = Number(currentTime[0].split(":")[0]), currentEnd = Number(currentTime[1].split(":")[0]); //取得输入的起始和结束时间 if (date === nowOrder.date && ((newStart >= currentStart && newStart < currentEnd) || (newEnd > currentStart && newEnd <= currentEnd)) && !nowOrder.cancel) { console.log('> ' + new Error('the booking conflicts with existing bookings!')); return; } } let timeSlice = countTimeSlice(newStart, newEnd); let [long1 = 0, long2 = 0, long3 = 0, long4 = 0] = timeSlice; //分别计算输入的时间分别在4个时间段内的长度 //计算新订单所带来的收入 if (weekDay > 0 && weekDay < 6) { price = 30 * long1 + 50 * long2 + 80 * long3 + 60 * long4; } else { price = 40 * long1 + 50 * long2 + (long3 + long4) * 60; } this.total += price; //每次有新订单则更新场地自己的总收入 this.orders.push(new Order(id, date, time, price)); console.log('> Success: the booking is accepted!'); } //取消订单时调用的函数 cancelOrder(id, date, time) { let i = 0; while (this.orders[i]) { let currentOrder = this.orders[i]; let previousPrice = currentOrder.price; if (currentOrder.id === id && currentOrder.time === time && currentOrder.date === date && currentOrder.cancel === false) { let weekDay = new Date(date).getDay(); //每次有订单取消则更新场地自己的总收入 if (weekDay > 0 && weekDay < 6) { currentOrder.price *= 0.5; this.total -= previousPrice * 0.5; } else { currentOrder.price *= 0.25; this.total -= previousPrice * 0.75; } currentOrder.cancel = true; console.log('> Success: the booking is accepted!'); return; } i++; } console.log('> ' + new Error('the booking being cancelled does not exist!')); return; } //各场地的打印函数 printItems() { this.orders.sort(compareFn); let outputStr; for (let i = 0; i < this.orders.length; i++) { let now = this.orders[i]; if (!now.cancel) { outputStr = '>' + now.date + ' ' + now.time + ' ' + now.price + '元'; } else { outputStr = '>' + now.date + ' ' + now.time + ' 违约金 ' + now.price + '元'; } console.log(outputStr); } console.log('> 小计:' + this.total + '元'); } } //订单类,包含订单的用户id、日期、时间、收入以及是否取消等信息 class Order { constructor(id, date, time, price) { this.id = id; this.date = date; this.time = time; this.cancel = false; this.price = price; } } //时间段判断函数 function countTimeSlice(start, end) { let timeLong = end - start, long1 = 0, long2 = 0, long3 = 0, long4 = 0; if (end <= 12) { long1 = timeLong; } else if (end <= 18) { if (start >= 12) { long2 = timeLong; } else { long1 = 12 - start; long2 = end - 12; } } else if (end <= 20) { if (start >= 18) { long3 = timeLong; } else if (start <= 12) { long2 = 18 - start; long3 = end - 18; } else { long1 = 12 - start; long2 = 6; long3 = end - 18; } } else { if (start >= 20) { long4 = timeLong; } else if (start >= 18) { long3 = 20 - start; long4 = end - 20; } else if (start >= 12) { long2 = 18 - start; long3 = 2; long4 = end - 20; } else { long1 = 12 - start; long2 = 6; long3 = 2; long4 = end - 20; } } return [long1, long2, long3, long4]; } //打印前对所有订单排序时调用的比较函数 function compareFn(a, b) { //先按日期排序再按时间排序 let dateA = new Date(a.date).getTime(); let dateB = new Date(b.date).getTime(); if (dateA !== dateB) { //以起始时间排序 let timeA = a.time.split('~'), startA = Number(timeA[0].split(":")[0]), timeB = b.time.split('~'), startB = Number(timeB[0].split(":")[0]); return startA - startB; } else { return dateA - dateB; } } let obj = new Program(); //监听输入并启动程序 let readline = require('readline'), read = readline.createInterface({ input: process.stdin, output: process.stdout }); read.on('line', obj.start.bind(obj)); })();
点赞 回复 分享
发布于 2017-09-12 17:11
前端的,昨天已经交了,不知道其他岗位一不一样
点赞 回复 分享
发布于 2017-09-11 11:42
大佬分享下
点赞 回复 分享
发布于 2017-09-11 09:38
求分享😂
点赞 回复 分享
发布于 2017-09-11 09:10
恩恩,能不能给我指导一下
点赞 回复 分享
发布于 2017-09-11 09:07
是羽毛球馆的么 刚提交。。。
点赞 回复 分享
发布于 2017-09-11 08:30

相关推荐

10-30 16:31
重庆大学 Java
代码飞升:你说你善于学习,大家都会说。你说你是985,985会替你表达一切
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
10-04 05:12
瑞雪兆丰年_:可以贴个超级大的校徽,以防HR眼拙
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务