Monday, April 18, 2016

Furniture test -- OOD

public class FurnitureTest {
    public static void main(String [] args) {
        Furniture [] furnitures = new Furniture[4];
        furnitures[0] = new Table(Furniture.Material.WOOD);
        furnitures[1] = new Table(Furniture.Material.STEEL);
        furnitures[2] = new Chair(Furniture.Material.WOOD);
        furnitures[3] = new Chair(Furniture.Material.STEEL);

        System.out.println("Test begin: ");

        for(Furniture f : furnitures) {
            System.out.println(f.getMaterialName() + " " + f.getName());
            if(f.addWeight(60)) {
                System.out.println("Weight test success");
            } else {
                System.out.println("Weigth test fail");
            }
            if(f.putFire()) {
                System.out.println("Fire test success");
            } else {
                System.out.println("Fire test fail");
            }
        }
    }
}

abstract class Furniture {
    enum Material {
        WOOD, STEEL
    }

    Material m_material;
    
    public abstract boolean addWeight(int w);

    public boolean putFire() {
        if(m_material.equals(Material.WOOD)){
        return false;
        }
        else if(m_material.equals(Material.STEEL)){
        return true;
        }
        return false;
    }

    public abstract String getName();

    public String getMaterialName() {
        return m_material.toString();
    }
}

class Table extends Furniture {
    public Table(Furniture.Material m) {
        super.m_material = m;
    }
    public boolean addWeight(int w) {
        return w < 100;
    }
    public String getName() {
        return "Table";
    }
}

class Chair extends Furniture {
    public Chair(Furniture.Material m) {
        super.m_material = m;
    }
    public boolean addWeight(int w) {
        return w < 50;
    }
    public String getName() {
        return "Chair";
    }

}

No comments:

Post a Comment