Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions oop/code/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

## Bank transfers in procedural and object-oriented programming
* [Procedural programming](../code/procedural_transfer.py)
* [Object-oriented programming](oop/src/main/java/com/scaler/lld/basics/OopBankAccount.java)
* [Unit test](oop/src/test/java/com/scaler/lld/basics/OopBankAccountTest.java)
* [Object-oriented programming](java/src/main/java/com/scaler/lld/basics/OopBankAccount.java)
* [Unit test](java/src/test/java/com/scaler/lld/basics/OopBankAccountTest.java)
* [Python OOP Code](python/main.py)
* [Golang Code](go/basic/main.go)

## Object-oriented programming
### Encapsulation
Expand Down
56 changes: 56 additions & 0 deletions oop/code/go/basic/bank/bank.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package bank

import "fmt"

type BankAccount struct {
AccountNumber int64
Name string
Balance float64
}

type BankAccountOps interface {
GetAccountNumber() (int64, error)
GetName() (string, error)
GetBalance() (float64, error)
Deposit(amount float64) error
Withdraw(amount float64) error
Transfer(destiantion *BankAccount, amount float64) error
PrintBalance()
}

func NewBankAccount(number int64, name string, balance float64) *BankAccount {
return &BankAccount{AccountNumber: number, Name: name, Balance: balance}
}

func (b *BankAccount) GetAccountNumber() (int64, error) {
return b.AccountNumber, nil
}

func (b *BankAccount) GetName() (string, error) {
return b.Name, nil
}

func (b *BankAccount) GetBalance() (float64, error) {
return b.Balance, nil
}

func (b *BankAccount) Deposit(amount float64) error {
b.Balance += amount
return nil
}

func (b *BankAccount) Withdraw(amount float64) error {
b.Balance -= amount
return nil
}

func (b *BankAccount) Transfer(destiantion *BankAccount, amount float64) error {
b.Withdraw(amount)
destiantion.Deposit(amount)

return nil
}

func (b *BankAccount) PrintBalance() {
fmt.Println(b.AccountNumber, b.Name, b.Balance)
}
53 changes: 53 additions & 0 deletions oop/code/go/basic/bird/bird.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package bird

import "fmt"

type Bird struct {
weight float64
colour string
size float64
}

func NewBird(weight, size float64, colour string) *Bird {
return &Bird{
weight: weight,
colour: colour,
size: size,
}
}

type FlyingBehaviour interface {
Fly()
}

type SwimmingBehaviour interface {
Swim()
}

type Eagle struct {
*Bird
}

func (e *Eagle) Fly() {
fmt.Println("Eagle is flying")
}

type Penguin struct {
*Bird
}

func (p *Penguin) Swim() {
fmt.Println("Penguin is swimming")
}

type Duck struct {
*Bird
}

func (d *Duck) Fly() {
fmt.Println("Duck is flying")
}

func (d *Duck) Swim() {
fmt.Println("Duck is swimming")
}
3 changes: 3 additions & 0 deletions oop/code/go/basic/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module basic

go 1.22.0
21 changes: 21 additions & 0 deletions oop/code/go/basic/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"basic/bird"
)

func main() {
bird1 := bird.NewBird(6.5, 2.1, "brown")
eagle := &bird.Eagle{Bird: bird1}

bird2 := bird.NewBird(15.0, 1.2, "black and white")
penguin := &bird.Penguin{Bird: bird2}

bird3 := bird.NewBird(2.5, 0.8, "white")
duck := &bird.Duck{Bird: bird3}

eagle.Fly()
penguin.Swim()
duck.Fly()
duck.Swim()
}
File renamed without changes.
2 changes: 1 addition & 1 deletion oop/code/python_code/main.py → oop/code/python/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def main():

#oop - inheritance

sam = Student("sam", "[email protected]", 25, "khulri", "Oct", 90, StudentStatus.ACTIVE)
sam = Student("sam", "[email protected]", "batch1", 90)
sam.print_details()

# oop - inheritance and polymorphism
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,4 @@ def get_name(self) -> str:
return self.__name

def print_details(self) -> None:
print("Print with no args")

def print_details(self, title: str) -> None:
print("\n In User:", title, self.get_name())
print("Print with no args")
Binary file modified os/code/os/target/classes/com/scaler/App.class
Binary file not shown.