Last active
April 23, 2021 01:01
-
-
Save Dangeranger/b3c4af389169515d27bc2b2b0cf57663 to your computer and use it in GitHub Desktop.
Deli counter example
This file contains hidden or 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
// Deli Counter | |
// 1. take a ticket | |
// 2. check if our number is up | |
// 3. order from counter if ready | |
// 4. change the deli inventory on order | |
// 5. receive our order, from the counter | |
// 6. pay for the order | |
// 7. leave if paid | |
const readline = require("readline"); | |
const readlineInterface = readline.createInterface( | |
process.stdin, | |
process.stdout | |
); | |
function ask(questionText) { | |
return new Promise((resolve, reject) => { | |
readlineInterface.question(questionText, resolve); | |
}); | |
} | |
class Customer { | |
constructor(funds) { | |
this.funds = funds; | |
} | |
makeOrder(someOrderDescription) { | |
this.order = someOrderDescription | |
} | |
takeTicket(ticketNumber) { | |
this.ticketNumber = ticketNumber; | |
} | |
} | |
class Deli { | |
constructor(order, ticket) { | |
this.order = order; | |
this.ticket = ticket; | |
} | |
take(thing) { | |
if (thing === "ticket") { | |
this.ticket = 3; | |
console.log("Here's a ticket for you. Ticket#" + this.ticket); | |
return this.ticket; | |
} | |
if (thing === "order") { | |
this.order = "a bagel with cream cheese"; | |
console.log("here is your order, it is " + this.order); | |
return this.order; | |
} | |
} | |
leave(thing) { | |
if (thing === "deli") { | |
console.log("Thanks for visiting, please come again!"); | |
process.exit(); | |
} | |
} | |
} | |
async function openDeli() { | |
const theDeli = new Deli('', 1); | |
while (true) { | |
let response = await ask("Hello, how can I help you?\n"); | |
let parts = response.split(' '); // ["take", "ticket"] | |
if (response.startWith("take")) { | |
deli.take(parts[1]); | |
} else if (response.startsWith("leave")) { | |
deli.leave(parts[1]); | |
} | |
} | |
} | |
console.log("Starting up the deli counter:"); | |
openDeli(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment