Skip to content

Instantly share code, notes, and snippets.

@anthonyburdi
Created July 11, 2024 17:50
Show Gist options
  • Save anthonyburdi/885fd6742e3a7652423411a6f4e428f4 to your computer and use it in GitHub Desktop.
Save anthonyburdi/885fd6742e3a7652423411a6f4e428f4 to your computer and use it in GitHub Desktop.
/*
============================================
Convenience Store Requirements Specification
============================================
===== Background =====
Hi and welcome to Pip's Bodega, a convenience store in NYC!
We sell everything you might expect from your local convenience store but unfortunately,
our goods are constantly degrading in quality as they approach their expiration date.
We've written up a system that updates our inventory for us but we think it needs some work.
===== Requirements =====
Here are the requirements we gave them:
- All items have a shelf life, which denotes the number of days we have to sell the item.
- All items have a price value, which denotes how valuable the item is.
- At the end of each day, the system automatically updates both values for every item.
- By default, both values are decremented by 1.
- Once the sell by date has passed, price degrades twice as fast.
- The price of an item is never negative and never more than $50.
You should also be aware of some special products:
- Aged brie actually increases in price the older it gets.
- Lottery tickets, like aged brie, increase in price as time elapses.
- Price increases by $2 when there are 10 days or less and by $3 when there are 5 days or less.
- After the lottery, the price drops to $0.
- Our bodega cat can never be sold and therefore maintains the same shelf life and price.
===== Your Task =====
Please review the below code for style, design/architecture, testability, and maintainability.
What do you think needs to be done before we can merge this code into production?
*/
import { requestAPI } from "pips_convenience/api.js";
class Item {
constructor(name, shelfLife, price){
this.name = name;
this.shelfLife = shelfLife;
this.price = price;
}
}
/**
* Our store
*/
class ConvenienceStore {
constructor(){
// Get items from prod API
this.items = requestAPI("http://api.prod.pipsconvenience.io/v1/items");
}
updatePrices() {
// We iterate through all of the items we have in the store
for (var i = 0; i < this.items.length; i++) {
if (this.items[i].name != 'Aged Brie' && this.items[i].name != 'Lottery Ticket') {
if (this.items[i].price > 0) {
if (this.items[i].name != 'Bodega Cat') {
this.items[i].price = this.items[i].price - 1;
}
}
} else {
if (this.items[i].price < 50) {
this.items[i].price = this.items[i].price + 1;
if (this.items[i].name == 'Lottery Ticket') {
if (this.items[i].shelfLife <= 10) {
if (this.items[i].price < 50) {
this.items[i].price = this.items[i].price + 1;
}
}
if (this.items[i].shelfLife <= 5) {
if (this.items[i].price < 50) {
this.items[i].price = this.items[i].price + 1;
}
}
}
}
}
if (this.items[i].name != 'Bodega Cat') {
this.items[i].shelfLife = this.items[i].shelfLife - 1;
}
if (this.items[i].shelfLife < 0) {
if (this.items[i].name != 'Aged Brie') {
if (this.items[i].name != 'Lottery Ticket') {
if (this.items[i].price > 0) {
if (this.items[i].name != 'Bodega Cat') {
this.items[i].price = this.items[i].price - 1;
}
}
} else {
this.items[i].price = this.items[i].price - this.items[i].price;
}
} else {
if (this.items[i].price < 50) {
this.items[i].price = this.items[i].price + 1;
}
}
}
}
return this.items;
}
}
module.exports = {
Item,
ConvenienceStore
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment