Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class VegetableStore {
- constructor(owner, location) {
- this.owner = owner;
- this.location = location;
- this.availableProducts = [];
- }
- loadingVegetables(vegetables) {
- let addedTypes = new Set();
- for (let data of vegetables) {
- let [type, quantity, price] = data.split(' ');
- addedTypes.add(type);
- let searchedType = this.availableProducts.find(
- (p) => p.type === type
- );
- if (!searchedType) {
- this.availableProducts.push({
- type: type,
- quantity: Number(quantity),
- price: Number(price),
- });
- } else {
- searchedType.quantity += Number(quantity);
- if (searchedType.price < Number(price)) {
- searchedType.price = Number(price);
- }
- }
- }
- return `Successfully added ${Array.from(addedTypes).join(', ')}`;
- }
- buyingVegetables(selectedProducts) {
- let currentTotal = 0;
- for (let data of selectedProducts) {
- let [currType, currQuantity] = data.split(' ');
- currQuantity = Number(currQuantity);
- let availableType = this.availableProducts.find(
- (p) => p.type === currType
- );
- if (!availableType) {
- throw new Error(
- `${currType} is not available in the store, your current bill is $${currentTotal.toFixed(
- 2
- )}.`
- );
- }
- if (availableType.quantity < currQuantity) {
- throw new Error(
- `The quantity ${currQuantity} for the vegetable ${currType} is not available in the store, your current bill is $${currentTotal.toFixed(
- 2
- )}.`
- );
- }
- currentTotal += currQuantity * availableType.price;
- availableType.quantity -= currQuantity;
- }
- return `Great choice! You must pay the following amount $${currentTotal.toFixed(
- 2
- )}.`;
- }
- rottingVegetable(type, quantity) {
- let availableType = this.availableProducts.find((p) => p.type === type);
- if (!availableType) {
- throw new Error(`${type} is not available in the store.`);
- }
- if (availableType.quantity < quantity) {
- availableType.quantity = 0;
- return `The entire quantity of the ${type} has been removed.`;
- }
- availableType.quantity -= quantity;
- return `Some quantity of the ${type} has been removed.`;
- }
- revision() {
- let result = [];
- result.push('Available vegetables:');
- this.availableProducts
- .sort((a, b) => a.price - b.price)
- .forEach((p) => result.push(`${p.type}-${p.quantity}-$${p.price}`));
- result.push(
- `The owner of the store is ${this.owner}, and the location is ${this.location}.`
- );
- return result.join('\n');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement