Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
deepcloudlabs committed Jul 19, 2022
1 parent feb2eed commit 04bbfef
Show file tree
Hide file tree
Showing 12 changed files with 211 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.example.banking.application;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

import com.example.banking.domain.Account;
import com.example.banking.domain.CheckingAccount;

public class StudyPolymorphism {

public static void main(String[] args) {
Account account ;

if (ThreadLocalRandom.current().nextBoolean()) {
account = new Account("tr1", 10_000);
} else {
account = new CheckingAccount("tr1", 10_000, 2_000);
}
System.err.println(account.getClass());
account.withdraw(3_000);

List<Account> accounts = List.of(
new Account("tr1", 10_000),
new CheckingAccount("tr1", 10_000, 2_000)
);

}

}
3 changes: 2 additions & 1 deletion core-banking/src/com/example/banking/domain/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Account extends Object { // DDD -> Entity
// i) instance variable (Heap) ii) reference variable
private final String iban;
// i) instance variable (Heap) ii) value-typed variable
private double balance;
protected double balance;
// public methods
// 2 Overloaded (1) Constructors
public Account(String iban, double balance) {
Expand Down Expand Up @@ -40,6 +40,7 @@ public boolean deposit(double amount) {
}

public boolean withdraw(double amount) {
System.err.println("Account::withdraw");
// validation
if (amount <= 0.0) return false;
// business rule
Expand Down
28 changes: 25 additions & 3 deletions core-banking/src/com/example/banking/domain/CheckingAccount.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
package com.example.banking.domain;
// Account -> super-class / base class
// CheckingAccount -> sub-class / derived class
public class CheckingAccount extends Account {
private double overdraftAmount;
public class CheckingAccount extends Account { // Single Inheritance
private final double overdraftAmount;

public CheckingAccount(String iban, double balance, double overdraftAmount) {
super(iban, balance);
this.overdraftAmount = overdraftAmount;
}


public double getOverdraftAmount() {
return overdraftAmount;
}

@Override
public boolean withdraw(double amount) {
System.err.println("CheckingAccount::withdraw");
if (amount <= 0.0) return false;
if (amount > (balance + overdraftAmount)) return false;
this.balance -= amount;
return true;
}

@Override
public String toString() {
return "CheckingAccount [overdraftAmount=" + overdraftAmount + ", balance=" + balance + ", iban="
+ getIban() + "]";
}




}
10 changes: 10 additions & 0 deletions zoo/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions zoo/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>zoo</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
2 changes: 2 additions & 0 deletions zoo/.settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8
14 changes: 14 additions & 0 deletions zoo/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=17
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=17
19 changes: 19 additions & 0 deletions zoo/src/com/example/zoo/domain/Animal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.zoo.domain;

abstract public class Animal {
protected int legs;

public Animal(int legs) {
this.legs = legs;
}

public int getLegs() {
return legs;
}

public void walk() {
System.out.println("Animal with %d legs is walking now...".formatted(legs));
}

abstract public void eat();
}
35 changes: 35 additions & 0 deletions zoo/src/com/example/zoo/domain/Cat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.example.zoo.domain;

public class Cat extends Animal implements Pet {
private String name;

public Cat() {
this("Tekir");
}

public Cat(String name) {
super(4);
this.name = name;
}

@Override
public void setName(String name) {
this.name = name;
}

@Override
public String getName() {
return name;
}

@Override
public void play() {
System.out.println("%s is playing now...".formatted(name));
}

@Override
public void eat() {
System.out.println("%s is eating now...".formatted(name));
}

}
37 changes: 37 additions & 0 deletions zoo/src/com/example/zoo/domain/Fish.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.example.zoo.domain;

public class Fish extends Animal implements Pet {

private String name;

public Fish(String name) {
super(0);
this.name = name;
}

@Override
public void setName(String name) {
this.name = name;
}

@Override
public String getName() {
return name;
}

@Override
public void play() {
System.out.println("%s is playing now...".formatted(name));
}

@Override
public void eat() {
System.out.println("%s is eating now...".formatted(name));
}

@Override
public void walk() {
System.out.println("%s is swimming now...".formatted(name));
}

}
7 changes: 7 additions & 0 deletions zoo/src/com/example/zoo/domain/Pet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.zoo.domain;

public abstract interface Pet {
abstract public void setName(String name);
abstract String getName();
public void play();
}
14 changes: 14 additions & 0 deletions zoo/src/com/example/zoo/domain/Spider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.zoo.domain;

public class Spider extends Animal {

public Spider() {
super(8);
}

@Override
public void eat() {
System.out.println("Spider is eating now...");
}

}

0 comments on commit 04bbfef

Please sign in to comment.