Skip to content

Instantly share code, notes, and snippets.

@nkhil
Last active August 8, 2019 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nkhil/16d7b9521b155cffd94a8ef879e4510a to your computer and use it in GitHub Desktop.
Save nkhil/16d7b9521b155cffd94a8ef879e4510a to your computer and use it in GitHub Desktop.
class Parcel {
constructor(dimensions) {
this.dimensions = dimensions;
this.parcelSize = Parcel.returnParcelSize(dimensions);
this.price = Parcel.calculatePrice(dimensions);
}
static get prices() {
return {
small: 3,
medium: 5,
large: 15,
extraLarge: 30,
};
}
static get parcelInfo() {
return {
small: { maxDimension: 10, price: 3 },
medium: { maxDimension: 50, price: 8 },
large: { maxDimension: 100, price: 15 },
extraLarge: { maxDimension: null, price: 25 },
};
}
static calculatePrice(dimensions) {
// [1, 2, 3]
// small / medium / large
const parcelSize = Parcel.returnParcelSize(dimensions);
const { price } = Parcel.parcelInfo[parcelSize];
return price;
}
static isParcelRightSize(dimensions, maxDimension) {
const biggestDimension = dimensions.slice().sort((a, b) => a < b)[0];
return biggestDimension < maxDimension;
}
static returnParcelSize(dimensions) {
let result;
switch (true) {
case Parcel.isParcelRightSize(
dimensions,
Parcel.parcelInfo.small.maxDimension
):
result = 'small';
break;
case Parcel.isParcelRightSize(
dimensions,
Parcel.parcelInfo.medium.maxDimension
):
result = 'medium';
break;
case Parcel.isParcelRightSize(
dimensions,
Parcel.parcelInfo.large.maxDimension
):
result = 'large';
break;
default:
result = 'extraLarge';
}
return result;
}
}
// testing
const parcel = new Parcel([11, 2, 3]);
console.log(parcel.dimensions);
console.log(parcel.parcelSize);
console.log(parcel.price);
/////////////////////////////////////////////////////////////////
const { Parcel } = require('./test');
class Order {
constructor(order, parcel, speedyStatus = false) {
this.order = order;
this.speedyStatus = speedyStatus;
this.Parcel = parcel;
}
createOrder() {
return this.order.map(parcel => new this.Parcel(parcel));
}
calculateTotal() {
const arrayOfOrders = this.createOrder();
const total = arrayOfOrders.reduce((sum, parcel) => {
return sum + parcel.price;
}, 0);
return this.speedyStatus ? total * 2 : total;
}
returnResult() {
return {
order: this.createOrder(),
totalCost: this.calculateTotal(),
};
}
}
const sampleOrder = [[1, 2, 3], [11, 2, 2], [51, 1, 1]];
const newOrder = new Order(sampleOrder, Parcel);
console.log('RESULT =>', newOrder.returnResult());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment