问题描述
我有一个类 ParkingLot,我需要编写一个名为 enter()
的方法来检查停车场是否有空位。如果是,我输入汽车车牌号并将空缺计数减 1。
到目前为止,除了停车场已满外,一切正常,我需要将 vacantSpaces
调整为 0。
到目前为止我所拥有的:
class ParkingLot {
constructor(capacity,rate) {
this.spaces = new Array(capacity).fill('vacant');
this.rate = rate;
this.revenue = 0;
this.queue = new Queue();
}
/**
* Returns the number of vacant parking spaces
* @returns {Number}
* the total number of spaces where the value is "vacant".
*/
get vacantSpaces() {
return this.spaces.reduce(
(sum,space,index) => sum + (space === 'vacant' ? 1 : 0),0
);
}
/**
* As cars enter the parking lot,the license plate number is entered and the car is parked in the first vacant space.
* If the lot is full,the car is added to the queue to be parked when a spot is available.
*
* @param licensePlateNumber
* the license plate number of the car entering
*/
enter(licensePlateNumber) {
const lot = new ParkingLot(licensePlateNumber);
if (this.spaces.length > 1) {
lot.enter(licensePlateNumber);
this.spaces.shift();
}
if (this.spaces.length < 1) {
lot.vacantSpaces = 0;
}
}
}
这是我的测试用例:
test("is zero when lot is full",() => {
const parkingLot = new ParkingLot(2,1);
parkingLot.enter("460-QRJ");
parkingLot.enter("127-HLN");
expect(parkingLot.vacantSpaces).toEqual(0);
});
我不断得到:
Error: expect(received).toEqual(expected) // deep equality
Expected: 0
Received: 1
我是否遗漏了边缘情况,或者我当前的逻辑设置不正确?
解决方法
您的 enter
实现中的一些问题:
- 不要创建新的停车场。您只需要使用当前的parkingLot 实例(
this
)。 - 不要在
shift
数组上调用this.spaces
(想象一下所有会移动到邻近地点的汽车!)。而是找到免费位置(使用indexOf
)并在那里写下车牌号。 - 您不能将
this.vacantSpaces
更新为属性。它是一个吸气剂,而不是一个数字。无需更新任何内容,因为此 getter 将在调用时动态计算有多少空闲空间。
这是更正后的版本。我还添加了一个 leave
方法,并扩展了测试以便队列也被使用:
class ParkingLot {
constructor(capacity,rate) {
this.spaces = new Array(capacity).fill('vacant');
this.rate = rate;
this.revenue = 0;
// Unless you have a very efficient Queue implementation,use a native
// Array,which already provides Queue operations
this.queue = [];
}
/**
* Returns the number of vacant parking spaces
* @returns {Number}
* the total number of spaces where the value is "vacant".
*/
get vacantSpaces() {
return this.spaces.reduce(
(sum,space,index) => sum + (space === 'vacant' ? 1 : 0),0
);
}
/**
* As cars enter the parking lot,the license plate number is entered and the car is parked in the first vacant space.
* If the lot is full,the car is added to the queue to be parked when a spot is available.
*
* @param licensePlateNumber
* the license plate number of the car entering
*/
enter(licensePlateNumber) {
// Don't create a new parkingLot. You want to use the current parkingLot instance (this)
let space = this.spaces.indexOf('vacant');
if (space >= 0) { // found a free spot
this.spaces[space] = licensePlateNumber; // Overwrite 'vacant' with license place number
// Don't shift the spaces array
} else { // No free space
this.queue.push(licensePlateNumber); // add to end of the queue
// Don't write a value to this.vacantSpaces -- it already dynamically provides an updated value
}
}
leave(licensePlateNumber) {
let space = this.spaces.indexOf(licensePlateNumber);
if (space >= 0) { // found its spot
this.spaces[space] = 'vacant'; // Overwrite license place number with 'vacant'
if (this.queue.length) { // There is a car waiting for a space
this.enter(this.queue.shift()); // Attribute the first car in the queue to the new free space
}
}
}
}
const parkingLot = new ParkingLot(2,1);
parkingLot.enter("460-QRJ");
parkingLot.enter("127-HLN");
console.log("vacant spaces: ",parkingLot.vacantSpaces);
parkingLot.enter("300-BEL");
console.log("vacant spaces: ",parkingLot.vacantSpaces);
parkingLot.leave("460-QRJ");
console.log("vacant spaces: ",parkingLot.vacantSpaces);
parkingLot.leave("127-HLN");
console.log("vacant spaces: ",parkingLot.vacantSpaces);