How To Make ATM Machine in TypeScript
How To Make ATM Machine in TypeScript
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:
interface User {
id: number;
pin: number;
name: string;
accountNumber: number;
balance: number;
}
const createUser =() =>{
let users:User[] = [];
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",
});
if (user) {
console.log(`Welcome ${user.name}`);
atmFunc(user)
return;
}
console.log("Invalid pin");
};
// atm function
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)