关注
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));
})();
查看原帖
点赞 4
相关推荐
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 第一次找实习,我建议__ #
17836次浏览 241人参与
# 你怎么评价今年的春招? #
141356次浏览 1384人参与
# 从mentor身上学到了__ #
15992次浏览 259人参与
# 秋招暂停,我将对以下公司做出处罚__ #
28160次浏览 127人参与
# 什么样的公司千万别去 #
14614次浏览 110人参与
# 韶音科技求职进展汇总 #
59346次浏览 504人参与
# 你听到的“最没用”的秋招建议 #
19394次浏览 222人参与
# 如果今天是你的last day,你会怎么度过? #
46938次浏览 294人参与
# 外出实习被同学举报 #
2736次浏览 29人参与
# 秋招我要惩罚这些公司 #
2301次浏览 22人参与
# 2025秋招体验点评 #
45272次浏览 465人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
3732次浏览 18人参与
# 你认为工作的意义是什么 #
201484次浏览 1268人参与
# 工作以后,你父母对你啥态度 #
8597次浏览 90人参与
# 打工人的至爽时刻or至暗时刻 #
41272次浏览 221人参与
# 在国企工作的人,躺平了吗? #
374804次浏览 3930人参与
# 秋招结束之后的日子 #
105265次浏览 1016人参与
# 实习生的蛐蛐区 #
834918次浏览 4092人参与
# 你的秋招第一面感觉怎么样 #
127755次浏览 795人参与
# 非技术岗简历怎么写 #
259725次浏览 3103人参与
查看14道真题和解析
