Thursday, October 1, 2015

[7.5] Online book reader system

1. Example

1.User membership creation and extension
2. Searching the database of books
3. Reading the books



2. Implementation






//
//keep all the book's information
//Method - add books
//Method - delete books
//Method - update books
//
public class Book
{
    private long ID;
    private String details;
    private static Set books;


    // Constructor
    public Book(long ID, Stirng details){...}


    // Class Method
    public static void addBook(long ID, String detials)
    {
         books.add(new Book(ID, details));
    }
    public static Book find(long ID)
    {
         for ( Book b: books)
             if (b.getID() == ID) return b
         return null;
    }
    public static  void delete (Book b)
    {
         books.remvoe(b);
    }


    // Object methods
    public void update (){}
}






//
//Keep all the information regarding the user ,and an identifier to //identify each user uniquely.
//We can add functionality like 
//Method - registering the user, 
//Method - charging a membership amount,
//Method - monthly / daily quota
//
public class User
{
     private long ID;
     private String details;
     private int accountType;
     private static Set users;

    
     // Constructor
     public User(long ID, Stirgn details, int accountType){...}



     // Class Method
     public static User find(long ID)
     {
          for (User u:users)
               if (u.getID() == ID) return u;
          return null;
     }
     public static void addUser()
     {
          users.add(  new User(ID, details, accountType));
     }



     // Object methods
     public Book searchLibrary(long ID) {return Book.find(id);}
     public void renewMembership(){...}    
}





//
//Manager class for managing the online book reader system which would //have a listen function to listen for any incoming requests to log in.
//It also provides 
//Method  - book search functionality and 
//Method  - display functionality.
//Because the end user interacts through this class, 
//SEARCH must be implemented
//
public class OnlineReaderSystem
{


       private Book b;
       private User u;



       // Constructor
       public OnlineReaderSystem(Book b, User u){...}
       


       // Object methods
       public void listenRequest(){}
       public Book searchBook(long ID){return book.find(ID);}
       public User searchUser(long ID){return user.find(ID);}
       public void display(){}


}



3. Similar Ones

[7.4] Chess Design

1. Example

     A     B   C     D     E      F     G     H
8   Br   Bn  Bb   Bq   Bk   Bb   Bn   Br
7   Bp  Bp  Bp   Bp   Bp   Bp   Bp   Bp
6
5
4
3
2  Wp  Wp Wp  Wp  Wp  Wp  Wp  Wp
1  Wr   Wp Wb  Wq  Wk  Wb  Wn  Wr


2. implementation


// 0. GameManager : Game run
public class GameManager 
{
     void processTurn(PlayerBase player){};
     boolean acceptTurn(ChessPieceTurn turn){ return true};
     Position currentPosition;
}
public class chessPieceTurn{};





// 1. PlayerBase
public abstract class PlayerBase
{
    public abstract ChessPieceTurn getTurn(Position p); 
}
class ComputerPlayer extends PlayerBase
{
    public ChessPieceTurn getTurn(Position p){return null;}
    public setDifficulty(){};
    public PositionEstimator estimator;
    public PositionBackTracker backtracker; 
}
public class HumanPalyer extends PlayerBase
{
    public chessPieceTurn getTurn (Position p){return null;}
}



// 2. ChessPieceBase
public abstract class chessPieceBase
{
    abstract boolean canBeChecked();
    abstract boolean isSupportCastle();
}
public class King extends ChessPieceBase{...}
public class Queen extends ChessPieceBase{...}






// 3. Represent chess position in compact form
public class Position
{ 
   ArrayList black;
   ArrayList white;
}




public class PositionBackTracker 
{
    public static Position getNext(Position p){return null;}
}




// PositionPotentialValue
public abstract class PositionPotentialValue
{
    abstract boolean lessThan(PositionPotentialValue pv);
}
public class PositionEstimator
{
    public static PositionPotentialValue estimate(Position p){...}
}




3. Similar Ones
http://rangerway.com/way/object-oriented-chess-game/#tfbml-data%7B%22close_iframe%22%3Atrue%2C%22location_url%22%3A%22http%3A%2F%2Frangerway.com%2Fway%2Fobject-oriented-chess-game%2F%22%7D

[7.3] music juke box

1 Example
Music Jukebox
               ________
              /   O     (|)  \
              |  =====    |
              |   _  _   _   |
              |  |_| |_| |_|  |
              |   _  _   _   |
              |  |_| |_| |_|  |      
              |________ |
                |           |
JukeBox
  - CDPlayer
  - User
  - Set<CD>
 - TRackSelector
 - Song getCurrentTrack()
 - void processOneUser(User u)
CDPlayer

TRackSelector
  - Song currentSong
  - void setTrack(Song s)
  - getCurrentSong()
Playlist
  - ArrayList<song>
  - Queue<Song> 
  - Song track
  - add 
  - getNextTrackToPlay()
  - void queueUpTrack(Song s) 
User
  -  name
  - ID
  - getID, setID
  - getName. setName
  - addUser(String n, long id)
Song
  -name

2. implementation
CD album
a Collection of all CD in a matrix form
matrix[][]
CD class
  - name
  - number of songs
  - album price
  - album published year
Song class
  -song name
  - song duration
  - song price
 - song singer
 - song published year
PLAY class
 - play
 - delete
 -

*********************
class CDPlayer VS class Playlist
Let's first understand the basic system components:
=>CD player
=>CD
=>Display() (display length of song, remaining time and playlist)

Now, let's break this down further:
-playlist creation (includes add, delete, shuffle etc sub functionalities)
- CD selector
-Queueing up a song
- Get next song from playlist
-Track selector

A user can be introduced:
 -Adding
 - Deleting
-credit information


public class CD{}
public class CDPlayer
{
    private Playlist p;
    private CD c;
    
    public Playlist getPlaylist() {return p;}
    public void setPlaylist() {this.p = p;}
    
    public CD getCD(){return c;}
    public void setCD(CD c ){ this.c = c;}
    


    // 3 Constructors
    public CDPlayer(Playlist p){this.p = p;}
    public CDPlayer(CD c, Playlist p){...}
    public CDPlayer(CD c){this.c = c;}
 

    
    public void playTrack(Song s){...}
}

//*** The Machine
public class JukeBox 
{
    private CDPlayer cdPlayer;
    private User user;
    private Set cdCollection;
    private TrackSelector ts;

  

    // Constructor
    public JukeBox(CDPlayer cdPlayer, User user, Set cdCollection, TrackSelector ts){...}



    public Song getCurrentTrack() {return ts.getCurrentSong();}
    public void processOneUser(User u){this.user = u;}
}

// a collection of songs, add a song and pop up a song
public class Playlist 
{
    private Song track;
    private Queue queue;



    // Constructor
    public Playlist(Song track, Queue queue){...}



    public Song getNextTrackToPlay() {return queue.peek();}
    public void queueUpTrack(Song s){queue.add(s);}
}
public class Song
{
    private String songName;
}

public class TrackSelector
{
    private Song currentSong;
    


    // Constructor
    public TrackSelector(Song s ){currentSong = s;}



    public void setTrack(Song s ){currentSong = s; }
    public Song getCurrentSong() {return currentSong;}
}

public class User
{
    private String name;
    private long ID; 
  
    
    // Constructor
    public User(String name, long ID){...}
    public User getUser(){return this;}


    public String getName() {return name;} 
    public void setName(String name) {this.name = name;}
    
    public long getID() {return ID;}
    public void setID(long ID){ ID = ID;}


    public static User addUser(String name, long ID){...}
    

}
.3 similar Ones

[7.2] getCallHandler()

1. Example

              ------------------>PM
              |                           |
              |-----------------?  TL  <-----------------------
                                          |                                      |
fresher1  fresher2  fresher3  fresher4  fresher5  fresher6

   X             X            X            X            X            O ->?

Call coming in districute call.rank=0 -> Emp check every level employee to see availability
                              Emp.receive(call)
                              Emp.cannotHandle(call)
                                    - call.rank +1
                                    - distribute call.rank+1 again
                                   - find next level available Emp
                        -> Queue, no one is available
Employee
   - free
   - rank
 - CallHandler instance
    - void ReceiveCall(Call call)
   - void CallHandled(Call call)
   - void CannontHandle(Call call)
Call
   - rank
  - void reply()
 - void disconnect()
CallHandler
 - Employee callHandler(Call call)
 - void dispatchCall(Call call)
 - void getNextCall(Employee emp)

2. Implementation
Q1: getHandler() check available and check canHandle or not
A1: If not handle ==> call CallHanlder class to do the distribute job
AND callHandler call next level people to Handle call
if handled, complete, otherwise, call CallHandler class again
A1: available -> every ppl has a field called free, ==> Employee has 3 types
Employee class
fresher class
TL class
PM class
CallHandler class hook up call and Employee
Call class ??


public class CallHandler  // Hook up call and employee
{
  static final int LEVELS = 3;// we have 3 level of employees
    static final int NUM_FRESHERS = 5;// we have 5 freshers
    // Use ArrayList[] instead of ArrayList> since we know the size
    ArrayList[] employeeLevels = new ArrayList[LEVELS];
    // queue for each call's rank
    Queue[]  callQueues = new LinkedList[LEVELS];



    public CallHandler(){...}




    // *** check every level employee to see who is free now
    // @Return: Employee - the one it is free
    Employee getCallHandler(Call call)
    {
         //for (int i = 0 ; employeeLevels[call.rank].size())
         for (int level = call.rank; level < LEVELS -1; level++) 
         {
              ArrayList employeeLevel = employeeLevels[level];
              for (Employee emp: employeeLevel)
              {
                  if (emp.free)
                  {
                     return emp;
                  }
              }
         }
         return null;
    }
    



    // ** route the call to an available employee, or adds to a queue
    void dispatchCall(Call call)
    {
          // try to route the call to an employee with minimal rank
          Employee emp = getCallHandler(call);
          // Someone is free
          if (emp != null)
               emp.ReceiveCall(call);
          // No one is free
          else
              // place the call into queue according to its rank
              callQueues[call.rank].add(call);
    } 



    void getNextCall(Employee e){...}// look for call for e's rank, put this as parameter



}

class Call
{
    int rank = 0;
    public void reply(String message){...}
    public void disconnect(){...}
}
class Employee
{
    CallHandler callHandler;


    // Fields
    int rank;// 0 - fresher, 1- technical lead, 2 - product manager
    boolean free; // availability


    // Constructor 
    Employee(int rank) {this.rank = rank;}


    // Function
    void ReceiveCall(Call call){...}
    void CallHandled(Call call){...}// call is complete
    void CannotHandle(Call call)// escalate call
    {
         call.rank = rank + 1;
         CallHandler.dispatchCall(call);
         free =  = true;
         CallHandler.getNextCall(this);// look for waiting call
    }
}
class Fresher extends Employee
{
     public Fresher(){super(0);}
}
class TechLead extends Employee
{
     public TechLead() {super(1);}
}
class ProductManager extends Employee
{
     public ProductManager(){super(2);}
}


3. Similar Ones

Sunday, September 20, 2015

[7.1]deck of card

1. Example
Design the data structures for a generic deck of cards.
Explain how you would subclass it to implement particular card game.



2. Implementation
Q1: deck of card?
A1: 52 cards is composed of 4 type of card (Spades, Heart, Diamond, Clubs) and each type has 13 cards from letter A,2,3,4,5,6,7,8,9,10,J,Q,K(think it of number 1 to number 13)
A2:An 2D array such as card[4][13], first dimension is type and second dimension is letter
Q1: how to play those cards?
A1: shuffle, take one from deck for each player, ???


public class Card 
{



     public enum Suit 
     {
         CLUBS (1), SPADES(2), HEARTS(3), DIAMONDS(4);
         int value;
         private Suit(int v){ value = v;}
     }




     private int card;
     private Suit suit;
      



     public Card(int r, Suit s)
     {
         card = r;
         suit = s;
     }
     public int getValue() {return card;}
     public Suit getSuit() {return suit;}




}
Public class BlackJackCard extends Card 
{
      public BlackJackCard(int r, Suit s) {super(r,s);}
     public int value() 
{
          int r = super.value();
           // card.value = A
           if (r==1) return 11; // aces are 11
           // card.vlaue = 2~9
           if (r < 10) return r;
           // card.value = 10,J,Q,K
           return 10;
      } 


      boolean isAce()
      {
           return super.value() ==1;
      }
}


Public class Hand 
{
      // use BlackJackCard class to get a set of cards
} 

3. Similar ones
Rock paper scissor
Tic tae toe