Amazon Interview Question for Software Engineer in Tests


Country: United States




Comment hidden because of low score. Click to expand.
0
of 0 vote

public class FurnitureStore {
    FurnitureFactory factory;

    public static void main (String[] args) {
        FurnitureStore client = new FurnitureStore();
        FurnitureFactory factory = client.createFactory("Chair");
        Furniture furniture = factory.makeFurniture(Material.METAL);
        furniture.doStressTest();
        furniture.doFireTest();
    }

    private FurnitureFactory createFactory(String factoryType) {
        if (factoryType.equalsIgnoreCase("Table")) {
            factory = new TableFactory();
        } else if (factoryType.equalsIgnoreCase("Chair")) {
            factory = new ChairFactory();
        }
        return factory;
    }
}

public enum Material {
    WOOD("Wood"), METAL("Metal");

    String type;

    Material (String type) {
        this.type = type;
    }

    public String getType () {
        return type;
    }
}

public abstract class FurnitureFactory {

    public abstract Furniture makeFurniture(Material material);

}

public class ChairFactory extends FurnitureFactory {

    @Override
    public Furniture makeFurniture(Material material) {
        if (material.type.equalsIgnoreCase("Metal"))
            return new MetalChair();
        else if (material.type.equalsIgnoreCase("Wood"))
            return new WoodenChair();
        return null;
    }

}

public class TableFactory extends FurnitureFactory {

    @Override
    public Furniture makeFurniture(Material material) {
        if (material.type.equalsIgnoreCase("Metal"))
            return new MetalTable();
        else if (material.type.equalsIgnoreCase("Wood"))
            return new WoodenTable();
        return null;
    }
}

public interface Furniture {

    public void doStressTest();
    public void doFireTest();
    public Furniture buildFurniture();

}

public abstract class Chair implements Furniture {

    protected Material material;

    public abstract String chairDetails();
}

public abstract class Table implements Furniture {

    protected Material material;

    public abstract String tableDetails();
}

public class WoodenChair extends Chair {

    @Override
    public Furniture buildFurniture() {
        return new WoodenChair();
    }

    @Override
    public void doStressTest() {
        System.out.println("Doing stress test on Wooden chair....");
        System.out.println("Passed stressed test");
    }

    @Override
    public void doFireTest() {
        System.out.println("Doing fire test on Wooden chair....");
        System.out.println("Passed fire test");
    }

    @Override
    public String chairDetails() {
        return "Constructed wooden chair";
    }
}

public class WoodenTable extends Table {

    @Override
    public Furniture buildFurniture() {
        return new WoodenTable();
    }

    @Override
    public void doStressTest() {
        System.out.println("Doing stress test on Wooden table....");
        System.out.println("Passed stressed test");
    }

    @Override
    public void doFireTest() {
        System.out.println("Doing fire test on Wooden table....");
        System.out.println("Passed fire test");
    }
    @Override
    public String tableDetails() {
        return "Constructed wooden table";
    }

}

public class MetalChair extends Chair {

    @Override
    public String chairDetails() {
        return "Constructed metal chair";
    }

    @Override
    public void doStressTest() {
        System.out.println("Doing stress test on metal chair....");
        System.out.println("Passed stress test");
    }

    @Override
    public void doFireTest() {
        System.out.println("Doing fire test on metal chair....");
        System.out.println("Passed fire test");
    }

    @Override
    public Furniture buildFurniture() {
        return new MetalChair();
    }
}

public class MetalTable extends Table {

    @Override
    public String tableDetails() {
        return "Constructed metal table";
    }

    @Override
    public void doStressTest() {
        System.out.println("Doing stress test on metal table....");
        System.out.println("Passed stress test");
    }

    @Override
    public void doFireTest() {
        System.out.println("Doing fire test on metal table....");
        System.out.println("Passed fire test");
    }

    @Override
    public Furniture buildFurniture() {
        return new MetalTable();
    }
}

- nspcareer December 24, 2018 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More