Last active
August 19, 2019 22:58
-
-
Save Lusamine/53ca8526dd23b2582cb5a08b878fb5ed to your computer and use it in GitHub Desktop.
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
/* | |
* The Pokemart | |
* | |
* This assignment has you working with Objects and Functions/Methods. | |
* The goal is to create code to represent a pokemart that sells items, and then | |
* simulate a player purchasing some items from the pokemart. | |
* | |
* Requirements [READ CAREFULLY]: | |
* The pokemart must be an Object containing at least 10 items. You can pick what | |
* items are sold and at what price. | |
* The player must have a predetermined amount of money, and must buy at least 20 items, | |
* at least 3 of which must be unique (duplicates are allowed, make sure to give the player enough money). | |
* Log how much money the player has to the console when the script starts. | |
* Use an object to keep track of what is currently in the player's cart. Use functions/methods to add items to the cart. | |
* Do NOT allow a player to have a negative number of an item in their cart. | |
* Log when an item is added to the cart to the console, including the quantity. | |
* The pokemart must have a checkout method to calculate how much money the player pays for the items. | |
* The checkout method must log: | |
* - A line for each item being purchased including the name of the item, quantity, and cost. | |
* - A line for the total cost of the cart | |
* - A line either saying how much money the player has left OR | |
* - A line stating the player cannot afford the purchase | |
* The checkout method must not allow a purchase that the player cannot afford. | |
* OPTIONAL BONUS: Add an extra item that cannot be purchased but it can be obtained in a simaler way to premier balls in the pokemon games. | |
* - If you do this, make sure this item cannot be added to the cart, and note how many were given (if > 0) after sucessfully checking out. | |
* OPTIONAL BONUS 2: Make it so that the player can remove items from their cart. This can be a new function/method or built into | |
* - the one for adding items to the cart (maybe a negative value indicates a removal?) | |
* OPTIONAL BONUS 3: Empty the player's cart after they checkout (in the checkout function), and have them make a second purchase. | |
* - (Second purchase only needs a total of 1 item and 1 unique item, unlike the first). | |
* | |
* HINTS: | |
* DRY (Don't repeat yourself) | |
* Try to make your code industrial strength. This means verify function arguments before doing what was asked. | |
* For example: When adding to cart, make sure the requested item is a valid item & can be purchased, also make sure | |
* the player will not have a negative number of that item in their cart after (or just block negative values). | |
* You can use the return keyword to terminate a function early, even if the return value is unimportant to the function. | |
* Use Math.floor(NUMBER) to round NUMBER down to the nearest whole number (returns an integer). | |
*/ | |
'use strict'; | |
let pokemart = { | |
pokeball: 200, | |
greatball: 600, | |
ultraball: 800, | |
potion: 200, | |
superpotion: 700, | |
hyperpotion: 1500, | |
maxpotion: 2500, | |
fullrestore: 3000, | |
revive: 2000, | |
antidote: 200, | |
paralyzeheal: 300, | |
awakening: 100, | |
burnheal: 300, | |
iceheal: 100, | |
fullheal: 400, | |
}; | |
function calculateCost(objCost = 0, numObj = 0) { | |
return (objCost * numObj); | |
} | |
// Starts out empty. | |
let cart = {}; | |
// Adds a certain amount of an item to the cart, if the user has enough money. | |
function addToCart (item, quantity) { | |
let totalCost = calculateCost(pokemart[item], quantity); | |
if (totalCost + cartValue > startingWealth) { | |
console.log(`You wouldn't have enough to buy that if you added it to your cart.`); | |
} else { | |
cartValue += totalCost; | |
console.log(`You added ${quantity} ${item}${addPlural(quantity)} from your cart.`); | |
console.log(`You have ${startingWealth - cartValue} Pokédollar${addPlural(startingWealth - cartValue)} to spend.\n`); | |
// If it's already in the cart, add to what we have. | |
if (cart[item] == undefined) { | |
cart[item] = quantity; | |
} else { | |
cart[item] += quantity; | |
} | |
} | |
} | |
function showCart () { | |
let found = false; | |
for (let property in cart) { | |
if (cart[property] > 0) { | |
console.log(`${property.padStart(15, ' ')} x ` + cart[property]); | |
found = true; | |
} | |
} | |
if (!found) | |
console.log(` Your cart is empty.`); | |
} | |
/* Removes a certain amount or all of an item from the cart. If we made it here, | |
* we already verified that they are removing a real item and correct quantity. | |
*/ | |
function removeFromCart (item, quantity) { | |
let totalCost = calculateCost(pokemart[item], quantity); | |
cartValue -= totalCost; | |
console.log(`You removed ${quantity} ${item}${addPlural(quantity)} from your cart.`); | |
console.log(`You have ${startingWealth - cartValue} Pokédollar${addPlural(startingWealth - cartValue)} to spend.\n`); | |
// If it hits 0, then delete it. | |
if (cart[item] == quantity) { | |
delete cart[item]; | |
} else { | |
cart[item] -= quantity; | |
} | |
} | |
function addPlural(amount) { | |
if (amount === 1) | |
return ""; | |
else | |
return "s"; | |
} | |
// Starting amount of money. | |
let startingWealth = 20000; | |
let cartValue = 0; | |
let readline = require('readline'); | |
let rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}) | |
// Lists the options available. | |
function showPrompt() { | |
console.log(` list: lists all items for sale` + | |
`\n buy: adds items from the Mart to your cart. e.g. "buy 2 potion"` + | |
`\n remove: removes items from your cart. e.g. "remove 2 potion" or "remove all potion"` + | |
`\n cart: shows the items in your cart` + | |
`\n wealth: shows how much money you have` + | |
`\n checkout: buys everything in your cart` + | |
`\n done: closes out of the Poke Mart\n\nWhat would you like to do?`); | |
} | |
console.log(`Welcome to the Poke Mart! May I help you?`); | |
console.log(`You have ${startingWealth} Pokédollar${addPlural(startingWealth)} to spend.`); | |
console.log(`Your cart is currently worth ${cartValue} Pokédollar${addPlural(cartValue)}.`); | |
showPrompt(); | |
rl.on('line', function (line) { | |
/* Break up what the entered word for word. This means the str[0] will | |
* be the command they used, and str[1] and str[2] will be arguments | |
* for buying and selling items. | |
*/ | |
let str = line.split(" "); | |
let quantity = parseInt(str[1]); | |
// 1) List all objects for sale. | |
if (str[0] === "list") { | |
console.log(`This is what we have for sale:`); | |
for (let property in pokemart) { | |
if (pokemart[property] >= 0) | |
console.log(`${property.padStart(15, ' ')}: ` + pokemart[property]); | |
} | |
console.log(`\nWhat would you like to do?`); | |
// 2) Add an object to the cart if we have enough money. | |
} else if (str[0] === "buy") { | |
// Go through all the conditions where they failed their syntax. | |
if (pokemart[str[2]] == undefined) { | |
console.log(`We don't have any of that for sale. Try checking our "list".`); | |
} else if (isNaN(quantity)) { | |
console.log(`You have to enter a number of that item to add to your cart!`); | |
} else if (quantity < 1) { | |
console.log(`You can't add less than 1 of that.`); | |
} else { | |
// If we made it to here, we try to add it to the cart. | |
addToCart(str[2], quantity); | |
} | |
// 3) Remove an item from the cart. | |
} else if (str[0] === "remove") { | |
// Go through all the conditions where they failed their syntax. | |
if (cart[str[2]] == undefined) { | |
console.log(`You don't have that in your cart. Try checking your "cart".`); | |
} else { | |
/* If they used "all" for the quantity, set it to the amount they | |
* have in their cart right now. */ | |
if (str[1] === "all") | |
quantity = cart[str[2]]; | |
if (isNaN(quantity)) { | |
console.log(`You have to enter a number of that item to remove from your cart!`); | |
} else if (quantity > cart[str[2]]) { | |
console.log(`You can't remove more of that than you have.`); | |
} else if (quantity < 1) { | |
console.log(`You can't remove less than 1 of that.`); | |
} else { | |
// If we made it to here, we try to remove it from the cart. | |
removeFromCart(str[2], quantity); | |
} | |
} | |
// 4) List current items in the cart. | |
} else if (str[0] === "cart") { | |
console.log(`This is what you have in your cart:`); | |
showCart(); | |
console.log(`Your cart is currently worth ${cartValue} Pokédollar${addPlural(cartValue)}.\n`); | |
// 5) Check how much money we have. | |
} else if (str[0] === "wealth") { | |
console.log(`You have ${startingWealth} Pokédollar${addPlural(startingWealth)} to spend.`); | |
console.log(`Your cart is currently worth ${cartValue} Pokédollar${addPlural(cartValue)}.`); | |
// 6) Checkout items in cart. | |
} else if (str[0] === "checkout") { | |
if (cartValue <= 0) { | |
console.log(`You have nothing to check out.`); | |
} else { | |
showCart(); | |
console.log(`You spent a total of ${cartValue} Pokédollar${addPlural(cartValue)}. Thanks for your purchase!`); | |
// They get 1 premierball for every 10 pokeballs. | |
if (cart["pokeball"] >= 10) { | |
let bonusBalls = Math.floor(cart["pokeball"] / 10); | |
console.log(`You also get ${bonusBalls} premierball${addPlural(bonusBalls)} as an added bonus.`); | |
} | |
// Spend their money and empty their cart. | |
startingWealth -= cartValue; | |
cartValue = 0; | |
cart = {}; | |
console.log(`You have ${startingWealth} Pokédollar${addPlural(startingWealth)} left to spend.\n`); | |
} | |
// 7) Finish up and leave. | |
} else if (str[0] === "done") { | |
console.log(`Please come again!`); | |
rl.close(); | |
// They entered a command we don't understand. | |
} else { | |
console.log(`I didn't quite catch that. How can I help you?`); | |
showPrompt(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment