diff --git a/README.md b/README.md index e04da1f..84b2df9 100755 --- a/README.md +++ b/README.md @@ -3,3 +3,17 @@ Welcome to the 3rd Mock Machine Coding Round by [workat.tech](http://workat.tech Please visit [this link](https://workat.tech/machine-coding/practice/trello-problem-t0nwwqt61buz) to participate. +NOTE: +1. Currently the unique id isn't based on hash, but a counter that gets appended to type of element being creaated. +2. The output is just printing stuff line after line, instead of in a dict. I'll try to change that later. +3. I've added some of my own methods, like SHOW USER, CREATE USER, etc. +4. Sample input is present in sample_input.txt + +TO RUN: + +$> g++ user.cpp card.cpp list.cpp board.cpp appManager.cpp driver.cpp -o trello + +$> ./trello + +TODO: +Cmake command diff --git a/appManager.cpp b/appManager.cpp new file mode 100644 index 0000000..35ac801 --- /dev/null +++ b/appManager.cpp @@ -0,0 +1,198 @@ +#include +#include "appManager.h" +#include "board.h" +#include "list.h" +#include "card.h" +#include "user.h" + +using namespace std; + +AppManager::AppManager(){ + board = 0; + card = 0; + list = 0; + user = 0; +} + +void AppManager::show(){ + if(boards.size()==0){ + cout << "No Board" << endl; + return; + } + for(map::iterator it=boards.begin(); it!=boards.end(); it++) + (it->second)->printBoard(); +} + +void AppManager::showBoard(string boardId){ + if(boards.find(boardId)==boards.end()) + cout << "Board " << boardId << " doesn't exist!" << endl; + else + boards[boardId]->printBoard(); + +} + +void AppManager::showList(string listId){ + if(lists.find(listId)==lists.end()) + cout << "List " << listId << " doesn't exist!" << endl; + else + lists[listId]->printList(); +} + +void AppManager::showCard(string cardId){ + if(cards.find(cardId)==cards.end()) + cout << "Card " << cardId << " doesn't exist!" << endl; + else + cards[cardId]->printCard(); +} + +void AppManager::showUserCards(string userId){ + if(users.find(userId)==users.end()) + cout << "User " << userId << " doesn't exist!" << endl; + else + users[userId]->myCards(); +} + +void AppManager::createBoard(string name){ + board++; + string id = "Board" + to_string(board); + Board* b = new Board(name, id); + boards[id] = b; + return; +} +void AppManager::removeBoard(string id){ + if(boards.find(id)==boards.end()) + cout << "Board " << id << " doesn't exist!" << endl; + else { + cout << "Deleting Board" << endl; + Board* b = boards[id]; + set listSet = b->getLists(); + while(begin(listSet) != end(listSet)){ + set::iterator it_list = listSet.begin(); + set cardSet = (*it_list)->getCards(); + while(begin(cardSet)!=end(cardSet)){ + set::iterator it_card = cardSet.begin(); + cards.erase((*it_card)->getId()); + delete *it_card; + } + lists.erase((*it_list)->getId()); + delete *it_list; + } + boards.erase(id); + delete(b); + } + return; +} + +void AppManager::addMemberToBoard(string boardId, string memberId){ + if(boards.find(boardId)==boards.end()) + cout << "Board " << boardId << " doesn't exist!" << endl; + else if(users.find(memberId)==users.end()) + cout << "User " << memberId << " doesn't exist!" << endl; + else + (boards[boardId])->addMember(users[memberId]); + return; +} + +void AppManager::removeMemberFromBoard(string boardId, string memberId){ + if(boards.find(boardId)==boards.end()) + cout << "Board " << boardId << " doesn't exist!" << endl; + else if(users.find(memberId)==users.end()) + cout << "User " << memberId << " doesn't exist!" << endl; + else + (boards[boardId])->removeMember(users[memberId]); + return; +} + +void AppManager::updateBoardName(string boardId, string newName){ + if(boards.find(boardId)==boards.end()) + cout << "Board " << boardId << " doesn't exist!" << endl; + else + (boards[boardId])->setName(newName); +} +void AppManager::updateBoardPrivacy(string boardId, string newPrivacy){ + if(boards.find(boardId)==boards.end()) + cout << "Board " << boardId << " doesn't exist!" << endl; + else + (boards[boardId])->setPrivacy(newPrivacy); +} + +void AppManager::createList(string boardId, string listName){ + if(boards.find(boardId)==boards.end()) + cout << "Board " << boardId << " doesn't exist!" << endl; + else { + list++; + string id = "List" + to_string(list); + List* list = new List(listName, id, boards[boardId]); + lists[id] = list; + } +} + +void AppManager::removeList(string listId){ + if(lists.find(listId) == lists.end()) + cout << "List " << listId << " doesn't exist!" << endl; + else { + cout << "Deleting list" << endl; + List* l = lists[listId]; + set cardSet = l->getCards(); + while(begin(cardSet)!=end(cardSet)){ + set::iterator it_card = cardSet.begin(); + cards.erase((*it_card)->getId()); + delete *it_card; + } + l->deleteFromParentBoard(); + lists.erase(listId); + delete(l); + } +} + +void AppManager::createCard(string listId, string cardName){ + if(lists.find(listId) == lists.end()) + cout << "List " << listId << " doesn't exist!" << endl; + else { + card++; + string id = "Card" + to_string(card); + Card* c = new Card(cardName, id, lists[listId]); + cards[id] = c; + } +} + +void AppManager::removeCard(string cardId){ + if(cards.find(cardId)==cards.end()) + cout << "Card " << cardId << " doesn't exist!" << endl; + else { + Card* c = cards[cardId]; + c->deleteThisCard(); + cards.erase(cardId); + delete(c); + } +} + +void AppManager::assignCard(string cardId, string memberId){ + if(cards.find(cardId)==cards.end()) + cout << "Card " << cardId << " doesn't exist!" << endl; + else if(users.find(memberId)==users.end()) + cout << "User " << memberId << " doesn't exist!" << endl; + else + cards[cardId]->assignTo(users[memberId]); +} + +void AppManager::updateCardName(string cardId, string newName){ + if(cards.find(cardId)==cards.end()) + cout << "Card " << cardId << " doesn't exist!" << endl; + else + cards[cardId]->setName(newName); +} + +void AppManager::updateDescription(string cardId, string description){ + if(cards.find(cardId)==cards.end()) + cout << "Card " << cardId << " doesn't exist!" << endl; + else + cards[cardId]->setDescription(description); +} + +void AppManager::createMember(string name, string email){ + user++; + string id = "User" + to_string(user); + User* user = new User(name, email, id); + users[id] = user; +} \ No newline at end of file diff --git a/appManager.h b/appManager.h new file mode 100644 index 0000000..b091a75 --- /dev/null +++ b/appManager.h @@ -0,0 +1,45 @@ +#ifndef APPMANAGER_H +#define APPMANAGER_H +#include + +using namespace std; + +class Board; +class List; +class Card; +class User; + +class AppManager { + map boards; + map lists; + map cards; + map users; + int board, list, card, user; + public: + AppManager(); + void show(); + void showBoard(string id); + void showList(string id); + void showCard(string id); + void showUserCards(string id); + + void createBoard(string name); + void removeBoard(string id); + void addMemberToBoard(string boardId, string memberId); + void removeMemberFromBoard(string boardId, string memberId); + void updateBoardName(string boardId, string newName); + void updateBoardPrivacy(string boardId, string newPrivacy); + + void createList(string boardId, string listName); + void removeList(string id); + + void createCard(string listId, string cardName); + void removeCard(string id); + void assignCard(string cardId, string memberId); + void updateCardName(string cardId, string newName); + void updateDescription(string cardId, string description); + + void createMember(string name, string email); +}; + +#endif \ No newline at end of file diff --git a/board.cpp b/board.cpp new file mode 100644 index 0000000..a7271d0 --- /dev/null +++ b/board.cpp @@ -0,0 +1,65 @@ +#include +#include "board.h" +#include "list.h" +#include "user.h" + +using namespace std; + +Board::Board(string name, string id){ + this->name = name; + this->id = id; + this->privacy = "PUBLIC"; + cout << "Board created with name:" << name << " id:" << id << " and privacy:" << privacy << endl; +} + +void Board::printBoard(){ + cout << "BOARD name:" << name << " id:" << id << " privacy:" << privacy << endl; + cout << "*** Members ***" << endl; + for(set::iterator it=members.begin(); it!=members.end(); it++) + (*it)->printUser(); + for(set::iterator it=lists.begin(); it!=lists.end(); it++){ + cout << "*** Lists ***" << endl; + (*it)->printList(); + } + cout << endl; +} + +void Board::setName(string newName){ + name = newName; +} + +void Board::setPrivacy(string newPrivacy){ + privacy = newPrivacy; +} + +void Board::addMember(User* member){ + members.insert(member); + cout << "Added member " << member->getName() << " to Board " << name << endl; +} + +void Board::removeMember(User* member){ + if (members.find(member)==members.end()) + cout << "User " << member->getName() << " isn't a part of Board " << name << endl; + else + members.erase(member); +} + +void Board::addList(List* list){ + lists.insert(list); + cout << "Added list " << list->getName() << " to Board " << name << endl; +} + +void Board::removeList(List* list){ + if (lists.find(list)==lists.end()) + cout << "List " << list->getName() << " isn't a part of Board " << name << endl; + else + lists.erase(list); +} + +set Board::getLists(){ + return lists; +} + +string Board::getName(){ + return name; +} \ No newline at end of file diff --git a/board.h b/board.h new file mode 100644 index 0000000..20fb60c --- /dev/null +++ b/board.h @@ -0,0 +1,26 @@ +#ifndef BOARD_H +#define BOARD_H +#include + +using namespace std; + +class List; +class User; + +class Board { + string id, name, privacy; + set members; + set lists; + public: + Board(string name, string id); + void printBoard(); + void setName(string name); + void setPrivacy(string privacy); + void addMember(User* member); + void removeMember(User* member); + void addList(List* list); + void removeList(List* list); + set getLists(); + string getName(); +}; +#endif \ No newline at end of file diff --git a/card.cpp b/card.cpp new file mode 100644 index 0000000..15a62e9 --- /dev/null +++ b/card.cpp @@ -0,0 +1,48 @@ +#include +#include "card.h" +#include "list.h" +#include "user.h" + +using namespace std; + +Card::Card(string name, string id, List* parentList){ + this->name = name; + this->id = id; + assignedTo = nullptr; + this->parentList = parentList; + parentList->addCard(this); + cout << "Card created with name:" << name << " id:" << id << " parentList:" << parentList->getName() << endl; +} + +void Card::printCard(){ + cout << "CARD name:" << name << " id:" << id << " assignedTo:" << ((assignedTo==nullptr)?"None":assignedTo->getName()) << " description:" << description << endl; +} + +void Card::assignTo(User* user){ + //apply check whether user is a part of this board or not. + assignedTo = user; + user->addCard(this); +} + +void Card::deleteThisCard(){ + parentList->removeCard(this); + if(assignedTo){ + assignedTo->unassignCard(this); + } +} + +string Card::getId(){ + return id; +} + +string Card::getName(){ + return name; +} + +void Card::setName(string newName){ + name = newName; +} + +void Card::setDescription(string newDescription){ + description = newDescription; +} \ No newline at end of file diff --git a/card.h b/card.h new file mode 100644 index 0000000..82683c2 --- /dev/null +++ b/card.h @@ -0,0 +1,26 @@ +#ifndef CARD_H +#define CARD_H + +#include + +using namespace std; + +class List; +class User; + +class Card { + string id, name, description; + User* assignedTo; + List* parentList; + public: + Card(string name, string id, List* parentList); + void printCard(); + void assignTo(User* user); + void deleteThisCard(); + string getId(); + string getName(); + void setName(string newName); + void setDescription(string newDesciption); +}; + +#endif \ No newline at end of file diff --git a/driver.cpp b/driver.cpp new file mode 100644 index 0000000..aea0529 --- /dev/null +++ b/driver.cpp @@ -0,0 +1,71 @@ +#include +#include "appManager.h" +#include "board.h" +#include "list.h" +#include "card.h" +#include "user.h" +#include "utils.h" + +using namespace std; + +int main(){ + AppManager* appManager = new AppManager(); + // appManager->createMember("Emilia", "ec@gmail.com"); + // appManager->createMenber("Keilah", "kk@gmail.com"); + // appManager->createMember("Jennifer", "ja@gmail.com"); + // appManager->createMember("Mikaela", "m@gmail.com"); + string input; + while(getline(cin, input)){ + if(input=="") + break; + vector args = tokenize(input, ' '); + if(args[0]=="SHOW"){ + if(args.size()==1) + appManager->show(); + else if(args[1]=="BOARD") + appManager->showBoard(args[2]); //id + else if(args[1]=="LIST") + appManager->showList(args[2]); //id + else if(args[1]=="CARD") + appManager->showCard(args[2]); //id + else if(args[1]=="USER") + appManager->showUserCards(args[2]); //id + } else if(args[0]=="BOARD"){ + if(args[1]=="CREATE") + appManager->createBoard(args[2]); //name + else if(args[1]=="REMOVE") + appManager->removeBoard(args[2]); //id + else if(args[2]=="ADD_MEMBER") + appManager->addMemberToBoard(args[1], args[3]); //boardId, memberId + else if(args[2]=="REMOVE_MEMBER") + appManager->removeMemberFromBoard(args[1], args[3]); //boardId, memberId + else if(args[2]=="NAME") + appManager->updateBoardName(args[1], args[3]); //boardId, newName + else if(args[2]=="PRIVACY") + appManager->updateBoardPrivacy(args[1], args[3]); //boardId, privacy + } else if(args[0]=="LIST"){ + if(args[1]=="CREATE") + appManager->createList(args[2], args[3]); //boardId, listName + else if(args[1]=="REMOVE") + appManager->removeList(args[2]); //listId + } else if(args[0]=="CARD"){ + if(args[1]=="CREATE") + appManager->createCard(args[2], args[3]); //listId, cardName + else if(args[1]=="REMOVE") + appManager->removeCard(args[2]); //cardId + else if(args[2]=="ASSIGN") + appManager->assignCard(args[1], args[3]); //cardId, memberId + else if(args[2]=="NAME") + appManager->updateCardName(args[1], args[3]); //cardId, newName + else if(args[2]=="DESCRIPTION"){ + string description = ""; + for(int i=3; iupdateDescription(args[1], description); //cardId, description + } + } else if(args[0]=="USER"){ + //args[1] is always CREATE + appManager->createMember(args[2], args[3]); //name, email + } + } +} \ No newline at end of file diff --git a/list.cpp b/list.cpp new file mode 100644 index 0000000..e099836 --- /dev/null +++ b/list.cpp @@ -0,0 +1,48 @@ +#include +#include "list.h" +#include "board.h" +#include "card.h" + +using namespace std; + +List::List(string name, string id, Board* parentBoard){ + this->name = name; + this->id = id; + this->parentBoard = parentBoard; + parentBoard->addList(this); + cout << "List created with name:" << name << " id:" << id << " parentBoard:" << parentBoard->getName() << endl; +} + +void List::printList(){ + cout << "LIST name:" << name << " id:" << id << endl; + cout << "*** CARDS ***" << endl; + for(set::iterator it=cards.begin(); it!=cards.end(); it++) + (*it)->printCard(); +} + +void List::addCard(Card* card){ + cards.insert(card); +} + +void List::removeCard(Card* card){ + if (cards.find(card)==cards.end()) + cout << "Card " << card->getName() << " isn't a part of List " << name << endl; + else + cards.erase(card); +} + +void List::deleteFromParentBoard(){ + parentBoard->removeList(this); +} + +set List::getCards(){ + return cards; +} + +string List::getId(){ + return id; +} + +string List::getName(){ + return name; +} \ No newline at end of file diff --git a/list.h b/list.h new file mode 100644 index 0000000..9f4352b --- /dev/null +++ b/list.h @@ -0,0 +1,26 @@ +#ifndef LIST_H +#define LIST_H + +#include + +using namespace std; + +class Board; +class Card; + +class List { + string id, name; + Board* parentBoard; + set cards; + public: + List(string name, string id, Board* parentBoard); + void printList(); + void addCard(Card* card); + void removeCard(Card* card); + void deleteFromParentBoard(); + set getCards(); + string getId(); + string getName(); +}; + +#endif \ No newline at end of file diff --git a/sample_input.txt b/sample_input.txt new file mode 100644 index 0000000..79290ce --- /dev/null +++ b/sample_input.txt @@ -0,0 +1,53 @@ +SHOW +USER CREATE emilia ec@gmail.com +USER CREATE keilah kk@gmail.com +USER CREATE jennifer ja@gmail.com +USER CREATE mikaela m@gmail.com +SHOW +SHOW USER user1 +SHOW USER User1 +BOARD CREATE first_board +SHOW BOARD Board1 +BOARD Board1 NAME myBoard +SHOW +BOARD Board1 ADD_MEMBER User1 +BOARD Board1 ADD_MEMBER User2 +SHOW BOARD Board1 +BOARD CREATE secondBoard +BOARD Board2 ADD_MEMBER User3 +BOARD Board2 ADD_MEMBER User4 +BOARD Board2 ADD_MEMBER User1 +BOARD Board2 REMOVE_MEMBER User4 +BOARD Board2 PRIVACY private +SHOW +LIST CREATE Board1 B1L1 +LIST CREATE Board1 B1L2 +SHOW BOARD Board1 +LIST REMOVE List1 +SHOW LIST List1 +SHOW LIST List2 +SHOW +LIST CREATE Board2 B2L1 +CARD CREATE List3 L3C1 +CARD CREATE List2 L2C1 +CARD CREATE List1 L1C1 +CARD CREATE List2 L2C2 +LIST CREATE Board1 B1L3 +CARD CREATE List4 L4C1 +SHOW LIST List2 +SHOW +SHOW LIST List1 +CARD Card2 ASSIGN User1 +CARD Card3 ASSIGN User2 +CARD Card1 DESCRIPTION this card is in list 1 of board 2 +SHOW CARD Card1 +SHOW LIST List2 +CARD Card2 NAME newCardName +SHOW CARD Card2 +SHOW +CARD Card4 ASSIGN User1 +SHOW USER User1 +SHOW USER User2 +CARD REMOVE Card4 +SHOW +SHOW USER User1 diff --git a/user.cpp b/user.cpp new file mode 100644 index 0000000..a70ae37 --- /dev/null +++ b/user.cpp @@ -0,0 +1,38 @@ +#include +#include "card.h" +#include "user.h" + +using namespace std; + +User::User(string name, string email, string id){ + this->name = name; + this->email = email; + this->id = id; + cout << "User created with name:" << name << " id:" << id << " email:" << email << endl; +} + +void User::myCards(){ + cout << "User " << name << " has following cards assigned:" << endl; + for(set::iterator it=cardsAssigned.begin(); it!=cardsAssigned.end(); it++) + (*it)->printCard(); +} + +void User::printUser(){ + cout << "User name:" << name << " id:" << id << " email:" << email << endl; +} + +void User::addCard(Card* card){ + cardsAssigned.insert(card); +} + +void User::unassignCard(Card* card){ + cardsAssigned.erase(card); +} + +string User::getId(){ + return id; +} + +string User::getName(){ + return name; +} \ No newline at end of file diff --git a/user.h b/user.h new file mode 100644 index 0000000..a4a1b61 --- /dev/null +++ b/user.h @@ -0,0 +1,23 @@ +#ifndef USER_H +#define USER_H + +#include + +using namespace std; + +class Card; + +class User { + string id, name, email; + set cardsAssigned; + public: + User(string name, string email, string id); + void myCards(); + void printUser(); + void addCard(Card* card); + void unassignCard(Card* card); + string getName(); + string getId(); +}; + +#endif \ No newline at end of file diff --git a/utils.h b/utils.h new file mode 100644 index 0000000..f945d7a --- /dev/null +++ b/utils.h @@ -0,0 +1,22 @@ +#include + +using namespace std; + +vector tokenize(string input, char delimiter){ + vector ans; + int i=0; + string current_word = ""; + while(i 0) + ans.push_back(current_word); + current_word=""; + } else { + current_word += input[i]; + } + i++; + } + if(current_word.length()>0) + ans.push_back(current_word); + return ans; +} \ No newline at end of file