Skip to main content

Command Palette

Search for a command to run...

Create Program Mini Coffe With Command Line: Node Js & Default Terminal Ubuntu

By Ahmad Afan Shobari

Updated
5 min read
Create Program Mini Coffe With Command Line: Node Js & Default Terminal Ubuntu
A
I'm a Programmer

Assalamualaikum, hello guys welcome back to my blog today we are learning about program mini coffe with command line , in this tutorial i use: terminal ubuntu and node js, let’s me show about tutorial

1. Create Ingredients

first we need for folder coffe shop, you can typing this

--create & open folder--
mkdir coffe-shop
cd coffe-shop

--initial--
npm init

then you can use npm init for give you package.json

then you can edit in folder package.json

then you can create new file in folder coffe-shop

2. Create Service

In this segmen we will create code every file in direcotory

import promptSync from "prompt-sync";
import makeCoffee from "./menu/create-coffe.js";
import makeTea from "./menu/create-tea.js"
import makeFriedRice from "./menu/create-fried-rice.js"
import makeGrilledChicken from "./menu/create-grilled-chiken.js"

const prompt = promptSync({ sigint: true });

function showMenu() {
  console.log("\n=== WARUNG SERVICES ===");
  console.log("1) Make Coffee");
  console.log("2) Make Tea        (please create the module)");
  console.log("3) Fried Rice      (please create the module)");
  console.log("4) Grilled Chicken (please create the module)");
  console.log("0) Close Shop");
}

function askNumber(label, def = 0) {
  const txt = prompt(`${label} (default ${def}): `).trim();
  const num = txt === "" ? def : Number(txt);
  return Number.isNaN(num) ? def : num;
}

function askBoolean(label, def = false) {
  const txt = prompt(`${label} (y/n, default ${def ? "y" : "n"}): `)
    .trim()
    .toLowerCase();
  if (txt === "") return def;
  return txt === "y" || txt === "yes";
}

function coffeeService() {
  console.log("\n— Coffee Menu —");
  const cups = askNumber("How many cups", 1);
  const sugar = askNumber("Sugar (tsp per cup)", 1);
  const withMilk = askBoolean("With milk?", false);
  const result = makeCoffee({ cups, sugar, withMilk });
  console.log("\n" + result + "\n");
}

function teaService() {
  console.log("\n— Tea Menu —");
  const cups = askNumber("How many cups", 1);
  const sugar = askNumber("Sugar (tsp per cup)", 1);
  const teaBag = askBoolean("add Tea Bag?", false);
  const lemonSqueeze = askBoolean("add lemonsqueeze?", false);
  console.log(cups, sugar, teaBag, lemonSqueeze);
  const result = makeTea({ cups, sugar, teaBag, lemonSqueeze });
  console.log("\n" + result + "\n");
}

function friedRiceService() {
  console.log("\n— Fried Chicken Menu —");
  const servings = askNumber("How many servings", 1);
  const spicy = askNumber("spicy level (0-5)", 1);
  const egg = askBoolean("with egg?", false);
  const chicken = askBoolean("with chicken?", false);
  console.log(servings, spicy, egg, chicken);
  const result = makeFriedRice({servings, spicy, egg, chicken});
  console.log("\n" + result + "\n");
}

function grilledChickenService() {
  console.log("\n— Grilled Chicken Menu —");
  const servings = askNumber("How many servings", 1);
  const sweet = askNumber("sweet level (0-5)", 1);
  const spice = askBoolean("with spice?", false);
  const grilling = askNumber("grilling time?", 1);
  const result = makeGrilledChicken({servings, sweet, spice, grilling});
  console.log("\n" + result + "\n");
}

function serviceLoop() {
  while (true) {
    showMenu();
    const choice = prompt("Choose a menu item (number): ").trim();

    switch (choice) {
      case "1":
        coffeeService();
        break;

      case "2":
        teaService()
        break;

      case "3":
        friedRiceService()
        break;

      case "4":
        grilledChickenService()

      case "0":
        console.log("\nShop closed. Thank you.\n");
        return;

      default:
        console.log("Invalid choice. Please try again.\n");
    }
  }
}

serviceLoop();

this a main program for mini caffe

3. Create File Menu

then you can create code in every file about menu

3.1 Menu Create Coffe

const TOTAL_STIRS = 18;

export default function makeCoffee(options = {}) {
  const { cups = 1, sugar = 1, withMilk = false } = options;
console.log(options)
  // Simple validation
  if (cups <= 0) return "Number of cups must be at least 1.";
  if (sugar < 0) return "Sugar cannot be negative.";

  const steps = [];
  steps.push(`Preparing ${cups} cups of coffee.`);
  steps.push(`Add coffee grounds and ${sugar} tsp of sugar per cup.`);
  steps.push("Pour hot water.");
  if (withMilk) {
    steps.push("Add milk to taste.");
  }

  // Stirring simulation: show stir #1 to #18
  for (let i = 1; i <= TOTAL_STIRS; i++) {
    steps.push(`Stirring... (${i} of ${TOTAL_STIRS})`);
  }

  steps.push("Coffee is ready to be served.");
  return steps.join("\n");
}

3.2 Menu Create Tea

const TOTAL_STIRS = 18;

export default function makeTea(options = {}) {
    console.log(options)
  const { cups = 1, sugar = 1, teaBag = false , lemonSqueeze = false } = options;

  // Simple validation
  if (cups <= 0) return "Number of cups must be at least 1.";
  if (sugar < 0) return "Sugar cannot be negative.";

  const steps = [];
  steps.push(`Preparing ${cups} cups of tea.`);
  steps.push(`Add tea grounds and ${sugar} tsp of sugar per cup.`);
  steps.push("Pour hot water.");
  if (lemonSqueeze){
      steps.push("add lemon to teste.");
    }
    if (teaBag) {
      steps.push("Add tea bag.");
    }

  steps.push("tea is ready to be served.");
  return steps.join("\n");
}

3.3 Menu Create Fried Rice

const TOTAL_FRYING = 12;

export default function makeFriedRice(options = {}) {
  const { servings = 1, spicy = 1, egg = false , chicken = false} = options;
console.log(options)
  // Simple validation
  if (servings < 0) return "Number of serving must be at least 1.";
  if (spicy < 0) return "spicy cannot be negative.";

  const steps = [];
  steps.push(`Preparing ${servings} servings of friedchiken.`);
  steps.push(`add chili peppers according to taste ${spicy}`);
  steps.push("starting cooking.");
  if (egg) {
    steps.push("Add egg to taste.");
  }
  if (chicken) {
    steps.push("Add chicken to taste.");
  }

  // Stirring simulation: show stir #1 to #18
  for (let i = 1; i <= TOTAL_FRYING; i++) {
    steps.push(`frying... (${i} of ${TOTAL_FRYING})`);
  }

  steps.push("fried rice is ready to be served.");
  return steps.join("\n");
}

3.4 Menu Create Grilled Chicken


export default function makeGrilledChicken(options = {}) {
  const { servings = 1, sweet = 1, spice = false , grilling = 1} = options;
console.log(options)
  // Simple validation
  if (servings < 0) return "Number of serving must be at least 1.";
  if (sweet < 0) return "sweet cannot be negative.";

  const steps = [];
  steps.push(`Preparing ${servings} servings of friedchiken.`);
  steps.push(`add sweet peppers according to taste ${sweet}`);
  steps.push("starting cooking.");
  if (spice) {
    steps.push("Add spice to taste.");
  }

  // Stirring simulation: show stir #1 to #18
  for (let i = 1; i <= grilling; i++) {
    steps.push(`frying... (${i} of ${grilling})`);
  }

  steps.push("grilling chicken is ready to be served.");
  return steps.join("\n");
}

4. Running Code

now you can running this project with node service.js let’s go to practic

node service

this ui for running this code

you can try make tea with typing 2

you will be asked about the menu you ordered, then process running

5. Closing

thanks for you watching and see you next day……..

M
MUHAMMAD7mo ago

NICE!