0

I am new to Java so I am trying to get my head around some of the concepts of Java, so be gentle with me!.

I have my main MASTER CONTROLPANEL CLASS which contains the main of the program, and I Insantiate the building class which contains instances of the rooms class, and the rooms class contains instances of the walls class, and the walls contains instances of the windows class.

Basically constructing the building, each instantiation of the rooms class will have different amount of instances of walls, and walls different amount of windows, primitive height and widths etc..

What is the best way to go about the design of this?, as in creating the dynamic amount of walls and windows, is the only way with accessor methods? and just altering the measurements in the MAIN of the program? or having a method to add each wall individually to an arraylist for each room? and if so where is the best place to place these?

Thanks very much for any help.

1
  • Maybe you can design the application in a tree-like structure using JTree with a database as simple as XML via JAXB. Each building component can be defined in nodes where each node's properties can be edited. Commented Mar 6, 2012 at 12:04

1 Answer 1

1

Follow the rule of thumb.

  1. Wherever your problem definition has has-a, you got composition. If you are going to has-many, you may use a Collection -- as you said, List of walls, and List of windows.

  2. You may also, abstract stuffs, have Window as abstract class and can use implementation for StandardWindow or ManSizeWindow -- this is a is-a relationship. as in StandardWindow is a Window. You are likely to have this kind of stuff in your problem where you are required to decorate the rooms with same kind of stuff but different properties.

Also, do not use rigid code, like have classes that have, say, three windows attributes and three getters/setters them. You are likely to regret later with this technique.

Sign up to request clarification or add additional context in comments.

4 Comments

But say I have 4 rooms each with each having a different amount of walls and windows, how do I go about setting these???
room1.getWindows().add(new StandardWindow(height, width, color));. So, you can add arbitrary number of Windows in any room. Because, room.getWindow() returns a List<Window>, you could keep adding as many Windows as you want. Clear? Same goes for walls
would the adding of new windows go in the main of the program?
oh for that. You have couple of options. You will be setting variables like number of rooms, number of windows in each, number of walls etc. You can 1. pass is as argument to your main class, 2. You can read it off a XML (or may be JSON) file that you will be altering based on your requirement. 3. or have variables in Main class but this will require compiling each time. You read the parameters and and the iterate based on number. Lets say for room1 you have 4 windows, just for loop for 4 times and instanciate Window and add to room's Window list. Simple

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.