Sapient Corporation Interview Question
Software ArchitectsCountry: India
Interview Type: Written Test
class Department
{
public:
Department(int numProd, char* prodNames[]);
void addDiscount(int discount, char* prodName="");
void addProduct(char* prodName);
void addProducts(char* prodNames[]);
char* name();
private:
HashTable or Vector of products;
}
class Catalog
{
public:
Catalog(int numDept, char* deptNames[]);
void addDiscount(int discount, char* deptName, char* prodName = "");
private:
HashTable or Vector of Departments;
}
Something of the above sort can be extended as per the requirements of the design interface.
Sorry, posting for the first time, didn't know it will take it like this
Though in C we cannot do much with classes, but here is my choice of data structures in C:-
struct Product
{
char *name;
char *department;
double price;
int quantity;
}product;
struct Department
{
char *name;
char *incharge_name;
int no_of_people;
struct Product **items;
}deptt;
struct List
{
struct Department **a;
}list;
Feel free to point if there is something wrong with my code..
import java.util.ArrayList;
interface Catalog {
public void showAllProducts();
public void addDiscount(double discountPercent);
}
class Product implements Catalog {
private String prodId;
private String prodName;
private double price;
public Product(String prodId, String prodName, double price){
this.prodId = prodId;
this.prodName = prodName;
this.price = price;
}
@Override
public void addDiscount(double discountPercent){
price = price - ((discountPercent*price)/100);
}
@Override
public void showAllProducts(){
System.out.println( "\n\n Product id: " + prodId + " " + "prodName: " +
prodName + " price : " + price + "\n\n");
}
}
class Department implements Catalog {
private ArrayList<Catalog> subCatalogList = new ArrayList<Catalog>();
private String departmentId;
private String departmentName;
public Department(String departmentId, String departmentName){
this.departmentName = departmentName;
this.departmentId = departmentId;
}
public void addProduct(Catalog catalog){
subCatalogList.add(catalog);
}
public void addSubDepartment(Catalog catalog){
subCatalogList.add(catalog);
}
@Override
public void addDiscount(double discountPercent){
for(Catalog subCatalog : subCatalogList){
subCatalog.addDiscount(discountPercent);
}
}
@Override
public void showAllProducts(){
for(Catalog subCatalog : subCatalogList){
subCatalog.showAllProducts();
}
}
}
class CatalogDirectory implements Catalog {
private ArrayList<Catalog> departmentList = new ArrayList<Catalog>();
public void addDepartment(Catalog department){
departmentList.add(department);
}
@Override
public void addDiscount(double discountPercent){
for(Catalog subCatalog : departmentList){
subCatalog.addDiscount(discountPercent);
}
}
@Override
public void showAllProducts(){
for(Catalog subCatalog : departmentList){
subCatalog.showAllProducts();
}
}
}
public class CatalogDesignPattern {
public static void main(String args[]){
CatalogDirectory directory = new CatalogDirectory();
Department groceries = new Department("1", "Groceries");
Department furniture = new Department("2", "furniture");
Department perishables = new Department("7", "Perishables");
Product bed = new Product("3", "bed", 13000);
Product almirah = new Product("4", "almirah", 15000);
Product apple = new Product("5", "apple", 100);
Product guava = new Product("6", "guava", 200);
Product cheese = new Product("8", "cheese", 50);
Product chocolate = new Product("9", "chocolate", 20);
perishables.addProduct(chocolate);
perishables.addProduct(cheese);
groceries.addSubDepartment(perishables);
groceries.addProduct(apple);
groceries.addProduct(guava);
groceries.addSubDepartment(perishables);
furniture.addProduct(bed);
furniture.addProduct(almirah);
directory.addDepartment(groceries);
directory.addDepartment(furniture);
directory.showAllProducts();
directory.addDiscount(5);
directory.showAllProducts();
}
}
IMHO this design problem can be solved by using or applying different design patterns in combination. Immediately what comes to my mind from the problem statement you can try Decorator, and Template design pattern. Decorator for catalogs having sub catalogs instead of inheritance and Template for addition of functionality at run time. Please check, a design problem might have multiple solution, the best one is based on requirement and application feasibility.
Since catalogs can either have sub-catalog or products under it, this implies recursive nature..similar to file systems(under directories we can have directories and files).
- BJ August 02, 2013So we can use composite pattern here.