-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
feb2eed
commit 04bbfef
Showing
12 changed files
with
211 additions
and
4 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
core-banking/src/com/example/banking/application/StudyPolymorphism.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 25 additions & 3 deletions
28
core-banking/src/com/example/banking/domain/CheckingAccount.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() + "]"; | ||
} | ||
|
||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
eclipse.preferences.version=1 | ||
encoding/<project>=UTF-8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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..."); | ||
} | ||
|
||
} |