Skip to content

Commit

Permalink
question: FruitStand
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelriosoliveira committed Jul 3, 2024
1 parent 6bacd39 commit a5b4be0
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ I started this in the [issue #176](https://buttondown.email/cassidoo/archive/we-
- [352 - maxProduct](src/2024/352-maxProduct)
- [355 - onlyEvens](src/2024/355-onlyEvens)
- [357 - sortNames](src/2024/357-sortNames)
- [359 - FruitStand](src/2024/359-FruitStand)
</details>

## Installing & Running
Expand Down
38 changes: 38 additions & 0 deletions src/2024/359-FruitStand/FruitStand.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { FruitStand } from './FruitStand';

describe('#FruitStand', () => {
let stand: FruitStand;

beforeEach(() => {
stand = new FruitStand();
});

it('should add a fruit correctly', () => {
stand.addFruit('apple', 10, 0.5);
expect(stand.fruits).toStrictEqual({ apple: { quantity: 10, price: 0.5 } });
});

it('should throw an error when adding an existing fruit', () => {
stand.addFruit('apple', 10, 0.5);
expect(() => stand.addFruit('apple', 5, 0.2)).toThrow(
'Fruit already exists. Use updateQuantity to change the quantity.',
);
});

it('should update the quantity of an existing fruit correctly', () => {
stand.addFruit('banana', 5, 0.2);
stand.updateQuantity('banana', 10);
expect(stand.fruits.banana.quantity).toBe(10);
});

it('should throw an error when updating a non-existent fruit', () => {
expect(() => stand.updateQuantity('cherry', 20)).toThrow('Fruit not found.');
});

it('should calculate the total value correctly', () => {
stand.addFruit('apple', 10, 0.5);
stand.addFruit('banana', 5, 0.2);
stand.addFruit('cherry', 20, 0.1);
expect(stand.totalValue()).toBe(10 * 0.5 + 5 * 0.2 + 20 * 0.1);
});
});
54 changes: 54 additions & 0 deletions src/2024/359-FruitStand/FruitStand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Define a FruitStand class that allows you to add different types of fruits with their quantities and
prices, update them, and calculate the total value of all the fruits in the stand.
Implement the following methods:
- addFruit(name, quantity, price)
- updateQuantity(name, quantity)
- totalValue()!
Example usage:
// Create a new fruit stand
let stand = FruitStand()
// Add fruits to the stand
stand.addFruit("apple", 10, 0.5)
stand.addFruit("banana", 5, 0.2)
stand.addFruit("cherry", 20, 0.1)
// Update the quantity of an existing fruit
stand.updateQuantity("banana", 10)
// Calculate the total value of all fruits in the stand
console.log(stand.totalValue())
*/

interface FruitData {
quantity: number;
price: number;
}

export class FruitStand {
constructor(public fruits: Record<string, FruitData> = {}) {}

addFruit(name: string, quantity: number, price: number) {
if (this.fruits[name]) {
throw new Error('Fruit already exists. Use updateQuantity to change the quantity.');
}
this.fruits[name] = { quantity, price };
}

updateQuantity(name: string, quantity: number) {
if (!this.fruits[name]) {
throw new Error('Fruit not found.');
}
this.fruits[name] = { ...this.fruits[name], quantity };
}

totalValue() {
return Object.values(this.fruits).reduce(
(total, { quantity, price }) => total + quantity * price,
0,
);
}
}
29 changes: 29 additions & 0 deletions src/2024/359-FruitStand/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# FruitStand

Interview question of the [issue #359 of rendezvous with cassidoo](https://buttondown.email/cassidoo/archive/the-days-you-work-are-the-best-days-georgia/).

## The Question

**Define a `FruitStand` class that allows you to add different types of fruits with their quantities and prices, update them, and calculate the total value of all the fruits in the stand.** Implement the following methods: `addFruit(name, quantity, price)`, `updateQuantity(name, quantity)`, and `totalValue()`!

Example usage:

```js
// Create a new fruit stand
let stand = FruitStand()

// Add fruits to the stand
stand.addFruit("apple", 10, 0.5)
stand.addFruit("banana", 5, 0.2)
stand.addFruit("cherry", 20, 0.1)

// Update the quantity of an existing fruit
stand.updateQuantity("banana", 10)

// Calculate the total value of all fruits in the stand
console.log(stand.totalValue())
```

## Installing & Running

Just `pnpm i` to install all dependencies and then `pnpm t` to run the tests!

0 comments on commit a5b4be0

Please sign in to comment.