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) {
- vegetables.forEach(x => {
- let [type, quantity, price] = x.split(' ');
- quantity = Number(quantity);
- price = Number(price);
- if (!this.availableProducts.some(x => x.split(' ')[0] === type)) {
- return this.availableProducts.push(x);
- }
- let index = this.availableProducts.find(x => x.split(' ')[0] === type);
- let [oldType, oldQuantity, oldPrice] = index.split(' ');
- oldQuantity = Number(oldQuantity);
- oldPrice = Number(oldPrice);
- oldQuantity += quantity;
- if (price > oldPrice) {
- oldPrice = price;
- }
- this.availableProducts.push(`${oldType} ${oldQuantity} ${oldPrice}`);
- let indexOf = this.availableProducts.indexOf(index);
- this.availableProducts.splice(indexOf, 1);
- });
- const vegis = [];
- vegetables.forEach(x => {
- let [one] = x.split(' ');
- vegis.push(one);
- });
- let result = new Set(vegis);
- return `Successfully added ${Array.from(result).join(', ')}`;
- }
- buyingVegetables(selectedProducts) {
- let totalPrice = 0
- selectedProducts.forEach(x => {
- let [type, quantity] = x.split(' ');
- let element = this.availableProducts.find(x => x.split(' ')[0] === type)
- if (!this.availableProducts.some(x => x.split(' ')[0] === type)) {
- throw new Error(`${type} is not available in the store, your current bill is ${totalPrice.toFixed(2)}.`);
- }
- if (quantity > element.split(' ')[1]) {
- throw new Error(`The quantity ${quantity} for the vegetable ${type} is not available in the store, your current bill is ${totalPrice}.`)
- }
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement