CreateBuildings Code

CreateBuildings.java

/*
 * Mark Hesser
 * HesserCAN 
 * [email protected]
 * www.hessercan.com
 */
 
package createbuildings;
 
/**
 * @author mark
 */
public class CreateBuildings 
{
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        Building[] buildings = new Building[6];
        buildings[0] = new House(100, 1, 2, 1.5);
        buildings[1] = new School(3000, 1, 20, 1);
        buildings[2] = new House(200, 2, 3, 2.5);
        buildings[3] = new School(3000, 2, 30, 2);
        buildings[4] = new House(300, 3, 4, 2.5);
        buildings[5] = new School(3000, 3, 40, 3);
 
        String info = "";
        for (int i = 0; i < buildings.length; i++)
        {
            if (buildings.length == (i + 1))
                info += buildings[i].toString() + "\n";
            else
                info += buildings[i].toString() + "\n\n";
        }
        DisplayInfo(info);
    }
 
    static void DisplayInfo(String info)
    {
        System.out.println(info);
    }
}

Building.java

/*
 * Mark Hesser
 * HesserCAN 
 * [email protected]
 * www.hessercan.com
 */
 
package createbuildings;
 
/**
 * @author mark
 */
public abstract class Building 
{
    private final double sqFt;
    private final int stories;
 
    public Building(double sf, int st)
    {
        sqFt = sf;
        stories = st;
    }
 
    public double getSqFt()
    {
        return sqFt;
    }
    public int getStories()
    {
        return stories;
    }
 
    public abstract String toString();
}

House.java

/*
 * Mark Hesser
 * HesserCAN 
 * [email protected]
 * www.hessercan.com
 */
 
package createbuildings;
 
/**
 * @author mark
 */
public class House extends Building
{
    private int beds; 
    private double baths;
 
    public House(double sf, int st, int be, double ba) 
    {
        super(sf, st);
        beds = be;
        baths = ba;
    }
    public int getNumBeds()
    {
        return beds;
    }
    public double getNumBaths()
    {
        return baths;
    }
    @Override
    public String toString()
    {
        return String.format("House \n%.2f SqFt \n%d Stories \n%d Beds, "
                + "%.1f Baths", super.getSqFt(), super.getStories(), 
                beds, baths);
    }
}

School.java

/*
 * Mark Hesser
 * HesserCAN 
 * [email protected]
 * www.hessercan.com
 */
 
package createbuildings;
 
/**
 * @author mark
 */
public class School extends Building
{
    private final int classRooms;
    private final String gradeLevel;
 
    /**
     * @param sf
     * @param st
     * @param cr
     * @param gl
     * Default Elementary
        * 1 for Elementary, 
        * 2 for Middle School, 
        * 3 for High School 
    */
    public School(double sf, int st, int cr, int gl) 
    {
        super(sf, st);
        classRooms = cr;
 
        switch (gl)
        {
            case 1:
                gradeLevel = "Elementary School";
                break;
            case 2:
                gradeLevel = "Middle School";
                break;
            case 3:
                gradeLevel = "High School";
                break;
            default:
                gradeLevel = "Elementary School";
                break;
        }      
    }
 
    public int getNumClassRooms()
    {
        return classRooms;
    }
 
    public String getGradeLevel()
    {
        return gradeLevel;
    }
    @Override
    public String toString()
    {
        return String.format("%s \n%.2f SqFt \n%d Stories \n%d Class Rooms", 
                gradeLevel, super.getSqFt(), super.getStories(), classRooms);
    }
}