Skip to content

Instantly share code, notes, and snippets.

@mxdi9i7
Created June 30, 2022 23:36
Show Gist options
  • Save mxdi9i7/e792028088ffe242c30adc9bf5e20b4b to your computer and use it in GitHub Desktop.
Save mxdi9i7/e792028088ffe242c30adc9bf5e20b4b to your computer and use it in GitHub Desktop.
Class OOP exercise JS
// const student = {
// firstName: 'John',
// lastName: 'Doe',
// age: 25,
// };
// class Student {
// constructor(firstName, lastName, age, teacherName) {
// this.firstName = firstName;
// this.lastName = lastName;
// this.age = age;
// this.teachers = [teacherName];
// }
// addTeacher(teacherName) {
// this.teachers.push(teacherName);
// }
// findAllTeachers() {
// return this.teachers;
// }
// getFullName() {
// return `${this.firstName} ${this.lastName}`;
// }
// }
// const student1 = new Student('John', 'Doe', 25, 'Peter Smith');
// const student2 = new Student('Jane', 'Doe', 23, 'Zack Smith');
// student1.addTeacher('Zack Smith');
// console.log(student1.teachers);
// console.log(student1.findAllTeachers());
// console.log(student1.getFullName());
// Design a bank account class
// balance, deposit, withdraw, getBalance
// @ts-nocheck
// class BankAccount {
// constructor(balance) {
// this.balance = balance;
// this.history = [];
// }
// deposit(amount) {
// this.balance += amount;
// this.history.push(
// `Deposit of $${amount}, remaining balance: $${this.balance}`,
// );
// }
// withdraw(amount) {
// this.balance -= amount;
// this.history.push(
// `Withdraw of $${amount}, remaining balance: $${this.balance}`,
// );
// }
// getBalance() {
// return this.balance;
// }
// getHistory() {
// return this.history;
// }
// }
// const account = new BankAccount(100);
// account.deposit(50);
// account.withdraw(10);
// console.log(account.getBalance());
// console.log(account.getHistory());
// Shopping cart, add item, remove item by title, get total
// every item: {name, price, quantity}
// Keep an items property in class
class ShoppingCart {
constructor() {
this.items = [];
}
addItem(item) {
this.items.push(item);
}
removeItem(name) {
const itemIndex = this.items.findIndex((item) => item.name === name);
if (itemIndex > -1) {
this.items.splice(itemIndex, 1);
}
}
getItems() {
return this.items.map((item) => {
return {
name: item.name,
quantity: item.quantity,
};
});
}
getTotal() {
return this.items.reduce(
(acc, item) => acc + item.price * item.quantity,
0,
);
}
getReceipt() {
return this.items
.map((item) => {
return `${item.name} * ${item.quantity}: $${
item.price * item.quantity
}`;
})
.join('\n');
}
clearCart() {
this.items = [];
}
}
// const cart = new ShoppingCart();
// cart.addItem({
// name: 'Bread',
// price: 1.99,
// quantity: 2,
// });
// cart.addItem({
// name: 'Milk',
// price: 2.99,
// quantity: 1,
// });
// cart.addItem({
// name: 'Eggs',
// price: 3.99,
// quantity: 3,
// });
// console.log(cart.getTotal());
// cart.removeItem('Bread');
// console.log(cart.getTotal());
// console.log(cart.getItems());
// console.log(cart.getReceipt());
// Stock Exchange Account,
// properties
// - cash = int,
// - portfolio = { symbol, quantity, purchasePrice }
// methods
// - buyStock(symbol, quantity)
// - sellStock(symbol, quantity)
// - getPortfolioValue() - returns total value of portfolio
// - getPortfolio - returns list of stock symbols that you own.;
const stocks = {
GOOG: {
symbol: 'GOOG',
price: 100,
},
AAPL: {
symbol: 'AAPL',
price: 200,
},
MSFT: {
symbol: 'MSFT',
price: 300,
},
FB: {
symbol: 'FB',
price: 400,
},
};
class StockExchangeAccount {
constructor(cash) {
this.cash = cash;
this.portfolio = [];
}
buyStock(symbol, quantity) {
const stock = stocks[symbol];
// const stock = Object.values(stocks).find((item) => item.symbol === symbol);
if (stock) {
const { symbol, price } = stock;
const totalCost = price * quantity;
if (this.cash >= totalCost) {
const foundStockIndex = this.findStockIndex(symbol);
if (foundStockIndex > -1) {
this.portfolio[foundStockIndex].quantity += quantity;
} else {
this.portfolio.push({ symbol, quantity, purchasePrice: price });
}
this.cash -= totalCost;
} else {
console.log(
'Sorry you do not have enough cash to create this transaction',
);
}
} else {
console.log('Did not find the stock you are looking for');
}
}
sellStock(symbol, quantity) {
const foundStockIndex = this.findStockIndex(symbol);
const stock = this.portfolio[foundStockIndex];
if (foundStockIndex > -1 && stock.quantity >= quantity) {
stock.quantity -= quantity;
if (stock.quantity === 0) {
this.portfolio.splice(foundStockIndex, 1);
}
this.cash += quantity * stock.purchasePrice;
}
}
findStockIndex(symbol) {
return this.portfolio.findIndex((item) => item.symbol === symbol);
}
getPortfolioValue() {
return this.portfolio.reduce(
(acc, item) => acc + item.quantity * item.purchasePrice,
0,
);
}
getPortfolio() {
return this.portfolio.map((item) => item.symbol);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment