-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added author solutions exam Sep 2016
- Loading branch information
KonstantinSimeonov
committed
Sep 15, 2016
1 parent
97e6dc2
commit 2e2e24b
Showing
15 changed files
with
4,046 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,150 @@ | ||
# Carts and Products | ||
|
||
- Implement a functionality to serve a Shopping Center | ||
- Export a class **`Product`** | ||
- Export a class **`ShoppingCart`** | ||
|
||
# Classes and functionality: | ||
|
||
### `Product` | ||
|
||
- Has the following methods and properties | ||
- `constructor` | ||
- **Parameters** | ||
- `productType` | ||
- `name` | ||
- `price` | ||
- _Example_: | ||
- `new Product("Sweets", "Shokolad Milka", 2)`; | ||
- **Public properties**: | ||
- `productType`: `String` | ||
- `name`: `String` | ||
- `price`: `Number` | ||
|
||
### `ShoppingCart` | ||
|
||
- Has the following methods and properties | ||
- `constructor` | ||
- **Parameters** | ||
- No parameters | ||
- _Example_: | ||
- `new ShoppingCart()`; | ||
- **Public properties**: | ||
- `products`: `Array` | ||
- **Public methods**: | ||
- `add(product)` | ||
- **Parameters**: | ||
- A `Product` or Product-like object | ||
- **Behavior**: | ||
- Adds the `product` to the `products` array in this `ShoppingCart` instance | ||
- A product can be added many times into the same `ShoppingCart` instance | ||
- Should provide chaining | ||
- `remove(product)` | ||
- **Parameters** | ||
- a `Product` or Product-like object | ||
- **Behavior**: | ||
- Removes the left-most object from the `products` array in this `ShoppingCart` instance, that has the same `name`, `price` and `productType` | ||
- **Throws** when: | ||
- The `ShoppingCart` instance does not contain this product | ||
- There are not products in the `ShoppingCart` instance | ||
- `showCost()` | ||
- **Parameters** | ||
- No parameters | ||
- **Behavior**: | ||
- Returns the sum from the costs of all products in this `ShoppingCart` instance | ||
- Returns `0` when there are no products in this `ShoppingCart` instance | ||
- `showProductTypes()` | ||
- **Parameters** | ||
- No parameters | ||
- **Behavior**: | ||
- Returns the **unique productTypes** of the products added to this `ShoppingCart` instance | ||
- The returned product types must be **sorted alphabetically** | ||
- Returns an empty array when there are no products in this `ShoppingCart` instance | ||
- `getInfo()` | ||
- **Parameters** | ||
- No parameters | ||
- **Behavior**: | ||
- Returns an object containing information about the products in this `ShoppingCart` instance. The returned object has two properties: | ||
- `products`: Groups products by their name | ||
- For each unique product name there creates an element: | ||
- The `name` of the products | ||
- Their total cost | ||
- The quantity of products with this name in the `ShoppingCart` instance | ||
- `totalPrice`: The total price of all products in this `ShoppingCart` instance | ||
- Returns an object with `totalPrice` equal to `0` and `products` - an empty array, when no products in this `ShoppingCart` instance | ||
|
||
# Solution template | ||
|
||
```javascript | ||
|
||
function solve(){ | ||
class Product{ | ||
/* .... */ | ||
} | ||
|
||
class ShoppingCart { | ||
/* .... */ | ||
} | ||
return { | ||
Product, ShoppingCart | ||
}; | ||
} | ||
``` | ||
|
||
# Example usage: | ||
|
||
```javascript | ||
let {Product, ShoppingCart} = solve(); | ||
|
||
let cart = new ShoppingCart(); | ||
|
||
let pr1 = new Product("Sweets", "Shokolad Milka", 2); | ||
cart.add(pr1); | ||
console.log(cart.showCost()); | ||
//prints `2` | ||
|
||
let pr2 = new Product("Groceries", "Salad", 0.5); | ||
cart.add(pr2); | ||
cart.add(pr2); | ||
console.log(cart.showCost()); | ||
//prints `3` | ||
|
||
console.log(cart.showProductTypes()); | ||
//prints ["Sweets", "Groceries"] | ||
|
||
console.log(cart.getInfo()); | ||
/* prints | ||
{ | ||
totalPrice: 3 | ||
products: [{ | ||
name: "Salad", | ||
totalPrice: 1, | ||
quantity: 2 | ||
}, { | ||
name: "Shokolad Milka", | ||
totalPrice: 2, | ||
quantity: 1 | ||
}] | ||
} | ||
*/ | ||
|
||
cart.remove({name:"salad", productType: "Groceries", price: 0.5}) | ||
//throws: "salad" is not equal to "Salad" | ||
|
||
cart.remove({name:"Salad", productType: "Groceries", price: 0.5}) | ||
console.log(cart.getInfo()); | ||
/* prints | ||
{ | ||
totalPrice: 2.5 | ||
products: [{ | ||
name: "Salad", | ||
totalPrice: 0.5, | ||
quantity: 1 | ||
}, { | ||
name: "Shokolad Milka", | ||
totalPrice: 2, | ||
quantity: 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,15 @@ | ||
{ | ||
"name": "test-driven-hw-mocha", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha -R spec ./tests" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"chai": "^3.5.0", | ||
"mocha": "^3.0.2" | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
Practical Exams/10-September-2016/task-1/task/.vscode/launch.json
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,46 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Launch", | ||
"type": "node", | ||
"request": "launch", | ||
"program": "${workspaceRoot}/app.js", | ||
"stopOnEntry": false, | ||
"args": [], | ||
"cwd": "${workspaceRoot}", | ||
"preLaunchTask": null, | ||
"runtimeExecutable": null, | ||
"runtimeArgs": [ | ||
"--nolazy" | ||
], | ||
"env": { | ||
"NODE_ENV": "development" | ||
}, | ||
"externalConsole": false, | ||
"sourceMaps": false, | ||
"outDir": null | ||
}, | ||
{ | ||
"name": "Attach", | ||
"type": "node", | ||
"request": "attach", | ||
"port": 5858, | ||
"address": "localhost", | ||
"restart": false, | ||
"sourceMaps": false, | ||
"outDir": null, | ||
"localRoot": "${workspaceRoot}", | ||
"remoteRoot": null | ||
}, | ||
{ | ||
"name": "Attach to Process", | ||
"type": "node", | ||
"request": "attach", | ||
"processId": "${command.PickProcess}", | ||
"port": 5858, | ||
"sourceMaps": false, | ||
"outDir": null | ||
} | ||
] | ||
} |
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,26 @@ | ||
/* globals require console*/ | ||
|
||
"use strict"; | ||
|
||
let { ShoppingCart, Product } = require("./task-2")(); | ||
|
||
let sc = new ShoppingCart(); | ||
|
||
|
||
let p = new Product("food", "Bread", "1"); | ||
|
||
sc.add(new Product("beverages", "Whiskey", "25")); | ||
|
||
sc.add(p); | ||
sc.add(p); | ||
sc.add(p); | ||
sc.add(p); | ||
|
||
console.log(sc.showCost()); | ||
console.log(sc.showProductTypes()); | ||
|
||
console.log(sc.getInfo()); | ||
|
||
sc.remove(p); | ||
|
||
console.log(sc.getInfo()); |
82 changes: 82 additions & 0 deletions
82
Practical Exams/10-September-2016/task-1/task/solution-koce.js
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,82 @@ | ||
function solve() { | ||
'use strict' | ||
|
||
class Product { | ||
constructor(productType, name, price) { | ||
this.productType = productType | ||
this.name = name | ||
this.price = Number(price) | ||
} | ||
equals(otherProduct) { | ||
return (this.name === otherProduct.name) && | ||
(this.productType === otherProduct.productType) && | ||
(this.price === otherProduct.price) | ||
} | ||
} | ||
|
||
class ShoppingCart { | ||
constructor() { | ||
this.products = [] | ||
} | ||
|
||
add(product) { | ||
this.products.push(product) | ||
return this | ||
} | ||
|
||
remove(product) { | ||
const indexOfProduct = this.products.findIndex(p => p.equals(product)) | ||
|
||
if (indexOfProduct === -1) { | ||
throw new Error('Cannot remove product that is not in the shopping cart!') | ||
} | ||
|
||
return this.products.splice(indexOfProduct, 1) | ||
} | ||
|
||
showCost() { | ||
return this.products.reduce((partialCost, currentProduct) => partialCost + currentProduct.price, 0) | ||
} | ||
|
||
showProductTypes() { | ||
const typesMap = {} | ||
|
||
for(const prod of this.products) { | ||
typesMap[prod.productType] = true | ||
} | ||
|
||
return Object.keys(typesMap).sort((first, second) => first.localeCompare(second)) | ||
} | ||
|
||
getInfo() { | ||
const productMap = {} | ||
|
||
for (const p of this.products) { | ||
if (!productMap[p.name]) { | ||
productMap[p.name] = { | ||
name: p.name, | ||
totalPrice: 0, | ||
quantity: 0 | ||
} | ||
} | ||
|
||
productMap[p.name].totalPrice += p.price | ||
productMap[p.name].quantity += 1 | ||
} | ||
|
||
const products = Object.keys(productMap).map(groupName => productMap[groupName]), | ||
totalPrice = products.reduce((partialPrice, currentProductGroup) => partialPrice + currentProductGroup.totalPrice, 0) | ||
|
||
return { | ||
products, | ||
totalPrice | ||
} | ||
} | ||
} | ||
return { | ||
Product, | ||
ShoppingCart | ||
} | ||
} | ||
|
||
module.exports = solve |
Oops, something went wrong.