Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let input = [
- '10',
- 'AddWish Electric Scooter 2000Z;1536.50;Stefan',
- 'AddWish Fortnite Skin;3000;Stefan',
- 'AddWish AMD Radeon;4099.99;Pesho',
- 'AddWish Apple AirPods;200000;Kiril',
- 'AddWish Socks;10000;Tosho',
- 'AddWish Swater;999;Stefan',
- 'FindWishesByChild Stefan',
- 'DeleteWishes Stefan',
- 'FindWishesByChild Stefan',
- 'FindWishesByPriceRange 100000;200000',
- ];
- let print = this.print || console.log;
- let gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
- //Code
- class Wish {
- constructor(item_name, estimated_price, child_name) {
- this.itemName = item_name;
- this.price = estimated_price;
- this.childName = child_name
- }
- toString() {
- return `{${this.itemName};${this.childName};${this.price.toFixed(2)}}`;
- }
- }
- class WishList {
- constructor() {
- this.wishes = [];
- }
- addWish(wish) {
- this.wishes.push(wish)
- console.log('Wish added')
- }
- deleteWishes(childName) {
- const deletedWishes = this.wishes.filter(w => w.childName === childName);
- this.wishes = this.wishes.filter(w => w.childName !== childName);
- console.log(`${deletedWishes.length} Wishes deleted`);
- }
- findWishesByPriceRange(fromPrice, toPrice) {
- const matchingWishes = this.wishes.filter(w => w.price >= fromPrice && w.price <= toPrice);
- if (matchingWishes.length === 0) {
- console.log('No Wishes found');
- }
- matchingWishes.sort((a, b) => a.itemName.localeCompare(b.itemName));
- console.log(matchingWishes.map(w => w.toString()).join('\n'));
- }
- findWishesByChild(childName) {
- const matchingWishes = this.wishes.filter(w => w.childName === childName);
- if (matchingWishes.length === 0) {
- console.log('No Wishes found');
- } else {
- matchingWishes.sort((a, b) => a.itemName.localeCompare(b.itemName));
- console.log(matchingWishes.map(w => w.toString()).join('\n'));}
- }
- }
- // const commandLine = 'AddWish Electric Scooter 2000Z;1536.50;Stefan'
- // const parts = commandLine.split(' ')
- // console.log(parts.shift())
- // console.log(parts.join(' ').split(';'))
- const executeCommand = (commandLine, wishlist) => {
- const parts = commandLine.split(' ');
- const command = parts.shift();
- if(command.toLowerCase() === 'addwish'){
- const itemName = parts.join(' ').split(';')[0];
- const estimatedPrice = parseFloat(parts.join(' ').split(';')[1]);
- const childName = parts.join(' ').split(';')[2]
- const wish = new Wish (itemName, estimatedPrice, childName);
- wishlist.addWish(wish)
- } else if (command.toLowerCase() === 'deletewishes'){
- const child_name = parts[0];
- wishlist.deleteWishes(child_name);
- } else if (command.toLowerCase() === 'findwishesbypricerange'){
- const priceLow = parseFloat(parts.join(' ').split(';')[0]);
- const priceUpper = parseFloat(parts.join(' ').split(';')[1])
- wishlist.findWishesByPriceRange(priceLow, priceUpper);
- } else if (command.toLowerCase() === 'findwishesbychild') {
- const childName = parts[0]
- wishlist.findWishesByChild(childName)
- }
- }
- const wishList = new WishList();
- let N = Number(gets())
- console.log(N);
- for (let i = 1 ; i <= N; i++) {
- let commandLine = gets();
- executeCommand(commandLine, wishList)
- }
Advertisement
Add Comment
Please, Sign In to add comment