Object Oriented Programming CLI:

Object Oriented Programming CLI:

Let's break down and explain the code step by step:

?

1. Importing Modules

import inquirer from 'inquirer';

import chalk from 'chalk';

?

inquirer: This module is used for creating interactive command-line interfaces.

chalk: This module is used for styling the terminal string output with colors.

?

2. BankAccount Class

class BankAccount {

??? accountNumber: number;

??? balance: number;

?

??? constructor(accountNumber: number, balance: number) {

??????? this.accountNumber = accountNumber;

??????? this.balance = balance;

??? }

?

??? withdraw(amount: number): void {

??????? if (this.balance >= amount) {

??????????? this.balance -= amount;

??????????? console.log(chalk.green(`Withdrawal of $${amount} successful. Remaining balance: $${this.balance}`));

??????? } else {

??????????? console.log(chalk.red("Insufficient balance."));

??????? }

??? }

?

??? deposit(amount: number): void {

??????? if (amount > 100) {

??????????? amount -= 1; // $1 fee charged if more than $100 is deposited

??????? }

??????? this.balance += amount;

??????? console.log(chalk.green(`Deposit of $${amount} successful. Remaining balance: $${this.balance}`));

??? }

?

??? checkBalance(): void {

??????? console.log(chalk.blue(`Current balance: $${this.balance}`));

??? }

}

? BankAccount Class: Represents a bank account with an account number and balance.

? constructor: Initializes the account number and balance.

? withdraw: Deducts the specified amount from the balance if sufficient funds are available.

? deposit: Adds the specified amount to the balance, deducting a $1 fee if the amount is greater than $100.

? checkBalance: Prints the current balance.

?

3. Customer Class

class Customer {

??? firstName: string;

??? lastName: string;

??? gender: string;

??? age: number;

??? mobileNumber: number;

??? account: BankAccount;

?

??? constructor(firstName: string, lastName: string, gender: string, age: number, mobileNumber: number, account: BankAccount) {

??????? this.firstName = firstName;

??????? this.lastName = lastName;

??????? this.gender = gender;

??????? this.age = age;

??????? this.mobileNumber = mobileNumber;

??????? this.account = account;

??? }

? Customer Class: Represents a bank customer with personal details and a linked bank account.

? constructor: Initializes the customer's personal details and assigns a bank account.

?

4. Creating Bank Accounts and Customers

??? const accounts: BankAccount[] = [

??? new BankAccount(1001, 500),

??? new BankAccount(1002, 1000),

??? new BankAccount(1003, 2000)

];

?

const customers: Customer[] = [

??? new Customer("Hassan", "Ali", "Male", 35, 5463767871, accounts[0]),

??? new Customer("Salma", "Asad", "Female", 24, 5487134256, accounts[1]),

??? new Customer("Ramsha", "Asghar", "Female", 28, 6767827871, accounts[2])

];

?

accounts: An array of BankAccount instances with predefined account numbers and balances.

customers: An array of Customer instances linked to the corresponding bank accounts.

?

5. Service Function

async function service(): Promise<void> {

??? while (true) {

??????? const { welcomeOption } = await inquirer.prompt<{ welcomeOption: string }>({

??????????? name: 'welcomeOption',

??????????? type: 'list',

??????????? message: 'Welcome to the Bank! Please select an option:',

??????????? choices: ['Enter Account Number', 'Exit']

??????? });

?

??????? if (welcomeOption === 'Exit') {

??????????? console.log(chalk.blue("Exiting bank program..."));

??????????? console.log(chalk.blue("\nThank you for using our bank service. Have a great day!"));

??????????? break;

??????? }

?

??????? const { accountNumber } = await inquirer.prompt<{ accountNumber: string }>({

??????????? name: 'accountNumber',

??????????? type: 'input',

??????????? message: 'Enter your account number:',

??????????? validate: value => !isNaN(Number(value)) && Number(value) > 0 ? true : 'Please enter a valid account number'

??????? });

?

??????? const customer = customers.find(customer => customer.account.accountNumber === Number(accountNumber));

?

??????? if (customer) {

??????????? console.log(chalk.yellow(`\nWelcome, ${customer.firstName} ${customer.lastName}!\n`));

?

??????????? const { operation } = await inquirer.prompt<{ operation: string }>([{

??????????????? name: 'operation',

??????????????? type: 'list',

??????????????? message: 'Select an operation:',

??????????????? choices: ['Deposit', 'Withdraw', 'Check Balance', 'Exit']

??????????? }]);

?

??????????? switch (operation) {

??????????????? case 'Deposit':

??????????????????? const { amount: depositAmount } = await inquirer.prompt<{ amount: string }>({

??????????????????????? name: 'amount',

??????????????????????? type: 'input',

??????????????????????? message: 'Enter the amount to deposit:',

??????????????????????? validate: value => !isNaN(Number(value)) && Number(value) > 0 ? true : 'Please enter a valid amount'

??????????????????? });

??????????????????? customer.account.deposit(parseFloat(depositAmount));

??????????????????? break;

?

??????????????? case 'Withdraw':

??????????????????? const { amount: withdrawAmount } = await inquirer.prompt<{ amount: string }>({

??????????????????????? name: 'amount',

??????????????????????? type: 'input',

??????????????????????? message: 'Enter the amount to withdraw:',

??????????????????????? validate: value => !isNaN(Number(value)) && Number(value) > 0 ? true : 'Please enter a valid amount'

??????????????????? });

??????????????????? customer.account.withdraw(parseFloat(withdrawAmount));

??????????????????? break;

?

??????????????? case 'Check Balance':

??????????????????? customer.account.checkBalance();

??????????????????? break;

?

??????????????? case 'Exit':

??????????????????? console.log(chalk.blue("Exiting bank program..."));

??????????????????? console.log(chalk.blue("\nThank you for using our bank service. Have a great day!"));

??????????????????? return;

??????????? }

??????? } else {

??????????? console.log(chalk.red("Invalid account number. Please try again."));

??????? }

??? }

}

?

service();

service Function: Handles user interaction with the banking system.

? Welcome Prompt: Presents the user with options to enter an account number or exit the program.

? Account Number Prompt: Prompts the user to enter their account number.

? Customer Validation: Checks if the entered account number corresponds to an existing customer.

? Operation Prompt: If a valid customer is found, presents options to deposit, withdraw, check balance, or exit.

? Deposit: Prompts for an amount to deposit and updates the account balance.

? Withdraw: Prompts for an amount to withdraw and updates the account balance.

? Check Balance: Displays the current balance.

? Exit: Exits the program and thanks the user.

?

The service function runs in a loop, continually prompting the user until they choose to exit.

My github link https://github.com/farida-bano/OOB-CLI-Project-10.git

my npx fbano1-oob-cli.

thank for reading .

要查看或添加评论,请登录

Farida Bano的更多文章

  • Python

    Python

    Python is a high-level, interpreted programming language developed by Guido van Rossum, a Dutch software developer, and…

  • DeepThink (R1): The Future of AI-Powered Data Analysis and Automation

    DeepThink (R1): The Future of AI-Powered Data Analysis and Automation

    1. Introduction to DeepThink (R1) What is DeepThink (R1)? DeepThink (R1) is an advanced AI-powered platform designed to…

  • What is Stripe

    What is Stripe

    What is Stripe? Stripe is a payment processing platform that allows businesses to accept payments online and in-person.…

  • ShipEngine

    ShipEngine

    What is ShipEngine? ShipEngine is a shipping API (Application Programming Interface) that helps developers integrate…

  • MOCK API

    MOCK API

    Mock API: A Simple and Effective Tool Mock API is a virtual interface that simulates the behavior of a real API. It…

  • Achievable Milestones for GIAIC 2025 Students to Master Next.js

    Achievable Milestones for GIAIC 2025 Students to Master Next.js

    1. Master the Basic Concepts of Next.

    2 条评论
  • Sanity

    Sanity

    Why Use Sanity with Next.js? A Complete Guide In today’s web development landscape, content management is crucial for…

  • Content Management System

    Content Management System

    A CMS (Content Management System) is a software application that allows you to create, manage, and modify digital…

    1 条评论
  • Serverless and Server-Based

    Serverless and Server-Based

    The main difference between serverless and server-based (or servered) architecture lies in how the infrastructure is…

  • Database

    Database

    What is a Database? A database is an organized collection of data, stored and accessed electronically. It enables…

社区洞察

其他会员也浏览了