Skip to content
This repository was archived by the owner on May 18, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 170 additions & 0 deletions TrelloMachingCoding/src/com/machine/coding/CommandExecutor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package com.machine.coding;

import com.machine.coding.common.ServiceFactory;
import com.machine.coding.model.Board;
import com.machine.coding.model.Card;
import com.machine.coding.model.CardList;
import com.machine.coding.service.BoardService;
import com.machine.coding.service.CardListService;
import com.machine.coding.service.CardService;

@SuppressWarnings("unchecked")
public class CommandExecutor {

// TODO : Change it to interface later.
private BoardService<String, Board> boardService = (BoardService) ServiceFactory.getService("BOARD_SERVICE");
private CardListService<String, CardList> cardListService = (CardListService) ServiceFactory
.getService("CARDLIST_SERVICE");
private CardService<String, Card> cardService = (CardService) ServiceFactory.getService("CARD_SERVICE");

public void execute(String inputCommand) {
String[] commands = inputCommand.split(" ");
switch (commands[0]) {
// BOARD.
case "BOARD":
switch (commands[1]) {
case "CREATE":
boardService.create(new Board(commands[2]));
break;

case "DELETE":
boardService.delete(commands[2], true);
break;

default:
switch (commands[2]) {
case "ADD_MEMBER":
boardService.addMember(commands[1], commands[3]);
break;

case "REMOVE_MEMBER":
boardService.removeMember(commands[1], commands[3]);
break;

default:

try {
boardService.updateField(commands[2], commands[3], commands[1]);
} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException
| SecurityException e) {
e.printStackTrace();
}

break;
}
break;
}
break;

// List
case "LIST":
switch (commands[1]) {
case "CREATE":
CardList cardList = new CardList(commands[3]);
cardList.setBoard(boardService.getById(commands[2]));
String id = cardListService.create(cardList);
boardService.addCardList(commands[2], id);
break;

case "DELETE":
cardListService.delete(commands[2], true);
break;

default:
switch (commands[2]) {

default:

try {
cardListService.updateField(commands[2], commands[3], commands[1]);
} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException
| SecurityException e) {
e.printStackTrace();
}

break;
}
break;
}
break;

// List
case "CARD":
switch (commands[1]) {
case "CREATE":
Card card = new Card(commands[3]);
card.setCardList(cardListService.getById(commands[2]));
String id = cardService.create(card);
cardListService.addCard(commands[2], id);
break;

case "DELETE":
cardService.delete(commands[2]);
break;

default:
switch (commands[2]) {

case "ASSIGN":
cardService.assign(commands[1], commands[3]);
break;

case "UNASSIGN":
cardService.unAssign(commands[1]);
break;

case "MOVE":
cardListService.moveCard(commands[1], commands[3]);
break;

default:

try {
cardService.updateField(commands[2], commands[3], commands[1]);
} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException
| SecurityException e) {
e.printStackTrace();
}

break;
}
break;
}
break;

// show
case "SHOW":
String output;
if (commands.length == 1) {
output = new Formattor<Board>().format(boardService.getAll());
System.out.println(output);
} else {

switch (commands[1]) {
case "BOARD":
// Due to time constraint using toString format.
output = new Formattor<Board>().format(boardService.getById(commands[2]));
System.out.println(output);
break;
case "LIST":
// Due to time constraint using toString format.
output = new Formattor<CardList>().format(cardListService.getById(commands[2]));
System.out.println(output);
break;
case "CARD":
// Due to time constraint using toString format.
output = new Formattor<Card>().format(cardService.getById(commands[2]));
System.out.println(output);
break;

default:
break;
}
}
break;

default:
break;
}
}
}
39 changes: 39 additions & 0 deletions TrelloMachingCoding/src/com/machine/coding/Driver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.machine.coding;

import java.util.Scanner;

import com.machine.coding.common.UserBase;
import com.machine.coding.model.User;

public class Driver {

public static void main(String[] args) {

CommandExecutor executor = new CommandExecutor();
Scanner sc = new Scanner(System.in);

createUserBase();

while (sc.hasNext()) {
String command = sc.nextLine();

executor.execute(command);

}
}

private static void createUserBase() {
// Creating dummy users for now , it can be created using some file or
// some other ways.
User user1 = new User("user1", "User1", "User1@gmail.com", "123");
User user2 = new User("user2", "User2", "User2@gmail.com", "1234");
User user3 = new User("user3", "User3", "User3@gmail.com", "1235");
User user4 = new User("user4", "User4", "User4@gmail.com", "123455");
UserBase.addUser(user1);
UserBase.addUser(user2);
UserBase.addUser(user3);
UserBase.addUser(user4);

}

}
25 changes: 25 additions & 0 deletions TrelloMachingCoding/src/com/machine/coding/Formattor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.machine.coding;

import java.util.List;

public class Formattor<V> {

// Due to time constraint using toString format.
public String format(V object) {
if (object == null)
return "No Boards";
return object.toString();
}

public String format(List<V> objects) {
StringBuilder builder = new StringBuilder();
if (objects == null || objects.isEmpty())
return "No Boards";
for (V v : objects) {
builder.append(v.toString());
builder.append("\n");
}
return builder.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.machine.coding.common;

import com.machine.coding.model.Board;
import com.machine.coding.model.Card;
import com.machine.coding.model.CardList;
import com.machine.coding.service.BoardService;
import com.machine.coding.service.CardListService;
import com.machine.coding.service.CardService;
import com.machine.coding.service.GenericService;

/**
* @author Hakim.s
*
*/
public class ServiceFactory {

private ServiceFactory() {
}

@SuppressWarnings("rawtypes")
public static GenericService getService(String type) {
switch (type) {
case "BOARD_SERVICE":
return new BoardService<String, Board>();

case "CARDLIST_SERVICE":
return new CardListService<String, CardList>();
case "CARD_SERVICE":

return new CardService<String, Card>();
default:
break;
}
return null;
}
}
19 changes: 19 additions & 0 deletions TrelloMachingCoding/src/com/machine/coding/common/UserBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.machine.coding.common;

import java.util.HashMap;
import java.util.Map;

import com.machine.coding.model.User;

public class UserBase {

static Map<String, User> userMap = new HashMap<String, User>();

public static User getUser(String userId) {
return userMap.get(userId);
}

public static void addUser(User user) {
userMap.put(user.getUserId(), user);
}
}
22 changes: 22 additions & 0 deletions TrelloMachingCoding/src/com/machine/coding/model/Base.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.machine.coding.model;

import java.io.Serializable;

public class Base implements Serializable{

/**
*
*/
private static final long serialVersionUID = 1L;

private String id;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

}
Loading