forked from luisburgos/design-patterns
-
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.
[Facade] Added pattern examples, bank and computer
- Loading branch information
1 parent
58d20ac
commit 5c1756c
Showing
11 changed files
with
247 additions
and
0 deletions.
There are no files selected for viewing
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 facade.examples.bank; | ||
|
||
/** | ||
* Created by luisburgos on 12/08/15. | ||
*/ | ||
public class AccountNumberCheck { | ||
|
||
private int accountNumber = 1234567890; | ||
|
||
public int getAccountNumber() { return accountNumber; } | ||
|
||
public boolean isAccountActive(int accountNumberToCheck){ | ||
if(accountNumberToCheck == getAccountNumber()) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
} |
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,64 @@ | ||
package facade.examples.bank; | ||
|
||
/** | ||
* Created by luisburgos on 12/08/15. | ||
*/ | ||
public class BankFacade { | ||
|
||
private int accountNumber; | ||
private int securityCode; | ||
|
||
private AccountNumberCheck accountChecker; | ||
private SecurityCodeCheck codeChecker; | ||
private FundsCheck fundsChecker; | ||
|
||
private WelcomeMessage bankWelcome; | ||
|
||
public BankFacade(int accountNumber, int securityCode){ | ||
|
||
this.accountNumber = accountNumber; | ||
this.securityCode = securityCode; | ||
|
||
bankWelcome = new WelcomeMessage(); | ||
accountChecker = new AccountNumberCheck(); | ||
codeChecker = new SecurityCodeCheck(); | ||
fundsChecker = new FundsCheck(); | ||
|
||
} | ||
|
||
private int getAccountNumber() { return accountNumber; } | ||
|
||
private int getSecurityCode() { return securityCode; } | ||
|
||
public void withdrawCash(double cashAmount){ | ||
|
||
if(canWithdraw(cashAmount)) { | ||
System.out.println("Transaction Complete\n"); | ||
} else { | ||
System.out.println("Transaction Failed\n"); | ||
} | ||
|
||
} | ||
|
||
|
||
public void depositCash(double cashAmount){ | ||
if(canDeposit(cashAmount)) { | ||
fundsChecker.makeDeposit(cashAmount); | ||
System.out.println("Transaction Complete\n"); | ||
} else { | ||
System.out.println("Transaction Failed\n"); | ||
} | ||
} | ||
|
||
private boolean canWithdraw(double cashAmount){ | ||
return accountChecker.isAccountActive(getAccountNumber()) && | ||
codeChecker.isCodeCorrect(getSecurityCode()) && | ||
fundsChecker.haveEnoughMoney(cashAmount); | ||
} | ||
|
||
private boolean canDeposit(double cashAmount){ | ||
return accountChecker.isAccountActive(getAccountNumber()) && | ||
codeChecker.isCodeCorrect(getSecurityCode()); | ||
} | ||
|
||
} |
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 facade.examples.bank; | ||
|
||
/** | ||
* Created by luisburgos on 12/08/15. | ||
*/ | ||
public class Client { | ||
|
||
public static void main(String[] args) { | ||
|
||
BankFacade accesingBank = new BankFacade(1234567890, 1234); | ||
|
||
accesingBank.withdrawCash(50.00); | ||
accesingBank.withdrawCash(900.00); | ||
accesingBank.depositCash(200.00); | ||
|
||
accesingBank.withdrawCash(900.00); | ||
|
||
} | ||
} |
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,36 @@ | ||
package facade.examples.bank; | ||
|
||
/** | ||
* Created by luisburgos on 12/08/15. | ||
*/ | ||
public class FundsCheck { | ||
|
||
private double cashInAccount = 1000.00; | ||
|
||
public double getCashInAccount() { return cashInAccount; } | ||
|
||
public void decreaseCashInAccount(double cashWithdrawn) { cashInAccount -= cashWithdrawn; } | ||
|
||
public void increaseCashInAccount(double cashDeposited) { cashInAccount += cashDeposited; } | ||
|
||
public boolean haveEnoughMoney(double cashToWithdrawal) { | ||
|
||
if(cashToWithdrawal > getCashInAccount()) { | ||
System.out.println("Error: You don't have enough money"); | ||
System.out.println("Current Balance: " + getCashInAccount()); | ||
return false; | ||
} else { | ||
decreaseCashInAccount(cashToWithdrawal); | ||
System.out.println("Withdrawal Complete: Current Balance is " + getCashInAccount()); | ||
return true; | ||
} | ||
|
||
} | ||
|
||
public void makeDeposit(double cashToDeposit) { | ||
increaseCashInAccount(cashToDeposit); | ||
System.out.println("Deposit Complete: Current Balance is " + getCashInAccount()); | ||
|
||
} | ||
|
||
} |
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,20 @@ | ||
package facade.examples.bank; | ||
|
||
/** | ||
* Created by luisburgos on 12/08/15. | ||
*/ | ||
public class SecurityCodeCheck { | ||
|
||
private int securityCode = 1234; | ||
|
||
public int getSecurityCode() { return securityCode; } | ||
|
||
public boolean isCodeCorrect(int securityCode){ | ||
|
||
if(securityCode == getSecurityCode()) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
} |
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 @@ | ||
package facade.examples.bank; | ||
|
||
/** | ||
* Created by luisburgos on 12/08/15. | ||
*/ | ||
public class WelcomeMessage { | ||
public WelcomeMessage(){ | ||
System.out.println("Welcome to this BANK."); | ||
} | ||
} |
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,18 @@ | ||
package facade.examples.computer; | ||
|
||
/** | ||
* Created by luisburgos on 12/08/15. | ||
*/ | ||
public class CPU { | ||
public void freeze() { | ||
System.out.println("CPU: Freezing..."); | ||
} | ||
|
||
public void execute() { | ||
System.out.println("CPU: Executing..."); | ||
} | ||
|
||
public void jump() { | ||
System.out.println("CPU: Jumping..."); | ||
} | ||
} |
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,27 @@ | ||
package facade.examples.computer; | ||
|
||
/** | ||
* Created by luisburgos on 12/08/15. | ||
*/ | ||
public class ComputerFacade { | ||
|
||
private CPU cpu; | ||
private HardDrive hardDrive; | ||
private Memory memory; | ||
|
||
public ComputerFacade(){ | ||
this.cpu = new CPU(); | ||
this.hardDrive = new HardDrive(); | ||
this.memory = new Memory(); | ||
} | ||
|
||
public void start(){ | ||
System.out.println("STARTING..."); | ||
cpu.freeze(); | ||
memory.load(); | ||
hardDrive.read(); | ||
cpu.jump(); | ||
cpu.execute(); | ||
} | ||
|
||
} |
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,11 @@ | ||
package facade.examples.computer; | ||
|
||
/** | ||
* Created by luisburgos on 12/08/15. | ||
*/ | ||
public class HardDrive { | ||
public void read() { | ||
System.out.println("HARD DRIVE: Reading..."); | ||
} | ||
|
||
} |
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 @@ | ||
package facade.examples.computer; | ||
|
||
/** | ||
* Created by luisburgos on 12/08/15. | ||
*/ | ||
public class Memory { | ||
public void load() { | ||
System.out.println("MEMORY: Loading..."); | ||
} | ||
} |
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,13 @@ | ||
package facade.examples.computer; | ||
|
||
/** | ||
* Created by luisburgos on 12/08/15. | ||
*/ | ||
public class User { | ||
|
||
public static void main(String[] args) { | ||
ComputerFacade computer = new ComputerFacade(); | ||
computer.start(); | ||
} | ||
|
||
} |