Skip to content

Instantly share code, notes, and snippets.

@freewayz
Created January 4, 2016 18:43
Show Gist options
  • Save freewayz/6fc322f164814d8472d5 to your computer and use it in GitHub Desktop.
Save freewayz/6fc322f164814d8472d5 to your computer and use it in GitHub Desktop.
Simple OOP concept in javascript
//how i started understanding my javascript oop concept based on ES5
//welcome comment and and
var Bank = function(name,acct_bal){
this.name = name; this.acct_bal = acct_bal;
}
Bank.prototype.deposit = function(amount){
this.acct_bal += amount;
console.log("Deposited =$" + amount + "\nYour balance is $" + this.acct_bal);
}
Bank.prototype.withdraw = function(amount){
this.acct_bal -= amount;
console.log("Debited =; $" + amount + "\nYour balance is $" + this.acct_bal);
}
function main(){
euroBank = new Bank("Peter", 2000);
var choice;
choice = parseInt(window.prompt("Enter 1 to Deposit\nEnter 2 to Withdraw"));
while(choice !== 0){
amount = parseInt(window.prompt("Enter Amount"));
if(choice === 1){
euroBank.deposit(amount);
}else if(choice == 2){
if(amount > this.acct_bal){
console.log("You cant withdraw more than the available balance");
console.log("Available Balance is " + this.acct_bal);
}else{
euroBank.withdraw(amount);
}
}else{
console.log("Wrong Choice Input");
}
choice = parseInt(window.prompt("Enter 1 to Deposit\nEnter 2 to Withdraw"));
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment