0% found this document useful (0 votes)
38 views

How To Make ATM Machine in TypeScript

This document provides instructions for setting up an environment to create an ATM machine simulation in TypeScript. It includes steps to initialize files like tsconfig.json and package.json, install dependencies like inquirer and prompt-sync, and configure the target and module settings. The document then provides example TypeScript code to create a user interface, define user data, run an ATM simulation based on pin number authentication, and implement functions for withdrawals, deposits, balance checks, and exiting.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

How To Make ATM Machine in TypeScript

This document provides instructions for setting up an environment to create an ATM machine simulation in TypeScript. It includes steps to initialize files like tsconfig.json and package.json, install dependencies like inquirer and prompt-sync, and configure the target and module settings. The document then provides example TypeScript code to create a user interface, define user data, run an ATM simulation based on pin number authentication, and implement functions for withdrawals, deposits, balance checks, and exiting.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

How to make ATM Machine in TypeScript

1) Setting up the environment.


After making the file open it using cmd to do this first open the file you had made and click on this bar:-

After clicking on that you will see a screen then type ” code . ” on that and this file will open on your
code editor, this process is mainly used by professionals because sometimes when you run your code
on terminal it show’s some error’s which are exactly not.

To set up your environment first make a new file and name it “anything.ts” then open your terminal by
pressing “ Ctrl + J ” then get ready to set up your environment.

To add “ tsconfig.json ” file type “ tsc -init ” command on your terminal and then your “ tsconfig.json ”
file will be created after that type “ npm init -y ” and then your “package.json” file will be created.
Sometimes your “Package.json” file is not created well so first check it out your file should have this
code:

{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"daemon": "^1.1.0",
"init": "^0.1.2"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}

After doing that type “ npm I @types/node -D ” on your terminal for “ package-lock.json ” file and it
should look like:
{
"name": "Test",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"dependencies": {
"init": "^0.1.2"
},
"devDependencies": {
"@types/node": "^20.5.9"
}
},
"node_modules/@types/node": {
"version": "20.5.9",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.5.9.tgz",
"integrity": "sha512-
PcGNd//40kHAS3sTlzKB9C9XL4K0sTup8nbG5lC14kzEteTNuAFh9u5nA0o5TWnSG2r/JNPRXFVcHJIIe
RlmqQ==",
"dev": true
},
"node_modules/daemon": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/daemon/-/daemon-1.1.0.tgz",
"integrity": "sha512-
1vX9YVcP21gt12nSD3SQRC/uPU7fyA6M8qyClTBIFuiRWoylFn57PwXhjBAqRl085bZAje7sILhZU48qc
S9SWw==",
"engines": {
"node": ">= 0.8.0"
}
},
"node_modules/init": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/init/-/init-0.1.2.tgz",
"integrity": "sha512-
IvHUjULS2q+BXJdiu4FHkByh3+qSFmkOXQ2ItSfYTtkdUksQc0yNX6f1uDyokzRV71tjpFsFc3ckeYLJX
unTGw==",
"dependencies": {
"daemon": ">=0.3.0"
},
"engines": {
"node": ">=0.4.7"
}
}
}
}
After that type “ npm i inquirer ” on your terminal for inquirer but in this case no file will be downloaded
but your “ package.json ” & “ package-lock.json ” will be changed.
If you want to install chalk you can by typing “ npm i Chalk ” on your terminal and for Prompt Sync type {
Npm i prompt-sync
Npm i --save-dev @types/prompt-sync
Npm i --save-dev @types/node
}
And then your environment be be all set now go to “ tsconfig.json ” file and change "target":
"es2016",
To "target": "es2022",
This "module": "commonjs",
To "module": "NodeNext",
And finally this // "moduleResolution": "node10",
From second of the down side from: "module": "NodeNext",
And now change that to "moduleResolution": "NodeNext",
And then go to your “ package.json ” and type there:

Change this as of image and get ready to do code TypeScript.

2) Open your TypeScript file and paste this on that:


import inquirer from "inquirer";
import { faker } from "@faker-js/faker";

interface User {
id: number;
pin: number;
name: string;
accountNumber: number;
balance: number;
}
const createUser =() =>{
let users:User[] = [];

for (let i = 0; i < 5; i++) {


let user: User = {
id: i,
pin: 1000 + i,
name: faker.person.fullName(),
accountNumber: Math.floor(100000000 * Math.random() * 900000000),
balance: 1000000 * i,
};

users.push(user);
}

return users;
};
// atm machine
const atmMachine = async (users:User[]) => {
const res = await inquirer.prompt({
type: "number",
message: "Please Enter your Pin",
name: "pin",
});

const user = users.find((val) => val.pin == res.pin);

if (user) {
console.log(`Welcome ${user.name}`);
atmFunc(user)
return;
}
console.log("Invalid pin");
};

// atm function

const atmFunc = async(user:User)=>{


const ans = await inquirer.prompt({
type:"list",
name:"select",
message:"Please select one of these",
choices:["withdraw","deposit","balance","exit"]
})

if(ans.select == "withdraw"){
const amount = await inquirer.prompt({
type:"number",
message:"Please enter you withdraw amount",
name:"rupee"
})
if(amount.rupee > user.balance){
return console.log("Insufficient balance")
}
if(amount.rupee > 50000){
return console.log("You cannot Withdraw more than 50 000 rupees")
}
console.log(`withdraw amount: ${amount.rupee}`)
console.log(`Balance: ${user.balance-amount.rupee}`)
}
if(ans.select == "deposit"){
const deposit = await inquirer. prompt({
type:"number",
message:"Please enter your correct Deposit amount",
name:"rupee"
})
console.log(`Deposit amount: ${deposit.rupee}`)
console.log(`Total Balance : ${user.balance + deposit.rupee} `)
}
if(ans.select == "balance"){
console.log(`Balance: ${user.balance}`)
}
if(ans.select == "exit"){
console.log("Thanks for using our atm Bye!!!")
}
}
const users = createUser()
atmMachine(users)

You might also like