-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6bacd39
commit a5b4be0
Showing
4 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |