Minggu, 18 Desember 2016

Turnstile - State Design Pattern





Mesin Coin Operated Turnstile merupakan sebuah alat yang berfungsi sebagai penghalang jalan yang hanya bisa dibuka jika kita memasukkan koin ke dalam mesin.

State Diagram


Berikut adalah state diagram dari mesin turnstile



Terdapat 2 state, yaitu LOCKED dan UNLOCKED. State awal adalah LOCKED. Panah pada gambar merupakan representasi dari perubahan suatu state. Untuk berubah state, maka perlu adanya event tertentu, misal memasukkan token/koin untuk menuju state unlocked.

Use Case


CRC


Turnstile

Responsibilities
Collaborator
Allow user to pass through the machine
UnlockedState
Allow user to insert token

Allow user to get refund

Turn on alarm


LockedState

Responsibilities
Collaborator
Set machine state to unlocked
Turnstile

UnlockedState


Responsibilities
Collaborator
Set machine state to locked
Turnstile

Deployment Diagram




Class Diagram




Source Code

Turnstile.java

 public class Turnstile  
 {  
   private State state;  
   private final State lockedState;  
   private final State unlockedState;  
   public Turnstile()  
   {  
     lockedState = new LockedState(this);  
     unlockedState = new UnlockedState(this);  
 // set initial state of turnstile  
     state = lockedState;  
   }  
   public State getState()  
   {  
     return state;  
   }  
   public void setState(State state)  
   {  
     this.state = state;  
   }  
   public void insertToken()  
   {  
     state.insertToken();  
   }  
   public void passThru()  
   {  
     state.passThru();  
   }  
   public void lock()  
   {  
     System.out.println("Locking the turnstile...");  
     setState(lockedState);  
     System.out.println(getState());  
   }  
   public void unlock()  
   {  
     System.out.println("Unlocking the turnstile...");  
     setState(unlockedState);  
     System.out.println(getState());  
   }  
   public void alarm()  
   {  
     System.out.println("Alert! Cannot pass through without inserting a token!!");  
   }  
   public void refund()  
   {  
     System.out.println("Refunding the token...");  
   }  
   public String toString()  
   {  
     StringBuffer result = new StringBuffer();  
     result.append("\n-- Welcome to the Turnstile Application --\n");  
     result.append(getState());  
     return result.toString();  
   }  
 }  

State.java

 public interface State  
 {  
   public void insertToken();  
   public void passThru();  
 }  

UnlockedState.java

 public class UnlockedState implements State  
 {  
   private final Turnstile turnstile;  
   public UnlockedState(Turnstile turnstile)  
     {  
     this.turnstile = turnstile;  
     }  
   public void insertToken()  
     {  
     System.out.println("insertToken(): Accepting token in the " + turnstile.getState() +  
     " state...");  
     turnstile.refund();  
     }  
   public void passThru()  
     {  
     System.out.println("passThru(): Passing through in the " + turnstile.getState() + " state...");  
     turnstile.lock();  
     }  
   public String toString()  
     {  
     return "UNLOCKED";  
     }  
 }  

LockedState.java

 public class LockedState implements State  
 {  
   private final Turnstile turnstile;  
   public LockedState(Turnstile turnstile)  
   {  
     this.turnstile = turnstile;  
   }  
   public void insertToken()  
   {  
     System.out.println("insertToken(): Accepting token in the " + turnstile.getState() +  
         " state...");  
     turnstile.unlock();  
   }  
   public void passThru()  
   {  
     System.out.println("passThru(): Passing through in the " + turnstile.getState() + " state....");  
     turnstile.alarm();  
   }  
   public String toString()  
   {  
     return "LOCKED";  
   }  
 }  

Berikut adalah source code untuk client

TurnstileApp.java

 import java.util.Scanner;  
 /**  
  * Created by A-BILAL on 12/18/2016.  
  */  
 public class TurnstileApp  
 {  
   private TurnstileApp()  
   {  
 // using default private constructor in lieu of creating a Singleton object  
   }  
   public static void main(String[] args)  
   {  
     Turnstile turnstile = new Turnstile();  
 // first person inserts a token and passes through the turnstile  
     System.out.println(turnstile);  
     turnstile.insertToken();  
     turnstile.passThru();  
 // second person attempts to pass through turnstile without inserting a token  
     System.out.println(turnstile);  
     turnstile.passThru();  
     turnstile.insertToken();  
     turnstile.passThru();  
 // third person attempts to pass through turnstile without initially inserting a token.  
     System.out.println(turnstile);  
     turnstile.insertToken();  
     turnstile.insertToken();  
     turnstile.passThru();  
     while(true){  
 //      System.out.println(turnstile);  
       Scanner reader = new Scanner(System.in); // Reading from System.in  
       System.out.println("Enter action (1 = Insert Token, 2 = Pass Through), 3 = Exit: ");  
       int n = reader.nextInt(); // Scans the next token of the input as an int.  
       if(n == 1){  
         turnstile.insertToken();  
       }  
       else if(n == 2){  
         turnstile.passThru();  
       }  
       else if(n == 3){  
         break;  
       }  
       else{  
         System.out.println("Input Tidak Valid");  
       }  
     }  
   }  
 }  

Output


Terdapat 3 percobaan pada program client, yaitu :

  1. Memasukkan token dan melewati mesin turnstile
    State akan berubah menjadi UNLOCKED dan mesin bisa terlewati lalu state akan kembali ke LOCKED
  2. Mencoba melewati mesin turnstile tanpa memasukkan token
    State tetap pada LOCKED dan tidak bisa melewati mesin serta terdapat pemanggilan fungsi alarm
  3. Memasukkan token 2 kali
    State pada mesin berubah menjadi UNLOCKED. Setelah memasukkan token ke-2 maka user akan mendapatkan refund. Setelah melewati mesin maka state kembali ke LOCKED.

Untuk input User :

  • 1 = Insert Token
  • 2 = Pass Through



Referensi :


Senin, 21 November 2016

Design Pattern - Facade

Definisi


Facade pattern menyembunyikan kerumitan dari sistem dan menyediakan sebuah interface kepada klien dengan menggunakan interface yang dapat klien akses. Tipe desain pattern ini ada berdasarkan structural pattern dimana pattern tersebut menambahkan interface ke sebuah sistem yang sudah ada untuk menyembunyikan kompleksitasnya / kerumitannya.

Pattern ini melibatkan sebuah single class yang menyediakan beberapa method yang telah disimplifikasi untuk.

Gambar



Implementasi


Step 1

Membuat sebuah interface.

Shape.java

 public interface Shape {  
   void draw();  
 }  

Step 2

Membuat concrete class yang mengimplementasikan interface yang sama.

Rectangle.java

 public class Rectangle implements Shape {  
   @Override  
   public void draw() {  
    System.out.println("Rectangle::draw()");  
   }  
 }  

Square.java

 public class Square implements Shape {  
   @Override  
   public void draw() {  
    System.out.println("Square::draw()");  
   }  
 }  

Circle.java

 public class Circle implements Shape {  
   @Override  
   public void draw() {  
    System.out.println("Circle::draw()");  
   }  
 }  

Step 3

Membuat facade class.

ShapeMaker.java

 public class ShapeMaker {  
   private Shape circle;  
   private Shape rectangle;  
   private Shape square;  
   public ShapeMaker() {  
    circle = new Circle();  
    rectangle = new Rectangle();  
    square = new Square();  
   }  
   public void drawCircle(){  
    circle.draw();  
   }  
   public void drawRectangle(){  
    rectangle.draw();  
   }  
   public void drawSquare(){  
    square.draw();  
   }  
 }  

Step 4

Menggunakan facade untuk menggambar shape.

FacadePatternDemo.java

 public class FacadePatternDemo {  
   public static void main(String[] args) {  
    ShapeMaker shapeMaker = new ShapeMaker();  
    shapeMaker.drawCircle();  
    shapeMaker.drawRectangle();  
    shapeMaker.drawSquare();            
   }  
 }  

Output




Referensi :

Minggu, 16 Oktober 2016

Calculator Application Design Top-Down

Source Code


 import javax.swing.*;  
 import java.awt.event.*;  
 class Calc implements ActionListener  
 {  
   JFrame f;  
   JTextField t;  
   JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bdiv,bmul,bsub,badd,bdec,beq,bdel,bclr;  
   static double a=0,b=0,result=0;  
   static int operator=0;  
   Calc()  
   {  
     f=new JFrame("Calculator");  
     t=new JTextField();  
     b1=new JButton("1");  
     b2=new JButton("2");  
     b3=new JButton("3");  
     b4=new JButton("4");  
     b5=new JButton("5");  
     b6=new JButton("6");  
     b7=new JButton("7");  
     b8=new JButton("8");  
     b9=new JButton("9");  
     b0=new JButton("0");  
     bdiv=new JButton("/");  
     bmul=new JButton("*");  
     bsub=new JButton("-");  
     badd=new JButton("+");  
     bdec=new JButton(".");  
     beq=new JButton("=");  
     bdel=new JButton("Delete");  
     bclr=new JButton("Clear");  
     t.setBounds(30,40,280,30);  
     b7.setBounds(40,100,50,40);  
     b8.setBounds(110,100,50,40);  
     b9.setBounds(180,100,50,40);  
     bdiv.setBounds(250,100,50,40);  
     b4.setBounds(40,170,50,40);  
     b5.setBounds(110,170,50,40);  
     b6.setBounds(180,170,50,40);  
     bmul.setBounds(250,170,50,40);  
     b1.setBounds(40,240,50,40);  
     b2.setBounds(110,240,50,40);  
     b3.setBounds(180,240,50,40);  
     bsub.setBounds(250,240,50,40);  
     bdec.setBounds(40,310,50,40);  
     b0.setBounds(110,310,50,40);  
     beq.setBounds(180,310,50,40);  
     badd.setBounds(250,310,50,40);  
     bdel.setBounds(60,380,100,40);  
     bclr.setBounds(180,380,100,40);  
     f.add(t);  
     f.add(b7);  
     f.add(b8);  
     f.add(b9);  
     f.add(bdiv);  
     f.add(b4);  
     f.add(b5);  
     f.add(b6);  
     f.add(bmul);  
     f.add(b1);  
     f.add(b2);  
     f.add(b3);  
     f.add(bsub);  
     f.add(bdec);  
     f.add(b0);  
     f.add(beq);  
     f.add(badd);  
     f.add(bdel);  
     f.add(bclr);  
     f.setLayout(null);  
     f.setVisible(true);  
     f.setSize(350,500);  
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
     f.setResizable(false);  
     b1.addActionListener(this);  
     b2.addActionListener(this);  
     b3.addActionListener(this);  
     b4.addActionListener(this);  
     b5.addActionListener(this);  
     b6.addActionListener(this);  
     b7.addActionListener(this);  
     b8.addActionListener(this);  
     b9.addActionListener(this);  
     b0.addActionListener(this);  
     badd.addActionListener(this);  
     bdiv.addActionListener(this);  
     bmul.addActionListener(this);  
     bsub.addActionListener(this);  
     bdec.addActionListener(this);  
     beq.addActionListener(this);  
     bdel.addActionListener(this);  
     bclr.addActionListener(this);  
   }  
   public void actionPerformed(ActionEvent e)  
   {  
     if(e.getSource()==b1)  
       t.setText(t.getText().concat("1"));  
     if(e.getSource()==b2)  
       t.setText(t.getText().concat("2"));  
     if(e.getSource()==b3)  
       t.setText(t.getText().concat("3"));  
     if(e.getSource()==b4)  
       t.setText(t.getText().concat("4"));  
     if(e.getSource()==b5)  
       t.setText(t.getText().concat("5"));  
     if(e.getSource()==b6)  
       t.setText(t.getText().concat("6"));  
     if(e.getSource()==b7)  
       t.setText(t.getText().concat("7"));  
     if(e.getSource()==b8)  
       t.setText(t.getText().concat("8"));  
     if(e.getSource()==b9)  
       t.setText(t.getText().concat("9"));  
     if(e.getSource()==b0)  
       t.setText(t.getText().concat("0"));  
     if(e.getSource()==bdec)  
       t.setText(t.getText().concat("."));  
     if(e.getSource()==badd)  
     {  
       a=Double.parseDouble(t.getText());  
       operator=1;  
       t.setText("");  
     }   
     if(e.getSource()==bsub)  
     {  
       a=Double.parseDouble(t.getText());  
       operator=2;  
       t.setText("");  
     }  
     if(e.getSource()==bmul)  
     {  
       a=Double.parseDouble(t.getText());  
       operator=3;  
       t.setText("");  
     }  
     if(e.getSource()==bdiv)  
     {  
       a=Double.parseDouble(t.getText());  
       operator=4;  
       t.setText("");  
     }  
     if(e.getSource()==beq)  
     {  
       b=Double.parseDouble(t.getText());  
       switch(operator)  
       {  
         case 1: result=a+b;  
           break;  
         case 2: result=a-b;  
           break;  
         case 3: result=a*b;  
           break;  
         case 4: result=a/b;  
           break;  
         default: result=0;  
       }  
       t.setText(""+result);  
     }  
     if(e.getSource()==bclr)  
       t.setText("");  
     if(e.getSource()==bdel)  
     {  
       String s=t.getText();  
       t.setText("");  
       for(int i=0;i<s.length()-1;i++)  
       t.setText(t.getText()+s.charAt(i));  
     }      
   }  
   public static void main(String...s)  
   {  
     new Calc();  
   }  
 }  

Source : http://www.thecrazyprogrammer.com/2014/06/program-to-create-calculator-using-java-swing.html

Use Case




Class Diagram



CRC Card


Class Calc


Responsibilities
Collabolator
Allow user to set operand

Allow user to add operand

Allow user to substract operand

Allow user to multiply operand

Allow user to divide operand

Allow user to add decimal point to operand

Allow user to delete operand

Allow user to clear the display



Sequence Diagram




Activity Diagram







Ahmad Bilal
5114100121
PPL C


Minggu, 02 Oktober 2016

CRC dan Class Diagram Aplikasi Address Book

1. Use Case



2. CRC Cards


Class AddressBook

Responsibilities
Collaborators
Menampung semua objek Person
Memiliki data jumlah semua objek Person

Update perubahan pada AddressBook

Update Person
Person
Remove Person
Person
Urutkan berdasarkan nama

Urutkan berdasarkan ZIP

Cetak / Print AddressBook

Class AddressBookController

Sebagai controller dari aplikasi (melayani fitur-fitur yang ada)


Responsibilities
Collaborators
User dapat melakukan Add Person Use Case
User dapat melakukan Edit Person Use Case
User dapat melakukan Delete a Person Use Case
User dapat melakukan Sort Entries by Name Use Case
User dapat melakukan Sort Entries by ZIP Use Case
User dapat melakukan Create New Address Book Use Case
User dapat melakukan Open Existing Address Book Use Case
User dapat melakukan Save Address Book Use Case
User dapat melakukan Save Address Book As ... Use Case
User dapat melakukan Print Entries Use Case


Class AddressBookGUI

Sebagai view dari aplikasi Address Book yang membolehkan user untuk berinteraksi dengan aplikasi.

Responsibilities
Collaborators
Menjaga objek address book yang sedang ditampilkan
Menampilkan nama-nama orang pada address book
Menampilkan judul dari address book
Menjaga state dari menu “Save” – dalam artian menu tersebut hanya bisa digunakan ketika ada perubahan pada address book sejak address book dibuka
Membolehkan user untuk menggunakan fungsi dari sebuah use case

Class FileSystem

Berfungsi untuk mengatur interaksi antara aplikasi dengan filesystem dimana aplikasi berjalan.
Collaborators
Membaca (Read) address book yang tersimpan dari sebuah file
Menyimpan sebuah address book ke suatu file

Class Person

Berfungsi menjaga informasi tentang suatu individu

Responsibilities
Collaborators
Membuat sebuah objek, diketahui nama seseorang, alamat, kota, state, ZIP/kode pos, dan phone (nomor telepon)
Edit nama awal
Edit nama akhir
Edit alamat
Edit kota
Edit state
Edit ZIP / kode pos
Edit nomor telepon
Update informasi seseorang dalam address book

3. Class












4. Class Relationship



5. Check Realization



USE CASE
CLASS
Add a Person
AddressBookController, Person
Edit a Person
AddressBookController, Person
Delete a Person
AddressBookController, Person
Sort Entries by Name
AddressBook, AddressBookController
Sort Entries by ZIP
AddressBook, AddressBookController
Print Entries
AddressBook, AddressBookController
Create New Address Book
AddressBookController, FileSystem
Open Existing Address Book
AddressBookController, FileSystem
Save Address Book
AddressBook , FileSystem
Save Address Book As
AddressBook
Quit Program
AddressBookApplication
Offer to Save Changes
AddressBook, AddressBookGUI


Referensi : 

Ahmad Bilal
5114100121
APSI C