cover-img

Object Oriented Programming with Real Life Examples

20 January, 2023

3

3

2

Refresher on Object-Oriented Programming

Object-oriented programming (OOP) is a programming paradigm that uses objects with properties and methods to represent real-world entities. In JavaScript, OOP can be achieved using the literal object notation, constructor functions, or classes.

Here are some examples of how OOP can be used to model real-world entities in JavaScript:

Modelling a car:


class Car {
constructor(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}

startEngine() {
console.log('Engine started.');
}

drive() {
console.log('Car is moving.');
}
}

let myCar = new Car('Honda', 'Civic', 2020);
console.log(myCar.make); // Honda
console.log(myCar.model); // Civic
console.log(myCar.year); // 2020
myCar.startEngine(); // Engine started.
myCar.drive(); // Car is moving.

In this example, the Car class is used to model a car object. It has properties such as make, model, and year, and methods such as startEngine and drive that represent the behaviour of a car.

Modeling a Bank Account:

class BankAccount {
constructor(name, balance) {
this._name = name;
this._balance = balance;
}
deposit(amount) {
this._balance += amount;
}
withdraw(amount) {
if (amount <= this._balance) {
this._balance -= amount;
} else {
console.log('Insufficient funds');
}
}
checkBalance() {
console.log(`Account balance: $${this._balance}`);
}
}

let account = new BankAccount('John Doe', 1000);
account.deposit(500);
account.checkBalance(); // Account balance: $1500
account.withdraw(200);
account.checkBalance(); // Account balance: $1300

This example uses the BankAccount class to model a bank account object. It has properties such as _name and _balance, and methods such as deposit, withdraw, and checkBalance that represent the behavior of a bank account. The _ before the name of the properties is a naming convention used to indicate that the property is private and should not be accessed directly.

A Shopping Cart:

class Item {
constructor(name, price) {
this.name = name;
this.price = price;
}
}

class ShoppingCart {
constructor() {
this.items = [];
}
addItem(item) {
this.items.push(item);
}
removeItem(item) {
let index = this.items.indexOf(item);
if (index !== -1) {
this.items.splice(index, 1);
}
}
getTotal() {
let total = 0;
for (let item of this.items) {
total += item.price;
}
return total;
}
}

let item1 = new Item("Shirt", 20);
let item2 = new Item("Pants", 30);
let item3 = new Item("Shoes", 40);

let cart = new ShoppingCart();
cart.addItem(item1);
cart.addItem(item2);
cart.addItem(item3);
console.log(cart.getTotal()); // 90
cart.removeItem(item2);
console.log(cart.getTotal()); // 60

In this example, the Item class models an item with a name and a price, and the ShoppingCart class models a shopping cart containing a list of items. The shopping cart has methods to add, remove, and calculate the total cost of all items in the cart. The addItem method pushes an item to the items array, and the removeItem method removes an item from the items array if the item is present in the array. The getTotal method iterates through the items array and returns the total cost of all items.

3

3

2

ShowwcaseHQ
Showwcase is where developers hang out and find new opportunities together as a community

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2024. Showcase Creators Inc. All rights reserved.