Skip to content

Commit

Permalink
[Facade] Added pattern examples, bank and computer
Browse files Browse the repository at this point in the history
  • Loading branch information
luisburgos committed Aug 13, 2015
1 parent 58d20ac commit 5c1756c
Show file tree
Hide file tree
Showing 11 changed files with 247 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/facade/examples/bank/AccountNumberCheck.java
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;
}
}
}
64 changes: 64 additions & 0 deletions src/facade/examples/bank/BankFacade.java
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());
}

}
19 changes: 19 additions & 0 deletions src/facade/examples/bank/Client.java
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);

}
}
36 changes: 36 additions & 0 deletions src/facade/examples/bank/FundsCheck.java
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());

}

}
20 changes: 20 additions & 0 deletions src/facade/examples/bank/SecurityCodeCheck.java
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;
}
}
}
10 changes: 10 additions & 0 deletions src/facade/examples/bank/WelcomeMessage.java
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.");
}
}
18 changes: 18 additions & 0 deletions src/facade/examples/computer/CPU.java
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...");
}
}
27 changes: 27 additions & 0 deletions src/facade/examples/computer/ComputerFacade.java
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();
}

}
11 changes: 11 additions & 0 deletions src/facade/examples/computer/HardDrive.java
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...");
}

}
10 changes: 10 additions & 0 deletions src/facade/examples/computer/Memory.java
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...");
}
}
13 changes: 13 additions & 0 deletions src/facade/examples/computer/User.java
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();
}

}

0 comments on commit 5c1756c

Please sign in to comment.